libusb10_desc.c (302408) | libusb10_desc.c (328142) |
---|---|
1/* $FreeBSD: stable/11/lib/libusb/libusb10_desc.c 301968 2016-06-16 16:17:29Z hselasky $ */ | 1/* $FreeBSD: stable/11/lib/libusb/libusb10_desc.c 328142 2018-01-18 21:39:03Z kevans $ */ |
2/*- 3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 418 unchanged lines hidden (view full) --- 428 429int 430libusb_parse_bos_descriptor(const void *buf, int len, 431 struct libusb_bos_descriptor **bos) 432{ 433 struct libusb_bos_descriptor *ptr; 434 struct libusb_usb_2_0_device_capability_descriptor *dcap_20 = NULL; 435 struct libusb_ss_usb_device_capability_descriptor *ss_cap = NULL; | 2/*- 3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 418 unchanged lines hidden (view full) --- 428 429int 430libusb_parse_bos_descriptor(const void *buf, int len, 431 struct libusb_bos_descriptor **bos) 432{ 433 struct libusb_bos_descriptor *ptr; 434 struct libusb_usb_2_0_device_capability_descriptor *dcap_20 = NULL; 435 struct libusb_ss_usb_device_capability_descriptor *ss_cap = NULL; |
436 uint8_t index = 0; |
|
436 437 if (buf == NULL || bos == NULL || len < 1) 438 return (LIBUSB_ERROR_INVALID_PARAM); 439 440 if (len > 65535) 441 len = 65535; 442 443 *bos = ptr = NULL; --- 4 unchanged lines hidden (view full) --- 448 449 dlen = ((const uint8_t *)buf)[0]; 450 dtype = ((const uint8_t *)buf)[1]; 451 452 if (dlen < 2 || dlen > len) 453 break; 454 455 if (dlen >= LIBUSB_DT_BOS_SIZE && | 437 438 if (buf == NULL || bos == NULL || len < 1) 439 return (LIBUSB_ERROR_INVALID_PARAM); 440 441 if (len > 65535) 442 len = 65535; 443 444 *bos = ptr = NULL; --- 4 unchanged lines hidden (view full) --- 449 450 dlen = ((const uint8_t *)buf)[0]; 451 dtype = ((const uint8_t *)buf)[1]; 452 453 if (dlen < 2 || dlen > len) 454 break; 455 456 if (dlen >= LIBUSB_DT_BOS_SIZE && |
456 dtype == LIBUSB_DT_BOS) { | 457 dtype == LIBUSB_DT_BOS && 458 ptr == NULL) { |
457 458 ptr = malloc(sizeof(*ptr) + sizeof(*dcap_20) + 459 sizeof(*ss_cap)); 460 461 if (ptr == NULL) 462 return (LIBUSB_ERROR_NO_MEM); 463 464 *bos = ptr; 465 466 ptr->bLength = LIBUSB_DT_BOS_SIZE; 467 ptr->bDescriptorType = dtype; 468 ptr->wTotalLength = ((const uint8_t *)buf)[2] | 469 (((const uint8_t *)buf)[3] << 8); 470 ptr->bNumDeviceCapabilities = ((const uint8_t *)buf)[4]; 471 ptr->usb_2_0_ext_cap = NULL; 472 ptr->ss_usb_cap = NULL; | 459 460 ptr = malloc(sizeof(*ptr) + sizeof(*dcap_20) + 461 sizeof(*ss_cap)); 462 463 if (ptr == NULL) 464 return (LIBUSB_ERROR_NO_MEM); 465 466 *bos = ptr; 467 468 ptr->bLength = LIBUSB_DT_BOS_SIZE; 469 ptr->bDescriptorType = dtype; 470 ptr->wTotalLength = ((const uint8_t *)buf)[2] | 471 (((const uint8_t *)buf)[3] << 8); 472 ptr->bNumDeviceCapabilities = ((const uint8_t *)buf)[4]; 473 ptr->usb_2_0_ext_cap = NULL; 474 ptr->ss_usb_cap = NULL; |
475 ptr->dev_capability = calloc(ptr->bNumDeviceCapabilities, sizeof(void *)); 476 if (ptr->dev_capability == NULL) { 477 free(ptr); 478 return (LIBUSB_ERROR_NO_MEM); 479 } |
|
473 474 dcap_20 = (void *)(ptr + 1); 475 ss_cap = (void *)(dcap_20 + 1); 476 } 477 if (dlen >= 3 && 478 ptr != NULL && 479 dtype == LIBUSB_DT_DEVICE_CAPABILITY) { | 480 481 dcap_20 = (void *)(ptr + 1); 482 ss_cap = (void *)(dcap_20 + 1); 483 } 484 if (dlen >= 3 && 485 ptr != NULL && 486 dtype == LIBUSB_DT_DEVICE_CAPABILITY) { |
487 if (index != ptr->bNumDeviceCapabilities) { 488 ptr->dev_capability[index] = malloc(dlen); 489 if (ptr->dev_capability[index] == NULL) { 490 libusb_free_bos_descriptor(ptr); 491 return LIBUSB_ERROR_NO_MEM; 492 } 493 memcpy(ptr->dev_capability[index], buf, dlen); 494 index++; 495 } |
|
480 switch (((const uint8_t *)buf)[2]) { 481 case LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY: 482 if (ptr->usb_2_0_ext_cap != NULL || dcap_20 == NULL) 483 break; 484 if (dlen < LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE) 485 break; 486 487 ptr->usb_2_0_ext_cap = dcap_20; --- 30 unchanged lines hidden (view full) --- 518 default: 519 break; 520 } 521 } 522 523 buf = ((const uint8_t *)buf) + dlen; 524 len -= dlen; 525 } | 496 switch (((const uint8_t *)buf)[2]) { 497 case LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY: 498 if (ptr->usb_2_0_ext_cap != NULL || dcap_20 == NULL) 499 break; 500 if (dlen < LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE) 501 break; 502 503 ptr->usb_2_0_ext_cap = dcap_20; --- 30 unchanged lines hidden (view full) --- 534 default: 535 break; 536 } 537 } 538 539 buf = ((const uint8_t *)buf) + dlen; 540 len -= dlen; 541 } |
526 if (ptr != NULL) | 542 543 if (ptr != NULL) { 544 ptr->bNumDeviceCapabilities = index; |
527 return (0); /* success */ | 545 return (0); /* success */ |
546 } |
|
528 529 return (LIBUSB_ERROR_IO); 530} 531 532void 533libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos) 534{ | 547 548 return (LIBUSB_ERROR_IO); 549} 550 551void 552libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos) 553{ |
554 uint8_t i; 555 |
|
535 if (bos == NULL) 536 return; 537 | 556 if (bos == NULL) 557 return; 558 |
559 for (i = 0; i != bos->bNumDeviceCapabilities; i++) 560 free(bos->dev_capability[i]); 561 free(bos->dev_capability); |
|
538 free(bos); 539} 540 541int 542libusb_get_bos_descriptor(libusb_device_handle *handle, 543 struct libusb_bos_descriptor **bos) 544{ 545 uint8_t bos_header[LIBUSB_DT_BOS_SIZE] = {0}; --- 145 unchanged lines hidden --- | 562 free(bos); 563} 564 565int 566libusb_get_bos_descriptor(libusb_device_handle *handle, 567 struct libusb_bos_descriptor **bos) 568{ 569 uint8_t bos_header[LIBUSB_DT_BOS_SIZE] = {0}; --- 145 unchanged lines hidden --- |