From 355d12e039caafbb8e0ae18427be3acef03719d1 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Sun, 6 Feb 2011 17:04:39 +0000
Subject: [PATCH] Tighten up the Bootloader GetDescriptor() function, as the
 descriptor size can be extracted from the header after the address has been
 found.

---
 Bootloaders/CDC/Descriptors.c | 19 +++++--------------
 Bootloaders/DFU/Descriptors.c | 33 ++++++++++++---------------------
 Bootloaders/DFU/Descriptors.h | 30 +++++++++++++++---------------
 3 files changed, 32 insertions(+), 50 deletions(-)

diff --git a/Bootloaders/CDC/Descriptors.c b/Bootloaders/CDC/Descriptors.c
index 5cffe2f69..267c5dcae 100644
--- a/Bootloaders/CDC/Descriptors.c
+++ b/Bootloaders/CDC/Descriptors.c
@@ -206,38 +206,29 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
                                     const uint8_t wIndex,
                                     const void** const DescriptorAddress)
 {
-	const uint8_t  DescriptorType   = (wValue >> 8);
-	const uint8_t  DescriptorNumber = (wValue & 0xFF);
+	const uint8_t DescriptorType   = (wValue >> 8);
+	const uint8_t DescriptorNumber = (wValue & 0xFF);
 
 	const void* Address = NULL;
-	uint16_t    Size    = NO_DESCRIPTOR;
 
 	switch (DescriptorType)
 	{
 		case DTYPE_Device:
 			Address = &DeviceDescriptor;
-			Size    = sizeof(USB_Descriptor_Device_t);
 			break;
 		case DTYPE_Configuration:
 			Address = &ConfigurationDescriptor;
-			Size    = sizeof(USB_Descriptor_Configuration_t);
 			break;
 		case DTYPE_String:
 			if (!(DescriptorNumber))
-			{
-				Address = &LanguageString;
-				Size    = LanguageString.Header.Size;
-			}
+			  Address = &LanguageString;
 			else
-			{
-				Address = &ProductString;
-				Size    = ProductString.Header.Size;
-			}
+			  Address = &ProductString;
 
 			break;
 	}
 
 	*DescriptorAddress = Address;
-	return Size;
+	return (Address != NULL) ? ((USB_Descriptor_Header_t*)Address)->Size : NO_DESCRIPTOR;
 }
 
diff --git a/Bootloaders/DFU/Descriptors.c b/Bootloaders/DFU/Descriptors.c
index 51dfff273..9baa8890d 100644
--- a/Bootloaders/DFU/Descriptors.c
+++ b/Bootloaders/DFU/Descriptors.c
@@ -73,17 +73,17 @@ USB_Descriptor_Configuration_t ConfigurationDescriptor =
 {
 	.Config =
 		{
-			.Header                   = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
+			.Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
 
-			.TotalConfigurationSize   = sizeof(USB_Descriptor_Configuration_t),
-			.TotalInterfaces          = 1,
+			.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
+			.TotalInterfaces        = 1,
 
-			.ConfigurationNumber      = 1,
-			.ConfigurationStrIndex    = NO_DESCRIPTOR,
+			.ConfigurationNumber    = 1,
+			.ConfigurationStrIndex  = NO_DESCRIPTOR,
 
-			.ConfigAttributes         = USB_CONFIG_ATTR_BUSPOWERED,
+			.ConfigAttributes       = USB_CONFIG_ATTR_BUSPOWERED,
 
-			.MaxPowerConsumption      = USB_CONFIG_POWER_MA(100)
+			.MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)
 		},
 
 	.DFU_Interface =
@@ -147,38 +147,29 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
                                     const uint8_t wIndex,
                                     const void** const DescriptorAddress)
 {
-	const uint8_t  DescriptorType   = (wValue >> 8);
-	const uint8_t  DescriptorNumber = (wValue & 0xFF);
+	const uint8_t DescriptorType   = (wValue >> 8);
+	const uint8_t DescriptorNumber = (wValue & 0xFF);
 
 	const void* Address = NULL;
-	uint16_t    Size    = NO_DESCRIPTOR;
 
 	switch (DescriptorType)
 	{
 		case DTYPE_Device:
 			Address = &DeviceDescriptor;
-			Size    = sizeof(USB_Descriptor_Device_t);
 			break;
 		case DTYPE_Configuration:
 			Address = &ConfigurationDescriptor;
-			Size    = sizeof(USB_Descriptor_Configuration_t);
 			break;
 		case DTYPE_String:
 			if (!(DescriptorNumber))
-			{
-				Address = &LanguageString;
-				Size    = LanguageString.Header.Size;
-			}
+			  Address = &LanguageString;
 			else
-			{
-				Address = &ProductString;
-				Size    = ProductString.Header.Size;
-			}
+			  Address = &ProductString;
 
 			break;
 	}
 
 	*DescriptorAddress = Address;
-	return Size;
+	return (Address != NULL) ? ((USB_Descriptor_Header_t*)Address)->Size : NO_DESCRIPTOR;
 }
 
diff --git a/Bootloaders/DFU/Descriptors.h b/Bootloaders/DFU/Descriptors.h
index 21c5da768..14203c3e9 100644
--- a/Bootloaders/DFU/Descriptors.h
+++ b/Bootloaders/DFU/Descriptors.h
@@ -137,21 +137,21 @@
 		 */
 		typedef struct
 		{
-			USB_Descriptor_Header_t               Header; /**< Standard descriptor header structure */
-
-			uint8_t                               Attributes; /**< DFU device attributes, a mask comprising of the
-			                                                   *  ATTR_* macros listed in this source file
-			                                                   */
-			uint16_t                              DetachTimeout; /**< Timeout in milliseconds between a USB_DETACH
-			                                                       *  command being issued and the device detaching
-			                                                       *  from the USB bus
-			                                                       */
-			uint16_t                              TransferSize; /**< Maximum number of bytes the DFU device can accept
-			                                                     *  from the host in a transaction
-			                                                     */
-			uint16_t                              DFUSpecification;	/**< BCD packed DFU specification number this DFU
-			                                                         *  device complies with
-			                                                         */
+			USB_Descriptor_Header_t Header; /**< Standard descriptor header structure */
+
+			uint8_t                 Attributes; /**< DFU device attributes, a mask comprising of the
+			                                     *  ATTR_* macros listed in this source file
+			                                     */
+			uint16_t                DetachTimeout; /**< Timeout in milliseconds between a USB_DETACH
+			                                        *  command being issued and the device detaching
+			                                        *  from the USB bus
+			                                        */
+			uint16_t                TransferSize; /**< Maximum number of bytes the DFU device can accept
+			                                       *  from the host in a transaction
+			                                       */
+			uint16_t                DFUSpecification; /**< BCD packed DFU specification number this DFU
+			                                           *  device complies with
+			                                           */
 		} USB_Descriptor_DFU_Functional_t;
 
 		/** Type define for the device configuration descriptor structure. This must be defined in the
-- 
GitLab