-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjparser.c
More file actions
577 lines (503 loc) · 14.2 KB
/
jparser.c
File metadata and controls
577 lines (503 loc) · 14.2 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**
* jparser.c
*
* Author: Haoling Zhou
*
* A custom JSON parser for the confidential inference project.
* This implementation does not parse unicode.
*
* July 2025
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "ordered_map.h"
#define sys_error(sys) \
do { perror(sys); exit(EXIT_FAILURE); } while (0)
#define parse_error(msg) \
do { printf(msg); exit(EXIT_FAILURE); } while (0)
#define PAGE 4096
#define ASCII_MAX 127
#define MAX_KEY_LEN 100 // enough for confidential inference
#define SIZE_ORDERED_MAP 20
#define SIZE_DYNAMIC_ARRAY 10
#define SIZE_SHORT_STRING 50
#define SIZE_NUM_STRING 100
#define TEST
typedef enum {
TYPE_OBJECT,
TYPE_ARRAY,
TYPE_STRING,
TYPE_NULL,
TYPE_NUMBER,
TYPE_BOOL
} json_data_type;
/**
* The null data type does not need space.
*/
typedef union {
ordered_map *obj; // object
dynamic_array *arr; // array
char *str; // string
char *num; // number
bool boo; // true | false
} json_data;
typedef struct {
json_data *data;
json_data_type type;
int layer;
} json_text;
json_text *parse_any(char *buf, int *idx, int size, int layer);
size_t hash_func(char *key, size_t table_size) {
size_t idx;
for (char *s = key; *s != '\0'; s++)
idx += *s;
if (idx >= table_size)
idx %= table_size;
return idx;
}
json_text *construct_json_text(int layer) {
json_text *res = (json_text *)malloc(sizeof(json_text));
res->data = (json_data *)malloc(sizeof(json_data));
res->type = -1;
res->layer = layer;
return res;
}
ordered_map *construct_ordered_map() {
ordered_map *res = (ordered_map *)malloc(sizeof(ordered_map));
res->size = SIZE_ORDERED_MAP;
res->lists = (chain_list **)malloc(sizeof(chain_list *) * SIZE_ORDERED_MAP);
res->node_list = (dynamic_array *)malloc(sizeof(dynamic_array));
res->node_list->size = 0;
res->node_list->capacity = 30; // initial total number of keys
res->node_list->elements = (void **)malloc(sizeof(void *) * 30);
for (int i = 0; i < SIZE_ORDERED_MAP; i++) {
res->lists[i] = (chain_list *)malloc(sizeof(chain_list));
res->lists[i]->head = NULL;
}
return res;
}
chain_node *construct_chain_node() {
chain_node *node = (chain_node *)malloc(sizeof(chain_node));
return node;
}
dynamic_array *construct_dynamic_array() {
dynamic_array *new_arr = (dynamic_array *)malloc(sizeof(dynamic_array));
new_arr->size = 0;
new_arr->capacity = SIZE_DYNAMIC_ARRAY;
new_arr->elements = (void **)malloc(sizeof(void *) * SIZE_DYNAMIC_ARRAY);
return new_arr;
}
/**
* free_json_text - recursively free the JSON text
* @jtext: root JSON text
*/
void free_json_text(json_text *jtext) {
json_data *data = jtext->data;
switch (jtext->type) {
case TYPE_STRING:
free(data->str);
break;
case TYPE_NUMBER:
free(data->num);
break;
case TYPE_ARRAY:
for (int i = 0; i < data->arr->size; i++) {
free_json_text((json_text *)data->arr->elements[i]); // recursive free
}
free(data->arr->elements);
free(data->arr);
break;
case TYPE_OBJECT:
for (int i = 0; i < data->obj->node_list->size; i++) {
chain_node *node = (chain_node *)data->obj->node_list->elements[i];
free(node->prev);
free(node->next);
free(node->key);
free_json_text((json_text *)node->val); // recursive free
free(node);
}
free(data->obj->node_list);
for (int j = 0; j < data->obj->size; j++) {
/**
* free the obj->lists[j]->head here would be double-free because head
* is not a dummy node. Head is already freed as a chain node.
*/
free(data->obj->lists[j]);
}
free(data->obj->lists);
free(data->obj);
break;
default:
}
free(data);
free(jtext);
}
bool is_digit(char c) {
return c - '0' >= 0 && c - '9' <= 9;
}
bool is_whitespace_char(char c) {
return c == ' ' || c == 'b' || c == 'f' ||
c == 'n' || c == 'r' || c == 't';
}
bool is_whitespace(char c) {
return c == ' ' || c == '\b' || c == '\f' ||
c == '\n' || c == '\r' || c == '\t';
}
void parse_whitespace(char *buf, int *idx, int size) {
while (buf[*idx] == ' ' || buf[*idx] == '\t' || buf[*idx] == '\n' || buf[*idx] == '\f' || buf[*idx] == '\r' || buf[*idx] == '\v')
(*idx)++;
}
/**
* parse_num - parse a valid number
* @buf: original plaintext
* @idx: index of the character which should be examined next
* @size: size of @buf, used to bound the search
*/
char *parse_num(char *buf, int *idx, int size) {
char *new_num = (char *)malloc(sizeof(char) * SIZE_NUM_STRING);
int i = 0;
while (!is_whitespace(buf[*idx]) && buf[*idx] != ',' && buf[*idx] != '}' && buf[*idx] != ']' && i < size) {
if (!is_digit(buf[*idx])) {
printf("buf[*idx]: %c\n", buf[*idx]);
parse_error("parse_num: invalid number\n");
}
new_num[i] = buf[*idx];
i++;
(*idx)++;
}
if (is_digit(buf[*idx]) || i == size)
parse_error("parse_num: buffer overflow\n");
new_num[i] = '\0';
return new_num;
}
/**
* parse_null - parse a null string
*
* A normal return indicates a valid null string.
*/
void parse_null(char *buf, int *idx, int size) {
size_t size_null_str = 4;
if (strncmp(buf + *idx, "null", size_null_str) != 0) {
parse_error("parse_null: undefined null string\n");
}
(*idx) += size_null_str;
}
/**
* parse_bool - parse a boolean string
* @boo_type: show whether looking for "true" or "false" string
*/
void parse_bool(char *buf, int *idx, int size, bool boo_type) {
size_t size_true_str = 4;
size_t size_false_str = 5;
if (boo_type) {
if (strncmp(buf + *idx, "true", size_true_str) != 0) {
parse_error("parse_bool: undefined \"true\" boolean string\n");
}
(*idx) += size_true_str;
} else {
if (strncmp(buf + *idx, "false", size_false_str) != 0) {
parse_error("parse_bool: undefined \"false\" boolean string\n");
}
(*idx) += size_false_str;
}
}
/**
* parse_short_string - parse and return a valid short-size string
*/
char *parse_short_string(char *buf, int *idx, int size) {
char *str = (char *)malloc(sizeof(char) * SIZE_SHORT_STRING);
int i = 0;
while (buf[*idx] != '\"' && *idx < size && i < SIZE_SHORT_STRING) {
if (buf[*idx] == '\\') {
str[i] = buf[*idx]; // store the backslash too and increment indicies
i++;
(*idx)++;
if (*idx < size && (is_whitespace_char(buf[*idx]) || buf[*idx] == '/' || buf[*idx] == '\\' || buf[*buf] == '\"')) {
str[i] = buf[*idx];
} else {
printf("Parsing error: invald escape sequence\n");
exit(EXIT_FAILURE);
}
} else {
str[i] = buf[*idx];
}
i++;
(*idx)++;
}
if (buf[*idx] != '\"') {
printf("parse_short_string: invald string or buffer overflow\n");
exit(EXIT_FAILURE);
}
str[i] = '\0';
(*idx)++;
return str;
}
/**
* parse_element - helper which recursively fills the JSON array
* @arr: the dynamic array which stores the elements of an JSON data type
*/
void *parse_element(char *buf, int *idx, int size, dynamic_array *arr, int layer) {
json_text *new_jtext;
new_jtext = parse_any(buf, idx, size, layer);
push_back(arr, (void *)new_jtext);
parse_whitespace(buf, idx, size);
if (buf[*idx] == ',') {
(*idx)++;
parse_whitespace(buf, idx, size);
parse_element(buf, idx, size, arr, layer);
}
}
/**
* parse_array - parse and return a valid array
*/
dynamic_array *parse_array(char *buf, int *idx, int size, int layer) {
dynamic_array *new_arr;
new_arr = construct_dynamic_array();
parse_element(buf, idx, size, new_arr, layer + 1);
if (buf[*idx] != ']') {
parse_error("parse_array: miss closing square bracket\n");
}
(*idx)++;
return new_arr;
}
/**
* parse_node - helper which recursively fills the JSON object
* @map: the dictionary in which node will be inserted
*/
void parse_node(char *buf, int *idx, int size, ordered_map *map, int layer) {
chain_node *new_node;
char *key;
json_text *val;
if (buf[*idx] != '\"')
parse_error("parse_object: key must be a valid string\n");
(*idx)++;
key = parse_short_string(buf, idx, size);
parse_whitespace(buf, idx, size);
if (buf[*idx] != ':')
parse_error("parse_object: miss colon after a key\n");
(*idx)++;
parse_whitespace(buf, idx, size);
val = parse_any(buf, idx, size, layer + 1);
new_node = construct_chain_node();
new_node->key = key;
new_node->val = val;
insert(new_node, map);
parse_whitespace(buf, idx, size);
if (buf[*idx] == ',') {
(*idx)++;
parse_whitespace(buf, idx, size);
parse_node(buf, idx, size, map, layer);
}
}
/**
* parse_object - parse text and return a map
*/
ordered_map *parse_object(char *buf, int *idx, int size, int layer) {
ordered_map *res;
res = construct_ordered_map();
parse_node(buf, idx, size, res, layer);
//printf("buf[*idx]: %c\n", buf[*idx]);
if (buf[*idx] != '}') {
parse_error("parse_object: miss closing curly bracket or miss comma separator\n");
}
return res;
}
/**
* parse_any - process and return an arbitrary JSON data
*
* Result can be one of the six data types:
* dictionary, array, stirng, number, null, bool (true/false).
*/
json_text *parse_any(char *buf, int *idx, int size, int layer) {
json_text *jtext;
jtext = construct_json_text(layer);
parse_whitespace(buf, idx, size);
switch (buf[*idx]) {
case '{':
(*idx)++;
parse_whitespace(buf, idx, size);
ordered_map *new_obj = parse_object(buf, idx, size, layer);
jtext->type = TYPE_OBJECT;
jtext->data->obj = new_obj;
break;
case '[':
(*idx)++;
parse_whitespace(buf, idx, size);
dynamic_array *new_arr = parse_array(buf, idx, size, layer);
jtext->type = TYPE_ARRAY;
jtext->data->arr = new_arr;
break;
case '"':
(*idx)++;
char *new_str = parse_short_string(buf, idx, size);
jtext->type = TYPE_STRING;
jtext->data->str = new_str;
break;
case 'n':
parse_null(buf, idx, size);
jtext->type = TYPE_NULL;
jtext->data = NULL;
break;
case 't':
parse_bool(buf, idx, size, true);
jtext->type = TYPE_BOOL;
jtext->data->boo = true;
break;
case 'f':
parse_bool(buf, idx, size, false);
jtext->type = TYPE_BOOL;
jtext->data->boo = false;
break;
default:
if (is_digit(buf[*idx])) {
char *new_num = parse_num(buf, idx, size);
jtext->type = TYPE_NUMBER;
jtext->data->num = new_num;
} else {
parse_error("parse_any: invalid first character\n");
}
}
return jtext;
}
/**
* print_json_oneline - print a one-line JSON text
* @jtext: a valid JSON object
*/
void print_json_oneline(json_text *jtext) {
json_data *data = jtext->data;
switch (jtext->type) {
case TYPE_OBJECT:
printf("{");
/**
* Iterate through the node list which is guaranteed to have valid KV
* pairs instead of the entire map.
*/
for (int i = 0; i < data->obj->node_list->size; i++) {
printf("\"%s\"", ((chain_node *)data->obj->node_list->elements[i])->key);
printf(":");
print_json_oneline((json_text *)((chain_node *)data->obj->node_list->elements[i])->val);
if (i < data->obj->node_list->size - 1) {
printf(",");
}
}
printf("}");
break;
case TYPE_ARRAY:
printf("[");
for (int i = 0; i < data->arr->size; i++) {
print_json_oneline(data->arr->elements[i]);
if (i < data->arr->size - 1) {
printf(",");
}
}
printf("]");
break;
case TYPE_STRING:
printf("\"%s\"", data->str);
break;
case TYPE_NULL:
printf("null");
break;
case TYPE_NUMBER:
printf("%s", data->num);
break;
case TYPE_BOOL:
if (data->boo) {
printf("true");
} else {
printf("false");
}
break;
}
}
/**
* print_json_format - print formatted JSON text
* @jtext: a valid JSON object
*/
void print_json_format(json_text *jtext) {
json_data *data = jtext->data;
switch (jtext->type) {
case TYPE_OBJECT:
printf("{\n");
for (int i = 0; i < data->obj->node_list->size; i++) {
for (int j = 0; j < jtext->layer + 1; j++) { // elements need indentation
printf("\t");
}
printf("\"%s\"", ((chain_node *)data->obj->node_list->elements[i])->key);
printf(":");
print_json_format((json_text *)((chain_node *)data->obj->node_list->elements[i])->val);
if (i < data->obj->node_list->size - 1) {
printf(",");
}
printf("\n");
}
for (int j = 0; j < jtext->layer; j++) { // closing bracket does not need indentation
printf("\t");
}
printf("}");
break;
case TYPE_ARRAY:
printf("[\n");
for (int i = 0; i < data->arr->size; i++) {
for (int j = 0; j < ((json_text *)data->arr->elements[0])->layer; j++) {
printf("\t");
}
print_json_format(data->arr->elements[i]);
if (i < data->arr->size - 1) {
printf(",");
}
printf("\n");
}
for (int j = 0; j < ((json_text *)data->arr->elements[0])->layer - 1; j++) {
printf("\t");
}
printf("]");
break;
case TYPE_STRING:
printf("\"%s\"", data->str);
break;
case TYPE_NULL:
printf("null");
break;
case TYPE_NUMBER:
printf("%s", data->num);
break;
case TYPE_BOOL:
if (data->boo) {
printf("true");
} else {
printf("false");
}
break;
}
}
/**
* main - read a json file and fill a json_text object
*/
void main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: ./jparser [json_file]\n");
exit(EXIT_FAILURE);
}
int fd;
if ((fd = open(argv[1], O_RDONLY)) == -1)
sys_error("open");
char buf[PAGE];
json_text *jtext;
int *idx = (int *)malloc(sizeof(int));
*idx = 0;
int numb;
while ((numb = read(fd, buf, PAGE)) > 0) {
jtext = parse_any(buf, idx, numb, 0);
}
if (numb < 0)
sys_error("read");
close(fd);
print_json_format(jtext);
printf("\n");
free_json_text(jtext);
}