summaryrefslogtreecommitdiff
path: root/common/usbx_pictbridge/src/ux_pictbridge_array_element_to_array_hexa.c
blob: 26a2c752ae2be71a5f8de4a377e300baa0e84c9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Pictbridge Application                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_pictbridge.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_pictbridge_array_element_to_array_hexa          PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function translates an array of elements into an hexa array    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    element                                Address of the element       */
/*    hexa_array                             address of array             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _ux_pictbridge_object_parse                                         */
/*                                                                        */
/**************************************************************************/
UINT  _ux_pictbridge_array_element_to_array_hexa(UCHAR *element, ULONG *hexa_array)
{

ULONG                   element_array_length;
ULONG                   local_hexa_value;
UCHAR                   element_content;
UCHAR                   element_hexa;
UCHAR                   *element_position;
ULONG                   remaining_length;
ULONG                   element_length;
ULONG                   saved_element_length = 0;
UINT                    string_length = UX_PICTBRIDGE_MAX_ELEMENT_SIZE;
UINT                    status;
ULONG                   *hexa_array_end = hexa_array + UX_PICTBRIDGE_MAX_DEVINFO_ARRAY_SIZE;


    /* Get the string length.  */
    status = _ux_utility_string_length_check(element, &string_length, UX_PICTBRIDGE_MAX_ELEMENT_SIZE);
    if (status != UX_SUCCESS)
        return(status);

    /* Get the element length.  */
    element_array_length = string_length + 1;

    /* Parse the entire element array.  */
    while(element_array_length != 0)
    {

        /* Search for the space if delimiter or the terminating 0.  */
        element_position = element;

        /* The length must be saved. */
        remaining_length = element_array_length;

        /* Parse this element.  */
        while(remaining_length !=0)
        {

            /* Check for delimiter.  */
            if (*element_position == UX_PICTBRIDGE_TAG_CHAR_SPACE || *element_position == 0)
            {

                /* We found an element.  Mark its end.  */
                *element_position = 0;

                /* Get the string length, max length 8 for 32-bit hex value.  */
                status = _ux_utility_string_length_check(element, &string_length, 8);
                if (status != UX_SUCCESS)
                    return(status);

                /* Get the element length.  */
                element_length = string_length;

                /* Save it.  */
                saved_element_length =  element_length + 1;

                /* Reset the local hexa value.  */
                local_hexa_value =  0;

                /* We parse the element and build the hexa value one byte at a type.  */
                while(element_length)
                {

                    /* Shift the previous content by 1 nibble.  */
                    local_hexa_value = (local_hexa_value << 4) & 0xFFFFFFFFu;

                    /* Get the element content.  */
                    element_content = *element;

                    /* Check for the element content. Should be >0 <9 or 'A' to 'F'.  */
                    if ((element_content >= '0' && element_content <= '9') ||
                         (element_content >= 'a' && element_content <= 'f') ||
                         (element_content >= 'A' && element_content <= 'F'))
                    {

                        /* We have a valid element content.  Turn it into a hexa decimal value.  */
                        if (element_content >= '0' && element_content <= '9')

                            /* We have a digit.  */
                            element_hexa = (UCHAR)(element_content - '0');

                        else
                        {
                            /* We have a 'A' to 'F' or 'a' to 'f' value.  */
                            if (element_content >= 'a' && element_content <= 'f')

                                /* We have a 'a' to 'f' char.  */
                                element_hexa = (UCHAR)(element_content - 'a' + 10);

                            else

                                /* We have a 'A' to 'F' char.  */
                                element_hexa = (UCHAR)(element_content - 'A' + 10);

                        }
                    }
                    else
                    {

                        /* Reset the last position in the array.  */
                        *hexa_array = 0;

                        /* We have a syntax violation.  */
                        return(UX_ERROR);
                    }

                    /* Add the found value to the current cumulated hexa value.  */
                    local_hexa_value |= element_hexa;

                    /* Next position.  */
                    element++;

                    /* Update length.  */
                    element_length--;

                }

                /* We have finished building the 32 bit hexa value.  Save it in the array.  */
                *hexa_array = local_hexa_value;

                /* Next element in the array.  */
                hexa_array++;

                /* We are done with this element.  */
                break;

            }

            else

                /* Next element position.  */
                element_position++;

            /* Adjust the remaining length.  */
            remaining_length--;
        }

        /* Check if the array is Full.  */
        if (hexa_array == hexa_array_end)
            break;

        /* Increment the element position to the next one if there.  */
        element++;

        /* Adjust the length of the array. */
        element_array_length -= saved_element_length;

    }

    /* Reset the remaining positions in the array.  */
    while(hexa_array != hexa_array_end)
    {
        *hexa_array = 0;
        hexa_array++;
    }

    /* Operation was successful.  */
    return(UX_SUCCESS);
}