Use const for the usbi_parse_descriptor() format string
[platform/upstream/libusb.git] / libusb / descriptor.c
1 /*
2  * USB descriptor handling functions for libusb
3  * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
4  * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <errno.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "libusbi.h"
27
28 #define DESC_HEADER_LENGTH              2
29 #define DEVICE_DESC_LENGTH              18
30 #define CONFIG_DESC_LENGTH              9
31 #define INTERFACE_DESC_LENGTH           9
32 #define ENDPOINT_DESC_LENGTH            7
33 #define ENDPOINT_AUDIO_DESC_LENGTH      9
34
35 /** @defgroup desc USB descriptors
36  * This page details how to examine the various standard USB descriptors
37  * for detected devices
38  */
39
40 /* set host_endian if the w values are already in host endian format,
41  * as opposed to bus endian. */
42 int usbi_parse_descriptor(unsigned char *source, const char *descriptor,
43         void *dest, int host_endian)
44 {
45         unsigned char *sp = source, *dp = dest;
46         uint16_t w;
47         const char *cp;
48
49         for (cp = descriptor; *cp; cp++) {
50                 switch (*cp) {
51                         case 'b':       /* 8-bit byte */
52                                 *dp++ = *sp++;
53                                 break;
54                         case 'w':       /* 16-bit word, convert from little endian to CPU */
55                                 dp += ((uintptr_t)dp & 1);      /* Align to word boundary */
56
57                                 if (host_endian) {
58                                         memcpy(dp, sp, 2);
59                                 } else {
60                                         w = (sp[1] << 8) | sp[0];
61                                         *((uint16_t *)dp) = w;
62                                 }
63                                 sp += 2;
64                                 dp += 2;
65                                 break;
66                 }
67         }
68
69         return (int) (sp - source);
70 }
71
72 static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
73 {
74         if (endpoint->extra)
75                 free((unsigned char *) endpoint->extra);
76 }
77
78 static int parse_endpoint(struct libusb_context *ctx,
79         struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
80         int size, int host_endian)
81 {
82         struct usb_descriptor_header header;
83         unsigned char *extra;
84         unsigned char *begin;
85         int parsed = 0;
86         int len;
87
88         usbi_parse_descriptor(buffer, "bb", &header, 0);
89
90         /* Everything should be fine being passed into here, but we sanity */
91         /*  check JIC */
92         if (header.bLength > size) {
93                 usbi_err(ctx, "ran out of descriptors parsing");
94                 return -1;
95         }
96
97         if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
98                 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
99                         header.bDescriptorType, LIBUSB_DT_ENDPOINT);
100                 return parsed;
101         }
102
103         if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
104                 usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
105         else if (header.bLength >= ENDPOINT_DESC_LENGTH)
106                 usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
107
108         buffer += header.bLength;
109         size -= header.bLength;
110         parsed += header.bLength;
111
112         /* Skip over the rest of the Class Specific or Vendor Specific */
113         /*  descriptors */
114         begin = buffer;
115         while (size >= DESC_HEADER_LENGTH) {
116                 usbi_parse_descriptor(buffer, "bb", &header, 0);
117
118                 if (header.bLength < 2) {
119                         usbi_err(ctx, "invalid descriptor length %d", header.bLength);
120                         return -1;
121                 }
122
123                 /* If we find another "proper" descriptor then we're done  */
124                 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
125                                 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
126                                 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
127                                 (header.bDescriptorType == LIBUSB_DT_DEVICE))
128                         break;
129
130                 usbi_dbg("skipping descriptor %x", header.bDescriptorType);
131                 buffer += header.bLength;
132                 size -= header.bLength;
133                 parsed += header.bLength;
134         }
135
136         /* Copy any unknown descriptors into a storage area for drivers */
137         /*  to later parse */
138         len = (int)(buffer - begin);
139         if (!len) {
140                 endpoint->extra = NULL;
141                 endpoint->extra_length = 0;
142                 return parsed;
143         }
144
145         extra = malloc(len);
146         endpoint->extra = extra;
147         if (!extra) {
148                 endpoint->extra_length = 0;
149                 return LIBUSB_ERROR_NO_MEM;
150         }
151
152         memcpy(extra, begin, len);
153         endpoint->extra_length = len;
154
155         return parsed;
156 }
157
158 static void clear_interface(struct libusb_interface *usb_interface)
159 {
160         int i;
161         int j;
162
163         if (usb_interface->altsetting) {
164                 for (i = 0; i < usb_interface->num_altsetting; i++) {
165                         struct libusb_interface_descriptor *ifp =
166                                 (struct libusb_interface_descriptor *)
167                                 usb_interface->altsetting + i;
168                         if (ifp->extra)
169                                 free((void *) ifp->extra);
170                         if (ifp->endpoint) {
171                                 for (j = 0; j < ifp->bNumEndpoints; j++)
172                                         clear_endpoint((struct libusb_endpoint_descriptor *)
173                                                 ifp->endpoint + j);
174                                 free((void *) ifp->endpoint);
175                         }
176                 }
177                 free((void *) usb_interface->altsetting);
178                 usb_interface->altsetting = NULL;
179         }
180
181 }
182
183 static int parse_interface(libusb_context *ctx,
184         struct libusb_interface *usb_interface, unsigned char *buffer, int size,
185         int host_endian)
186 {
187         int i;
188         int len;
189         int r;
190         int parsed = 0;
191         int tmp;
192         struct usb_descriptor_header header;
193         struct libusb_interface_descriptor *ifp;
194         unsigned char *begin;
195
196         usb_interface->num_altsetting = 0;
197
198         while (size >= INTERFACE_DESC_LENGTH) {
199                 struct libusb_interface_descriptor *altsetting =
200                         (struct libusb_interface_descriptor *) usb_interface->altsetting;
201                 altsetting = realloc(altsetting,
202                         sizeof(struct libusb_interface_descriptor) *
203                         (usb_interface->num_altsetting + 1));
204                 if (!altsetting) {
205                         r = LIBUSB_ERROR_NO_MEM;
206                         goto err;
207                 }
208                 usb_interface->altsetting = altsetting;
209
210                 ifp = altsetting + usb_interface->num_altsetting;
211                 usb_interface->num_altsetting++;
212                 usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
213                 ifp->extra = NULL;
214                 ifp->extra_length = 0;
215                 ifp->endpoint = NULL;
216
217                 /* Skip over the interface */
218                 buffer += ifp->bLength;
219                 parsed += ifp->bLength;
220                 size -= ifp->bLength;
221
222                 begin = buffer;
223
224                 /* Skip over any interface, class or vendor descriptors */
225                 while (size >= DESC_HEADER_LENGTH) {
226                         usbi_parse_descriptor(buffer, "bb", &header, 0);
227                         if (header.bLength < 2) {
228                                 usbi_err(ctx, "invalid descriptor of length %d",
229                                         header.bLength);
230                                 r = LIBUSB_ERROR_IO;
231                                 goto err;
232                         }
233
234                         /* If we find another "proper" descriptor then we're done */
235                         if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
236                                         (header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
237                                         (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
238                                         (header.bDescriptorType == LIBUSB_DT_DEVICE))
239                                 break;
240
241                         buffer += header.bLength;
242                         parsed += header.bLength;
243                         size -= header.bLength;
244                 }
245
246                 /* Copy any unknown descriptors into a storage area for */
247                 /*  drivers to later parse */
248                 len = (int)(buffer - begin);
249                 if (len) {
250                         ifp->extra = malloc(len);
251                         if (!ifp->extra) {
252                                 r = LIBUSB_ERROR_NO_MEM;
253                                 goto err;
254                         }
255                         memcpy((unsigned char *) ifp->extra, begin, len);
256                         ifp->extra_length = len;
257                 }
258
259                 /* Did we hit an unexpected descriptor? */
260                 usbi_parse_descriptor(buffer, "bb", &header, 0);
261                 if ((size >= DESC_HEADER_LENGTH) &&
262                                 ((header.bDescriptorType == LIBUSB_DT_CONFIG) ||
263                                  (header.bDescriptorType == LIBUSB_DT_DEVICE)))
264                         return parsed;
265
266                 if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
267                         usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);
268                         r = LIBUSB_ERROR_IO;
269                         goto err;
270                 }
271
272                 if (ifp->bNumEndpoints > 0) {
273                         struct libusb_endpoint_descriptor *endpoint;
274                         tmp = ifp->bNumEndpoints * sizeof(struct libusb_endpoint_descriptor);
275                         endpoint = malloc(tmp);
276                         ifp->endpoint = endpoint;
277                         if (!endpoint) {
278                                 r = LIBUSB_ERROR_NO_MEM;
279                                 goto err;
280                         }
281
282                         memset(endpoint, 0, tmp);
283                         for (i = 0; i < ifp->bNumEndpoints; i++) {
284                                 usbi_parse_descriptor(buffer, "bb", &header, 0);
285
286                                 if (header.bLength > size) {
287                                         usbi_err(ctx, "ran out of descriptors parsing");
288                                         r = LIBUSB_ERROR_IO;
289                                         goto err;
290                                 }
291
292                                 r = parse_endpoint(ctx, endpoint + i, buffer, size,
293                                         host_endian);
294                                 if (r < 0)
295                                         goto err;
296
297                                 buffer += r;
298                                 parsed += r;
299                                 size -= r;
300                         }
301                 }
302
303                 /* We check to see if it's an alternate to this one */
304                 ifp = (struct libusb_interface_descriptor *) buffer;
305                 if (size < LIBUSB_DT_INTERFACE_SIZE ||
306                                 ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
307                                 !ifp->bAlternateSetting)
308                         return parsed;
309         }
310
311         return parsed;
312 err:
313         clear_interface(usb_interface);
314         return r;
315 }
316
317 static void clear_configuration(struct libusb_config_descriptor *config)
318 {
319         if (config->interface) {
320                 int i;
321                 for (i = 0; i < config->bNumInterfaces; i++)
322                         clear_interface((struct libusb_interface *)
323                                 config->interface + i);
324                 free((void *) config->interface);
325         }
326         if (config->extra)
327                 free((void *) config->extra);
328 }
329
330 static int parse_configuration(struct libusb_context *ctx,
331         struct libusb_config_descriptor *config, unsigned char *buffer,
332         int host_endian)
333 {
334         int i;
335         int r;
336         int size;
337         int tmp;
338         struct usb_descriptor_header header;
339         struct libusb_interface *usb_interface;
340
341         usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
342         size = config->wTotalLength;
343
344         if (config->bNumInterfaces > USB_MAXINTERFACES) {
345                 usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
346                 return LIBUSB_ERROR_IO;
347         }
348
349         tmp = config->bNumInterfaces * sizeof(struct libusb_interface);
350         usb_interface = malloc(tmp);
351         config->interface = usb_interface;
352         if (!config->interface)
353                 return LIBUSB_ERROR_NO_MEM;
354
355         memset(usb_interface, 0, tmp);
356         buffer += config->bLength;
357         size -= config->bLength;
358
359         config->extra = NULL;
360         config->extra_length = 0;
361
362         for (i = 0; i < config->bNumInterfaces; i++) {
363                 int len;
364                 unsigned char *begin;
365
366                 /* Skip over the rest of the Class Specific or Vendor */
367                 /*  Specific descriptors */
368                 begin = buffer;
369                 while (size >= DESC_HEADER_LENGTH) {
370                         usbi_parse_descriptor(buffer, "bb", &header, 0);
371
372                         if ((header.bLength > size) ||
373                                         (header.bLength < DESC_HEADER_LENGTH)) {
374                                 usbi_err(ctx, "invalid descriptor length of %d",
375                                         header.bLength);
376                                 r = LIBUSB_ERROR_IO;
377                                 goto err;
378                         }
379
380                         /* If we find another "proper" descriptor then we're done */
381                         if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
382                                         (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
383                                         (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
384                                         (header.bDescriptorType == LIBUSB_DT_DEVICE))
385                                 break;
386
387                         usbi_dbg("skipping descriptor 0x%x\n", header.bDescriptorType);
388                         buffer += header.bLength;
389                         size -= header.bLength;
390                 }
391
392                 /* Copy any unknown descriptors into a storage area for */
393                 /*  drivers to later parse */
394                 len = (int)(buffer - begin);
395                 if (len) {
396                         /* FIXME: We should realloc and append here */
397                         if (!config->extra_length) {
398                                 config->extra = malloc(len);
399                                 if (!config->extra) {
400                                         r = LIBUSB_ERROR_NO_MEM;
401                                         goto err;
402                                 }
403
404                                 memcpy((unsigned char *) config->extra, begin, len);
405                                 config->extra_length = len;
406                         }
407                 }
408
409                 r = parse_interface(ctx, usb_interface + i, buffer, size, host_endian);
410                 if (r < 0)
411                         goto err;
412
413                 buffer += r;
414                 size -= r;
415         }
416
417         return size;
418
419 err:
420         clear_configuration(config);
421         return r;
422 }
423
424 /** \ingroup desc
425  * Get the USB device descriptor for a given device.
426  *
427  * This is a non-blocking function; the device descriptor is cached in memory.
428  *
429  * \param dev the device
430  * \param desc output location for the descriptor data
431  * \returns 0 on success or a LIBUSB_ERROR code on failure
432  */
433 int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
434         struct libusb_device_descriptor *desc)
435 {
436         unsigned char raw_desc[DEVICE_DESC_LENGTH];
437         int host_endian = 0;
438         int r;
439
440         usbi_dbg("");
441         r = usbi_backend->get_device_descriptor(dev, raw_desc, &host_endian);
442         if (r < 0)
443                 return r;
444
445         memcpy((unsigned char *) desc, raw_desc, sizeof(raw_desc));
446         if (!host_endian) {
447                 desc->bcdUSB = libusb_le16_to_cpu(desc->bcdUSB);
448                 desc->idVendor = libusb_le16_to_cpu(desc->idVendor);
449                 desc->idProduct = libusb_le16_to_cpu(desc->idProduct);
450                 desc->bcdDevice = libusb_le16_to_cpu(desc->bcdDevice);
451         }
452         return 0;
453 }
454
455 /** \ingroup desc
456  * Get the USB configuration descriptor for the currently active configuration.
457  * This is a non-blocking function which does not involve any requests being
458  * sent to the device.
459  *
460  * \param dev a device
461  * \param config output location for the USB configuration descriptor. Only
462  * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
463  * after use.
464  * \returns 0 on success
465  * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
466  * \returns another LIBUSB_ERROR code on error
467  * \see libusb_get_config_descriptor
468  */
469 int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
470         struct libusb_config_descriptor **config)
471 {
472         struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
473         unsigned char tmp[8];
474         unsigned char *buf = NULL;
475         int host_endian = 0;
476         int r;
477
478         usbi_dbg("");
479         if (!_config)
480                 return LIBUSB_ERROR_NO_MEM;
481
482         r = usbi_backend->get_active_config_descriptor(dev, tmp, sizeof(tmp),
483                 &host_endian);
484         if (r < 0)
485                 goto err;
486
487         usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
488         buf = malloc(_config->wTotalLength);
489         if (!buf) {
490                 r = LIBUSB_ERROR_NO_MEM;
491                 goto err;
492         }
493
494         r = usbi_backend->get_active_config_descriptor(dev, buf,
495                 _config->wTotalLength, &host_endian);
496         if (r < 0)
497                 goto err;
498
499         r = parse_configuration(dev->ctx, _config, buf, host_endian);
500         if (r < 0) {
501                 usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
502                 goto err;
503         } else if (r > 0) {
504                 usbi_warn(dev->ctx, "descriptor data still left");
505         }
506
507         free(buf);
508         *config = _config;
509         return 0;
510
511 err:
512         free(_config);
513         if (buf)
514                 free(buf);
515         return r;
516 }
517
518 /** \ingroup desc
519  * Get a USB configuration descriptor based on its index.
520  * This is a non-blocking function which does not involve any requests being
521  * sent to the device.
522  *
523  * \param dev a device
524  * \param config_index the index of the configuration you wish to retrieve
525  * \param config output location for the USB configuration descriptor. Only
526  * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
527  * after use.
528  * \returns 0 on success
529  * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
530  * \returns another LIBUSB_ERROR code on error
531  * \see libusb_get_active_config_descriptor()
532  * \see libusb_get_config_descriptor_by_value()
533  */
534 int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
535         uint8_t config_index, struct libusb_config_descriptor **config)
536 {
537         struct libusb_config_descriptor *_config;
538         unsigned char tmp[8];
539         unsigned char *buf = NULL;
540         int host_endian = 0;
541         int r;
542
543         usbi_dbg("index %d", config_index);
544         if (config_index >= dev->num_configurations)
545                 return LIBUSB_ERROR_NOT_FOUND;
546
547         _config = malloc(sizeof(*_config));
548         if (!_config)
549                 return LIBUSB_ERROR_NO_MEM;
550
551         r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
552                 sizeof(tmp), &host_endian);
553         if (r < 0)
554                 goto err;
555
556         usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
557         buf = malloc(_config->wTotalLength);
558         if (!buf) {
559                 r = LIBUSB_ERROR_NO_MEM;
560                 goto err;
561         }
562
563         host_endian = 0;
564         r = usbi_backend->get_config_descriptor(dev, config_index, buf,
565                 _config->wTotalLength, &host_endian);
566         if (r < 0)
567                 goto err;
568
569         r = parse_configuration(dev->ctx, _config, buf, host_endian);
570         if (r < 0) {
571                 usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
572                 goto err;
573         } else if (r > 0) {
574                 usbi_warn(dev->ctx, "descriptor data still left");
575         }
576
577         free(buf);
578         *config = _config;
579         return 0;
580
581 err:
582         free(_config);
583         if (buf)
584                 free(buf);
585         return r;
586 }
587
588 /* iterate through all configurations, returning the index of the configuration
589  * matching a specific bConfigurationValue in the idx output parameter, or -1
590  * if the config was not found.
591  * returns 0 or a LIBUSB_ERROR code
592  */
593 int usbi_get_config_index_by_value(struct libusb_device *dev,
594         uint8_t bConfigurationValue, int *idx)
595 {
596         uint8_t i;
597
598         usbi_dbg("value %d", bConfigurationValue);
599         for (i = 0; i < dev->num_configurations; i++) {
600                 unsigned char tmp[6];
601                 int host_endian;
602                 int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
603                         &host_endian);
604                 if (r < 0)
605                         return r;
606                 if (tmp[5] == bConfigurationValue) {
607                         *idx = i;
608                         return 0;
609                 }
610         }
611
612         *idx = -1;
613         return 0;
614 }
615
616 /** \ingroup desc
617  * Get a USB configuration descriptor with a specific bConfigurationValue.
618  * This is a non-blocking function which does not involve any requests being
619  * sent to the device.
620  *
621  * \param dev a device
622  * \param bConfigurationValue the bConfigurationValue of the configuration you
623  * wish to retrieve
624  * \param config output location for the USB configuration descriptor. Only
625  * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
626  * after use.
627  * \returns 0 on success
628  * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
629  * \returns another LIBUSB_ERROR code on error
630  * \see libusb_get_active_config_descriptor()
631  * \see libusb_get_config_descriptor()
632  */
633 int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
634         uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
635 {
636         int idx;
637         int r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
638         if (r < 0)
639                 return r;
640         else if (idx == -1)
641                 return LIBUSB_ERROR_NOT_FOUND;
642         else
643                 return libusb_get_config_descriptor(dev, (uint8_t) idx, config);
644 }
645
646 /** \ingroup desc
647  * Free a configuration descriptor obtained from
648  * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
649  * It is safe to call this function with a NULL config parameter, in which
650  * case the function simply returns.
651  *
652  * \param config the configuration descriptor to free
653  */
654 void API_EXPORTED libusb_free_config_descriptor(
655         struct libusb_config_descriptor *config)
656 {
657         if (!config)
658                 return;
659
660         clear_configuration(config);
661         free(config);
662 }
663
664 /** \ingroup desc
665  * Retrieve a string descriptor in C style ASCII.
666  *
667  * Wrapper around libusb_get_string_descriptor(). Uses the first language
668  * supported by the device.
669  *
670  * \param dev a device handle
671  * \param desc_index the index of the descriptor to retrieve
672  * \param data output buffer for ASCII string descriptor
673  * \param length size of data buffer
674  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
675  */
676 int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
677         uint8_t desc_index, unsigned char *data, int length)
678 {
679         unsigned char tbuf[255]; /* Some devices choke on size > 255 */
680         int r, si, di;
681         uint16_t langid;
682
683         /* Asking for the zero'th index is special - it returns a string
684          * descriptor that contains all the language IDs supported by the device.
685          * Typically there aren't many - often only one. The language IDs are 16
686          * bit numbers, and they start at the third byte in the descriptor. See
687          * USB 2.0 specification section 9.6.7 for more information. */
688         r = libusb_get_string_descriptor(dev, 0, 0, tbuf, sizeof(tbuf));
689         if (r < 0)
690                 return r;
691
692         if (r < 4)
693                 return LIBUSB_ERROR_IO;
694
695         langid = tbuf[2] | (tbuf[3] << 8);
696
697         r = libusb_get_string_descriptor(dev, desc_index, langid, tbuf,
698                 sizeof(tbuf));
699         if (r < 0)
700                 return r;
701
702         if (tbuf[1] != LIBUSB_DT_STRING)
703                 return LIBUSB_ERROR_IO;
704
705         if (tbuf[0] > r)
706                 return LIBUSB_ERROR_IO;
707
708         for (di = 0, si = 2; si < tbuf[0]; si += 2) {
709                 if (di >= (length - 1))
710                         break;
711
712                 if (tbuf[si + 1]) /* high byte */
713                         data[di++] = '?';
714                 else
715                         data[di++] = tbuf[si];
716         }
717
718         data[di] = 0;
719         return di;
720 }
721