void u0025_jsonwriter__verify(SG_context * pCtx, SG_string* pstr) { const char *psz = SG_string__sz(pstr); SG_int32* putf32 = NULL; SG_uint32 len = 0; JSON_checker jc = new_JSON_checker(20); SG_uint32 i; int ok = 0; SG_utf8__to_utf32__sz(pCtx, psz, &putf32, &len); for (i=0; i<len; i++) { ok = JSON_checker_char(jc, putf32[i]); VERIFY_COND("JSON_checker_char", ok); if (!ok) { break; } } if (ok) { ok = JSON_checker_done(jc); VERIFY_COND("JSON_checker_done", ok); } SG_NULLFREE(pCtx, putf32); }
int sg_json_check(const char *json, int check_depth) { JSON_checker checker; size_t i, len; sg_assert(json); sg_assert(check_depth > 0); len = strlen(json); if (len == 0) /* content null */ return -1; checker = new_JSON_checker(check_depth); for (i = 0; i < len; i++) { if (json[i] <= 0) break; if (!JSON_checker_char(checker, json[i])) /* syntax error */ return -1; } if (!JSON_checker_done(checker)) /* syntax error */ return -1; return 0; }
/* Check for both UTF-8ness and JSONness in one pass */ int checkUTF8JSON(const unsigned char* data, size_t size) { int expect = 0; /* Expect UTF code point to extend this many bytes */ int badjson = 0; int badutf = 0; const unsigned char *end = data + size; JSON_checker jc = new_JSON_checker((size/2) + 1); for(;data < end; data++) { if(!JSON_checker_char(jc, *data)) { badjson = 1; break; } if(*data <= 0x7F) { if(expect != 0) { /* Must not be expecting >0x7F. */ badutf = 1; break; } continue; } if((*data & 0xC0) == 0xC0) { if(expect != 0) { /* Beginning of UTF-8 multi-byte sequence inside of another one. */ badutf = 1; break; } expect++; if(*data & 0x20) expect++; if((*data & 0x10) && expect == 2) expect++; /* Verify zero bit separates count bits and codepoint bits */ if(expect == 3 && (*data & 0x8)) return false; continue; } if(expect) { expect--; } else { /* Got > 0x7F when not expecting it */ badutf = 1; break; } } if(!badjson) { /* Feed fake space to the validator to force it to finish validating */ /* numerical values, iff it hasn't marked the current stream as valid */ if(jc->state != OK) { badjson = !JSON_checker_char(jc, 32); } if(!badjson) { badjson = !JSON_checker_done(jc); } } return (!badjson && !badutf); }
int main(int argc, char ** argv) { int c; JSON_checker jc = new_JSON_checker(255); (void)argv; if (argc != 1) die(2, "wrong number of arguments"); for (c = getchar(); c > 0; c = getchar()) if (!JSON_checker_char(jc, c)) exit(1); if (!JSON_checker_done(jc)) exit(1); exit(0); }
int main(int argc, char* argv[]) { /* Read STDIN. Exit with a message if the input is not well-formed JSON text. jc will contain a JSON_checker with a maximum depth of 20. */ JSON_checker jc = new_JSON_checker(20); for (;;) { int next_char = getchar(); if (next_char <= 0) { break; } if (!JSON_checker_char(jc, next_char)) { fprintf(stderr, "JSON_checker_char: syntax error\n"); exit(1); } } if (!JSON_checker_done(jc)) { fprintf(stderr, "JSON_checker_end: syntax error\n"); exit(1); } }