-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_parse.c
4464 lines (4029 loc) · 122 KB
/
json_parse.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
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_parse - JSON parser support code
*
* "Because specs w/o version numbers are forced to commit to their original design flaws." :-)
*
* This JSON parser was co-developed in 2022 by:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
* and:
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* "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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
#include <time.h>
#include <limits.h>
#include <inttypes.h>
#include <math.h>
#include <string.h>
/*
* util - common utility functions for the JSON parser
*/
#include "util.h"
/*
* json_parse - JSON parser support code
*/
#include "json_parse.h"
/*
* json_util - general JSON parser utility support functions
*/
#include "json_util.h"
/* for json string decoding */
static char *decode_json_string(char const *ptr, size_t len, size_t mlen, size_t *retlen);
/* for json number strings */
static bool json_process_decimal(struct json_number *item, char const *str, size_t len);
static bool json_process_floating(struct json_number *item, char const *str, size_t len);
/*
* byte2asciistr - a trivial way to map an 8-bit byte into string of ASCII characters
*
* NOTE: JSON_BYTE_VALUES is #defined as (BYTE_VALUES) (see util.h) and this table
* MUST be 256 in length.
*
* NOTE: This table assumes we process on an 8-bit byte basis.
*
* This map is not a canonical way to encode Unicode characters.
* Instead it helps translate certain "forbidden" bytes within a
* JSON encoded string.
*/
struct byte2asciistr byte2asciistr[JSON_BYTE_VALUES] = {
/* \x00 - \x0f */
{0x00, 6, "\\u0000", 2}, {0x01, 6, "\\u0001", 1}, {0x02, 6, "\\u0002", 1}, {0x03, 6, "\\u0003",1},
{0x04, 6, "\\u0004", 1}, {0x05, 6, "\\u0005", 1}, {0x06, 6, "\\u0006", 1}, {0x07, 6, "\\u0007",1},
{0x08, 2, "\\b", 1}, {0x09, 2, "\\t", 1}, {0x0a, 2, "\\n", 1}, {0x0b, 6, "\\u000b", 1},
{0x0c, 2, "\\f", 1}, {0x0d, 2, "\\r", 1}, {0x0e, 6, "\\u000e", 1}, {0x0f, 6, "\\u000f", 1},
/* \x10 - \x1f */
{0x10, 6, "\\u0010", 1}, {0x11, 6, "\\u0011", 1}, {0x12, 6, "\\u0012", 1}, {0x13, 6, "\\u0013", 1},
{0x14, 6, "\\u0014", 1}, {0x15, 6, "\\u0015", 1}, {0x16, 6, "\\u0016", 1}, {0x17, 6, "\\u0017", 1},
{0x18, 6, "\\u0018", 1}, {0x19, 6, "\\u0019", 1}, {0x1a, 6, "\\u001a", 1}, {0x1b, 6, "\\u001b", 1},
{0x1c, 6, "\\u001c", 1}, {0x1d, 6, "\\u001d", 1}, {0x1e, 6, "\\u001e", 1}, {0x1f, 6, "\\u001f", 1},
/* \x20 - \x2f */
{' ', 1, " ", 1}, {'!', 1, "!", 1}, {'"', 2, "\\\"", 1}, {'#', 1, "#", 1},
{'$', 1, "$", 1}, {'%', 1, "%", 1}, {'&', 1, "&", 1}, {'\'', 1, "'", 1},
{'(', 1, "(", 1}, {')', 1, ")", 1}, {'*', 1, "*", 1}, {'+', 1, "+", 1},
{',', 1, ",", 1}, {'-', 1, "-", 1}, {'.', 1, ".", 1}, {'/', 1, "/", 1},
/* \x30 - \x3f */
{'0', 1, "0", 1}, {'1', 1, "1", 1}, {'2', 1, "2", 1}, {'3', 1, "3", 1},
{'4', 1, "4", 1}, {'5', 1, "5", 1}, {'6', 1, "6", 1}, {'7', 1, "7", 1},
{'8', 1, "8", 1}, {'9', 1, "9", 1}, {':', 1, ":", 1}, {';', 1, ";", 1},
{'<', 1, "<", 1}, {'=', 1, "=", 1}, {'>', 1, ">", 1}, {'?', 1, "?", 1},
/* \x40 - \x4f */
{'@', 1, "@", 1}, {'A', 1, "A", 1}, {'B', 1, "B", 1}, {'C', 1, "C", 1},
{'D', 1, "D", 1}, {'E', 1, "E", 1}, {'F', 1, "F", 1}, {'G', 1, "G", 1},
{'H', 1, "H", 1}, {'I', 1, "I", 1}, {'J', 1, "J", 1}, {'K', 1, "K", 1},
{'L', 1, "L", 1}, {'M', 1, "M", 1}, {'N', 1, "N", 1}, {'O', 1, "O", 1},
/* \x50 - \x5f */
{'P', 1, "P", 1}, {'Q', 1, "Q", 1}, {'R', 1, "R", 1}, {'S', 1, "S", 1},
{'T', 1, "T", 1}, {'U', 1, "U", 1}, {'V', 1, "V", 1}, {'W', 1, "W", 1},
{'X', 1, "X", 1}, {'Y', 1, "Y", 1}, {'Z', 1, "Z", 1}, {'[', 1, "[", 1},
{'\\', 2, "\\\\", 1}, {']', 1, "]",1}, {'^', 1, "^", 1}, {'_', 1, "_", 1},
/* \x60 - \x6f */
{'`', 1, "`", 1}, {'a', 1, "a", 1}, {'b', 1, "b", 1}, {'c', 1, "c", 1},
{'d', 1, "d", 1}, {'e', 1, "e", 1}, {'f', 1, "f", 1}, {'g', 1, "g", 1},
{'h', 1, "h", 1}, {'i', 1, "i", 1}, {'j', 1, "j", 1}, {'k', 1, "k", 1},
{'l', 1, "l", 1}, {'m', 1, "m", 1}, {'n', 1, "n", 1}, {'o', 1, "o", 1},
/* \x70 - \x7f */
{'p', 1, "p", 1}, {'q', 1, "q", 1}, {'r', 1, "r", 1}, {'s', 1, "s", 1},
{'t', 1, "t", 1}, {'u', 1, "u", 1}, {'v', 1, "v", 1}, {'w', 1, "w", 1},
{'x', 1, "x", 1}, {'y', 1, "y", 1}, {'z', 1, "z", 1}, {'{', 1, "{", 1},
{'|', 1, "|", 1}, {'}', 1, "}", 1}, {'~', 1, "~", 1}, {0x7f, 6, "\\u007f", 1},
/* \x80 - \x8f */
{0x80, 1, "\x80", 1}, {0x81, 1, "\x81", 1}, {0x82, 1, "\x82", 1}, {0x83, 1, "\x83", 1},
{0x84, 1, "\x84", 1}, {0x85, 1, "\x85", 1}, {0x86, 1, "\x86", 1}, {0x87, 1, "\x87", 1},
{0x88, 1, "\x88", 1}, {0x89, 1, "\x89", 1}, {0x8a, 1, "\x8a", 1}, {0x8b, 1, "\x8b", 1},
{0x8c, 1, "\x8c", 1}, {0x8d, 1, "\x8d", 1}, {0x8e, 1, "\x8e", 1}, {0x8f, 1, "\x8f", 1},
/* \x90 - \x9f */
{0x90, 1, "\x90", 1}, {0x91, 1, "\x91", 1}, {0x92, 1, "\x92", 1}, {0x93, 1, "\x93", 1},
{0x94, 1, "\x94", 1}, {0x95, 1, "\x95", 1}, {0x96, 1, "\x96", 1}, {0x97, 1, "\x97", 1},
{0x98, 1, "\x98", 1}, {0x99, 1, "\x99", 1}, {0x9a, 1, "\x9a", 1}, {0x9b, 1, "\x9b", 1},
{0x9c, 1, "\x9c", 1}, {0x9d, 1, "\x9d", 1}, {0x9e, 1, "\x9e", 1}, {0x9f, 1, "\x9f", 1},
/* \xa0 - \xaf */
{0xa0, 1, "\xa0", 1}, {0xa1, 1, "\xa1", 1}, {0xa2, 1, "\xa2", 1}, {0xa3, 1, "\xa3", 1},
{0xa4, 1, "\xa4", 1}, {0xa5, 1, "\xa5", 1}, {0xa6, 1, "\xa6", 1}, {0xa7, 1, "\xa7", 1},
{0xa8, 1, "\xa8", 1}, {0xa9, 1, "\xa9", 1}, {0xaa, 1, "\xaa", 1}, {0xab, 1, "\xab", 1},
{0xac, 1, "\xac", 1}, {0xad, 1, "\xad", 1}, {0xae, 1, "\xae", 1}, {0xaf, 1, "\xaf", 1},
/* \xb0 - \xbf */
{0xb0, 1, "\xb0", 1}, {0xb1, 1, "\xb1", 1}, {0xb2, 1, "\xb2", 1}, {0xb3, 1, "\xb3", 1},
{0xb4, 1, "\xb4", 1}, {0xb5, 1, "\xb5", 1}, {0xb6, 1, "\xb6", 1}, {0xb7, 1, "\xb7", 1},
{0xb8, 1, "\xb8", 1}, {0xb9, 1, "\xb9", 1}, {0xba, 1, "\xba", 1}, {0xbb, 1, "\xbb", 1},
{0xbc, 1, "\xbc", 1}, {0xbd, 1, "\xbd", 1}, {0xbe, 1, "\xbe", 1}, {0xbf, 1, "\xbf", 1},
/* \xc0 - \xcf */
{0xc0, 1, "\xc0", 1}, {0xc1, 1, "\xc1", 1}, {0xc2, 1, "\xc2", 1}, {0xc3, 1, "\xc3", 1},
{0xc4, 1, "\xc4", 1}, {0xc5, 1, "\xc5", 1}, {0xc6, 1, "\xc6", 1}, {0xc7, 1, "\xc7", 1},
{0xc8, 1, "\xc8", 1}, {0xc9, 1, "\xc9", 1}, {0xca, 1, "\xca", 1}, {0xcb, 1, "\xcb", 1},
{0xcc, 1, "\xcc", 1}, {0xcd, 1, "\xcd", 1}, {0xce, 1, "\xce", 1}, {0xcf, 1, "\xcf", 1},
/* \xd0 - \xdf */
{0xd0, 1, "\xd0", 1}, {0xd1, 1, "\xd1", 1}, {0xd2, 1, "\xd2", 1}, {0xd3, 1, "\xd3", 1},
{0xd4, 1, "\xd4", 1}, {0xd5, 1, "\xd5", 1}, {0xd6, 1, "\xd6", 1}, {0xd7, 1, "\xd7", 1},
{0xd8, 1, "\xd8", 1}, {0xd9, 1, "\xd9", 1}, {0xda, 1, "\xda", 1}, {0xdb, 1, "\xdb", 1},
{0xdc, 1, "\xdc", 1}, {0xdd, 1, "\xdd", 1}, {0xde, 1, "\xde", 1}, {0xdf, 1, "\xdf", 1},
/* \xe0 - \xef */
{0xe0, 1, "\xe0", 1}, {0xe1, 1, "\xe1", 1}, {0xe2, 1, "\xe2", 1}, {0xe3, 1, "\xe3", 1},
{0xe4, 1, "\xe4", 1}, {0xe5, 1, "\xe5", 1}, {0xe6, 1, "\xe6", 1}, {0xe7, 1, "\xe7", 1},
{0xe8, 1, "\xe8", 1}, {0xe9, 1, "\xe9", 1}, {0xea, 1, "\xea", 1}, {0xeb, 1, "\xeb", 1},
{0xec, 1, "\xec", 1}, {0xed, 1, "\xed", 1}, {0xee, 1, "\xee", 1}, {0xef, 1, "\xef", 1},
/* \xf0 - \xff */
{0xf0, 1, "\xf0", 1}, {0xf1, 1, "\xf1", 1}, {0xf2, 1, "\xf2", 1}, {0xf3, 1, "\xf3", 1},
{0xf4, 1, "\xf4", 1}, {0xf5, 1, "\xf5", 1}, {0xf6, 1, "\xf6", 1}, {0xf7, 1, "\xf7", 1},
{0xf8, 1, "\xf8", 1}, {0xf9, 1, "\xf9", 1}, {0xfa, 1, "\xfa", 1}, {0xfb, 1, "\xfb", 1},
{0xfc, 1, "\xfc", 1}, {0xfd, 1, "\xfd", 1}, {0xfe, 1, "\xfe", 1}, {0xff, 1, "\xff", 1}
};
/*
* json_encode - return a JSON encoding of a block of memory
*
* JSON string encoding:
*
* Along with Unicode code points (in the form of \uxxxx where x is a
* hexadecimal digit), these escape characters are required by JSON, all of
* which have their code points (as listed below):
*
* old new
* ---------------------------------------
* \x00-\x07 \u0000 - \u0007
* <backspace> \b (\x08)
* <horizontal_tab> \t (\x09)
* <newline> \n (\x0a)
* \x0b \u000b <vertical_tab>
* <form_feed> \f (\x0c)
* <enter> \r (\x0d)
* \x0e-\x1f \u000e - \x001f
* <double_quote> \" (\x22)
* \ \\ (\x5c)
*
* given:
* ptr start of memory block to encode
* len length of block to encode in bytes
* retlen address of where to store allocated length,
* if retlen != NULL
* skip_quote true ==> ignore any double quotes if they are both
* at the start and end of the memory block
* returns:
* allocated JSON encoding of a block, or NULL ==> error
* NOTE: retlen, if non-NULL, is set to 0 on error
*/
char *
json_encode(char const *ptr, size_t len, size_t *retlen, bool skip_quote)
{
char *ret = NULL; /* allocated encoding string or NULL */
char *beyond = NULL; /* beyond the end of the allocated encoding string */
ssize_t mlen = 0; /* length of allocated encoded string */
char *p; /* next place to encode */
size_t i;
/*
* firewall
*/
if (ptr == NULL) {
/* error - clear allocated length */
if (retlen != NULL) {
*retlen = 0;
}
warn(__func__, "called with NULL ptr");
return NULL;
}
/*
* count the bytes that will be in the encoded allocated string
*/
for (i=0; i < len; ++i) {
mlen += byte2asciistr[(uint8_t)(ptr[i])].len;
}
if (mlen < 0) { /* paranoia */
/* error - clear allocated length */
if (retlen != NULL) {
*retlen = 0;
}
warn(__func__, "mlen #0: %ju < 0", (uintmax_t)mlen);
return NULL;
}
/*
* malloc the encoded string
*/
ret = malloc((size_t)mlen + 1 + 1);
if (ret == NULL) {
/* error - clear allocated length */
if (retlen != NULL) {
*retlen = 0;
}
warn(__func__, "malloc of %ju bytes failed", (uintmax_t)(mlen + 1 + 1));
return NULL;
}
ret[mlen] = '\0'; /* terminate string */
ret[mlen + 1] = '\0'; /* paranoia */
beyond = &(ret[mlen]);
/*
* skip any enclosing quotes if requested
*
* We only skip enclosing quotes if skip_quote is true, the memory block is
* long enough to contain two '"'s and the memory block starts and ends with
* a '"'.
*/
if (skip_quote == true && len > 1 && ptr[0] == '"' && ptr[len-1] == '"') {
i = 1; /* start encoding on the next byte beyond the " */
--len; /* do not encode the last byte */
} else {
i = 0; /* start encoding at the first byte */
}
/*
* JSON encode each byte
*/
for (p=ret; i < len; ++i) {
if (p+byte2asciistr[(uint8_t)(ptr[i])].len > beyond) {
/* error - clear allocated length */
if (retlen != NULL) {
*retlen = 0;
}
if (ret != NULL) {
free(ret);
ret = NULL;
}
warn(__func__, "encoding ran beyond end of allocated encoded string");
return NULL;
}
strcpy(p, byte2asciistr[(uint8_t)(ptr[i])].enc);
p += byte2asciistr[(uint8_t)(ptr[i])].len;
}
*p = '\0'; /* paranoia */
mlen = p - ret; /* paranoia */
if (mlen < 0) { /* paranoia */
warn(__func__, "mlen #1: %ju < 0", (uintmax_t)mlen);
if (ret != NULL) {
free(ret);
ret = NULL;
}
return NULL;
}
/*
* return result
*/
dbg(DBG_VVVHIGH, "returning from json_encode(ptr, %ju, *%ju, %s)",
(uintmax_t)len, (uintmax_t)mlen, booltostr(skip_quote));
if (retlen != NULL) {
*retlen = (size_t)mlen;
}
return ret;
}
/*
* json_encode_str - return a JSON encoding of a string
*
* This is a simplified interface for json_encode().
*
* given:
* str NUL terminated C-style string to encode
* retlen address of where to store allocated length, if retlen != NULL
* skip_quotes true ==> ignore any double quotes if they are both
* at the start and end of the memory block
* false ==> process all bytes in the block
*
* returns:
* allocated JSON encoding of a block, or NULL ==> error
* NOTE: retlen, if non-NULL, is set to 0 on error
*/
char *
json_encode_str(char const *str, size_t *retlen, bool skip_quote)
{
void *ret = NULL; /* allocated encoding string or NULL */
size_t len = 0; /* length of string to encode */
/*
* firewall
*/
if (str == NULL) {
/* error - clear allocated length */
if (retlen != NULL) {
*retlen = 0;
}
warn(__func__, "called with NULL ptr");
return NULL;
}
len = strlen(str);
/*
* convert to json_encode() call
*/
ret = json_encode(str, len, retlen, skip_quote);
if (ret == NULL) {
dbg(DBG_VVHIGH, "returning NULL for encoding of: <%s>", str);
} else {
dbg(DBG_VVHIGH, "string: JSON encoded as: <%s>", (char *)ret);
}
/*
* return encoded result or NULL
*/
return ret;
}
/*
* jdecencchk - validate that JSON encoding and decoding works
*
* This function performs various sanity checks in JSON encoding and decoding.
*
* given:
*
* entertainment entertainment level :-)
*
* This function does not return on error.
*/
void
jdecencchk(int entertainment)
{
char const *decstr; /* string to decode */
char const *decstr2;/* string to decode */
char *mstr = NULL; /* allocated encoding string */
size_t mlen = 0; /* length of allocated encoding string */
char *mstr2 = NULL; /* allocated decoding string */
size_t mlen2 = 0; /* length of allocated decoding string */
/*
* :-)
*/
decstr = "\\ud83d\\ude4f\\uD83D\\uDD25\\uD83D\\uDC09";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(147, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
}
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(148, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2, (uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(149, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
if (entertainment > 0) {
msg("%s", mstr2);
}
}
/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
/*
* for this test \u0302\u006f should NOT be the same as \u006f\u0302
*/
decstr = "\\u0302\\u006f";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(150, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
}
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
/*
* decode another string that should not match the above decoded string
*/
decstr2 = "\\u006f\\u0302";
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen)", decstr2);
mstr = json_decode_str(decstr2, false, &mlen);
if (mstr == NULL) {
err(151, __func__, "json_decode_str(<%s>, false, *mlen: %ju) == NULL",
decstr2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the decoded strings do NOT match
*/
if (strcmp(mstr2, mstr) == 0) {
err(152, __func__, "mstr2 %s == mstr: %s", mstr2, mstr);
not_reached();
} else {
dbg(DBG_MED, "%s != %s: %s != %s: true", decstr, decstr2, mstr, mstr2);
}
/*
* free the strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
/*
* case: entertainment mode
*/
if (entertainment > 0) {
/*
* :-) :-)
*/
decstr = "\\uD83C\\uDF0E\\u2604";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(153, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
} else {
dbg(DBG_MED, "%s == %s", decstr, mstr2);
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(154, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2,
(uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(155, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
msg(mstr2);
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
}
/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
}
/*
* :-) :-) :-)
*/
decstr = "\\uD83D\\uDD25\\uD83C\\uDF0E\\uD83E\\uDD96\\uD83E\\uDD95\\u2604";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(156, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
} else {
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
dbg(DBG_MED, "%s == %s", decstr, mstr2);
/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(157, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2,
(uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(158, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
msg(mstr2);
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
}
/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
}
/*
* and now we entertain, at least for those with a dark sense of humour :-)
*/
if (entertainment > 1) {
/*
* :-) :-) :-) :-)
*/
decstr = "\\uD83D\\uDD25\\uD83E\\uDDD9";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(159, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
} else {
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
dbg(DBG_MED, "%s == %s", decstr, mstr2);
/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(160, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2,
(uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(161, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
msg(mstr2);
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
}
/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
}
}
if (entertainment > 2) {
/*
* :-) :-) :-) :-) :-)
*/
decstr = "\\uD83E\\uDEC3\\uD83D\\uDD25\\uD83D\\uDC09";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(162, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
} else {
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
dbg(DBG_MED, "%s == %s", decstr, mstr2);
/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(163, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2,
(uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(164, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
msg(mstr2);
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
}
/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
}
/*
* and if one has an even darker sense of humour ... :-)
*/
if (entertainment > 2) {
dbg(DBG_VVHIGH, "entertainment level %u", entertainment);
/*
* :-) :-) :-) :-) :-) :-)
*/
decstr = "\\uD83E\\uDD30\\uD83D\\uDD25\\uD83D\\uDC09";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(165, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
} else {
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
dbg(DBG_MED, "%s == %s", decstr, mstr2);
/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(166, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2,
(uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(167, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
msg(mstr2);
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
}
/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
}
}
}
}
/*
* free strings if != NULL
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
/*
* :-)
*/
decstr = "\\uD83D\\uDC8D\\uD83C\\uDF0B";
/*
* test decoding the JSON encoded string
*/
dbg(DBG_VVVHIGH, "testing json_decode_str(<%s>, false, &mlen2)", decstr);
/* test json_decode_str() */
mstr2 = json_decode_str(decstr, false, &mlen2);
if (mstr2 == NULL) {
err(168, __func__, "json_decode_str(<%s>, false, *mlen2: %ju) == NULL",
decstr, (uintmax_t)mlen2);
not_reached();
}
dbg(DBG_HIGH, "decoded string: %s (len: %ju)", mstr2, mlen2);
/*
* encode the string we just decoded
*/
dbg(DBG_VVVHIGH, "testing json_encode(mstr2, 1, mlen): %s", mstr2);
mstr = json_encode(mstr2, mlen2, &mlen, false);
if (mstr == NULL) {
err(169, __func__, "json_encode(mstr2: %s, mlen2: %ju, mlen: %ju) == NULL", mstr2, (uintmax_t)mlen2, (uintmax_t)mlen);
not_reached();
}
dbg(DBG_HIGH, "encoded string: %s (len: %ju)", mstr, mlen);
/*
* verify that the encoded string matches the original string
*/
if (strcmp(mstr2, mstr) != 0) {
err(170, __func__, "mstr2: %s != decstr: %s", mstr2, mstr);
not_reached();
} else {
dbg(DBG_MED, "%s: %s == %s: true", decstr, mstr, mstr2);
if (entertainment > 0) {
msg("%s", mstr2);
}
}
/*
* free strings
*/
if (mstr != NULL) {
free(mstr);
mstr = NULL;
}
if (mstr2 != NULL) {
free(mstr2);
mstr2 = NULL;
}
/*
* Don't Panic, unless you're the praying human or the One Ring (or, as
* close as possible with the existing emojis)! :-)
*/
return;
}
/*
* chkbyte2asciistr - validate the contents of the byte2asciistr[] table
*
* This function performs various sanity checks on the byte2asciistr[] table.
*
* This function does not return on error.
*/
void
chkbyte2asciistr(void)
{
unsigned int int_hexval;/* hex value from xxxx part of \uxxxx */
char guard; /* scanf guard to catch excess amount of input */
int indx; /* test index */
char const *encstr; /* encoding string */
int ret; /* libc function return value */
char str[2]; /* character string to encode */
char *mstr = NULL; /* allocated encoding string */
size_t mlen = 0; /* length of allocated encoding string */
char *mstr2 = NULL; /* allocated decoding string */
size_t mlen2 = 0; /* length of allocated decoding string */
unsigned int i;
/*
* assert: bits in byte must be 8
*/
if (BITS_IN_BYTE != 8) {
err(171, __func__, "BITS_IN_BYTE: %d != 8", BITS_IN_BYTE);
not_reached();
}
/*
* assert: JSON_BYTE_VALUES must be 256
*/
if (JSON_BYTE_VALUES != 256) {
err(172, __func__, "JSON_BYTE_VALUES: %d != 256", JSON_BYTE_VALUES);
not_reached();
}
/*
* assert: table must be of size 256
*/
if (TBLLEN(byte2asciistr) != JSON_BYTE_VALUES) {
err(173, __func__, "byte2asciistr table length is %ju instead of %d",
(uintmax_t)TBLLEN(byte2asciistr), JSON_BYTE_VALUES);
not_reached();
}
/*
* assert: byte must be an index from 0 to 256
*/
for (i=0; i < JSON_BYTE_VALUES; ++i) {
if (byte2asciistr[i].byte != i) {
err(174, __func__, "byte2asciistr[0x%02x].byte: %d != %d", i, byte2asciistr[i].byte, i);
not_reached();
}
}
/*
* assert: enc string must be non-NULL
*/
for (i=0; i < JSON_BYTE_VALUES; ++i) {
if (byte2asciistr[i].enc == NULL) {
err(175, __func__, "byte2asciistr[0x%02x].enc == NULL", i);
not_reached();
}
}
/*
* assert: length of enc string must match len
*/
for (i=0; i < JSON_BYTE_VALUES; ++i) {
if (strlen(byte2asciistr[i].enc) != byte2asciistr[i].len) {
err(176, __func__, "byte2asciistr[0x%02x].enc length: %ju != byte2asciistr[0x%02x].len: %ju",
i, (uintmax_t)strlen(byte2asciistr[i].enc),
i, (uintmax_t)byte2asciistr[i].len);
not_reached();
}
}
/*
* assert: \x00-\x07 encodes to \uxxxx
*/
for (i=0x00; i <= 0x07; ++i) {
if (byte2asciistr[i].len != LITLEN("\\uxxxx")) {
err(177, __func__, "byte2asciistr[0x%02x].enc length: %ju != %ju",
i, (uintmax_t)strlen(byte2asciistr[i].enc),
(uintmax_t)LITLEN("\\uxxxx"));
not_reached();
}
ret = sscanf(byte2asciistr[i].enc, "\\u%04x%c", &int_hexval, &guard);
if (ret != 1) {
err(178, __func__, "byte2asciistr[0x%02x].enc: <%s> is not in <\\uxxxx> form", i, byte2asciistr[i].enc);
not_reached();
}
if (i != int_hexval) {
err(179, __func__, "byte2asciistr[0x%02x].enc: <%s> != <\\u%04x> form", i, byte2asciistr[i].enc, i);
not_reached();
}
}
/*
* assert: \x08 encodes to \b
*/
indx = 0x08;
encstr = "\\b";
if (byte2asciistr[indx].len != LITLEN("\\b")) {
err(180, __func__, "byte2asciistr[0x%02x].enc length: %ju != %ju",
indx, (uintmax_t)strlen(byte2asciistr[indx].enc),
(uintmax_t)strlen(encstr));
not_reached();
}
if (strcmp(byte2asciistr[indx].enc, encstr) != 0) {
err(181, __func__, "byte2asciistr[0x%02x].enc: <%s> != <%s>", indx, byte2asciistr[indx].enc, encstr);
not_reached();
}
/*
* assert: \x09 encodes to \t
*/
indx = 0x09;
encstr = "\\t";
if (byte2asciistr[indx].len != LITLEN("\\b")) {
err(182, __func__, "byte2asciistr[0x%02x].enc length: %ju != %ju",
indx, (uintmax_t)strlen(byte2asciistr[indx].enc),
(uintmax_t)strlen(encstr));
not_reached();
}
if (strcmp(byte2asciistr[indx].enc, encstr) != 0) {
err(183, __func__, "byte2asciistr[0x%02x].enc: <%s> != <%s>", indx, byte2asciistr[indx].enc, encstr);
not_reached();
}
/*
* assert: \x0a encodes to \n