GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_utility_descriptor_parse.c Lines: 37 39 94.9 %
Date: 2026-03-06 18:57:10 Branches: 15 16 93.8 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** USBX Component                                                        */
17
/**                                                                       */
18
/**   Utility                                                             */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
24
/* Include necessary system files.  */
25
26
#define UX_SOURCE_CODE
27
28
#include "ux_api.h"
29
30
31
/**************************************************************************/
32
/*                                                                        */
33
/*  FUNCTION                                               RELEASE        */
34
/*                                                                        */
35
/*    _ux_utility_descriptor_parse                        PORTABLE C      */
36
/*                                                           6.3.0        */
37
/*  AUTHOR                                                                */
38
/*                                                                        */
39
/*    Chaoqiong Xiao, Microsoft Corporation                               */
40
/*                                                                        */
41
/*  DESCRIPTION                                                           */
42
/*                                                                        */
43
/*    This function will unpack a USB descriptor from the bus into a      */
44
/*    memory aligned structure.                                           */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    raw_descriptor                        Pointer to packed descriptor  */
49
/*    descriptor_structure                  Components of the descriptor  */
50
/*    descriptor_entries                    Number of entries in the      */
51
/*                                            descriptor                  */
52
/*    descriptor                            Pointer to the unpacked       */
53
/*                                            descriptor                  */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    None                                                                */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    _ux_utility_long_get                  Get 32-bit value              */
62
/*    _ux_utility_short_get                 Get 16-bit value              */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    USBX Components                                                     */
67
/*                                                                        */
68
/**************************************************************************/
69
22067
VOID  _ux_utility_descriptor_parse(UCHAR * raw_descriptor, UCHAR * descriptor_structure,
70
                        UINT descriptor_entries, UCHAR * descriptor)
71
{
72
73
    /* Loop on all the entries in this descriptor.  */
74
217347
    while(descriptor_entries--)
75
    {
76
77
        /* Get the length of that component.  */
78
195280
        switch(*descriptor_structure++)
79
        {
80
81
        /* Check the size then build the component from the source and
82
           insert it into the target descriptor.  */
83
527
        case 4:
84
85
            /* Padding zeros so address is aligned.  */
86
697
            while((ALIGN_TYPE) descriptor & 3u)
87
170
                *descriptor++ =  0;
88
89
            /* Save the DW.  */
90
527
            *((ULONG *) descriptor) =  _ux_utility_long_get(raw_descriptor);
91
527
            raw_descriptor +=  4;
92
527
            descriptor += 4;
93
527
            break;
94
95
26744
        case 2:
96
97
            /* Padding zeros so address is aligned.  */
98
27296
            while((ALIGN_TYPE) descriptor & 1u)
99
552
                *descriptor++ =  0;
100
101
            /* Save the word.  */
102
26744
            *((USHORT *) descriptor) = (USHORT) _ux_utility_short_get(raw_descriptor);
103
26744
            raw_descriptor += 2;
104
26744
            descriptor += 2;
105
26744
            break;
106
107
168009
        default:
108
109
            /* Save the byte.  */
110
168009
            *((UCHAR *) descriptor) =  (UCHAR) *raw_descriptor;
111
168009
            raw_descriptor++;
112
168009
            descriptor ++;
113
        }
114
    }
115
116
    /* Return to caller.  */
117
22067
    return;
118
}
119
120
/**************************************************************************/
121
/*                                                                        */
122
/*  FUNCTION                                               RELEASE        */
123
/*                                                                        */
124
/*    _ux_utility_descriptor_parse_size                   PORTABLE C      */
125
/*                                                           6.3.0        */
126
/*  AUTHOR                                                                */
127
/*                                                                        */
128
/*    Chaoqiong Xiao, Microsoft Corporation                               */
129
/*                                                                        */
130
/*  DESCRIPTION                                                           */
131
/*                                                                        */
132
/*    This function will calculate the size of a parsed USB descriptor.   */
133
/*                                                                        */
134
/*  INPUT                                                                 */
135
/*                                                                        */
136
/*    descriptor_structure                  Components of the descriptor  */
137
/*    descriptor_entries                    Number of entries in the      */
138
/*                                            descriptor                  */
139
/*    size_align_mask                       Size alignment mask           */
140
/*                                                                        */
141
/*  OUTPUT                                                                */
142
/*                                                                        */
143
/*    size                                  Size of the parsed descriptor */
144
/*                                                                        */
145
/*  CALLS                                                                 */
146
/*                                                                        */
147
/*    None                                                                */
148
/*                                                                        */
149
/*  CALLED BY                                                             */
150
/*                                                                        */
151
/*    USBX Components                                                     */
152
/*                                                                        */
153
/**************************************************************************/
154
25
ULONG _ux_utility_descriptor_parse_size(UCHAR * descriptor_structure, UINT descriptor_entries, UINT size_align_mask)
155
{
156
157
25
ULONG           size = 0;
158
ULONG           entry_size;
159
160
    /* Loop on all the entries in this descriptor.  */
161
223
    while(descriptor_entries--)
162
    {
163
164
        /* Get entry size.  */
165
198
        entry_size = (ULONG)*descriptor_structure ++;
166
167
        /* Check the size then build the component from the source and
168
           insert it into the target descriptor.  */
169
198
        switch(entry_size)
170
        {
171
172
54
        case 4: /* Fall through.  */
173
        case 2:
174
175
            /* Padding zeros so address is aligned.  */
176
64
            while(size & (entry_size - 1))
177
10
                size++;
178
179
            /* Add to the size.  */
180
54
            size += entry_size;
181
54
            break;
182
183
144
        case 1:
184
185
            /* Add to the size.  */
186
144
            size += 1;
187
144
            break;
188
189
        default:
190
191
            /* Invalid entry size.  */
192
            return(0);
193
        }
194
    }
195
196
    /* Align the size.  */
197
25
    size = (size + size_align_mask) & (~size_align_mask);
198
199
    /* Return the size.  */
200
25
    return(size);
201
}