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_hexa_to_element                      PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function translates an hexa value into an element.             */
45 /*                                                                        */
46 /*    Note: the size of the element buffer must be equal to or larger than*/
47 /*    8 to fill all converted characters.                                 */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    hexa_value                             Value to be translated       */
52 /*    element                                Where to store the element   */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _ux_pictbridge_object_parse                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            verified memset and memcpy  */
72 /*                                            cases,                      */
73 /*                                            resulting in version 6.1    */
74 /*  04-25-2022     Yajun Xia                Modified comment(s),          */
75 /*                                            internal clean up,          */
76 /*                                            resulting in version 6.1.11 */
77 /*                                                                        */
78 /**************************************************************************/
_ux_pictbridge_hexa_to_element(ULONG hexa_value,UCHAR * element)79 UINT  _ux_pictbridge_hexa_to_element(ULONG hexa_value, UCHAR *element)
80 {
81 
82 ULONG                   element_length;
83 ULONG                   local_hexa_value;
84 UCHAR                   element_hexa;
85 ULONG                   element_shift;
86 
87     /* Reset the element buffer.  */
88     _ux_utility_memory_set(element, 0, 8); /* Use case of memset is verified. */
89 
90     /* Reset the value of the length.*/
91     element_length = 0;
92 
93     /* Init the shift value.  */
94     element_shift = 32 - 4;
95 
96     /* We parse the hexa value element and build the hexa value one byte at a type.  */
97     while(element_length < 8)
98     {
99 
100         /* Shift the 4 bit value we are interested in.  We keep the lowest nibble.  */
101         local_hexa_value = (hexa_value >> element_shift) & 0x0f;
102 
103         /* See if this value is from 0-9 or A to F.  */
104         if (local_hexa_value <= 9)
105 
106             /* We have a digit.  */
107             element_hexa = (UCHAR)(local_hexa_value + '0');
108 
109         else
110 
111             /* We have  'A' to 'F' value.  */
112             element_hexa = (UCHAR)(local_hexa_value - 10 + 'A');
113 
114         /* Store the converted hexa value.  */
115         *element = element_hexa;
116 
117         /* Next position.  */
118         element++;
119 
120         /* Update length.  */
121         element_length++;
122 
123         /* Continue shifting by one nibble.  */
124         if (element_shift >= 4)
125         {
126             element_shift = element_shift - 4;
127         }
128         else
129         {
130             break;
131         }
132     }
133 
134     /* Operation was successful.  */
135     return(UX_SUCCESS);
136 }
137 
138