1 /* https://github.com/mnunberg/jsonsl */
2
3 /* Copyright (C) 2012-2015 Mark Nunberg.
4 *
5 * See included LICENSE file for license details.
6 */
7
8 #include "jsonsl.h"
9 #include <limits.h>
10 #include <ctype.h>
11
12 #ifdef JSONSL_USE_METRICS
13 #define XMETRICS \
14 X(STRINGY_INSIGNIFICANT) \
15 X(STRINGY_SLOWPATH) \
16 X(ALLOWED_WHITESPACE) \
17 X(QUOTE_FASTPATH) \
18 X(SPECIAL_FASTPATH) \
19 X(SPECIAL_WSPOP) \
20 X(SPECIAL_SLOWPATH) \
21 X(GENERIC) \
22 X(STRUCTURAL_TOKEN) \
23 X(SPECIAL_SWITCHFIRST) \
24 X(STRINGY_CATCH) \
25 X(NUMBER_FASTPATH) \
26 X(ESCAPES) \
27 X(TOTAL) \
28
29 struct jsonsl_metrics_st {
30 #define X(m) \
31 unsigned long metric_##m;
32 XMETRICS
33 #undef X
34 };
35
36 static struct jsonsl_metrics_st GlobalMetrics = { 0 };
37 static unsigned long GenericCounter[0x100] = { 0 };
38 static unsigned long StringyCatchCounter[0x100] = { 0 };
39
40 #define INCR_METRIC(m) \
41 GlobalMetrics.metric_##m++;
42
43 #define INCR_GENERIC(c) \
44 INCR_METRIC(GENERIC); \
45 GenericCounter[c]++; \
46
47 #define INCR_STRINGY_CATCH(c) \
48 INCR_METRIC(STRINGY_CATCH); \
49 StringyCatchCounter[c]++;
50
51 JSONSL_API
jsonsl_dump_global_metrics(void)52 void jsonsl_dump_global_metrics(void)
53 {
54 int ii;
55 printf("JSONSL Metrics:\n");
56 #define X(m) \
57 printf("\t%-30s %20lu (%0.2f%%)\n", #m, GlobalMetrics.metric_##m, \
58 (float)((float)(GlobalMetrics.metric_##m/(float)GlobalMetrics.metric_TOTAL)) * 100);
59 XMETRICS
60 #undef X
61 printf("Generic Characters:\n");
62 for (ii = 0; ii < 0xff; ii++) {
63 if (GenericCounter[ii]) {
64 printf("\t[ %c ] %lu\n", ii, GenericCounter[ii]);
65 }
66 }
67 printf("Weird string loop\n");
68 for (ii = 0; ii < 0xff; ii++) {
69 if (StringyCatchCounter[ii]) {
70 printf("\t[ %c ] %lu\n", ii, StringyCatchCounter[ii]);
71 }
72 }
73 }
74
75 #else
76 #define INCR_METRIC(m)
77 #define INCR_GENERIC(c)
78 #define INCR_STRINGY_CATCH(c)
79 JSONSL_API
jsonsl_dump_global_metrics(void)80 void jsonsl_dump_global_metrics(void) { }
81 #endif /* JSONSL_USE_METRICS */
82
83 #define CASE_DIGITS \
84 case '1': \
85 case '2': \
86 case '3': \
87 case '4': \
88 case '5': \
89 case '6': \
90 case '7': \
91 case '8': \
92 case '9': \
93 case '0':
94
95 static unsigned extract_special(unsigned);
96 static int is_special_end(unsigned);
97 static int is_allowed_whitespace(unsigned);
98 static int is_allowed_escape(unsigned);
99 static int is_simple_char(unsigned);
100 static char get_escape_equiv(unsigned);
101
102 JSONSL_API
jsonsl_new(int nlevels)103 jsonsl_t jsonsl_new(int nlevels)
104 {
105 unsigned int ii;
106 struct jsonsl_st * jsn;
107
108 if (nlevels < 2) {
109 return NULL;
110 }
111
112 jsn = (struct jsonsl_st *)
113 calloc(1, sizeof (*jsn) +
114 ( (nlevels-1) * sizeof (struct jsonsl_state_st) )
115 );
116
117 jsn->levels_max = (unsigned int) nlevels;
118 jsn->max_callback_level = UINT_MAX;
119 jsonsl_reset(jsn);
120 for (ii = 0; ii < jsn->levels_max; ii++) {
121 jsn->stack[ii].level = ii;
122 }
123 return jsn;
124 }
125
126 JSONSL_API
jsonsl_reset(jsonsl_t jsn)127 void jsonsl_reset(jsonsl_t jsn)
128 {
129 jsn->tok_last = 0;
130 jsn->can_insert = 1;
131 jsn->pos = 0;
132 jsn->level = 0;
133 jsn->stopfl = 0;
134 jsn->in_escape = 0;
135 jsn->expecting = 0;
136 }
137
138 JSONSL_API
jsonsl_destroy(jsonsl_t jsn)139 void jsonsl_destroy(jsonsl_t jsn)
140 {
141 if (jsn) {
142 free(jsn);
143 }
144 }
145
146
147 #define FASTPARSE_EXHAUSTED 1
148 #define FASTPARSE_BREAK 0
149
150 /*
151 * This function is meant to accelerate string parsing, reducing the main loop's
152 * check if we are indeed a string.
153 *
154 * @param jsn the parser
155 * @param[in,out] bytes_p A pointer to the current buffer (i.e. current position)
156 * @param[in,out] nbytes_p A pointer to the current size of the buffer
157 * @return true if all bytes have been exhausted (and thus the main loop can
158 * return), false if a special character was examined which requires greater
159 * examination.
160 */
161 static int
jsonsl__str_fastparse(jsonsl_t jsn,const jsonsl_uchar_t ** bytes_p,size_t * nbytes_p)162 jsonsl__str_fastparse(jsonsl_t jsn,
163 const jsonsl_uchar_t **bytes_p, size_t *nbytes_p)
164 {
165 const jsonsl_uchar_t *bytes = *bytes_p;
166 const jsonsl_uchar_t *end;
167 for (end = bytes + *nbytes_p; bytes != end; bytes++) {
168 if (
169 #ifdef JSONSL_USE_WCHAR
170 *bytes >= 0x100 ||
171 #endif /* JSONSL_USE_WCHAR */
172 (is_simple_char(*bytes))) {
173 INCR_METRIC(TOTAL);
174 INCR_METRIC(STRINGY_INSIGNIFICANT);
175 } else {
176 /* Once we're done here, re-calculate the position variables */
177 jsn->pos += (bytes - *bytes_p);
178 *nbytes_p -= (bytes - *bytes_p);
179 *bytes_p = bytes;
180 return FASTPARSE_BREAK;
181 }
182 }
183
184 /* Once we're done here, re-calculate the position variables */
185 jsn->pos += (bytes - *bytes_p);
186 return FASTPARSE_EXHAUSTED;
187 }
188
189 /* Functions exactly like str_fastparse, except it also accepts a 'state'
190 * argument, since the number's value is updated in the state. */
191 static int
jsonsl__num_fastparse(jsonsl_t jsn,const jsonsl_uchar_t ** bytes_p,size_t * nbytes_p,struct jsonsl_state_st * state)192 jsonsl__num_fastparse(jsonsl_t jsn,
193 const jsonsl_uchar_t **bytes_p, size_t *nbytes_p,
194 struct jsonsl_state_st *state)
195 {
196 int exhausted = 1;
197 size_t nbytes = *nbytes_p;
198 const jsonsl_uchar_t *bytes = *bytes_p;
199
200 for (; nbytes; nbytes--, bytes++) {
201 jsonsl_uchar_t c = *bytes;
202 if (isdigit(c)) {
203 INCR_METRIC(TOTAL);
204 INCR_METRIC(NUMBER_FASTPATH);
205 state->nelem = (state->nelem * 10) + (c - 0x30);
206 } else {
207 exhausted = 0;
208 break;
209 }
210 }
211 jsn->pos += (*nbytes_p - nbytes);
212 if (exhausted) {
213 return FASTPARSE_EXHAUSTED;
214 }
215 *nbytes_p = nbytes;
216 *bytes_p = bytes;
217 return FASTPARSE_BREAK;
218 }
219
220 JSONSL_API
221 void
jsonsl_feed(jsonsl_t jsn,const jsonsl_char_t * bytes,size_t nbytes)222 jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
223 {
224
225 #define INVOKE_ERROR(eb) \
226 if (jsn->error_callback(jsn, JSONSL_ERROR_##eb, state, (char*)c)) { \
227 goto GT_AGAIN; \
228 } \
229 return;
230
231 #define STACK_PUSH \
232 if (jsn->level >= (levels_max-1)) { \
233 jsn->error_callback(jsn, JSONSL_ERROR_LEVELS_EXCEEDED, state, (char*)c); \
234 return; \
235 } \
236 state = jsn->stack + (++jsn->level); \
237 state->ignore_callback = jsn->stack[jsn->level-1].ignore_callback; \
238 state->pos_begin = jsn->pos;
239
240 #define STACK_POP_NOPOS \
241 state->pos_cur = jsn->pos; \
242 state = jsn->stack + (--jsn->level);
243
244
245 #define STACK_POP \
246 STACK_POP_NOPOS; \
247 state->pos_cur = jsn->pos;
248
249 #define CALLBACK_AND_POP_NOPOS(T) \
250 state->pos_cur = jsn->pos; \
251 DO_CALLBACK(T, POP); \
252 state->nescapes = 0; \
253 state = jsn->stack + (--jsn->level);
254
255 #define CALLBACK_AND_POP(T) \
256 CALLBACK_AND_POP_NOPOS(T); \
257 state->pos_cur = jsn->pos;
258
259 #define SPECIAL_POP \
260 CALLBACK_AND_POP(SPECIAL); \
261 jsn->expecting = 0; \
262 jsn->tok_last = 0; \
263
264 #define CUR_CHAR (*(jsonsl_uchar_t*)c)
265
266 #define DO_CALLBACK(T, action) \
267 if (jsn->call_##T && \
268 jsn->max_callback_level > state->level && \
269 state->ignore_callback == 0) { \
270 \
271 if (jsn->action_callback_##action) { \
272 jsn->action_callback_##action(jsn, JSONSL_ACTION_##action, state, (jsonsl_char_t*)c); \
273 } else if (jsn->action_callback) { \
274 jsn->action_callback(jsn, JSONSL_ACTION_##action, state, (jsonsl_char_t*)c); \
275 } \
276 if (jsn->stopfl) { return; } \
277 }
278
279 /**
280 * Verifies that we are able to insert the (non-string) item into a hash.
281 */
282 #define ENSURE_HVAL \
283 if (state->nelem % 2 == 0 && state->type == JSONSL_T_OBJECT) { \
284 INVOKE_ERROR(HKEY_EXPECTED); \
285 }
286
287 #define VERIFY_SPECIAL(lit) \
288 if (CUR_CHAR != (lit)[jsn->pos - state->pos_begin]) { \
289 INVOKE_ERROR(SPECIAL_EXPECTED); \
290 }
291
292 #define VERIFY_SPECIAL_CI(lit) \
293 if (tolower(CUR_CHAR) != (lit)[jsn->pos - state->pos_begin]) { \
294 INVOKE_ERROR(SPECIAL_EXPECTED); \
295 }
296
297 #define STATE_SPECIAL_LENGTH \
298 (state)->nescapes
299
300 #define IS_NORMAL_NUMBER \
301 ((state)->special_flags == JSONSL_SPECIALf_UNSIGNED || \
302 (state)->special_flags == JSONSL_SPECIALf_SIGNED)
303
304 #define STATE_NUM_LAST jsn->tok_last
305
306 #define CONTINUE_NEXT_CHAR() continue
307
308 const jsonsl_uchar_t *c = (jsonsl_uchar_t*)bytes;
309 size_t levels_max = jsn->levels_max;
310 struct jsonsl_state_st *state = jsn->stack + jsn->level;
311 jsn->base = bytes;
312
313 for (; nbytes; nbytes--, jsn->pos++, c++) {
314 unsigned state_type;
315 INCR_METRIC(TOTAL);
316
317 GT_AGAIN:
318 state_type = state->type;
319 /* Most common type is typically a string: */
320 if (state_type & JSONSL_Tf_STRINGY) {
321 /* Special escape handling for some stuff */
322 if (jsn->in_escape) {
323 jsn->in_escape = 0;
324 if (!is_allowed_escape(CUR_CHAR)) {
325 INVOKE_ERROR(ESCAPE_INVALID);
326 } else if (CUR_CHAR == 'u') {
327 DO_CALLBACK(UESCAPE, UESCAPE);
328 if (jsn->return_UESCAPE) {
329 return;
330 }
331 }
332 CONTINUE_NEXT_CHAR();
333 }
334
335 if (jsonsl__str_fastparse(jsn, &c, &nbytes) ==
336 FASTPARSE_EXHAUSTED) {
337 /* No need to readjust variables as we've exhausted the iterator */
338 return;
339 } else {
340 if (CUR_CHAR == '"') {
341 goto GT_QUOTE;
342 } else if (CUR_CHAR == '\\') {
343 goto GT_ESCAPE;
344 } else {
345 INVOKE_ERROR(WEIRD_WHITESPACE);
346 }
347 }
348 INCR_METRIC(STRINGY_SLOWPATH);
349
350 } else if (state_type == JSONSL_T_SPECIAL) {
351 /* Fast track for signed/unsigned */
352 if (IS_NORMAL_NUMBER) {
353 if (jsonsl__num_fastparse(jsn, &c, &nbytes, state) ==
354 FASTPARSE_EXHAUSTED) {
355 return;
356 } else {
357 goto GT_SPECIAL_NUMERIC;
358 }
359 } else if (state->special_flags == JSONSL_SPECIALf_DASH) {
360 #ifdef JSONSL_PARSE_NAN
361 if (CUR_CHAR == 'I' || CUR_CHAR == 'i') {
362 /* parsing -Infinity? */
363 state->special_flags = JSONSL_SPECIALf_NEG_INF;
364 CONTINUE_NEXT_CHAR();
365 }
366 #endif
367
368 if (!isdigit(CUR_CHAR)) {
369 INVOKE_ERROR(INVALID_NUMBER);
370 }
371
372 if (CUR_CHAR == '0') {
373 state->special_flags = JSONSL_SPECIALf_ZERO|JSONSL_SPECIALf_SIGNED;
374 } else if (isdigit(CUR_CHAR)) {
375 state->special_flags = JSONSL_SPECIALf_SIGNED;
376 state->nelem = CUR_CHAR - 0x30;
377 } else {
378 INVOKE_ERROR(INVALID_NUMBER);
379 }
380 CONTINUE_NEXT_CHAR();
381
382 } else if (state->special_flags == JSONSL_SPECIALf_ZERO) {
383 if (isdigit(CUR_CHAR)) {
384 /* Following a zero! */
385 INVOKE_ERROR(INVALID_NUMBER);
386 }
387 /* Unset the 'zero' flag: */
388 if (state->special_flags & JSONSL_SPECIALf_SIGNED) {
389 state->special_flags = JSONSL_SPECIALf_SIGNED;
390 } else {
391 state->special_flags = JSONSL_SPECIALf_UNSIGNED;
392 }
393 goto GT_SPECIAL_NUMERIC;
394 }
395
396 if ((state->special_flags & JSONSL_SPECIALf_NUMERIC) &&
397 !(state->special_flags & JSONSL_SPECIALf_INF)) {
398 GT_SPECIAL_NUMERIC:
399 switch (CUR_CHAR) {
400 CASE_DIGITS
401 STATE_NUM_LAST = '1';
402 CONTINUE_NEXT_CHAR();
403
404 case '.':
405 if (state->special_flags & JSONSL_SPECIALf_FLOAT) {
406 INVOKE_ERROR(INVALID_NUMBER);
407 }
408 state->special_flags |= JSONSL_SPECIALf_FLOAT;
409 STATE_NUM_LAST = '.';
410 CONTINUE_NEXT_CHAR();
411
412 case 'e':
413 case 'E':
414 if (state->special_flags & JSONSL_SPECIALf_EXPONENT) {
415 INVOKE_ERROR(INVALID_NUMBER);
416 }
417 state->special_flags |= JSONSL_SPECIALf_EXPONENT;
418 STATE_NUM_LAST = 'e';
419 CONTINUE_NEXT_CHAR();
420
421 case '-':
422 case '+':
423 if (STATE_NUM_LAST != 'e') {
424 INVOKE_ERROR(INVALID_NUMBER);
425 }
426 STATE_NUM_LAST = '-';
427 CONTINUE_NEXT_CHAR();
428
429 default:
430 if (is_special_end(CUR_CHAR)) {
431 goto GT_SPECIAL_POP;
432 }
433 INVOKE_ERROR(INVALID_NUMBER);
434 break;
435 }
436 }
437 /* else if (!NUMERIC) */
438 if (!is_special_end(CUR_CHAR)) {
439 STATE_SPECIAL_LENGTH++;
440
441 /* Verify TRUE, FALSE, NULL */
442 if (state->special_flags == JSONSL_SPECIALf_TRUE) {
443 VERIFY_SPECIAL("true");
444 } else if (state->special_flags == JSONSL_SPECIALf_FALSE) {
445 VERIFY_SPECIAL("false");
446 } else if (state->special_flags == JSONSL_SPECIALf_NULL) {
447 VERIFY_SPECIAL("null");
448 #ifdef JSONSL_PARSE_NAN
449 } else if (state->special_flags == JSONSL_SPECIALf_POS_INF) {
450 VERIFY_SPECIAL_CI("infinity");
451 } else if (state->special_flags == JSONSL_SPECIALf_NEG_INF) {
452 VERIFY_SPECIAL_CI("-infinity");
453 } else if (state->special_flags == JSONSL_SPECIALf_NAN) {
454 VERIFY_SPECIAL_CI("nan");
455 } else if (state->special_flags & JSONSL_SPECIALf_NULL ||
456 state->special_flags & JSONSL_SPECIALf_NAN) {
457 /* previous char was "n", are we parsing null or nan? */
458 if (CUR_CHAR != 'u') {
459 state->special_flags &= ~JSONSL_SPECIALf_NULL;
460 }
461
462 if (tolower(CUR_CHAR) != 'a') {
463 state->special_flags &= ~JSONSL_SPECIALf_NAN;
464 }
465 #endif
466 }
467 INCR_METRIC(SPECIAL_FASTPATH);
468 CONTINUE_NEXT_CHAR();
469 }
470
471 GT_SPECIAL_POP:
472 jsn->can_insert = 0;
473 if (IS_NORMAL_NUMBER) {
474 /* Nothing */
475 } else if (state->special_flags == JSONSL_SPECIALf_ZERO ||
476 state->special_flags == (JSONSL_SPECIALf_ZERO|JSONSL_SPECIALf_SIGNED)) {
477 /* 0 is unsigned! */
478 state->special_flags = JSONSL_SPECIALf_UNSIGNED;
479 } else if (state->special_flags == JSONSL_SPECIALf_DASH) {
480 /* Still in dash! */
481 INVOKE_ERROR(INVALID_NUMBER);
482 } else if (state->special_flags & JSONSL_SPECIALf_INF) {
483 if (STATE_SPECIAL_LENGTH != 8) {
484 INVOKE_ERROR(SPECIAL_INCOMPLETE);
485 }
486 state->nelem = 1;
487 } else if (state->special_flags & JSONSL_SPECIALf_NUMERIC) {
488 /* Check that we're not at the end of a token */
489 if (STATE_NUM_LAST != '1') {
490 INVOKE_ERROR(INVALID_NUMBER);
491 }
492 } else if (state->special_flags == JSONSL_SPECIALf_TRUE) {
493 if (STATE_SPECIAL_LENGTH != 4) {
494 INVOKE_ERROR(SPECIAL_INCOMPLETE);
495 }
496 state->nelem = 1;
497 } else if (state->special_flags == JSONSL_SPECIALf_FALSE) {
498 if (STATE_SPECIAL_LENGTH != 5) {
499 INVOKE_ERROR(SPECIAL_INCOMPLETE);
500 }
501 } else if (state->special_flags == JSONSL_SPECIALf_NULL) {
502 if (STATE_SPECIAL_LENGTH != 4) {
503 INVOKE_ERROR(SPECIAL_INCOMPLETE);
504 }
505 }
506 SPECIAL_POP;
507 jsn->expecting = ',';
508 if (is_allowed_whitespace(CUR_CHAR)) {
509 CONTINUE_NEXT_CHAR();
510 }
511 /**
512 * This works because we have a non-whitespace token
513 * which is not a special token. If this is a structural
514 * character then it will be gracefully handled by the
515 * switch statement. Otherwise it will default to the 'special'
516 * state again,
517 */
518 goto GT_STRUCTURAL_TOKEN;
519 } else if (is_allowed_whitespace(CUR_CHAR)) {
520 INCR_METRIC(ALLOWED_WHITESPACE);
521 /* So we're not special. Harmless insignificant whitespace
522 * passthrough
523 */
524 CONTINUE_NEXT_CHAR();
525 } else if (extract_special(CUR_CHAR)) {
526 /* not a string, whitespace, or structural token. must be special */
527 goto GT_SPECIAL_BEGIN;
528 }
529
530 INCR_GENERIC(CUR_CHAR);
531
532 if (CUR_CHAR == '"') {
533 GT_QUOTE:
534 jsn->can_insert = 0;
535 switch (state_type) {
536
537 /* the end of a string or hash key */
538 case JSONSL_T_STRING:
539 CALLBACK_AND_POP(STRING);
540 CONTINUE_NEXT_CHAR();
541 case JSONSL_T_HKEY:
542 CALLBACK_AND_POP(HKEY);
543 CONTINUE_NEXT_CHAR();
544
545 case JSONSL_T_OBJECT:
546 state->nelem++;
547 if ( (state->nelem-1) % 2 ) {
548 /* Odd, this must be a hash value */
549 if (jsn->tok_last != ':') {
550 INVOKE_ERROR(MISSING_TOKEN);
551 }
552 jsn->expecting = ','; /* Can't figure out what to expect next */
553 jsn->tok_last = 0;
554
555 STACK_PUSH;
556 state->type = JSONSL_T_STRING;
557 DO_CALLBACK(STRING, PUSH);
558
559 } else {
560 /* hash key */
561 if (jsn->expecting != '"') {
562 INVOKE_ERROR(STRAY_TOKEN);
563 }
564 jsn->tok_last = 0;
565 jsn->expecting = ':';
566
567 STACK_PUSH;
568 state->type = JSONSL_T_HKEY;
569 DO_CALLBACK(HKEY, PUSH);
570 }
571 CONTINUE_NEXT_CHAR();
572
573 case JSONSL_T_LIST:
574 state->nelem++;
575 STACK_PUSH;
576 state->type = JSONSL_T_STRING;
577 jsn->expecting = ',';
578 jsn->tok_last = 0;
579 DO_CALLBACK(STRING, PUSH);
580 CONTINUE_NEXT_CHAR();
581
582 case JSONSL_T_SPECIAL:
583 INVOKE_ERROR(STRAY_TOKEN);
584 break;
585
586 default:
587 INVOKE_ERROR(STRING_OUTSIDE_CONTAINER);
588 break;
589 } /* switch(state->type) */
590 } else if (CUR_CHAR == '\\') {
591 GT_ESCAPE:
592 INCR_METRIC(ESCAPES);
593 /* Escape */
594 if ( (state->type & JSONSL_Tf_STRINGY) == 0 ) {
595 INVOKE_ERROR(ESCAPE_OUTSIDE_STRING);
596 }
597 state->nescapes++;
598 jsn->in_escape = 1;
599 CONTINUE_NEXT_CHAR();
600 } /* " or \ */
601
602 GT_STRUCTURAL_TOKEN:
603 switch (CUR_CHAR) {
604 case ':':
605 INCR_METRIC(STRUCTURAL_TOKEN);
606 if (jsn->expecting != CUR_CHAR) {
607 INVOKE_ERROR(STRAY_TOKEN);
608 }
609 jsn->tok_last = ':';
610 jsn->can_insert = 1;
611 jsn->expecting = '"';
612 CONTINUE_NEXT_CHAR();
613
614 case ',':
615 INCR_METRIC(STRUCTURAL_TOKEN);
616 /**
617 * The comma is one of the more generic tokens.
618 * In the context of an OBJECT, the can_insert flag
619 * should never be set, and no other action is
620 * necessary.
621 */
622 if (jsn->expecting != CUR_CHAR) {
623 /* make this branch execute only when we haven't manually
624 * just placed the ',' in the expecting register.
625 */
626 INVOKE_ERROR(STRAY_TOKEN);
627 }
628
629 if (state->type == JSONSL_T_OBJECT) {
630 /* end of hash value, expect a string as a hash key */
631 jsn->expecting = '"';
632 } else {
633 jsn->can_insert = 1;
634 }
635
636 jsn->tok_last = ',';
637 jsn->expecting = '"';
638 CONTINUE_NEXT_CHAR();
639
640 /* new list or object */
641 /* hashes are more common */
642 case '{':
643 case '[':
644 INCR_METRIC(STRUCTURAL_TOKEN);
645 if (!jsn->can_insert) {
646 INVOKE_ERROR(CANT_INSERT);
647 }
648
649 ENSURE_HVAL;
650 state->nelem++;
651
652 STACK_PUSH;
653 /* because the constants match the opening delimiters, we can do this: */
654 state->type = CUR_CHAR;
655 state->nelem = 0;
656 jsn->can_insert = 1;
657 if (CUR_CHAR == '{') {
658 /* If we're a hash, we expect a key first, which is quouted */
659 jsn->expecting = '"';
660 }
661 if (CUR_CHAR == JSONSL_T_OBJECT) {
662 DO_CALLBACK(OBJECT, PUSH);
663 } else {
664 DO_CALLBACK(LIST, PUSH);
665 }
666 jsn->tok_last = 0;
667 CONTINUE_NEXT_CHAR();
668
669 /* closing of list or object */
670 case '}':
671 case ']':
672 INCR_METRIC(STRUCTURAL_TOKEN);
673 if (jsn->tok_last == ',' && jsn->options.allow_trailing_comma == 0) {
674 INVOKE_ERROR(TRAILING_COMMA);
675 }
676
677 jsn->can_insert = 0;
678 jsn->level--;
679 jsn->expecting = ',';
680 jsn->tok_last = 0;
681 if (CUR_CHAR == ']') {
682 if (state->type != '[') {
683 INVOKE_ERROR(BRACKET_MISMATCH);
684 }
685 DO_CALLBACK(LIST, POP);
686 } else {
687 if (state->type != '{') {
688 INVOKE_ERROR(BRACKET_MISMATCH);
689 } else if (state->nelem && state->nelem % 2 != 0) {
690 INVOKE_ERROR(VALUE_EXPECTED);
691 }
692 DO_CALLBACK(OBJECT, POP);
693 }
694 state = jsn->stack + jsn->level;
695 state->pos_cur = jsn->pos;
696 CONTINUE_NEXT_CHAR();
697
698 default:
699 GT_SPECIAL_BEGIN:
700 /**
701 * Not a string, not a structural token, and not benign whitespace.
702 * Technically we should iterate over the character always, but since
703 * we are not doing full numerical/value decoding anyway (but only hinting),
704 * we only check upon entry.
705 */
706 if (state->type != JSONSL_T_SPECIAL) {
707 int special_flags = extract_special(CUR_CHAR);
708 if (!special_flags) {
709 /**
710 * Try to do some heuristics here anyway to figure out what kind of
711 * error this is. The 'special' case is a fallback scenario anyway.
712 */
713 if (CUR_CHAR == '\0') {
714 INVOKE_ERROR(FOUND_NULL_BYTE);
715 } else if (CUR_CHAR < 0x20) {
716 INVOKE_ERROR(WEIRD_WHITESPACE);
717 } else {
718 INVOKE_ERROR(SPECIAL_EXPECTED);
719 }
720 }
721 ENSURE_HVAL;
722 state->nelem++;
723 if (!jsn->can_insert) {
724 INVOKE_ERROR(CANT_INSERT);
725 }
726 STACK_PUSH;
727 state->type = JSONSL_T_SPECIAL;
728 state->special_flags = special_flags;
729 STATE_SPECIAL_LENGTH = 1;
730
731 if (special_flags == JSONSL_SPECIALf_UNSIGNED) {
732 state->nelem = CUR_CHAR - 0x30;
733 STATE_NUM_LAST = '1';
734 } else {
735 STATE_NUM_LAST = '-';
736 state->nelem = 0;
737 }
738 DO_CALLBACK(SPECIAL, PUSH);
739 }
740 CONTINUE_NEXT_CHAR();
741 }
742 }
743 }
744
745 JSONSL_API
jsonsl_strerror(jsonsl_error_t err)746 const char* jsonsl_strerror(jsonsl_error_t err)
747 {
748 if (err == JSONSL_ERROR_SUCCESS) {
749 return "SUCCESS";
750 }
751 #define X(t) \
752 if (err == JSONSL_ERROR_##t) \
753 return #t;
754 JSONSL_XERR;
755 #undef X
756 return "<UNKNOWN_ERROR>";
757 }
758
759 JSONSL_API
jsonsl_strtype(jsonsl_type_t type)760 const char *jsonsl_strtype(jsonsl_type_t type)
761 {
762 #define X(o,c) \
763 if (type == JSONSL_T_##o) \
764 return #o;
765 JSONSL_XTYPE
766 #undef X
767 return "UNKNOWN TYPE";
768
769 }
770
771 /*
772 *
773 * JPR/JSONPointer functions
774 *
775 *
776 */
777 #ifndef JSONSL_NO_JPR
778 static
779 jsonsl_jpr_type_t
populate_component(char * in,struct jsonsl_jpr_component_st * component,char ** next,jsonsl_error_t * errp)780 populate_component(char *in,
781 struct jsonsl_jpr_component_st *component,
782 char **next,
783 jsonsl_error_t *errp)
784 {
785 unsigned long pctval;
786 char *c = NULL, *outp = NULL, *end = NULL;
787 size_t input_len;
788 jsonsl_jpr_type_t ret = JSONSL_PATH_NONE;
789
790 if (*next == NULL || *(*next) == '\0') {
791 return JSONSL_PATH_NONE;
792 }
793
794 /* Replace the next / with a NULL */
795 *next = strstr(in, "/");
796 if (*next != NULL) {
797 *(*next) = '\0'; /* drop the forward slash */
798 input_len = *next - in;
799 end = *next;
800 *next += 1; /* next character after the '/' */
801 } else {
802 input_len = strlen(in);
803 end = in + input_len + 1;
804 }
805
806 component->pstr = in;
807
808 /* Check for special components of interest */
809 if (*in == JSONSL_PATH_WILDCARD_CHAR && input_len == 1) {
810 /* Lone wildcard */
811 ret = JSONSL_PATH_WILDCARD;
812 goto GT_RET;
813 } else if (isdigit(*in)) {
814 /* ASCII Numeric */
815 char *endptr;
816 component->idx = strtoul(in, &endptr, 10);
817 if (endptr && *endptr == '\0') {
818 ret = JSONSL_PATH_NUMERIC;
819 goto GT_RET;
820 }
821 }
822
823 /* Default, it's a string */
824 ret = JSONSL_PATH_STRING;
825 for (c = outp = in; c < end; c++, outp++) {
826 char origc;
827 if (*c != '%') {
828 goto GT_ASSIGN;
829 }
830 /*
831 * c = { [+0] = '%', [+1] = 'b', [+2] = 'e', [+3] = '\0' }
832 */
833
834 /* Need %XX */
835 if (c+2 >= end) {
836 *errp = JSONSL_ERROR_PERCENT_BADHEX;
837 return JSONSL_PATH_INVALID;
838 }
839 if (! (isxdigit(*(c+1)) && isxdigit(*(c+2))) ) {
840 *errp = JSONSL_ERROR_PERCENT_BADHEX;
841 return JSONSL_PATH_INVALID;
842 }
843
844 /* Temporarily null-terminate the characters */
845 origc = *(c+3);
846 *(c+3) = '\0';
847 pctval = strtoul(c+1, NULL, 16);
848 *(c+3) = origc;
849
850 *outp = (char) pctval;
851 c += 2;
852 continue;
853
854 GT_ASSIGN:
855 *outp = *c;
856 }
857 /* Null-terminate the string */
858 for (; outp < c; outp++) {
859 *outp = '\0';
860 }
861
862 GT_RET:
863 component->ptype = ret;
864 if (ret != JSONSL_PATH_WILDCARD) {
865 component->len = strlen(component->pstr);
866 }
867 return ret;
868 }
869
870 JSONSL_API
871 jsonsl_jpr_t
jsonsl_jpr_new(const char * path,jsonsl_error_t * errp)872 jsonsl_jpr_new(const char *path, jsonsl_error_t *errp)
873 {
874 char *my_copy = NULL;
875 int count, curidx;
876 struct jsonsl_jpr_st *ret = NULL;
877 struct jsonsl_jpr_component_st *components = NULL;
878 size_t origlen;
879 jsonsl_error_t errstacked;
880
881 #define JPR_BAIL(err) *errp = err; goto GT_ERROR;
882
883 if (errp == NULL) {
884 errp = &errstacked;
885 }
886
887 if (path == NULL || *path != '/') {
888 JPR_BAIL(JSONSL_ERROR_JPR_NOROOT);
889 }
890
891 count = 1;
892 path++;
893 {
894 const char *c = path;
895 for (; *c; c++) {
896 if (*c == '/') {
897 count++;
898 if (*(c+1) == '/') {
899 JPR_BAIL(JSONSL_ERROR_JPR_DUPSLASH);
900 }
901 }
902 }
903 }
904 if(*path) {
905 count++;
906 }
907
908 components = (struct jsonsl_jpr_component_st *)
909 malloc(sizeof(*components) * count);
910 if (!components) {
911 JPR_BAIL(JSONSL_ERROR_ENOMEM);
912 }
913
914 my_copy = (char *)malloc(strlen(path) + 1);
915 if (!my_copy) {
916 JPR_BAIL(JSONSL_ERROR_ENOMEM);
917 }
918
919 strcpy(my_copy, path);
920
921 components[0].ptype = JSONSL_PATH_ROOT;
922
923 if (*my_copy) {
924 char *cur = my_copy;
925 int pathret = JSONSL_PATH_STRING;
926 curidx = 1;
927 while (curidx < count) {
928 pathret = populate_component(cur, components + curidx, &cur, errp);
929 if (pathret > 0) {
930 curidx++;
931 } else {
932 break;
933 }
934 }
935
936 if (pathret == JSONSL_PATH_INVALID) {
937 JPR_BAIL(JSONSL_ERROR_JPR_BADPATH);
938 }
939 } else {
940 curidx = 1;
941 }
942
943 path--; /*revert path to leading '/' */
944 origlen = strlen(path) + 1;
945 ret = (struct jsonsl_jpr_st *)malloc(sizeof(*ret));
946 if (!ret) {
947 JPR_BAIL(JSONSL_ERROR_ENOMEM);
948 }
949 ret->orig = (char *)malloc(origlen);
950 if (!ret->orig) {
951 JPR_BAIL(JSONSL_ERROR_ENOMEM);
952 }
953 ret->components = components;
954 ret->ncomponents = curidx;
955 ret->basestr = my_copy;
956 ret->norig = origlen-1;
957 strcpy(ret->orig, path);
958
959 return ret;
960
961 GT_ERROR:
962 free(my_copy);
963 free(components);
964 if (ret) {
965 free(ret->orig);
966 }
967 free(ret);
968 return NULL;
969 #undef JPR_BAIL
970 }
971
jsonsl_jpr_destroy(jsonsl_jpr_t jpr)972 void jsonsl_jpr_destroy(jsonsl_jpr_t jpr)
973 {
974 free(jpr->components);
975 free(jpr->basestr);
976 free(jpr->orig);
977 free(jpr);
978 }
979
980 /**
981 * Call when there is a possibility of a match, either as a final match or
982 * as a path within a match
983 * @param jpr The JPR path
984 * @param component Component corresponding to the current element
985 * @param prlevel The level of the *parent*
986 * @param chtype The type of the child
987 * @return Match status
988 */
989 static jsonsl_jpr_match_t
jsonsl__match_continue(jsonsl_jpr_t jpr,const struct jsonsl_jpr_component_st * component,unsigned prlevel,unsigned chtype)990 jsonsl__match_continue(jsonsl_jpr_t jpr,
991 const struct jsonsl_jpr_component_st *component,
992 unsigned prlevel, unsigned chtype)
993 {
994 const struct jsonsl_jpr_component_st *next_comp = component + 1;
995 if (prlevel == jpr->ncomponents - 1) {
996 /* This is the match. Check the expected type of the match against
997 * the child */
998 if (jpr->match_type == 0 || jpr->match_type == chtype) {
999 return JSONSL_MATCH_COMPLETE;
1000 } else {
1001 return JSONSL_MATCH_TYPE_MISMATCH;
1002 }
1003 }
1004 if (chtype == JSONSL_T_LIST) {
1005 if (next_comp->ptype == JSONSL_PATH_NUMERIC) {
1006 return JSONSL_MATCH_POSSIBLE;
1007 } else {
1008 return JSONSL_MATCH_TYPE_MISMATCH;
1009 }
1010 } else if (chtype == JSONSL_T_OBJECT) {
1011 if (next_comp->ptype == JSONSL_PATH_NUMERIC) {
1012 return JSONSL_MATCH_TYPE_MISMATCH;
1013 } else {
1014 return JSONSL_MATCH_POSSIBLE;
1015 }
1016 } else {
1017 return JSONSL_MATCH_TYPE_MISMATCH;
1018 }
1019 }
1020
1021 JSONSL_API
1022 jsonsl_jpr_match_t
jsonsl_path_match(jsonsl_jpr_t jpr,const struct jsonsl_state_st * parent,const struct jsonsl_state_st * child,const char * key,size_t nkey)1023 jsonsl_path_match(jsonsl_jpr_t jpr,
1024 const struct jsonsl_state_st *parent,
1025 const struct jsonsl_state_st *child,
1026 const char *key, size_t nkey)
1027 {
1028 const struct jsonsl_jpr_component_st *comp;
1029 if (!parent) {
1030 /* No parent. Return immediately since it's always a match */
1031 return jsonsl__match_continue(jpr, jpr->components, 0, child->type);
1032 }
1033
1034 comp = jpr->components + parent->level;
1035
1036 /* note that we don't need to verify the type of the match, this is
1037 * always done through the previous call to jsonsl__match_continue.
1038 * If we are in a POSSIBLE tree then we can be certain the types (at
1039 * least at this level) are correct */
1040 if (parent->type == JSONSL_T_OBJECT) {
1041 if (comp->len != nkey || strncmp(key, comp->pstr, nkey) != 0) {
1042 return JSONSL_MATCH_NOMATCH;
1043 }
1044 } else {
1045 if (comp->idx != parent->nelem - 1) {
1046 return JSONSL_MATCH_NOMATCH;
1047 }
1048 }
1049 return jsonsl__match_continue(jpr, comp, parent->level, child->type);
1050 }
1051
1052 JSONSL_API
1053 jsonsl_jpr_match_t
jsonsl_jpr_match(jsonsl_jpr_t jpr,unsigned int parent_type,unsigned int parent_level,const char * key,size_t nkey)1054 jsonsl_jpr_match(jsonsl_jpr_t jpr,
1055 unsigned int parent_type,
1056 unsigned int parent_level,
1057 const char *key,
1058 size_t nkey)
1059 {
1060 /* find our current component. This is the child level */
1061 int cmpret;
1062 struct jsonsl_jpr_component_st *p_component;
1063 p_component = jpr->components + parent_level;
1064
1065 if (parent_level >= jpr->ncomponents) {
1066 return JSONSL_MATCH_NOMATCH;
1067 }
1068
1069 /* Lone query for 'root' element. Always matches */
1070 if (parent_level == 0) {
1071 if (jpr->ncomponents == 1) {
1072 return JSONSL_MATCH_COMPLETE;
1073 } else {
1074 return JSONSL_MATCH_POSSIBLE;
1075 }
1076 }
1077
1078 /* Wildcard, always matches */
1079 if (p_component->ptype == JSONSL_PATH_WILDCARD) {
1080 if (parent_level == jpr->ncomponents-1) {
1081 return JSONSL_MATCH_COMPLETE;
1082 } else {
1083 return JSONSL_MATCH_POSSIBLE;
1084 }
1085 }
1086
1087 /* Check numeric array index. This gets its special block so we can avoid
1088 * string comparisons */
1089 if (p_component->ptype == JSONSL_PATH_NUMERIC) {
1090 if (parent_type == JSONSL_T_LIST) {
1091 if (p_component->idx != nkey) {
1092 /* Wrong index */
1093 return JSONSL_MATCH_NOMATCH;
1094 } else {
1095 if (parent_level == jpr->ncomponents-1) {
1096 /* This is the last element of the path */
1097 return JSONSL_MATCH_COMPLETE;
1098 } else {
1099 /* Intermediate element */
1100 return JSONSL_MATCH_POSSIBLE;
1101 }
1102 }
1103 } else if (p_component->is_arridx) {
1104 /* Numeric and an array index (set explicitly by user). But not
1105 * a list for a parent */
1106 return JSONSL_MATCH_TYPE_MISMATCH;
1107 }
1108 } else if (parent_type == JSONSL_T_LIST) {
1109 return JSONSL_MATCH_TYPE_MISMATCH;
1110 }
1111
1112 /* Check lengths */
1113 if (p_component->len != nkey) {
1114 return JSONSL_MATCH_NOMATCH;
1115 }
1116
1117 /* Check string comparison */
1118 cmpret = strncmp(p_component->pstr, key, nkey);
1119 if (cmpret == 0) {
1120 if (parent_level == jpr->ncomponents-1) {
1121 return JSONSL_MATCH_COMPLETE;
1122 } else {
1123 return JSONSL_MATCH_POSSIBLE;
1124 }
1125 }
1126
1127 return JSONSL_MATCH_NOMATCH;
1128 }
1129
1130 JSONSL_API
jsonsl_jpr_match_state_init(jsonsl_t jsn,jsonsl_jpr_t * jprs,size_t njprs)1131 void jsonsl_jpr_match_state_init(jsonsl_t jsn,
1132 jsonsl_jpr_t *jprs,
1133 size_t njprs)
1134 {
1135 size_t ii, *firstjmp;
1136 if (njprs == 0) {
1137 return;
1138 }
1139 jsn->jprs = (jsonsl_jpr_t *)malloc(sizeof(jsonsl_jpr_t) * njprs);
1140 jsn->jpr_count = njprs;
1141 jsn->jpr_root = (size_t*)calloc(1, sizeof(size_t) * njprs * jsn->levels_max);
1142 memcpy(jsn->jprs, jprs, sizeof(jsonsl_jpr_t) * njprs);
1143 /* Set the initial jump table values */
1144
1145 firstjmp = jsn->jpr_root;
1146 for (ii = 0; ii < njprs; ii++) {
1147 firstjmp[ii] = ii+1;
1148 }
1149 }
1150
1151 JSONSL_API
jsonsl_jpr_match_state_cleanup(jsonsl_t jsn)1152 void jsonsl_jpr_match_state_cleanup(jsonsl_t jsn)
1153 {
1154 if (jsn->jpr_count == 0) {
1155 return;
1156 }
1157
1158 free(jsn->jpr_root);
1159 free(jsn->jprs);
1160 jsn->jprs = NULL;
1161 jsn->jpr_root = NULL;
1162 jsn->jpr_count = 0;
1163 }
1164
1165 /**
1166 * This function should be called exactly once on each element...
1167 * This should also be called in recursive order, since we rely
1168 * on the parent having been initalized for a match.
1169 *
1170 * Since the parent is checked for a match as well, we maintain a 'serial' counter.
1171 * Whenever we traverse an element, we expect the serial to be the same as a global
1172 * integer. If they do not match, we re-initialize the context, and set the serial.
1173 *
1174 * This ensures a type of consistency without having a proactive reset by the
1175 * main lexer itself.
1176 *
1177 */
1178 JSONSL_API
jsonsl_jpr_match_state(jsonsl_t jsn,struct jsonsl_state_st * state,const char * key,size_t nkey,jsonsl_jpr_match_t * out)1179 jsonsl_jpr_t jsonsl_jpr_match_state(jsonsl_t jsn,
1180 struct jsonsl_state_st *state,
1181 const char *key,
1182 size_t nkey,
1183 jsonsl_jpr_match_t *out)
1184 {
1185 struct jsonsl_state_st *parent_state;
1186 jsonsl_jpr_t ret = NULL;
1187
1188 /* Jump and JPR tables for our own state and the parent state */
1189 size_t *jmptable, *pjmptable;
1190 size_t jmp_cur, ii, ourjmpidx;
1191
1192 if (!jsn->jpr_root) {
1193 *out = JSONSL_MATCH_NOMATCH;
1194 return NULL;
1195 }
1196
1197 pjmptable = jsn->jpr_root + (jsn->jpr_count * (state->level-1));
1198 jmptable = pjmptable + jsn->jpr_count;
1199
1200 /* If the parent cannot match, then invalidate it */
1201 if (*pjmptable == 0) {
1202 *jmptable = 0;
1203 *out = JSONSL_MATCH_NOMATCH;
1204 return NULL;
1205 }
1206
1207 parent_state = jsn->stack + state->level - 1;
1208
1209 if (parent_state->type == JSONSL_T_LIST) {
1210 nkey = (size_t) parent_state->nelem;
1211 }
1212
1213 *jmptable = 0;
1214 ourjmpidx = 0;
1215 memset(jmptable, 0, sizeof(int) * jsn->jpr_count);
1216
1217 for (ii = 0; ii < jsn->jpr_count; ii++) {
1218 jmp_cur = pjmptable[ii];
1219 if (jmp_cur) {
1220 jsonsl_jpr_t jpr = jsn->jprs[jmp_cur-1];
1221 *out = jsonsl_jpr_match(jpr,
1222 parent_state->type,
1223 parent_state->level,
1224 key, nkey);
1225 if (*out == JSONSL_MATCH_COMPLETE) {
1226 ret = jpr;
1227 *jmptable = 0;
1228 return ret;
1229 } else if (*out == JSONSL_MATCH_POSSIBLE) {
1230 jmptable[ourjmpidx] = ii+1;
1231 ourjmpidx++;
1232 }
1233 } else {
1234 break;
1235 }
1236 }
1237 if (!*jmptable) {
1238 *out = JSONSL_MATCH_NOMATCH;
1239 }
1240 return NULL;
1241 }
1242
1243 JSONSL_API
jsonsl_strmatchtype(jsonsl_jpr_match_t match)1244 const char *jsonsl_strmatchtype(jsonsl_jpr_match_t match)
1245 {
1246 #define X(T,v) \
1247 if ( match == JSONSL_MATCH_##T ) \
1248 return #T;
1249 JSONSL_XMATCH
1250 #undef X
1251 return "<UNKNOWN>";
1252 }
1253
1254 #endif /* JSONSL_WITH_JPR */
1255
1256 static char *
jsonsl__writeutf8(uint32_t pt,char * out)1257 jsonsl__writeutf8(uint32_t pt, char *out)
1258 {
1259 #define ADD_OUTPUT(c) *out = (char)(c); out++;
1260
1261 if (pt < 0x80) {
1262 ADD_OUTPUT(pt);
1263 } else if (pt < 0x800) {
1264 ADD_OUTPUT((pt >> 6) | 0xC0);
1265 ADD_OUTPUT((pt & 0x3F) | 0x80);
1266 } else if (pt < 0x10000) {
1267 ADD_OUTPUT((pt >> 12) | 0xE0);
1268 ADD_OUTPUT(((pt >> 6) & 0x3F) | 0x80);
1269 ADD_OUTPUT((pt & 0x3F) | 0x80);
1270 } else {
1271 ADD_OUTPUT((pt >> 18) | 0xF0);
1272 ADD_OUTPUT(((pt >> 12) & 0x3F) | 0x80);
1273 ADD_OUTPUT(((pt >> 6) & 0x3F) | 0x80);
1274 ADD_OUTPUT((pt & 0x3F) | 0x80);
1275 }
1276 return out;
1277 #undef ADD_OUTPUT
1278 }
1279
1280 /* Thanks snej (https://github.com/mnunberg/jsonsl/issues/9) */
1281 static int
jsonsl__digit2int(char ch)1282 jsonsl__digit2int(char ch) {
1283 int d = ch - '0';
1284 if ((unsigned) d < 10) {
1285 return d;
1286 }
1287 d = ch - 'a';
1288 if ((unsigned) d < 6) {
1289 return d + 10;
1290 }
1291 d = ch - 'A';
1292 if ((unsigned) d < 6) {
1293 return d + 10;
1294 }
1295 return -1;
1296 }
1297
1298 /* Assume 's' is at least 4 bytes long */
1299 static int
jsonsl__get_uescape_16(const char * s)1300 jsonsl__get_uescape_16(const char *s)
1301 {
1302 int ret = 0;
1303 int cur;
1304
1305 #define GET_DIGIT(off) \
1306 cur = jsonsl__digit2int(s[off]); \
1307 if (cur == -1) { return -1; } \
1308 ret |= (cur << (12 - (off * 4)));
1309
1310 GET_DIGIT(0);
1311 GET_DIGIT(1);
1312 GET_DIGIT(2);
1313 GET_DIGIT(3);
1314 #undef GET_DIGIT
1315 return ret;
1316 }
1317
1318 /**
1319 * Utility function to convert escape sequences
1320 */
1321 JSONSL_API
jsonsl_util_unescape_ex(const char * in,char * out,size_t len,const int toEscape[128],unsigned * oflags,jsonsl_error_t * err,const char ** errat)1322 size_t jsonsl_util_unescape_ex(const char *in,
1323 char *out,
1324 size_t len,
1325 const int toEscape[128],
1326 unsigned *oflags,
1327 jsonsl_error_t *err,
1328 const char **errat)
1329 {
1330 const unsigned char *c = (const unsigned char*)in;
1331 char *begin_p = out;
1332 unsigned oflags_s;
1333 uint16_t last_codepoint = 0;
1334
1335 if (!oflags) {
1336 oflags = &oflags_s;
1337 }
1338 *oflags = 0;
1339
1340 #define UNESCAPE_BAIL(e,offset) \
1341 *err = JSONSL_ERROR_##e; \
1342 if (errat) { \
1343 *errat = (const char*)(c+ (ptrdiff_t)(offset)); \
1344 } \
1345 return 0;
1346
1347 for (; len; len--, c++, out++) {
1348 int uescval;
1349 if (*c != '\\') {
1350 /* Not an escape, so we don't care about this */
1351 goto GT_ASSIGN;
1352 }
1353
1354 if (len < 2) {
1355 UNESCAPE_BAIL(ESCAPE_INVALID, 0);
1356 }
1357 if (!is_allowed_escape(c[1])) {
1358 UNESCAPE_BAIL(ESCAPE_INVALID, 1)
1359 }
1360 if ((toEscape && toEscape[(unsigned char)c[1] & 0x7f] == 0 &&
1361 c[1] != '\\' && c[1] != '"')) {
1362 /* if we don't want to unescape this string, write the escape sequence to the output */
1363 *out++ = *c++;
1364 --len;
1365 goto GT_ASSIGN;
1366 }
1367
1368 if (c[1] != 'u') {
1369 /* simple skip-and-replace using pre-defined maps.
1370 * TODO: should the maps actually reflect the desired
1371 * replacement character in toEscape?
1372 */
1373 char esctmp = get_escape_equiv(c[1]);
1374 if (esctmp) {
1375 /* Check if there is a corresponding replacement */
1376 *out = esctmp;
1377 } else {
1378 /* Just gobble up the 'reverse-solidus' */
1379 *out = c[1];
1380 }
1381 len--;
1382 c++;
1383 /* do not assign, just continue */
1384 continue;
1385 }
1386
1387 /* next == 'u' */
1388 if (len < 6) {
1389 /* Need at least six characters.. */
1390 UNESCAPE_BAIL(UESCAPE_TOOSHORT, 2);
1391 }
1392
1393 uescval = jsonsl__get_uescape_16((const char *)c + 2);
1394 if (uescval == -1) {
1395 UNESCAPE_BAIL(PERCENT_BADHEX, -1);
1396 }
1397
1398 if (last_codepoint) {
1399 uint16_t w1 = last_codepoint, w2 = (uint16_t)uescval;
1400 uint32_t cp;
1401
1402 if (uescval < 0xDC00 || uescval > 0xDFFF) {
1403 UNESCAPE_BAIL(INVALID_CODEPOINT, -1);
1404 }
1405
1406 cp = (w1 & 0x3FF) << 10;
1407 cp |= (w2 & 0x3FF);
1408 cp += 0x10000;
1409
1410 out = jsonsl__writeutf8(cp, out) - 1;
1411 last_codepoint = 0;
1412
1413 } else if (uescval < 0xD800 || uescval > 0xDFFF) {
1414 *oflags |= JSONSL_SPECIALf_NONASCII;
1415 out = jsonsl__writeutf8(uescval, out) - 1;
1416
1417 } else if (uescval < 0xDC00) {
1418 *oflags |= JSONSL_SPECIALf_NONASCII;
1419 last_codepoint = (uint16_t)uescval;
1420 out--;
1421 } else {
1422 UNESCAPE_BAIL(INVALID_CODEPOINT, 2);
1423 }
1424
1425 /* Post uescape cleanup */
1426 len -= 5; /* Gobble up 5 chars after 'u' */
1427 c += 5;
1428 continue;
1429
1430 /* Only reached by previous branches */
1431 GT_ASSIGN:
1432 *out = *c;
1433 }
1434
1435 if (last_codepoint) {
1436 *err = JSONSL_ERROR_INVALID_CODEPOINT;
1437 return 0;
1438 }
1439
1440 *err = JSONSL_ERROR_SUCCESS;
1441 return out - begin_p;
1442 }
1443
1444 /**
1445 * Character Table definitions.
1446 * These were all generated via srcutil/genchartables.pl
1447 */
1448
1449 /**
1450 * This table contains the beginnings of non-string
1451 * allowable (bareword) values.
1452 */
1453 static unsigned short Special_Table[0x100] = {
1454 /* 0x00 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x1f */
1455 /* 0x20 */ 0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x2c */
1456 /* 0x2d */ JSONSL_SPECIALf_DASH /* <-> */, /* 0x2d */
1457 /* 0x2e */ 0,0, /* 0x2f */
1458 /* 0x30 */ JSONSL_SPECIALf_ZERO /* <0> */, /* 0x30 */
1459 /* 0x31 */ JSONSL_SPECIALf_UNSIGNED /* <1> */, /* 0x31 */
1460 /* 0x32 */ JSONSL_SPECIALf_UNSIGNED /* <2> */, /* 0x32 */
1461 /* 0x33 */ JSONSL_SPECIALf_UNSIGNED /* <3> */, /* 0x33 */
1462 /* 0x34 */ JSONSL_SPECIALf_UNSIGNED /* <4> */, /* 0x34 */
1463 /* 0x35 */ JSONSL_SPECIALf_UNSIGNED /* <5> */, /* 0x35 */
1464 /* 0x36 */ JSONSL_SPECIALf_UNSIGNED /* <6> */, /* 0x36 */
1465 /* 0x37 */ JSONSL_SPECIALf_UNSIGNED /* <7> */, /* 0x37 */
1466 /* 0x38 */ JSONSL_SPECIALf_UNSIGNED /* <8> */, /* 0x38 */
1467 /* 0x39 */ JSONSL_SPECIALf_UNSIGNED /* <9> */, /* 0x39 */
1468 /* 0x3a */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x48 */
1469 /* 0x49 */ JSONSL__INF_PROXY /* <I> */, /* 0x49 */
1470 /* 0x4a */ 0,0,0,0, /* 0x4d */
1471 /* 0x4e */ JSONSL__NAN_PROXY /* <N> */, /* 0x4e */
1472 /* 0x4f */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x65 */
1473 /* 0x66 */ JSONSL_SPECIALf_FALSE /* <f> */, /* 0x66 */
1474 /* 0x67 */ 0,0, /* 0x68 */
1475 /* 0x69 */ JSONSL__INF_PROXY /* <i> */, /* 0x69 */
1476 /* 0x6a */ 0,0,0,0, /* 0x6d */
1477 /* 0x6e */ JSONSL_SPECIALf_NULL|JSONSL__NAN_PROXY /* <n> */, /* 0x6e */
1478 /* 0x6f */ 0,0,0,0,0, /* 0x73 */
1479 /* 0x74 */ JSONSL_SPECIALf_TRUE /* <t> */, /* 0x74 */
1480 /* 0x75 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x94 */
1481 /* 0x95 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb4 */
1482 /* 0xb5 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xd4 */
1483 /* 0xd5 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xf4 */
1484 /* 0xf5 */ 0,0,0,0,0,0,0,0,0,0, /* 0xfe */
1485 };
1486
1487 /**
1488 * Contains characters which signal the termination of any of the 'special' bareword
1489 * values.
1490 */
1491 static int Special_Endings[0x100] = {
1492 /* 0x00 */ 0,0,0,0,0,0,0,0,0, /* 0x08 */
1493 /* 0x09 */ 1 /* <TAB> */, /* 0x09 */
1494 /* 0x0a */ 1 /* <LF> */, /* 0x0a */
1495 /* 0x0b */ 0,0, /* 0x0c */
1496 /* 0x0d */ 1 /* <CR> */, /* 0x0d */
1497 /* 0x0e */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x1f */
1498 /* 0x20 */ 1 /* <SP> */, /* 0x20 */
1499 /* 0x21 */ 0, /* 0x21 */
1500 /* 0x22 */ 1 /* " */, /* 0x22 */
1501 /* 0x23 */ 0,0,0,0,0,0,0,0,0, /* 0x2b */
1502 /* 0x2c */ 1 /* , */, /* 0x2c */
1503 /* 0x2d */ 0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x39 */
1504 /* 0x3a */ 1 /* : */, /* 0x3a */
1505 /* 0x3b */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x5a */
1506 /* 0x5b */ 1 /* [ */, /* 0x5b */
1507 /* 0x5c */ 1 /* \ */, /* 0x5c */
1508 /* 0x5d */ 1 /* ] */, /* 0x5d */
1509 /* 0x5e */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x7a */
1510 /* 0x7b */ 1 /* { */, /* 0x7b */
1511 /* 0x7c */ 0, /* 0x7c */
1512 /* 0x7d */ 1 /* } */, /* 0x7d */
1513 /* 0x7e */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x9d */
1514 /* 0x9e */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xbd */
1515 /* 0xbe */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xdd */
1516 /* 0xde */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xfd */
1517 /* 0xfe */ 0 /* 0xfe */
1518 };
1519
1520 /**
1521 * This table contains entries for the allowed whitespace as per RFC 4627
1522 */
1523 static int Allowed_Whitespace[0x100] = {
1524 /* 0x00 */ 0,0,0,0,0,0,0,0,0, /* 0x08 */
1525 /* 0x09 */ 1 /* <TAB> */, /* 0x09 */
1526 /* 0x0a */ 1 /* <LF> */, /* 0x0a */
1527 /* 0x0b */ 0,0, /* 0x0c */
1528 /* 0x0d */ 1 /* <CR> */, /* 0x0d */
1529 /* 0x0e */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x1f */
1530 /* 0x20 */ 1 /* <SP> */, /* 0x20 */
1531 /* 0x21 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x40 */
1532 /* 0x41 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x60 */
1533 /* 0x61 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 */
1534 /* 0x81 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0 */
1535 /* 0xa1 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xc0 */
1536 /* 0xc1 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xe0 */
1537 /* 0xe1 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0xfe */
1538 };
1539
1540 static const int String_No_Passthrough[0x100] = {
1541 /* 0x00 */ 1 /* <NUL> */, /* 0x00 */
1542 /* 0x01 */ 1 /* <SOH> */, /* 0x01 */
1543 /* 0x02 */ 1 /* <STX> */, /* 0x02 */
1544 /* 0x03 */ 1 /* <ETX> */, /* 0x03 */
1545 /* 0x04 */ 1 /* <EOT> */, /* 0x04 */
1546 /* 0x05 */ 1 /* <ENQ> */, /* 0x05 */
1547 /* 0x06 */ 1 /* <ACK> */, /* 0x06 */
1548 /* 0x07 */ 1 /* <BEL> */, /* 0x07 */
1549 /* 0x08 */ 1 /* <BS> */, /* 0x08 */
1550 /* 0x09 */ 1 /* <HT> */, /* 0x09 */
1551 /* 0x0a */ 1 /* <LF> */, /* 0x0a */
1552 /* 0x0b */ 1 /* <VT> */, /* 0x0b */
1553 /* 0x0c */ 1 /* <FF> */, /* 0x0c */
1554 /* 0x0d */ 1 /* <CR> */, /* 0x0d */
1555 /* 0x0e */ 1 /* <SO> */, /* 0x0e */
1556 /* 0x0f */ 1 /* <SI> */, /* 0x0f */
1557 /* 0x10 */ 1 /* <DLE> */, /* 0x10 */
1558 /* 0x11 */ 1 /* <DC1> */, /* 0x11 */
1559 /* 0x12 */ 1 /* <DC2> */, /* 0x12 */
1560 /* 0x13 */ 1 /* <DC3> */, /* 0x13 */
1561 /* 0x14 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x21 */
1562 /* 0x22 */ 1 /* <"> */, /* 0x22 */
1563 /* 0x23 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x42 */
1564 /* 0x43 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x5b */
1565 /* 0x5c */ 1 /* <\> */, /* 0x5c */
1566 /* 0x5d */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x7c */
1567 /* 0x7d */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x9c */
1568 /* 0x9d */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xbc */
1569 /* 0xbd */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xdc */
1570 /* 0xdd */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xfc */
1571 /* 0xfd */ 0,0, /* 0xfe */
1572 };
1573
1574 /**
1575 * Allowable two-character 'common' escapes:
1576 */
1577 static int Allowed_Escapes[0x100] = {
1578 /* 0x00 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x1f */
1579 /* 0x20 */ 0,0, /* 0x21 */
1580 /* 0x22 */ 1 /* <"> */, /* 0x22 */
1581 /* 0x23 */ 0,0,0,0,0,0,0,0,0,0,0,0, /* 0x2e */
1582 /* 0x2f */ 1 /* </> */, /* 0x2f */
1583 /* 0x30 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f */
1584 /* 0x50 */ 0,0,0,0,0,0,0,0,0,0,0,0, /* 0x5b */
1585 /* 0x5c */ 1 /* <\> */, /* 0x5c */
1586 /* 0x5d */ 0,0,0,0,0, /* 0x61 */
1587 /* 0x62 */ 1 /* <b> */, /* 0x62 */
1588 /* 0x63 */ 0,0,0, /* 0x65 */
1589 /* 0x66 */ 1 /* <f> */, /* 0x66 */
1590 /* 0x67 */ 0,0,0,0,0,0,0, /* 0x6d */
1591 /* 0x6e */ 1 /* <n> */, /* 0x6e */
1592 /* 0x6f */ 0,0,0, /* 0x71 */
1593 /* 0x72 */ 1 /* <r> */, /* 0x72 */
1594 /* 0x73 */ 0, /* 0x73 */
1595 /* 0x74 */ 1 /* <t> */, /* 0x74 */
1596 /* 0x75 */ 1 /* <u> */, /* 0x75 */
1597 /* 0x76 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x95 */
1598 /* 0x96 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb5 */
1599 /* 0xb6 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xd5 */
1600 /* 0xd6 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xf5 */
1601 /* 0xf6 */ 0,0,0,0,0,0,0,0,0, /* 0xfe */
1602 };
1603
1604 /**
1605 * This table contains the _values_ for a given (single) escaped character.
1606 */
1607 static unsigned char Escape_Equivs[0x100] = {
1608 /* 0x00 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x1f */
1609 /* 0x20 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x3f */
1610 /* 0x40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x5f */
1611 /* 0x60 */ 0,0, /* 0x61 */
1612 /* 0x62 */ 8 /* <b> */, /* 0x62 */
1613 /* 0x63 */ 0,0,0, /* 0x65 */
1614 /* 0x66 */ 12 /* <f> */, /* 0x66 */
1615 /* 0x67 */ 0,0,0,0,0,0,0, /* 0x6d */
1616 /* 0x6e */ 10 /* <n> */, /* 0x6e */
1617 /* 0x6f */ 0,0,0, /* 0x71 */
1618 /* 0x72 */ 13 /* <r> */, /* 0x72 */
1619 /* 0x73 */ 0, /* 0x73 */
1620 /* 0x74 */ 9 /* <t> */, /* 0x74 */
1621 /* 0x75 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x94 */
1622 /* 0x95 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb4 */
1623 /* 0xb5 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xd4 */
1624 /* 0xd5 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xf4 */
1625 /* 0xf5 */ 0,0,0,0,0,0,0,0,0,0 /* 0xfe */
1626 };
1627
1628 /* Definitions of above-declared static functions */
get_escape_equiv(unsigned c)1629 static char get_escape_equiv(unsigned c) {
1630 return Escape_Equivs[c & 0xff];
1631 }
extract_special(unsigned c)1632 static unsigned extract_special(unsigned c) {
1633 return Special_Table[c & 0xff];
1634 }
is_special_end(unsigned c)1635 static int is_special_end(unsigned c) {
1636 return Special_Endings[c & 0xff];
1637 }
is_allowed_whitespace(unsigned c)1638 static int is_allowed_whitespace(unsigned c) {
1639 return c == ' ' || Allowed_Whitespace[c & 0xff];
1640 }
is_allowed_escape(unsigned c)1641 static int is_allowed_escape(unsigned c) {
1642 return Allowed_Escapes[c & 0xff];
1643 }
is_simple_char(unsigned c)1644 static int is_simple_char(unsigned c) {
1645 return !String_No_Passthrough[c & 0xff];
1646 }
1647
1648 /* Clean up all our macros! */
1649 #undef INCR_METRIC
1650 #undef INCR_GENERIC
1651 #undef INCR_STRINGY_CATCH
1652 #undef CASE_DIGITS
1653 #undef INVOKE_ERROR
1654 #undef STACK_PUSH
1655 #undef STACK_POP_NOPOS
1656 #undef STACK_POP
1657 #undef CALLBACK_AND_POP_NOPOS
1658 #undef CALLBACK_AND_POP
1659 #undef SPECIAL_POP
1660 #undef CUR_CHAR
1661 #undef DO_CALLBACK
1662 #undef ENSURE_HVAL
1663 #undef VERIFY_SPECIAL
1664 #undef STATE_SPECIAL_LENGTH
1665 #undef IS_NORMAL_NUMBER
1666 #undef STATE_NUM_LAST
1667 #undef FASTPARSE_EXHAUSTED
1668 #undef FASTPARSE_BREAK
1669