-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjson_sem.c
More file actions
2649 lines (2402 loc) · 75 KB
/
json_sem.c
File metadata and controls
2649 lines (2402 loc) · 75 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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* json_sem - JSON semantics support
*
* "Because grammar and syntax alone do not make a complete language." :-)
*
* Copyright (c) 2022-2026 by Cody Boone Ferguson and Landon Curt Noll. All
* rights reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright, this permission notice and text
* this comment, and the disclaimer below appear in all of the following:
*
* supporting documentation
* source copies
* source works derived from this source
* binaries derived from this source or from derived source
*
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE OR JSON.
*
* This JSON parser, library and tools were co-developed in 2022-2026 by Cody Boone
* Ferguson and Landon Curt Noll:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
* and:
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* "Because sometimes even the IOCCC Judges need some help." :-)
*
* "Share and Enjoy!"
* -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
*/
/* special comments for the seqcexit tool */
/* exit code out of numerical order - ignore in sequencing - ooo */
/* exit code change of order - use new value in sequencing - coo */
#include <limits.h>
/*
* json_sem - JSON semantics support
*/
#include "json_sem.h"
/*
* static variables
*/
/* NULL pointer error */
static struct json_sem_val_err sem_null_ptr =
{
NULL, /* JSON parse node in question or NULL */
UINT_MAX, /* JSON parse tree node depth or UINT_MAX */
NULL, /* semantic node in question or NULL */
-1, /* json_sem ptr is NULL, not in table */
"NULL pointer given to werr_sem_val", /* diagnostic message or NULL */
false /* true ==> struct json_sem_val_err was calloced */
/* false ==> this is a static struct json_sem_val_err */
};
/* calloc failure */
static struct json_sem_val_err sem_calloc_err =
{
NULL, /* JSON parse node in question or NULL */
UINT_MAX, /* JSON parse tree node depth or UINT_MAX */
NULL, /* semantic node in question or NULL */
-1, /* json_sem ptr is NULL, not in table */
"calloc failure", /* diagnostic message or NULL */
false /* true ==> struct json_sem_val_err was calloced */
/* false ==> this is a static struct json_sem_val_err */
};
/* strdup failure */
static struct json_sem_val_err sem_strdup_err =
{
NULL, /* JSON parse node in question or NULL */
UINT_MAX, /* JSON parse tree node depth or UINT_MAX */
NULL, /* semantic node in question or NULL */
-1, /* json_sem ptr is NULL, not in table */
"strdup failure", /* diagnostic message or NULL */
false /* true ==> struct json_sem_val_err was calloced */
/* false ==> this is a static struct json_sem_val_err */
};
/* validation failure w/o json_sem_val_err */
static struct json_sem_val_err sem_val_err_NULL =
{
NULL, /* JSON parse node in question or NULL */
UINT_MAX, /* JSON parse tree node depth or UINT_MAX */
NULL, /* semantic node in question or NULL */
-1, /* json_sem ptr is NULL, not in table */
"validation failed yet json_sem_val_err is NULL", /* diagnostic message or NULL */
false /* true ==> struct json_sem_val_err was calloced */
/* false ==> this is a static struct json_sem_val_err */
};
/*
* static functions
*/
static void sem_walk(struct json *node, unsigned int depth, va_list ap);
/*
* werr_sem_val - form a struct json_sem_val_err with an error message string
*
* This function fills out a struct json_sem_val_err containing JSON validation error
* information about a failure to validate a JSON node's semantic context.
*
* Callers should examine the calloced element of the structure returned in determine
* if the return value should be freed.
*
* given:
* val_err validate function specific error code, 0 ==> not an error
* node JSON parse node in question or NULL (OK to be NULL)
* depth JSON parse tree node depth
* sem semantic node in question or NULL (OK to be NULL)
* name name of function issuing the warning (must NOT be NULL)
* fmt format of the warning (must NOT be NULL)
* ... optional format args
*
* returns:
* pointer to a struct json_sem_val_err containing JSON validation error information
* This may be a pointer to a calloced struct json_sem_val_err (ret->calloced == true),
* or a pointer to a static struct json_sem_val_err (ret->calloced == false).
*
* NOTE: This function will never return NULL.
*
* NOTE: The ret->diagnostic will never be set to NULL.
*
* NOTE: In case of calloc or strdup error, or if name is NULL, or if fmt is NULL,
* then ret->calloced will be set to false and a pointer to a static
* struct json_sem_val_err will be returned.
*/
struct json_sem_val_err *
werr_sem_val(int val_err, struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, char const *fmt, ...)
{
va_list ap; /* variable argument list */
struct json_sem_val_err *ret = NULL; /* calloced return value */
char message[BUFSIZ+1]; /* vsnwerr() message buffer */
char *diagnostic = NULL; /* pointer to duplicated message buffer */
/*
* firewall
*/
if (name == NULL || fmt == NULL) {
/* report NULL pointer error */
return &sem_null_ptr;
}
/*
* stdarg variable argument list setup
*/
va_start(ap, fmt);
/*
* copy a werr() message (via vsnwerr()) into the static buffer
*/
message[0] = '\0'; /* paranoia */
message[BUFSIZ] = '\0'; /* paranoia */
vsnwerr(val_err, message, BUFSIZ, name, fmt, ap);
/*
* stdarg variable argument list cleanup
*/
va_end(ap);
/*
* duplicate the error message
*/
diagnostic = strdup(message);
if (diagnostic == NULL) {
/* report strdup error */
return &sem_strdup_err;
}
/*
* calloc the return value
*/
ret = calloc(1, sizeof(*ret));
if (ret == NULL) {
/* report calloc error */
if (diagnostic != NULL) {
free(diagnostic);
diagnostic = NULL;
}
return &sem_calloc_err;
}
/*
* fill the json_sem_val_err return value
*/
ret->node = node;
ret->depth = depth;
ret->sem = sem;
ret->diagnostic = diagnostic;
ret->calloced = true;
/*
* return pointer to new json_sem_val_err
*/
return ret;
}
/*
* werrp_sem_val - form a struct json_sem_val_err with an error message string with errno details
*
* This function fills out a struct json_sem_val_err containing JSON validation error
* information about a failure to validate a JSON node's semantic context.
*
* Callers should examine the calloced element of the structure returned in determine
* if the return value should be freed.
*
* given:
* val_err validate function specific error code, 0 ==> not an error
* node JSON parse node in question or NULL (OK to be NULL)
* depth JSON parse tree node depth
* sem semantic node in question or NULL (OK to be NULL)
* name name of function issuing the warning (must NOT be NULL)
* fmt format of the warning (must NOT be NULL)
* ... optional format args
*
* returns:
* pointer to a struct json_sem_val_err containing JSON validation error information
* This may be a pointer to a calloced struct json_sem_val_err (ret->calloced == true),
* or a pointer to a static struct json_sem_val_err (ret->calloced == false).
*
* NOTE: This function will never return NULL.
*
* NOTE: The ret->diagnostic will never be set to NULL.
*
* NOTE: In case of calloc or strdup error, or if name is NULL, or if fmt is NULL,
* then ret->calloced will be set to false and a pointer to a static
* struct json_sem_val_err will be returned.
*/
struct json_sem_val_err *
werrp_sem_val(int val_err, struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, char const *fmt, ...)
{
va_list ap; /* variable argument list */
struct json_sem_val_err *ret = NULL; /* calloced return value */
char mesg[BUFSIZ+1]; /* vsnwerr() message buffer */
char *diagnostic = NULL; /* pointer to duplicated message buffer */
/*
* firewall
*/
if (name == NULL || fmt == NULL) {
/* report NULL pointer error */
return &sem_null_ptr;
}
/*
* stdarg variable argument list setup
*/
va_start(ap, fmt);
/*
* copy a werr() message (via vsnwerrp()) into the static buffer
*/
mesg[0] = '\0'; /* paranoia */
mesg[BUFSIZ] = '\0'; /* paranoia */
vsnwerrp(val_err, mesg, BUFSIZ, name, fmt, ap);
/*
* stdarg variable argument list cleanup
*/
va_end(ap);
/*
* duplicate the error message
*/
diagnostic = strdup(mesg);
if (diagnostic == NULL) {
/* report strdup error */
return &sem_strdup_err;
}
/*
* calloc the return value
*/
ret = calloc(1, sizeof(*ret));
if (ret == NULL) {
/* report calloc error */
if (diagnostic != NULL) {
free(diagnostic);
diagnostic = NULL;
}
return &sem_calloc_err;
}
/*
* fill the json_sem_val_err return value
*/
ret->node = node;
ret->depth = depth;
ret->sem = sem;
ret->diagnostic = diagnostic;
ret->calloced = true;
/*
* return pointer to new json_sem_val_err
*/
return ret;
}
/*
* sem_chk_null_args - check arguments for NULL
*
* Check if critical if args (node, sem, name) are NULL. Report if a critical arg
* is NULL via a val_err pointer to address where to place a JSON semantic validation error.
*
* given:
* node JSON parse node being checked
* depth depth of node in the JSON parse tree (0 ==> tree root)
* sem JSON semantic node triggering the check
* name name of caller function (NULL ==> "((NULL))")
* val_err pointer to address where to place a JSON semantic validation error,
* NULL ==> do not report a JSON semantic validation error
*
* returns:
* true ==> one or more args are NULL, *val_err if non-NULL is JSON semantic error
* false ==> all args are non-NULL
*/
bool
sem_chk_null_args(struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, struct json_sem_val_err **val_err)
{
/*
* firewall - args
*/
if (name == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(10, node, depth, sem, "((NULL))", "name is NULL");
}
return true;
}
if (node == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(11, node, depth, sem, name, "node is NULL");
}
return true;
}
if (sem == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(12, node, depth, sem, name, "sem is NULL");
}
return true;
}
return false;
}
/*
* sem_node_valid - determine if a JSON node is a converted and valid type
*
* Given a JSON node, perform sanity checks on the JSON node.
*
* Because this call is often made when a function is first given a JSON node,
* we make a few other JTYPE specific checks such as looking for invalid
* pointers to NULL. Where there is an integer length, we check for < 0.
*
* For JTYPE_MEMBER, verify that the name is a valid JTYPE_STRING.
* This valid JTYPE_STRING check is a manual check to avoid potential problems
* with recursion and loops.
*
* given:
* node JSON parse node being checked
* depth depth of node in the JSON parse tree (0 ==> tree root)
* sem JSON semantic node triggering the check
* name name of caller function (NULL ==> "((NULL))")
* val_err pointer to address where to place a JSON semantic validation error,
* NULL ==> do not report a JSON semantic validation error
*
* returns:
* true ==> JSON node is converted and a valid JTYPE
* The val_err arg is ignored
* NULL ==> JSON node is not converted, invalid node type, or internal error
* If val_err != NULL then *val_err is JSON semantic validation error (struct json_count_err)
*/
bool
sem_node_valid(struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, struct json_sem_val_err **val_err)
{
/*
* firewall - args
*/
if (sem_chk_null_args(node, depth, sem, name, val_err) == true) {
/* sem_chk_null_args() will have set *val_err */
return false;
}
/*
* validate JSON parse node type and check for not converted
*/
switch (node->type) {
case JTYPE_UNSET: /* JSON item has not been set - must be the value 0 */
if (val_err != NULL) {
*val_err = werr_sem_val(13, node, depth, sem, name, "node is JTYPE_UNSET this type is invalid here");
}
return false;
break;
case JTYPE_NUMBER: /* JSON item is number - see struct json_number */
{
struct json_number const *item = &(node->item.number);
/* converted check */
if (!VALID_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(14, node, depth, sem, name, "JTYPE_NUMBER node: converted is false");
}
return false;
}
/* JTYPE_NUMBER specific sanity checks */
if (item->as_str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(15, node, depth, sem, name, "JTYPE_NUMBER node: as_str is NULL");
}
return false;
}
if (item->first == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(16, node, depth, sem, name, "JTYPE_NUMBER node: first is NULL");
}
return false;
}
}
break;
case JTYPE_STRING: /* JSON item is a string - see struct json_string */
{
struct json_string const *item = &(node->item.string);
/* converted check */
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(17, node, depth, sem, name, "JTYPE_STRING node: converted is false");
}
return false;
}
/* JTYPE_STRING specific sanity checks */
if (item->as_str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(18, node, depth, sem, name, "JTYPE_STRING node: as_str is NULL");
}
return false;
}
if (item->str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(19, node, depth, sem, name, "JTYPE_STRING node: str is NULL");
}
return false;
}
}
break;
case JTYPE_BOOL: /* JSON item is a boolean - see struct json_boolean */
{
struct json_boolean const *item = &(node->item.boolean);
/* converted check */
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(20, node, depth, sem, name, "JTYPE_BOOL node: converted is false");
}
return false;
}
/* JTYPE_BOOL specific sanity checks */
if (item->as_str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(21, node, depth, sem, name, "JTYPE_BOOL node: as_str is NULL");
}
return false;
}
}
break;
case JTYPE_NULL: /* JSON item is a null - see struct json_null */
{
struct json_null const *item = &(node->item.null);
/* converted check */
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(22, node, depth, sem, name, "JTYPE_NULL node: converted is false");
}
return false;
}
/* JTYPE_NULL specific sanity checks */
if (item->as_str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(23, node, depth, sem, name, "JTYPE_NULL node: as_str is NULL");
}
return false;
}
if (item->value != NULL) { /* yes, value must be NULL for a converted JTYPE_NULL */
if (val_err != NULL) {
*val_err = werr_sem_val(24, node, depth, sem, name, "JTYPE_NULL node: ironically value is NOT NULL");
}
return false;
}
}
break;
case JTYPE_MEMBER: /* JSON item is a member */
{
struct json_member const *item = &(node->item.member);
struct json *member_name = NULL; /* name part of JTYPE_MEMBER */
struct json_string *member_name_string; /* name part of JTYPE_MEMBER as JTYPE_STRING */
/* converted check */
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(25, node, depth, sem, name, "JTYPE_MEMBER node: converted is false");
}
return false;
}
/* JTYPE_MEMBER specific sanity checks */
if (item->name_as_str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(26, node, depth, sem, name, "JTYPE_MEMBER node: name_as_str is NULL");
}
return false;
}
if (item->name_str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(27, node, depth, sem, name, "JTYPE_MEMBER node: name_str is NULL");
}
return false;
}
if (item->name == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(28, node, depth, sem, name, "JTYPE_MEMBER node: name is NULL");
}
return false;
}
if (item->value == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(29, node, depth, sem, name, "JTYPE_MEMBER node: value is NULL");
}
return false;
}
/* specific checks on name of JTYPE_MEMBER */
member_name = item->name;
if (member_name->type != JTYPE_STRING) {
if (val_err != NULL) {
*val_err = werr_sem_val(30, node, depth, sem, name,
"JTYPE_MEMBER name node type: %d <%s> != JTYPE_STRING",
node->type, json_type_name(node->type));
}
return false;
}
member_name_string = &(member_name->item.string);
if (!CONVERTED_PARSED_JSON_NODE(member_name_string)) {
if (val_err != NULL) {
*val_err = werr_sem_val(31, node, depth, sem, name, "JTYPE_MEMBER name node: converted is false");
}
return false;
}
if (member_name_string->as_str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(32, node, depth, sem, name, "JTYPE_MEMBER name node as_str is NULL");
}
return false;
}
if (member_name_string->str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(33, node, depth, sem, name, "JTYPE_MEMBER name node str is NULL");
}
return false;
}
}
break;
case JTYPE_OBJECT: /* JSON item is a { members } */
{
struct json_object const *item = &(node->item.object);
/* converted check */
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(34, node, depth, sem, name, "JTYPE_OBJECT node: converted is false");
}
return false;
}
/* JTYPE_OBJECT specific sanity checks */
if (item->len < 0) {
if (val_err != NULL) {
*val_err = werr_sem_val(35, node, depth, sem, name, "JTYPE_OBJECT node: len: %jd < 0",
item->len);
}
return false;
}
if (item->set == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(36, node, depth, sem, name, "JTYPE_OBJECT node: set is NULL");
}
return false;
}
if (item->s == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(37, node, depth, sem, name, "JTYPE_OBJECT node: s is NULL");
}
return false;
}
}
break;
case JTYPE_ARRAY: /* JSON item is a [ elements ] */
{
struct json_array const *item = &(node->item.array);
/* converted check */
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(38, node, depth, sem, name, "JTYPE_ARRAY node: converted is false");
}
return false;
}
/* JTYPE_ARRAY specific sanity checks */
if (item->len < 0) {
if (val_err != NULL) {
*val_err = werr_sem_val(39, node, depth, sem, name, "JTYPE_ARRAY node: len: %jd < 0",
item->len);
}
return false;
}
if (item->set == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(40, node, depth, sem, name, "JTYPE_ARRAY node: set is NULL");
}
return false;
}
if (item->s == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(41, node, depth, sem, name, "JTYPE_ARRAY node: s is NULL");
}
return false;
}
}
break;
case JTYPE_ELEMENTS: /* JSON elements is zero or more JSON values */
{
struct json_elements const *item = &(node->item.elements);
/* converted check */
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(42, node, depth, sem, name, "JTYPE_ELEMENTS node: converted is false");
}
return false;
}
/* JTYPE_ELEMENTS specific sanity checks */
if (item->len < 0) {
if (val_err != NULL) {
*val_err = werr_sem_val(43, node, depth, sem, name, "JTYPE_ELEMENTS node: len: %jd < 0",
item->len);
}
return false;
}
if (item->set == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(44, node, depth, sem, name, "JTYPE_ELEMENTS node: set is NULL");
}
return false;
}
if (item->s == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(45, node, depth, sem, name, "JTYPE_ARRAY node: s is NULL");
}
return false;
}
}
break;
default:
if (val_err != NULL) {
*val_err = werr_sem_val(46, node, depth, sem, name, "node type is unknown: %d <%s>",
node->type, json_type_name(node->type));
}
return false;
break;
}
/*
* node is converted and a valid JTYPE
*/
return true;
}
/*
* sem_member_name - return JSON node that is the name of a JTYPE_MEMBER
*
* Given a JSON parse node of the JTYPE_MEMBER type, validate that node
* and return the JSON node that is the name of said JTYPE_MEMBER.
*
* given:
* node JSON parse node being checked
* depth depth of node in the JSON parse tree (0 ==> tree root)
* sem JSON semantic node triggering the check
* name name of caller function (NULL ==> "((NULL))")
* val_err pointer to address where to place a JSON semantic validation error,
* NULL ==> do not report a JSON semantic validation error
*
* returns:
* != NULL ==> JSON node that is the name of a JTYPE_MEMBER
* The val_err arg is ignored
* NULL ==> invalid arguments or JSON error
* If val_err != NULL then *val_err is JSON semantic validation error (struct json_count_err)
*/
struct json *
sem_member_name(struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, struct json_sem_val_err **val_err)
{
struct json_member const *item = NULL; /* JSON member */
struct json *n = NULL; /* name of JTYPE_MEMBER */
bool valid = false; /* true ==> JSON node is converted and valid JTYPE */
/*
* firewall - args
*/
if (sem_chk_null_args(node, depth, sem, name, val_err) == true) {
/* sem_chk_null_args() will have set *val_err */
return NULL;
}
/*
* validate JSON parse node type
*/
valid = sem_node_valid(node, depth, sem, name, val_err);
if (valid == false) {
/* sem_node_valid() will have set *val_err */
return NULL;
}
if (node->type != JTYPE_MEMBER) {
if (val_err != NULL) {
*val_err = werr_sem_val(47, node, depth, sem, name, "node type %s != JTYPE_MEMBER",
json_type_name(node->type));
}
return NULL;
}
item = &(node->item.member);
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(48, node, depth, sem, name, "JTYPE_MEMBER node converted is false");
}
return NULL;
}
/*
* firewall - name
*/
n = item->name;
if (n == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(49, node, depth, sem, name, "node name is NULL");
}
return NULL;
}
/*
* validate JSON parse node name type
*/
valid = sem_node_valid(n, depth+1, sem, name, val_err);
if (valid == false) {
/* sem_node_valid() will have set *val_err */
return NULL;
}
if (n->type != JTYPE_STRING) {
if (val_err != NULL) {
*val_err = werr_sem_val(50, node, depth, sem, name, "node name type %s != JTYPE_STRING",
json_type_name(n->type));
}
return NULL;
}
return n;
}
/*
* sem_member_value - return JSON node that is the value of a JTYPE_MEMBER
*
* Given a JSON parse node of the JTYPE_MEMBER type, validate that node
* and return the JSON node that is the value of said JTYPE_MEMBER.
*
* given:
* node JSON parse node being checked
* depth depth of node in the JSON parse tree (0 ==> tree root)
* sem JSON semantic node triggering the check
* name name of caller function (NULL ==> "((NULL))")
* val_err pointer to address where to place a JSON semantic validation error,
* NULL ==> do not report a JSON semantic validation error
*
* returns:
* != NULL ==> JSON node that is the value of a JTYPE_MEMBER
* The val_err arg is ignored
* NULL ==> invalid arguments or JSON error
* If val_err != NULL then *val_err is JSON semantic validation error (struct json_count_err)
*/
struct json *
sem_member_value(struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, struct json_sem_val_err **val_err)
{
struct json_member const *item = NULL; /* JSON member */
struct json *n = NULL; /* name of JTYPE_MEMBER */
bool valid = false; /* true ==> JSON node is converted and valid JTYPE */
/*
* firewall - args
*/
if (sem_chk_null_args(node, depth, sem, name, val_err) == true) {
/* sem_chk_null_args() will have set *val_err */
return NULL;
}
/*
* validate JSON parse node type
*/
valid = sem_node_valid(node, depth, sem, name, val_err);
if (valid == false) {
/* sem_node_valid() will have set *val_err */
return NULL;
}
if (node->type != JTYPE_MEMBER) {
if (val_err != NULL) {
*val_err = werr_sem_val(51, node, depth, sem, name, "node type %s != JTYPE_MEMBER",
json_type_name(node->type));
}
return NULL;
}
item = &(node->item.member);
if (!CONVERTED_PARSED_JSON_NODE(item)) {
if (val_err != NULL) {
*val_err = werr_sem_val(52, node, depth, sem, name, "JTYPE_MEMBER node converted is false");
}
return NULL;
}
/*
* firewall - name
*/
n = item->value;
if (n == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(53, node, depth, sem, name, "node name is NULL");
}
return NULL;
}
valid = sem_node_valid(n, depth+1, sem, name, val_err);
if (valid == false) {
/* sem_node_valid() will have set *val_err */
return NULL;
}
return n;
}
/*
* sem_member_name_decoded_str - return the JSON decoded name string from a JTYPE_MEMBER
*
* Given a JSON node of type JTYPE_MEMBER, look at the name of the JSON member
* and if it is a JTYPE_STRING (JSON string), return the name's JSON decoded string
* or return NULL on error or invalid input.
*
* given:
* node JSON parse node being checked
* depth depth of node in the JSON parse tree (0 ==> tree root)
* sem JSON semantic node triggering the check
* name name of caller function (NULL ==> "((NULL))")
* val_err pointer to address where to place a JSON semantic validation error,
* NULL ==> do not report a JSON semantic validation error
*
* returns:
* != NULL ==> decoded JTYPE_STRING from the name part of JTYPE_MEMBER
* The val_err arg is ignored
* NULL ==> invalid arguments or JSON conversion error
* If val_err != NULL, then *val_err is JSON semantic validation error (struct json_count_err)
*/
char *
sem_member_name_decoded_str(struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, struct json_sem_val_err **val_err)
{
struct json *n = NULL; /* name of JTYPE_MEMBER */
struct json_string *istr = NULL; /* JTYPE_MEMBER name as JTYPE_STRING */
char *str = NULL; /* JTYPE_STRING as decoded JSON string */
/*
* obtain JTYPE_MEMBER name
*
* NOTE: The sem_member_name() call checks args via sem_chk_null_args().
* NOTE: The sem_member_name() call verifies that node is of type JTYPE_MEMBER.
*/
n = sem_member_name(node, depth, sem, name, val_err);
if (n == NULL) {
/* sem_member_name() will have set *val_err */
return NULL;
}
/*
* validate JSON parse node name type
*/
if (n->type != JTYPE_STRING) {
if (val_err != NULL) {
*val_err = werr_sem_val(54, node, depth, sem, name, "node name type %s != JTYPE_STRING",
json_type_name(n->type));
}
return NULL;
}
istr = &(n->item.string);
if (!CONVERTED_PARSED_JSON_NODE(istr)) {
if (val_err != NULL) {
*val_err = werr_sem_val(55, node, depth, sem, name, "node name JTYPE_STRING converted is false");
}
return NULL;
}
/*
* firewall - decoded JSON string
*/
str = istr->str;
if (str == NULL) {
if (val_err != NULL) {
*val_err = werr_sem_val(56, node, depth, sem, name, "node name decoded JSON string is NULL");
}
return NULL;
}
/*
* case success: return decoded JSON string
*/
return str;
}
/*
* sem_member_value_decoded_str - return the JSON decoded value string from a JTYPE_MEMBER
*
* Given a JSON node of type JTYPE_MEMBER, look at the value of the JSON member
* and if it is a JTYPE_STRING (JSON string), return the value's JSON decoded string
* or return NULL on error or invalid input.
*
* given:
* node JSON parse node being checked
* depth depth of node in the JSON parse tree (0 ==> tree root)
* sem JSON semantic node triggering the check
* name name of caller function (NULL ==> "((NULL))")
* val_err pointer to address where to place a JSON semantic validation error,
* NULL ==> do not report a JSON semantic validation error
*
* returns:
* != NULL ==> decoded JTYPE_STRING from the value part of JTYPE_MEMBER
* The val_err arg is ignored
* NULL ==> invalid arguments or JSON conversion error
* If val_err != NULL then *val_err is JSON semantic validation error (struct json_sem_val_err)
*/
char *
sem_member_value_decoded_str(struct json const *node, unsigned int depth, struct json_sem *sem,
char const *name, struct json_sem_val_err **val_err)
{
struct json *value = NULL; /* value of JTYPE_MEMBER */
struct json_string *istr = NULL; /* JTYPE_MEMBER value as JTYPE_STRING */
char *str = NULL; /* JTYPE_STRING as decoded JSON string */
/*
* obtain JTYPE_MEMBER value
*
* NOTE: The sem_member_value() call checks args via sem_chk_null_args().
* NOTE: The sem_member_value() call verifies that node is of type JTYPE_MEMBER.
*/
value = sem_member_value(node, depth, sem, name, val_err);
if (value == NULL) {
/* sem_member_value() will have set *val_err */
return NULL;
}