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_decimal                   PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function translates an element into a decimal value.           */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    element                                Where to store the element   */
49 /*    decimal_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 /*                                                                        */
70 /**************************************************************************/
_ux_pictbridge_element_to_decimal(UCHAR * element,ULONG * decimal_value)71 UINT  _ux_pictbridge_element_to_decimal(UCHAR *element, ULONG *decimal_value)
72 {
73 
74 ULONG                   element_length;
75 ULONG                   local_decimal_value;
76 UCHAR                   element_content;
77 UCHAR                   element_decimal;
78 ULONG                   multiplier;
79 ULONG                   multiplier_length;
80 UINT                    string_length = 0;
81 UINT                    status;
82 
83 
84     /* Get the string length, max 4294967295 (0xFFFFFFFF), 10 digits.  */
85     status = _ux_utility_string_length_check(element, &string_length, 10);
86     if (status != UX_SUCCESS)
87         return(status);
88 
89     /* Get the element length. Should not be more than 8 characters.  */
90     element_length = string_length;
91 
92     /* Check the length. Error if 0.  */
93     if (element_length == 0)
94 
95         /* We have a syntax violation.  */
96         return(UX_ERROR);
97 
98     /* Reset the local decimal value.  */
99     local_decimal_value =  0;
100 
101     /* Calculate the multiplier value.  First reset it. */
102     multiplier = 1;
103 
104     /* Set the length of the string.  */
105     multiplier_length = element_length;
106 
107     /* Build the multiplier : 10 square length.  */
108     while (multiplier_length-- > 1)
109 
110         /* Add another decimal.  */
111         multiplier = multiplier * 10;
112 
113     /* We parse the element and build the decimal value one byte at a type.  */
114     while(element_length)
115     {
116 
117         /* Get the element content.  */
118         element_content = *element;
119 
120         /* Check for the element content. Should be >0 <9.  */
121         if (element_content >= '0' && element_content <= '9')
122 
123             /* We have a digit.  */
124             element_decimal = (UCHAR)(element_content - '0');
125 
126         else
127             /* We have a syntax violation.  */
128             return(UX_ERROR);
129 
130         /* Add the found value to the current cumulated decimal value.  */
131         local_decimal_value += (ULONG) element_decimal * multiplier;
132 
133         /* Next position.  */
134         element++;
135 
136         /* Update length.  */
137         element_length--;
138 
139         /* Reduce the multiplier by one decimal. */
140         multiplier = multiplier / 10;
141 
142     }
143 
144     /* We have finished building the 32 bit decimal value.  */
145     *decimal_value = local_decimal_value;
146 
147     /* Operation was successful.  */
148     return(UX_SUCCESS);
149 }
150 
151