summaryrefslogtreecommitdiff
path: root/common/usbx_pictbridge/src/ux_pictbridge_object_parse.c
blob: 565292eed6c49c6101b0bf061628c3d0ba974caf (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/***************************************************************************
 * 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_object_parse                         PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function parses a XML based pictbridge object.                 */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    pictbridge                             Pictbridge instance          */
/*    xml_object_buffer                      Pointer to object buffer     */
/*    xml_object_length                      Length of the object         */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _ux_pictbridge_object_get                                           */
/*                                                                        */
/**************************************************************************/
UINT  _ux_pictbridge_object_parse(UX_PICTBRIDGE *pictbridge, UCHAR *xml_object_buffer,
                                    ULONG xml_object_length)
{
UX_PICTBRIDGE_XML_ITEM                  *tag_item;
UX_PICTBRIDGE_XML_ITEM                  *tag_entry =  UX_NULL;
UCHAR                                   tag_name[UX_PICTBRIDGE_MAX_TAG_SIZE];
UCHAR                                   variable_name[UX_PICTBRIDGE_MAX_VARIABLE_SIZE];
UCHAR                                   variable_string[UX_PICTBRIDGE_MAX_STRING_SIZE];
UCHAR                                   xml_parameter[UX_PICTBRIDGE_MAX_STRING_SIZE];
UX_PICTBRIDGE_XML_ITEM                  *tag_history[UX_PICTBRIDGE_MAX_TAG_DEPTH];
ULONG                                   tag_history_index;
ULONG                                   tag_flag;
ULONG                                   closing_tag_count;
UINT                                    tag_name_length;
UINT                                    tag_history_length;
UINT                                    status;

    /* Set the tag position at root.  */
    tag_item = _ux_pictbridge_xml_item_root;

    /* Clear the input tags.  */
    pictbridge -> ux_pictbridge_input_tags =  0;

    /* Clear the input request.  */
    pictbridge -> ux_pictbridge_input_request =  0;

    /* Tag history index is at root.  */
    tag_history_index =  0;

    /* Closing tag count is reset.  */
    closing_tag_count = 0;

    /* This variable remains untouched if the XML object's length is 0.  */
    status =  UX_SUCCESS;

    /* We parse the object until all items are parsed or there is a syntax error
       or a short object.  */
    while(xml_object_length)
    {

        /* Scan the object buffer for a tag.  */
        status = _ux_pictbridge_tag_name_get(xml_object_buffer, xml_object_length, tag_name,
                                        variable_name, variable_string, xml_parameter,
                                        &xml_object_buffer, &xml_object_length, &tag_flag);

        /* We may have an error.  Check if this is an empty line in which case we are done. */
        if (status != UX_SUCCESS)
        {

            /* Check for empty line.  */
            if (status == UX_PICTBRIDGE_ERROR_EMPTY_LINE)

                /* Yes, we have an empty line. Do not proceed but we have a successful completion. */
                status = UX_SUCCESS;

            /* Do not proceed.  */
            break;
        }

        /* Check if this is a closing tag ? */
        if (tag_flag & UX_PICTBRIDGE_TAG_FLAG_CLOSING)
        {

            /* Assume the worst.  */
            status = UX_PICTBRIDGE_ERROR_SCRIPT_SYNTAX_ERROR;

            /* Ensure we do not have a closing tag without a prior normal tag entry.  */
            if (tag_history_index == 0)

                /* Syntax error.  */
                break;

            /* One step back in the tag history.  */
            tag_history_index--;

            /* Calculate the length of the tag item.  */
            status = _ux_utility_string_length_check(tag_name, &tag_name_length, UX_PICTBRIDGE_MAX_TAG_SIZE);
            if (status != UX_SUCCESS)

                /* Bad name, we should never here if _tag_name_get is OK.  */
                break;

            /* Calculate the length of the current tag name in the tag history.  */
            status = _ux_utility_string_length_check(tag_history[tag_history_index] -> ux_pictbridge_xml_item_tag_name, &tag_history_length, UX_PICTBRIDGE_MAX_TAG_SIZE);
            if (status != UX_SUCCESS)

                /* Bad name, we should never here if _tag_name_get is OK.  */
                return(status);

            /* If both length do not match, no need to check for a match.  */
            if (tag_history_length == tag_name_length)
            {

                /* Both length match, we may have a tag match. Check the names */
                if (_ux_utility_memory_compare(tag_name, tag_history[tag_history_index] -> ux_pictbridge_xml_item_tag_name,
                                        tag_name_length) != UX_SUCCESS)

                    /* Syntax error.  */
                    break;
            }
            else

                /* Syntax error.  */
                break;

            /* Increment the closing tag count. */
            closing_tag_count++;

            /* If we have more than one closing tag consecutively, look for the parent.  */
            if (closing_tag_count > 1)
            {

                /* Get the parent for this item and set it as the current tag_item.  We may already be at the root.  */
                if (tag_entry -> ux_pictbridge_xml_item_parent != UX_NULL)

                    /* We have a parent, set it.  */
                    tag_item =  tag_entry -> ux_pictbridge_xml_item_parent;
            }

            /* We set status to success.  */
            status = UX_SUCCESS;

        }
        else
        {

            /* The tag name is in the tag_name variable but has not been verified yet.  */
            status = _ux_pictbridge_tag_name_scan(tag_item, tag_name, &tag_entry);

            /* We may have an error.  */
            if (status != UX_SUCCESS)

                /* Do not proceed.  */
                break;

            /* Check if this is a comment tag ? */
            if ((tag_flag & UX_PICTBRIDGE_TAG_FLAG_COMMENT) == 0)
            {

                /* Reset the closing tag count.  */
                closing_tag_count =  0;

                /* Check if the tag history depth is fine for saving.  */
                if (tag_history_index >= UX_PICTBRIDGE_MAX_TAG_DEPTH)
                {

                    /* Syntax error.  */
                    status = UX_BUFFER_OVERFLOW;

                    /* Do not proceed.  */
                    break;
                }

                /* Save the current tag in the tag history.  */
                tag_history[tag_history_index] = tag_entry;

                /* Increase the current tag index.  */
                tag_history_index++;

                /* Check if there is a main tag specified in the tag entry. */
                if (tag_entry -> ux_pictbridge_xml_item_tag_code != 0)
                {

                    /* If this is a leaf, the tag is not a main request but a variable to be returned.  */
                    if (tag_entry -> ux_pictbridge_xml_item_child == UX_PICTBRIDGE_XML_LEAF)

                        /* This is a variable. Add it to the other ones.  */
                        pictbridge -> ux_pictbridge_input_tags |=  tag_entry -> ux_pictbridge_xml_item_tag_code;

                    else

                        /* Yes we have a tag code, memorize it.  This is a main input request. */
                        pictbridge -> ux_pictbridge_input_request =  tag_entry -> ux_pictbridge_xml_item_tag_code;
                }
                /* We may have a function associated with this tag. Do a sanity check.  */
                if (tag_entry -> ux_pictbridge_xml_item_function != UX_NULL)
                {
                    /* There is a function associated, so call it.  */
                    status = tag_entry -> ux_pictbridge_xml_item_function(pictbridge, variable_name, variable_string, xml_parameter);

                    /* We may have an error.  */
                    if (status != UX_SUCCESS)

                       /* Do not proceed.  */
                        break;
                }

                /* Check if the tag has a child attached to it.
                   If there is neither a child or a function, it means we do not treat this tag name. */
                if (tag_entry -> ux_pictbridge_xml_item_child != UX_NULL && tag_entry -> ux_pictbridge_xml_item_child != UX_PICTBRIDGE_XML_LEAF)
                {

                    /* We have a child, set it.  */
                    tag_item =  tag_entry -> ux_pictbridge_xml_item_child;

                    /* Reset the tag codes.  */
                    pictbridge -> ux_pictbridge_input_tags =  0;
                }

                /* Check if this is a self closing tag ? */
                if (tag_flag & UX_PICTBRIDGE_TAG_FLAG_SELF_CLOSING)
                {

                    /* Assume the worst.  */
                    status = UX_PICTBRIDGE_ERROR_SCRIPT_SYNTAX_ERROR;

                    /* Ensure we do not have a closing tag without a prior normal tag entry.  */
                    if (tag_history_index == 0)

                        /* Syntax error.  */
                        break;

                    /* One step back in the tag history.  */
                    tag_history_index--;

                    /* Calculate the length of the tag item.   */
                    status = _ux_utility_string_length_check(tag_name, &tag_name_length, UX_PICTBRIDGE_MAX_TAG_SIZE);
                    if (status != UX_SUCCESS)

                        /* Bad name, we should never here if _tag_name_scan is OK!  */
                        break;

                    /* Calculate the length of the current tag name in the tag history.  */
                    status = _ux_utility_string_length_check(tag_history[tag_history_index] -> ux_pictbridge_xml_item_tag_name, &tag_history_length, UX_PICTBRIDGE_MAX_TAG_SIZE);
                    if (status != UX_SUCCESS)

                        /* Bad name, we should never here if _tag_name_scan is OK!  */
                        break;

                    /* If both length do not match, no need to check for a match.  */
                    if (tag_history_length == tag_name_length)
                    {

                        /* Both length match, we may have a tag match. Check the names */
                        if (_ux_utility_memory_compare(tag_name, tag_history[tag_history_index] -> ux_pictbridge_xml_item_tag_name,
                                            tag_name_length) != UX_SUCCESS)

                            /* Syntax error.  */
                            break;
                    }
                    else

                        /* Syntax error.  */
                        break;

                    /* Increment the closing tag count. */
                    closing_tag_count++;

                    /* We set status to success.  */
                    status = UX_SUCCESS;

                }
            }
        }
    }

    /* Return completion status.  */
    return(status);
}