1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Pictbridge Application                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_pictbridge.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_pictbridge_array_element_to_array_hexa          PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function translates an array of elements into an hexa array    */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    element                                Address of the element       */
48 /*    hexa_array                             address of array             */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    _ux_pictbridge_object_parse                                         */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
66 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
67 /*                                            resulting in version 6.1    */
68 /*  04-25-2022     Yajun Xia                Modified comment(s),          */
69 /*                                            internal clean up,          */
70 /*                                            resulting in version 6.1.11 */
71 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            limited output array size,  */
73 /*                                            resulting in version 6.3.0  */
74 /*                                                                        */
75 /**************************************************************************/
_ux_pictbridge_array_element_to_array_hexa(UCHAR * element,ULONG * hexa_array)76 UINT  _ux_pictbridge_array_element_to_array_hexa(UCHAR *element, ULONG *hexa_array)
77 {
78 
79 ULONG                   element_array_length;
80 ULONG                   local_hexa_value;
81 UCHAR                   element_content;
82 UCHAR                   element_hexa;
83 UCHAR                   *element_position;
84 ULONG                   remaining_length;
85 ULONG                   element_length;
86 ULONG                   saved_element_length = 0;
87 UINT                    string_length = UX_PICTBRIDGE_MAX_ELEMENT_SIZE;
88 UINT                    status;
89 ULONG                   *hexa_array_end = hexa_array + UX_PICTBRIDGE_MAX_DEVINFO_ARRAY_SIZE;
90 
91 
92     /* Get the string length.  */
93     status = _ux_utility_string_length_check(element, &string_length, UX_PICTBRIDGE_MAX_ELEMENT_SIZE);
94     if (status != UX_SUCCESS)
95         return(status);
96 
97     /* Get the element length.  */
98     element_array_length = string_length + 1;
99 
100     /* Parse the entire element array.  */
101     while(element_array_length != 0)
102     {
103 
104         /* Search for the space if delimiter or the terminating 0.  */
105         element_position = element;
106 
107         /* The length must be saved. */
108         remaining_length = element_array_length;
109 
110         /* Parse this element.  */
111         while(remaining_length !=0)
112         {
113 
114             /* Check for delimiter.  */
115             if (*element_position == UX_PICTBRIDGE_TAG_CHAR_SPACE || *element_position == 0)
116             {
117 
118                 /* We found an element.  Mark its end.  */
119                 *element_position = 0;
120 
121                 /* Get the string length, max length 8 for 32-bit hex value.  */
122                 status = _ux_utility_string_length_check(element, &string_length, 8);
123                 if (status != UX_SUCCESS)
124                     return(status);
125 
126                 /* Get the element length.  */
127                 element_length = string_length;
128 
129                 /* Save it.  */
130                 saved_element_length =  element_length + 1;
131 
132                 /* Reset the local hexa value.  */
133                 local_hexa_value =  0;
134 
135                 /* We parse the element and build the hexa value one byte at a type.  */
136                 while(element_length)
137                 {
138 
139                     /* Shift the previous content by 1 nibble.  */
140                     local_hexa_value = (local_hexa_value << 4) & 0xFFFFFFFFu;
141 
142                     /* Get the element content.  */
143                     element_content = *element;
144 
145                     /* Check for the element content. Should be >0 <9 or 'A' to 'F'.  */
146                     if ((element_content >= '0' && element_content <= '9') ||
147                          (element_content >= 'a' && element_content <= 'f') ||
148                          (element_content >= 'A' && element_content <= 'F'))
149                     {
150 
151                         /* We have a valid element content.  Turn it into a hexa decimal value.  */
152                         if (element_content >= '0' && element_content <= '9')
153 
154                             /* We have a digit.  */
155                             element_hexa = (UCHAR)(element_content - '0');
156 
157                         else
158                         {
159                             /* We have a 'A' to 'F' or 'a' to 'f' value.  */
160                             if (element_content >= 'a' && element_content <= 'f')
161 
162                                 /* We have a 'a' to 'f' char.  */
163                                 element_hexa = (UCHAR)(element_content - 'a' + 10);
164 
165                             else
166 
167                                 /* We have a 'A' to 'F' char.  */
168                                 element_hexa = (UCHAR)(element_content - 'A' + 10);
169 
170                         }
171                     }
172                     else
173                     {
174 
175                         /* Reset the last position in the array.  */
176                         *hexa_array = 0;
177 
178                         /* We have a syntax violation.  */
179                         return(UX_ERROR);
180                     }
181 
182                     /* Add the found value to the current cumulated hexa value.  */
183                     local_hexa_value |= element_hexa;
184 
185                     /* Next position.  */
186                     element++;
187 
188                     /* Update length.  */
189                     element_length--;
190 
191                 }
192 
193                 /* We have finished building the 32 bit hexa value.  Save it in the array.  */
194                 *hexa_array = local_hexa_value;
195 
196                 /* Next element in the array.  */
197                 hexa_array++;
198 
199                 /* We are done with this element.  */
200                 break;
201 
202             }
203 
204             else
205 
206                 /* Next element position.  */
207                 element_position++;
208 
209             /* Adjust the remaining length.  */
210             remaining_length--;
211         }
212 
213         /* Check if the array is Full.  */
214         if (hexa_array == hexa_array_end)
215             break;
216 
217         /* Increment the element position to the next one if there.  */
218         element++;
219 
220         /* Adjust the length of the array. */
221         element_array_length -= saved_element_length;
222 
223     }
224 
225     /* Reset the remaining positions in the array.  */
226     while(hexa_array != hexa_array_end)
227     {
228         *hexa_array = 0;
229         hexa_array++;
230     }
231 
232     /* Operation was successful.  */
233     return(UX_SUCCESS);
234 }
235 
236