1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** USBX Component */
17 /** */
18 /** Pictbridge Application */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23
24 /* Include necessary system files. */
25
26 #define UX_SOURCE_CODE
27
28 #include "ux_api.h"
29 #include "ux_pictbridge.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_pictbridge_element_to_hexa PORTABLE C */
37 /* 6.1.11 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function translates an element into an hexa value. */
45 /* */
46 /* INPUT */
47 /* */
48 /* element Where to store the element */
49 /* hexa_value Value to be returned */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* Completion Status */
54 /* */
55 /* CALLS */
56 /* */
57 /* */
58 /* CALLED BY */
59 /* */
60 /* _ux_pictbridge_object_parse */
61 /* */
62 /* RELEASE HISTORY */
63 /* */
64 /* DATE NAME DESCRIPTION */
65 /* */
66 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
67 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
68 /* resulting in version 6.1 */
69 /* 04-25-2022 Yajun Xia Modified comment(s), */
70 /* internal clean up, */
71 /* resulting in version 6.1.11 */
72 /* */
73 /**************************************************************************/
_ux_pictbridge_element_to_hexa(UCHAR * element,ULONG * hexa_value)74 UINT _ux_pictbridge_element_to_hexa(UCHAR *element, ULONG *hexa_value)
75 {
76
77 ULONG element_length;
78 ULONG local_hexa_value;
79 UCHAR element_content;
80 UCHAR element_hexa;
81 UINT string_length = 0;
82 UINT status;
83
84
85 /* Get the string length, max 0xFFFFFFFF, 8 digits. */
86 status = _ux_utility_string_length_check(element, &string_length, 8);
87 if (status != UX_SUCCESS)
88 return(status);
89
90 /* Get the element length. Should not be more than 8 characters. */
91 element_length = string_length;
92
93 /* Check the length. Error if 0. */
94 if (element_length == 0)
95
96 /* We have a syntax violation. */
97 return(UX_ERROR);
98
99 /* Reset the local hexa value. */
100 local_hexa_value = 0;
101
102 /* We parse the element and build the hexa value one byte at a type. */
103 while(element_length)
104 {
105
106 /* Shift the previous content by 1 nibble. */
107 local_hexa_value = (local_hexa_value << 4) & 0xFFFFFFFFu;
108
109 /* Get the element content. */
110 element_content = *element;
111
112 /* Check for the element content. Should be >0 <9 or 'A' to 'F'. */
113 if ((element_content >= '0' && element_content <= '9') ||
114 (element_content >= 'a' && element_content <= 'f') ||
115 (element_content >= 'A' && element_content <= 'F'))
116 {
117
118 /* We have a valid element content. Turn it into a hexa decimal value. */
119 if (element_content >= '0' && element_content <= '9')
120
121 /* We have a digit. */
122 element_hexa = (UCHAR)(element_content - '0');
123
124 else
125 {
126
127 /* We have a 'A' to 'F' or 'a' to 'f' value. */
128 if (element_content >= 'a' && element_content <= 'f')
129
130 /* We have a 'a' to 'f' char. */
131 element_hexa = (UCHAR)(element_content - 'a' + 10);
132
133 else
134
135 /* We have a 'A' to 'F' char. */
136 element_hexa = (UCHAR)(element_content - 'A' + 10);
137
138
139 }
140 }
141 else
142 /* We have a syntax violation. */
143 return(UX_ERROR);
144
145 /* Add the found value to the current cumulated hexa value. */
146 local_hexa_value |= element_hexa;
147
148 /* Next position. */
149 element++;
150
151 /* Update length. */
152 element_length--;
153
154 }
155
156 /* We have finished building the 32 bit hexa value. */
157 *hexa_value = local_hexa_value;
158
159 /* Operation was successful. */
160 return(UX_SUCCESS);
161 }
162
163