Lines Matching refs:tok

96 enum json_tokener_error json_tokener_get_error(json_tokener *tok)  in json_tokener_get_error()  argument
98 return tok->err; in json_tokener_get_error()
109 struct json_tokener *tok; in json_tokener_new_ex() local
111 tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener)); in json_tokener_new_ex()
112 if (!tok) return NULL; in json_tokener_new_ex()
113 tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec)); in json_tokener_new_ex()
114 if (!tok->stack) { in json_tokener_new_ex()
115 free(tok); in json_tokener_new_ex()
118 tok->pb = printbuf_new(); in json_tokener_new_ex()
119 tok->max_depth = depth; in json_tokener_new_ex()
120 json_tokener_reset(tok); in json_tokener_new_ex()
121 return tok; in json_tokener_new_ex()
129 void json_tokener_free(struct json_tokener *tok) in json_tokener_free() argument
131 json_tokener_reset(tok); in json_tokener_free()
132 if (tok->pb) printbuf_free(tok->pb); in json_tokener_free()
133 if (tok->stack) free(tok->stack); in json_tokener_free()
134 free(tok); in json_tokener_free()
137 static void json_tokener_reset_level(struct json_tokener *tok, int depth) in json_tokener_reset_level() argument
139 tok->stack[depth].state = json_tokener_state_eatws; in json_tokener_reset_level()
140 tok->stack[depth].saved_state = json_tokener_state_start; in json_tokener_reset_level()
141 json_object_put(tok->stack[depth].current); in json_tokener_reset_level()
142 tok->stack[depth].current = NULL; in json_tokener_reset_level()
143 free(tok->stack[depth].obj_field_name); in json_tokener_reset_level()
144 tok->stack[depth].obj_field_name = NULL; in json_tokener_reset_level()
147 void json_tokener_reset(struct json_tokener *tok) in json_tokener_reset() argument
150 if (!tok) in json_tokener_reset()
153 for(i = tok->depth; i >= 0; i--) in json_tokener_reset()
154 json_tokener_reset_level(tok, i); in json_tokener_reset()
155 tok->depth = 0; in json_tokener_reset()
156 tok->err = json_tokener_success; in json_tokener_reset()
169 struct json_tokener* tok; in json_tokener_parse_verbose() local
172 tok = json_tokener_new(); in json_tokener_parse_verbose()
173 if (!tok) in json_tokener_parse_verbose()
175 obj = json_tokener_parse_ex(tok, str, -1); in json_tokener_parse_verbose()
176 *error = tok->err; in json_tokener_parse_verbose()
177 if(tok->err != json_tokener_success) { in json_tokener_parse_verbose()
183 json_tokener_free(tok); in json_tokener_parse_verbose()
187 #define state tok->stack[tok->depth].state
188 #define saved_state tok->stack[tok->depth].saved_state
189 #define current tok->stack[tok->depth].current
190 #define obj_field_name tok->stack[tok->depth].obj_field_name
210 #define PEEK_CHAR(dest, tok) \ argument
211 (((tok)->char_offset == len) ? \
212 …(((tok)->depth == 0 && state == json_tokener_state_eatws && saved_state == json_tokener_state_fini…
213 (((tok)->err = json_tokener_success), 0) \
215 (((tok)->err = json_tokener_continue), 0) \
225 #define ADVANCE_CHAR(str, tok) \ argument
226 ( ++(str), ((tok)->char_offset)++, c)
232 struct json_object* json_tokener_parse_ex(struct json_tokener *tok, in json_tokener_parse_ex() argument
245 tok->char_offset = 0; in json_tokener_parse_ex()
246 tok->err = json_tokener_success; in json_tokener_parse_ex()
254 tok->err = json_tokener_error_size; in json_tokener_parse_ex()
258 while (PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
266 if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok))) in json_tokener_parse_ex()
269 if(c == '/' && !(tok->flags & JSON_TOKENER_STRICT)) { in json_tokener_parse_ex()
270 printbuf_reset(tok->pb); in json_tokener_parse_ex()
271 printbuf_memappend_fast(tok->pb, &c, 1); in json_tokener_parse_ex()
294 printbuf_reset(tok->pb); in json_tokener_parse_ex()
295 tok->st_pos = 0; in json_tokener_parse_ex()
300 printbuf_reset(tok->pb); in json_tokener_parse_ex()
301 tok->st_pos = 0; in json_tokener_parse_ex()
304 if (tok->flags & JSON_TOKENER_STRICT) { in json_tokener_parse_ex()
306 tok->err = json_tokener_error_parse_unexpected; in json_tokener_parse_ex()
311 printbuf_reset(tok->pb); in json_tokener_parse_ex()
312 tok->quote_char = c; in json_tokener_parse_ex()
319 printbuf_reset(tok->pb); in json_tokener_parse_ex()
320 tok->st_pos = 0; in json_tokener_parse_ex()
338 printbuf_reset(tok->pb); in json_tokener_parse_ex()
339 tok->is_double = 0; in json_tokener_parse_ex()
342 tok->err = json_tokener_error_parse_unexpected; in json_tokener_parse_ex()
348 if(tok->depth == 0) goto out; in json_tokener_parse_ex()
350 json_tokener_reset_level(tok, tok->depth); in json_tokener_parse_ex()
351 tok->depth--; in json_tokener_parse_ex()
359 printbuf_memappend_fast(tok->pb, &c, 1); in json_tokener_parse_ex()
360 size_inf = json_min(tok->st_pos+1, json_inf_str_len); in json_tokener_parse_ex()
361 char *infbuf = tok->pb->buf; in json_tokener_parse_ex()
367 if ((!(tok->flags & JSON_TOKENER_STRICT) && in json_tokener_parse_ex()
372 if (tok->st_pos == json_inf_str_len) in json_tokener_parse_ex()
380 tok->err = json_tokener_error_parse_unexpected; in json_tokener_parse_ex()
383 tok->st_pos++; in json_tokener_parse_ex()
390 printbuf_memappend_fast(tok->pb, &c, 1); in json_tokener_parse_ex()
391 size = json_min(tok->st_pos+1, json_null_str_len); in json_tokener_parse_ex()
392 size_nan = json_min(tok->st_pos+1, json_nan_str_len); in json_tokener_parse_ex()
393 if((!(tok->flags & JSON_TOKENER_STRICT) && in json_tokener_parse_ex()
394 strncasecmp(json_null_str, tok->pb->buf, size) == 0) in json_tokener_parse_ex()
395 || (strncmp(json_null_str, tok->pb->buf, size) == 0) in json_tokener_parse_ex()
397 if (tok->st_pos == json_null_str_len) { in json_tokener_parse_ex()
404 else if ((!(tok->flags & JSON_TOKENER_STRICT) && in json_tokener_parse_ex()
405 strncasecmp(json_nan_str, tok->pb->buf, size_nan) == 0) || in json_tokener_parse_ex()
406 (strncmp(json_nan_str, tok->pb->buf, size_nan) == 0) in json_tokener_parse_ex()
409 if (tok->st_pos == json_nan_str_len) in json_tokener_parse_ex()
417 tok->err = json_tokener_error_parse_null; in json_tokener_parse_ex()
420 tok->st_pos++; in json_tokener_parse_ex()
430 tok->err = json_tokener_error_parse_comment; in json_tokener_parse_ex()
433 printbuf_memappend_fast(tok->pb, &c, 1); in json_tokener_parse_ex()
441 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
442 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
446 printbuf_memappend_fast(tok->pb, case_start, 1+str-case_start); in json_tokener_parse_ex()
456 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
457 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
461 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
462 MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf); in json_tokener_parse_ex()
468 printbuf_memappend_fast(tok->pb, &c, 1); in json_tokener_parse_ex()
470 MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf); in json_tokener_parse_ex()
482 if(c == tok->quote_char) { in json_tokener_parse_ex()
483 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
484 current = json_object_new_string_len(tok->pb->buf, tok->pb->bpos); in json_tokener_parse_ex()
489 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
494 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
495 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
507 printbuf_memappend_fast(tok->pb, &c, 1); in json_tokener_parse_ex()
515 if(c == 'b') printbuf_memappend_fast(tok->pb, "\b", 1); in json_tokener_parse_ex()
516 else if(c == 'n') printbuf_memappend_fast(tok->pb, "\n", 1); in json_tokener_parse_ex()
517 else if(c == 'r') printbuf_memappend_fast(tok->pb, "\r", 1); in json_tokener_parse_ex()
518 else if(c == 't') printbuf_memappend_fast(tok->pb, "\t", 1); in json_tokener_parse_ex()
519 else if(c == 'f') printbuf_memappend_fast(tok->pb, "\f", 1); in json_tokener_parse_ex()
523 tok->ucs_char = 0; in json_tokener_parse_ex()
524 tok->st_pos = 0; in json_tokener_parse_ex()
528 tok->err = json_tokener_error_parse_string; in json_tokener_parse_ex()
540 tok->ucs_char += ((unsigned int)jt_hexdigit(c) << ((3-tok->st_pos++)*4)); in json_tokener_parse_ex()
541 if(tok->st_pos == 4) { in json_tokener_parse_ex()
545 if (IS_LOW_SURROGATE(tok->ucs_char)) { in json_tokener_parse_ex()
547 tok->ucs_char = DECODE_SURROGATE_PAIR(got_hi_surrogate, tok->ucs_char); in json_tokener_parse_ex()
551 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); in json_tokener_parse_ex()
556 if (tok->ucs_char < 0x80) { in json_tokener_parse_ex()
557 unescaped_utf[0] = tok->ucs_char; in json_tokener_parse_ex()
558 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 1); in json_tokener_parse_ex()
559 } else if (tok->ucs_char < 0x800) { in json_tokener_parse_ex()
560 unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6); in json_tokener_parse_ex()
561 unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f); in json_tokener_parse_ex()
562 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 2); in json_tokener_parse_ex()
563 } else if (IS_HIGH_SURROGATE(tok->ucs_char)) { in json_tokener_parse_ex()
568 got_hi_surrogate = tok->ucs_char; in json_tokener_parse_ex()
570 if ((tok->char_offset+1 != len) && in json_tokener_parse_ex()
571 (tok->char_offset+2 != len) && in json_tokener_parse_ex()
579 if( !ADVANCE_CHAR(str, tok) || !ADVANCE_CHAR(str, tok) ) { in json_tokener_parse_ex()
580 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); in json_tokener_parse_ex()
585 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
586 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); in json_tokener_parse_ex()
589 tok->ucs_char = 0; in json_tokener_parse_ex()
590 tok->st_pos = 0; in json_tokener_parse_ex()
597 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); in json_tokener_parse_ex()
599 } else if (IS_LOW_SURROGATE(tok->ucs_char)) { in json_tokener_parse_ex()
601 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); in json_tokener_parse_ex()
602 } else if (tok->ucs_char < 0x10000) { in json_tokener_parse_ex()
603 unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12); in json_tokener_parse_ex()
604 unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f); in json_tokener_parse_ex()
605 unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f); in json_tokener_parse_ex()
606 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 3); in json_tokener_parse_ex()
607 } else if (tok->ucs_char < 0x110000) { in json_tokener_parse_ex()
608 unescaped_utf[0] = 0xf0 | ((tok->ucs_char >> 18) & 0x07); in json_tokener_parse_ex()
609 unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f); in json_tokener_parse_ex()
610 unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f); in json_tokener_parse_ex()
611 unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f); in json_tokener_parse_ex()
612 printbuf_memappend_fast(tok->pb, (char*)unescaped_utf, 4); in json_tokener_parse_ex()
615 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); in json_tokener_parse_ex()
621 tok->err = json_tokener_error_parse_string; in json_tokener_parse_ex()
624 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
626 printbuf_memappend_fast(tok->pb, (char*)utf8_replacement_char, 3); in json_tokener_parse_ex()
636 printbuf_memappend_fast(tok->pb, &c, 1); in json_tokener_parse_ex()
637 size1 = json_min(tok->st_pos+1, json_true_str_len); in json_tokener_parse_ex()
638 size2 = json_min(tok->st_pos+1, json_false_str_len); in json_tokener_parse_ex()
639 if((!(tok->flags & JSON_TOKENER_STRICT) && in json_tokener_parse_ex()
640 strncasecmp(json_true_str, tok->pb->buf, size1) == 0) in json_tokener_parse_ex()
641 || (strncmp(json_true_str, tok->pb->buf, size1) == 0) in json_tokener_parse_ex()
643 if(tok->st_pos == json_true_str_len) { in json_tokener_parse_ex()
649 } else if((!(tok->flags & JSON_TOKENER_STRICT) && in json_tokener_parse_ex()
650 strncasecmp(json_false_str, tok->pb->buf, size2) == 0) in json_tokener_parse_ex()
651 || (strncmp(json_false_str, tok->pb->buf, size2) == 0)) { in json_tokener_parse_ex()
652 if(tok->st_pos == json_false_str_len) { in json_tokener_parse_ex()
659 tok->err = json_tokener_error_parse_boolean; in json_tokener_parse_ex()
662 tok->st_pos++; in json_tokener_parse_ex()
674 tok->is_double = 1; in json_tokener_parse_ex()
675 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
676 printbuf_memappend_fast(tok->pb, case_start, case_len); in json_tokener_parse_ex()
681 printbuf_memappend_fast(tok->pb, case_start, case_len); in json_tokener_parse_ex()
684 if (tok->pb->buf[0] == '-' && case_len == 1 && in json_tokener_parse_ex()
694 if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) { in json_tokener_parse_ex()
695 if (num64 && tok->pb->buf[0]=='0' && (tok->flags & JSON_TOKENER_STRICT)) { in json_tokener_parse_ex()
697 tok->err = json_tokener_error_parse_number; in json_tokener_parse_ex()
702 else if(tok->is_double && json_parse_double(tok->pb->buf, &numd) == 0) in json_tokener_parse_ex()
704 current = json_object_new_double_s(numd, tok->pb->buf); in json_tokener_parse_ex()
706 tok->err = json_tokener_error_parse_number; in json_tokener_parse_ex()
719 (tok->flags & JSON_TOKENER_STRICT)) in json_tokener_parse_ex()
721 tok->err = json_tokener_error_parse_unexpected; in json_tokener_parse_ex()
727 if(tok->depth >= tok->max_depth-1) { in json_tokener_parse_ex()
728 tok->err = json_tokener_error_depth; in json_tokener_parse_ex()
732 tok->depth++; in json_tokener_parse_ex()
733 json_tokener_reset_level(tok, tok->depth); in json_tokener_parse_ex()
752 tok->err = json_tokener_error_parse_array; in json_tokener_parse_ex()
761 (tok->flags & JSON_TOKENER_STRICT)) in json_tokener_parse_ex()
763 tok->err = json_tokener_error_parse_unexpected; in json_tokener_parse_ex()
769 tok->quote_char = c; in json_tokener_parse_ex()
770 printbuf_reset(tok->pb); in json_tokener_parse_ex()
773 tok->err = json_tokener_error_parse_object_key_name; in json_tokener_parse_ex()
783 if(c == tok->quote_char) { in json_tokener_parse_ex()
784 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
785 obj_field_name = strdup(tok->pb->buf); in json_tokener_parse_ex()
790 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
795 if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok)) { in json_tokener_parse_ex()
796 printbuf_memappend_fast(tok->pb, case_start, str-case_start); in json_tokener_parse_ex()
808 tok->err = json_tokener_error_parse_object_key_sep; in json_tokener_parse_ex()
814 if(tok->depth >= tok->max_depth-1) { in json_tokener_parse_ex()
815 tok->err = json_tokener_error_depth; in json_tokener_parse_ex()
819 tok->depth++; in json_tokener_parse_ex()
820 json_tokener_reset_level(tok, tok->depth); in json_tokener_parse_ex()
839 tok->err = json_tokener_error_parse_object_value_sep; in json_tokener_parse_ex()
845 if (!ADVANCE_CHAR(str, tok)) in json_tokener_parse_ex()
852 (tok->depth == 0) && in json_tokener_parse_ex()
853 (tok->flags & JSON_TOKENER_STRICT)) { in json_tokener_parse_ex()
855 tok->err = json_tokener_error_parse_unexpected; in json_tokener_parse_ex()
860 tok->err = json_tokener_error_parse_eof; in json_tokener_parse_ex()
868 if (tok->err == json_tokener_success) in json_tokener_parse_ex()
874 for(ii = tok->depth; ii >= 0; ii--) in json_tokener_parse_ex()
875 json_tokener_reset_level(tok, ii); in json_tokener_parse_ex()
880 json_tokener_errors[tok->err], tok->char_offset); in json_tokener_parse_ex()
884 void json_tokener_set_flags(struct json_tokener *tok, int flags) in json_tokener_set_flags() argument
886 tok->flags = flags; in json_tokener_set_flags()