Fixed HID report parser corruption when parsing PUSH and POP report item elements.
[pub/lufa.git] / LUFA / Drivers / USB / Class / Common / HIDParser.c
index 4b4667b..87eaf7c 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2012.
+     Copyright (C) Dean Camera, 2018.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2018  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
@@ -18,7 +18,7 @@
   advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
-  The author disclaim all warranties with regard to this
+  The author disclaims all warranties with regard to this
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
@@ -37,12 +37,12 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
                              HID_ReportInfo_t* const ParserData)
 {
        HID_StateTable_t      StateTable[HID_STATETABLE_STACK_DEPTH];
-       HID_StateTable_t*     CurrStateTable          = &StateTable[0];
-       HID_CollectionPath_t* CurrCollectionPath      = NULL;
-       HID_ReportSizeInfo_t* CurrReportIDInfo        = &ParserData->ReportIDSizes[0];
+       HID_StateTable_t*     CurrStateTable     = &StateTable[0];
+       HID_CollectionPath_t* CurrCollectionPath = NULL;
+       HID_ReportSizeInfo_t* CurrReportIDInfo   = &ParserData->ReportIDSizes[0];
        uint16_t              UsageList[HID_USAGE_STACK_DEPTH];
-       uint8_t               UsageListSize           = 0;
-       HID_MinMax_t          UsageMinMax             = {0, 0};
+       uint8_t               UsageListSize      = 0;
+       HID_MinMax_t          UsageMinMax        = {0, 0};
 
        memset(ParserData,       0x00, sizeof(HID_ReportInfo_t));
        memset(CurrStateTable,   0x00, sizeof(HID_StateTable_t));
@@ -92,46 +92,57 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
 
                                memcpy((CurrStateTable + 1),
                                       CurrStateTable,
-                                      sizeof(HID_ReportItem_t));
+                                      sizeof(HID_StateTable_t));
 
                                CurrStateTable++;
                                break;
+
                        case HID_RI_POP(0):
                                if (CurrStateTable == &StateTable[0])
                                  return HID_PARSE_HIDStackUnderflow;
 
                                CurrStateTable--;
                                break;
+
                        case HID_RI_USAGE_PAGE(0):
                                if ((HIDReportItem & HID_RI_DATA_SIZE_MASK) == HID_RI_DATA_BITS_32)
                                  CurrStateTable->Attributes.Usage.Page = (ReportItemData >> 16);
 
                                CurrStateTable->Attributes.Usage.Page       = ReportItemData;
                                break;
+
                        case HID_RI_LOGICAL_MINIMUM(0):
                                CurrStateTable->Attributes.Logical.Minimum  = ReportItemData;
                                break;
+
                        case HID_RI_LOGICAL_MAXIMUM(0):
                                CurrStateTable->Attributes.Logical.Maximum  = ReportItemData;
                                break;
+
                        case HID_RI_PHYSICAL_MINIMUM(0):
                                CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
                                break;
+
                        case HID_RI_PHYSICAL_MAXIMUM(0):
                                CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
                                break;
+
                        case HID_RI_UNIT_EXPONENT(0):
                                CurrStateTable->Attributes.Unit.Exponent    = ReportItemData;
                                break;
+
                        case HID_RI_UNIT(0):
                                CurrStateTable->Attributes.Unit.Type        = ReportItemData;
                                break;
+
                        case HID_RI_REPORT_SIZE(0):
                                CurrStateTable->Attributes.BitSize          = ReportItemData;
                                break;
+
                        case HID_RI_REPORT_COUNT(0):
                                CurrStateTable->ReportCount                 = ReportItemData;
                                break;
+
                        case HID_RI_REPORT_ID(0):
                                CurrStateTable->ReportID                    = ReportItemData;
 
@@ -162,18 +173,22 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
 
                                CurrReportIDInfo->ReportID = CurrStateTable->ReportID;
                                break;
+
                        case HID_RI_USAGE(0):
                                if (UsageListSize == HID_USAGE_STACK_DEPTH)
                                  return HID_PARSE_UsageListOverflow;
 
                                UsageList[UsageListSize++] = ReportItemData;
                                break;
+
                        case HID_RI_USAGE_MINIMUM(0):
                                UsageMinMax.Minimum = ReportItemData;
                                break;
+
                        case HID_RI_USAGE_MAXIMUM(0):
                                UsageMinMax.Maximum = ReportItemData;
                                break;
+
                        case HID_RI_COLLECTION(0):
                                if (CurrCollectionPath == NULL)
                                {
@@ -203,8 +218,8 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
                                {
                                        CurrCollectionPath->Usage.Usage = UsageList[0];
 
-                                       for (uint8_t i = 0; i < UsageListSize; i++)
-                                         UsageList[i] = UsageList[i + 1];
+                                       for (uint8_t i = 1; i < UsageListSize; i++)
+                                         UsageList[i - 1] = UsageList[i];
 
                                        UsageListSize--;
                                }
@@ -214,12 +229,14 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
                                }
 
                                break;
+
                        case HID_RI_END_COLLECTION(0):
                                if (CurrCollectionPath == NULL)
                                  return HID_PARSE_UnexpectedEndCollection;
 
                                CurrCollectionPath = CurrCollectionPath->Parent;
                                break;
+
                        case HID_RI_INPUT(0):
                        case HID_RI_OUTPUT(0):
                        case HID_RI_FEATURE(0):
@@ -239,8 +256,8 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
                                        {
                                                NewReportItem.Attributes.Usage.Usage = UsageList[0];
 
-                                               for (uint8_t i = 0; i < UsageListSize; i++)
-                                                 UsageList[i] = UsageList[i + 1];
+                                               for (uint8_t i = 1; i < UsageListSize; i++)
+                                                 UsageList[i - 1] = UsageList[i];
 
                                                UsageListSize--;
                                        }
@@ -275,7 +292,7 @@ uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
                                }
 
                                break;
-                       
+
                        default:
                                break;
                }
@@ -347,8 +364,8 @@ void USB_SetHIDReportItemInfo(uint8_t* ReportData,
 
        while (DataBitsRem--)
        {
-               if (ReportItem->Value & (1 << (CurrentBit % 8)))
-                 ReportData[CurrentBit / 8] |= BitMask;
+               if (ReportItem->Value & BitMask)
+                 ReportData[CurrentBit / 8] |= (1 << (CurrentBit % 8));
 
                CurrentBit++;
                BitMask <<= 1;