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_decimal_string               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 a decimal string.       */
45 /*                                                                        */
46 /*    Note:                                                               */
47 /*    Number of max_digit_string_size must be large enough to fit digits  */
48 /*    converted.                                                          */
49 /*    The space of the decimal_string buffer must be equal to or larger   */
50 /*    than max_digit_string_size to accept converted characters.          */
51 /*    When leading zeros are off, the converted string ends before        */
52 /*    reaching buffer end and zeros are padded.                           */
53 /*                                                                        */
54 /*  INPUT                                                                 */
55 /*                                                                        */
56 /*    hexa_value                             Value to be translated       */
57 /*    decimal_string                         Where to store the element   */
58 /*    leading_zero_flag                      If leading zeroes are OK     */
59 /*    max_digit_string_size                  Max number of digits (<=8)   */
60 /*                                                                        */
61 /*  OUTPUT                                                                */
62 /*                                                                        */
63 /*    Completion Status                                                   */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    _ux_pictbridge_object_parse                                         */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
77 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            verified memset and memcpy  */
79 /*                                            cases,                      */
80 /*                                            resulting in version 6.1    */
81 /*  04-25-2022     Yajun Xia                Modified comment(s),          */
82 /*                                            fixed maximum decimal       */
83 /*                                            calculation issue,          */
84 /*                                            resulting in version 6.1.11 */
85 /*                                                                        */
86 /**************************************************************************/
_ux_pictbridge_hexa_to_decimal_string(ULONG hexa_value,UCHAR * decimal_string,ULONG leading_zero_flag,ULONG max_digit_string_size)87 UINT  _ux_pictbridge_hexa_to_decimal_string(ULONG hexa_value, UCHAR *decimal_string,
88                                             ULONG leading_zero_flag, ULONG max_digit_string_size)
89 {
90 
91 ULONG                   decimal_string_shift;
92 ULONG                   decimal_value;
93 ULONG                   leading_flag;
94 ULONG                   decimal_max[8] = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999};
95 
96     /* Set the value of the shift decimal.*/
97     switch(max_digit_string_size)
98     {
99         case 1 :
100             decimal_string_shift = 1;
101             break;
102         case 2 :
103             decimal_string_shift = 10;
104             break;
105         case 3 :
106             decimal_string_shift = 100;
107             break;
108         case 4 :
109             decimal_string_shift = 1000;
110             break;
111         case 5 :
112             decimal_string_shift = 10000;
113             break;
114         case 6 :
115             decimal_string_shift = 100000;
116             break;
117         case 7 :
118             decimal_string_shift = 1000000;
119             break;
120         case 8 :
121             decimal_string_shift = 10000000;
122             break;
123         default :
124             return(UX_ERROR);
125     }
126 
127     /* If value exceeds, do not proceed.  */
128     if (hexa_value > decimal_max[max_digit_string_size - 1])
129         return(UX_ERROR);
130 
131     /* Set the leading zero flag.  */
132     if (leading_zero_flag == UX_PICTBRIDGE_LEADING_ZERO_ON)
133         leading_flag = UX_FALSE;
134     else
135         leading_flag = UX_TRUE;
136 
137     /* Reset the decimal_string buffer.  */
138     _ux_utility_memory_set(decimal_string, 0, max_digit_string_size); /* Use case of memset is verified. */
139 
140     /* We parse the hexa value and build the decimal string one byte at a type.  */
141     while(decimal_string_shift)
142     {
143         /* Divide the hexa value by the shift and decode the leading digital.  */
144         decimal_value = hexa_value / decimal_string_shift;
145 
146         /* If the result is non zero, we can insert that decimal in the string.
147            There is a special case if the value is the last decimal or if the leading flag is off. */
148         if (decimal_value != 0 || decimal_string_shift == 1 || leading_flag == UX_FALSE)
149         {
150             /* Insert the value.  */
151             *decimal_string++ = (UCHAR)(decimal_value + '0');
152 
153             /* Reset the leading flag.  */
154             leading_flag = UX_FALSE;
155         }
156 
157         /* Reduce the shift value to the next decimal.  */
158         decimal_string_shift = decimal_string_shift / 10;
159 
160     }
161 
162     /* Operation was successful.  */
163     return(UX_SUCCESS);
164 }
165 
166