-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
360 lines (323 loc) · 10.6 KB
/
parser.c
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/****************************************************************************
*
* Copyright (c) 2017 Windhover Labs, L.L.C. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name Windhover Labs nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
#include "parser.h"
#include <string.h>
#include <stdio.h>
bool json_parse(json_object * jobj, unsigned int *call_count, explain_data_t *handle)
{
enum json_type type = 0;
bool returnBool = false;
/* Null pointer check */
if(0 == jobj || 0 == call_count)
{
goto end_of_function;
}
/* Recursive call stack depth check */
if (*call_count >= MAX_RECURSIVE_CALL_COUNT)
{
printf("Recursive call stack exceeded %d\n", *call_count);
goto end_of_function;
}
else
{
*call_count = *call_count + 1;
}
json_object_object_foreach(jobj, key, val)
{
type = json_object_get_type(val);
switch (type)
{
case json_type_boolean:
{
printf("Unknown json type boolean in parser\n");
break;
}
case json_type_double:
{
printf("Unknown json type double in parser\n");
break;
}
case json_type_int:
{
returnBool = parse_json_value(val, key, handle);
break;
}
case json_type_string:
{
returnBool = parse_json_value(val, key, handle);
break;
}
case json_type_object:
{
printf("Unknown json type object in parser\n");
//jobj = json_object_object_get(jobj, key);
//json_parse(jobj, state, cursor);
break;
}
case json_type_array:
{
//printf("json_type_array\n");
returnBool = json_parse_array(jobj, key, call_count, handle);
break;
}
default :
{
printf("Unkown json type in parser\n");
}
}
}
end_of_function:
return (returnBool);
}
bool json_parse_array(json_object *jobj, const char *key, unsigned int *call_count, explain_data_t *handle)
{
unsigned int i = 0;
enum json_type type = 0;
bool returnBool = false;
/* Null pointer check */
if(0 == jobj || 0 == key || 0 == call_count)
{
goto end_of_function;
}
/* Recursive call stack depth check */
if (*call_count >= MAX_RECURSIVE_CALL_COUNT)
{
printf("Recursive call stack exceeded in array %d\n", *call_count);
goto end_of_function;
}
else
{
*call_count = *call_count + 1;
}
json_object *jarray = jobj;
/* Simply get the array. */
if(key)
{
/* Getting the array if it is a key value pair. */
jarray = json_object_object_get(jobj, key);
}
/* Getting the length of the array. */
int arraylen = json_object_array_length(jarray);
//printf("Array Length: %d\n", arraylen);
json_object * jvalue;
for (i = 0; i < arraylen; ++i)
{
/* Getting the array element at position i */
jvalue = json_object_array_get_idx(jarray, i);
type = json_object_get_type(jvalue);
if (type == json_type_array)
{
returnBool = json_parse_array(jvalue, NULL, call_count, handle);
}
else if (type != json_type_object)
{
parse_json_value(jvalue, key, handle);
}
else
{
returnBool = json_parse(jvalue, call_count, handle);
}
}
end_of_function:
return (returnBool);
}
bool parse_json_value(json_object *jobj, const char* key, explain_data_t *handle)
{
enum json_type type = 0;
bool returnBool = false;
/* Null pointer check */
if(0 == jobj || 0 == key)
{
goto end_of_function;
}
/* Getting the type of the json object */
type = json_object_get_type(jobj);
switch (type)
{
case json_type_boolean:
{
printf("Unknown json type boolean in parser\n");
break;
}
case json_type_double:
{
printf("Unknown json type double in parser\n");
break;
}
case json_type_int:
{
returnBool = load_int(jobj, key, handle);
break;
}
case json_type_string:
{
returnBool = load_string(jobj, key, handle);
break;
}
default :
{
printf("Unkown json type value in parser\n");
}
}
end_of_function:
return (returnBool);
}
bool load_int(json_object * jobj, const char *key, explain_data_t *handle)
{
bool returnBool = false;
int value = json_object_get_int(jobj);
/* Null pointer check */
if(0 == jobj || 0 == key)
{
goto end_of_function;
}
if (0 == strncmp(key, LENGTH_KEY, MAX_KEY_LENGTH))
{
//printf("got key fields length %d\n", value);
handle->currentField.length = value;
returnBool = true;
}
else if (0 == strncmp(key, SOURCE_OFFSET_KEY, MAX_KEY_LENGTH))
{
//printf("got key fields source %d\n", value);
handle->currentField.srcOffset = value;
returnBool = true;
}
else if (0 == strncmp(key, DESTINATION_OFFSET_KEY, MAX_KEY_LENGTH))
{
//printf("got key fields destination %d\n", value);
handle->currentField.dstOffset = value;
/* For now assume a field is complete. */
/* TODO error checking. */
(void) explain_field_add(handle->currentMsg, &handle->currentField);
returnBool = true;
}
else
{
/* Error state */
printf("got unknown key in load_int %s\n", key);
}
end_of_function:
return (returnBool);
}
bool load_string(json_object * jobj, const char *key, explain_data_t *handle)
{
bool returnBool = FALSE;
const char *value = json_object_get_string(jobj);
/* Null pointer check */
if(0 == jobj || 0 == key)
{
goto end_of_function;
}
/* Check keys for known values */
if (0 == strncmp(key, IDENTIFICATION_KEY, MAX_KEY_LENGTH))
{
handle->currentMsg->id = strtoul(value, NULL, 16);
//printf("got key bitmap id %d\n", handle->currentMsg->id);
returnBool = true;
}
else if (0 == strncmp(key, DESTINATION_SYMBOL, MAX_KEY_LENGTH))
{
strncpy(handle->currentMsg->dstSymbol, value, EXPLAIN_MAX_OPS_NAME_LENGTH);
//printf("got key bitmap dest symbol %s\n", handle->currentMsg->opsName);
returnBool = true;
}
else if (0 == strncmp(key, SOURCE_SYMBOL, MAX_KEY_LENGTH))
{
strncpy(handle->currentMsg->srcSymbol, value, EXPLAIN_MAX_OPS_NAME_LENGTH);
//printf("got key bitmap src symbol %s\n", handle->currentMsg->opsName);
returnBool = true;
}
else if (0 == strncmp(key, OPS_MESSAGE_NAME_KEY, MAX_KEY_LENGTH))
{
strncpy(handle->currentMsg->opsName, value, EXPLAIN_MAX_OPS_NAME_LENGTH);
//printf("got key bitmap ops_name %s\n", handle->currentMsg->opsName);
returnBool = true;
}
else if (0 == strncmp(key, OPS_FIELD_NAME_KEY, MAX_KEY_LENGTH))
{
strncpy(handle->currentField.opName, value, EXPLAIN_MAX_OPS_NAME_LENGTH);
//printf("got key field op_name %s\n", handle->currentField.opName);
returnBool = true;
}
else if (0 == strncmp(key, SOURCE_ENDIANNESS_KEY, MAX_KEY_LENGTH))
{
/* Decode endianess. */
if(0 == strncmp(value, LITTLE_ENDIAN_VALUE, MAX_ENDIANNESS_LENGTH))
{
handle->currentMsg->srcEndian = EXPLAIN_LITTLE_ENDIAN;
returnBool = true;
}
else if (0 == strncmp(value, BIG_ENDIAN_VALUE, MAX_ENDIANNESS_LENGTH))
{
handle->currentMsg->srcEndian = EXPLAIN_BIG_ENDIAN;
returnBool = true;
}
else
{
/* TODO error condition unknown endianness value. */
printf("Unknown endianness value in parser, got %s\n", value);
}
//printf("got key field src endian %d\n", app_data.currentMsg->srcEndian);
}
else if (0 == strncmp(key, DEST_ENDIANNESS_KEY, MAX_KEY_LENGTH))
{
/* Decode endianness. */
if(0 == strncmp(value, LITTLE_ENDIAN_VALUE, MAX_ENDIANNESS_LENGTH))
{
handle->currentMsg->dstEndian = EXPLAIN_LITTLE_ENDIAN;
returnBool = true;
}
else if (0 == strncmp(value, BIG_ENDIAN_VALUE, MAX_ENDIANNESS_LENGTH))
{
handle->currentMsg->dstEndian = EXPLAIN_BIG_ENDIAN;
returnBool = true;
}
else
{
/* TODO error condition unknown endianness value. */
printf("Unknown endianness value in parser, got %s\n", value);
}
//printf("got key field dst endian %d\n", app_data.currentMsg->dstEndian);
/* For now assume we've completed a message. */
/* Add a new message. */
handle->currentMsg = explain_message_add(&handle->message_list);
}
else
{
/* Error state */
printf("got unknown key in load_string %s\n", key);
}
end_of_function:
return (returnBool);
}