| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- /* ujson: see ujson.h. */
- #include "ujson.h"
- struct pstate {
- struct ujson *j;
- const char *s;
- unsigned int len, pos;
- int depth;
- };
- static int fail(struct pstate *p, const char *msg)
- {
- if (!p->j->error) {
- p->j->error = msg;
- p->j->error_pos = p->pos;
- }
- return -1;
- }
- static void skip_ws(struct pstate *p)
- {
- while (p->pos < p->len) {
- char c = p->s[p->pos];
- if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
- break;
- p->pos++;
- }
- }
- static int new_node(struct pstate *p, unsigned char type)
- {
- struct ujson *j = p->j;
- struct ujson_node *n;
- if (j->n >= j->cap)
- return fail(p, "too many JSON nodes");
- n = &j->nodes[j->n];
- n->type = type;
- n->key = 0;
- n->str = 0;
- n->len = 0;
- n->child = -1;
- n->next = -1;
- return (int)j->n++;
- }
- static int sbuf_put(struct pstate *p, char c)
- {
- struct ujson *j = p->j;
- if (j->sn >= j->scap)
- return fail(p, "JSON string pool full");
- j->sbuf[j->sn++] = c;
- return 0;
- }
- static int put_utf8(struct pstate *p, unsigned int cp)
- {
- if (cp < 0x80)
- return sbuf_put(p, (char)cp);
- if (cp < 0x800) {
- if (sbuf_put(p, (char)(0xC0 | (cp >> 6))) ||
- sbuf_put(p, (char)(0x80 | (cp & 0x3F))))
- return -1;
- return 0;
- }
- if (cp < 0x10000) {
- if (sbuf_put(p, (char)(0xE0 | (cp >> 12))) ||
- sbuf_put(p, (char)(0x80 | ((cp >> 6) & 0x3F))) ||
- sbuf_put(p, (char)(0x80 | (cp & 0x3F))))
- return -1;
- return 0;
- }
- if (sbuf_put(p, (char)(0xF0 | (cp >> 18))) ||
- sbuf_put(p, (char)(0x80 | ((cp >> 12) & 0x3F))) ||
- sbuf_put(p, (char)(0x80 | ((cp >> 6) & 0x3F))) ||
- sbuf_put(p, (char)(0x80 | (cp & 0x3F))))
- return -1;
- return 0;
- }
- static int hex4(struct pstate *p, unsigned int *out)
- {
- unsigned int v = 0, i;
- if (p->pos + 4 > p->len)
- return fail(p, "short \\u escape");
- for (i = 0; i < 4; i++) {
- char c = p->s[p->pos + i];
- unsigned int d;
- if (c >= '0' && c <= '9')
- d = (unsigned int)(c - '0');
- else if (c >= 'a' && c <= 'f')
- d = (unsigned int)(c - 'a' + 10);
- else if (c >= 'A' && c <= 'F')
- d = (unsigned int)(c - 'A' + 10);
- else
- return fail(p, "bad \\u escape");
- v = v * 16 + d;
- }
- p->pos += 4;
- *out = v;
- return 0;
- }
- /* Parse a string literal at p->pos (on the opening quote) into the pool. */
- static int parse_string(struct pstate *p, const char **out, unsigned int *olen)
- {
- unsigned int start;
- p->pos++; /* opening quote */
- start = p->j->sn;
- for (;;) {
- unsigned char c;
- if (p->pos >= p->len)
- return fail(p, "unterminated string");
- c = (unsigned char)p->s[p->pos++];
- if (c == '"')
- break;
- if (c < 0x20)
- return fail(p, "control character in string");
- if (c != '\\') {
- if (sbuf_put(p, (char)c))
- return -1;
- continue;
- }
- if (p->pos >= p->len)
- return fail(p, "unterminated escape");
- c = (unsigned char)p->s[p->pos++];
- switch (c) {
- case '"': c = '"'; break;
- case '\\': c = '\\'; break;
- case '/': c = '/'; break;
- case 'b': c = 8; break;
- case 'f': c = 12; break;
- case 'n': c = '\n'; break;
- case 'r': c = '\r'; break;
- case 't': c = '\t'; break;
- case 'u': {
- unsigned int cp, lo;
- if (hex4(p, &cp))
- return -1;
- if (cp >= 0xD800 && cp <= 0xDBFF) {
- /* High surrogate: a low one should follow. */
- if (p->pos + 6 <= p->len &&
- p->s[p->pos] == '\\' &&
- p->s[p->pos + 1] == 'u') {
- unsigned int save = p->pos;
- p->pos += 2;
- if (hex4(p, &lo))
- return -1;
- if (lo >= 0xDC00 && lo <= 0xDFFF) {
- cp = 0x10000 +
- ((cp - 0xD800) << 10) +
- (lo - 0xDC00);
- } else {
- p->pos = save;
- cp = 0xFFFD;
- }
- } else {
- cp = 0xFFFD;
- }
- } else if (cp >= 0xDC00 && cp <= 0xDFFF) {
- cp = 0xFFFD;
- }
- if (put_utf8(p, cp))
- return -1;
- continue;
- }
- default:
- return fail(p, "bad escape");
- }
- if (sbuf_put(p, (char)c))
- return -1;
- }
- *olen = p->j->sn - start;
- if (sbuf_put(p, '\0'))
- return -1;
- *out = p->j->sbuf + start;
- return 0;
- }
- static int match_word(struct pstate *p, const char *w)
- {
- unsigned int i;
- for (i = 0; w[i]; i++)
- if (p->pos + i >= p->len || p->s[p->pos + i] != w[i])
- return 0;
- p->pos += i;
- return 1;
- }
- static int parse_value(struct pstate *p);
- static int parse_number(struct pstate *p)
- {
- unsigned int start = p->pos, i;
- int idx, digits = 0;
- struct ujson_node *n;
- if (p->pos < p->len && p->s[p->pos] == '-')
- p->pos++;
- while (p->pos < p->len) {
- char c = p->s[p->pos];
- if (c >= '0' && c <= '9') {
- digits++;
- } else if (c != '.' && c != 'e' && c != 'E' &&
- c != '+' && c != '-') {
- break;
- }
- p->pos++;
- }
- if (!digits)
- return fail(p, "bad number");
- idx = new_node(p, UJSON_NUMBER);
- if (idx < 0)
- return -1;
- n = &p->j->nodes[idx];
- n->str = p->j->sbuf + p->j->sn;
- n->len = p->pos - start;
- for (i = start; i < p->pos; i++)
- if (sbuf_put(p, p->s[i]))
- return -1;
- if (sbuf_put(p, '\0'))
- return -1;
- return idx;
- }
- static int parse_container(struct pstate *p, int obj)
- {
- int idx, last = -1;
- char close = obj ? '}' : ']';
- idx = new_node(p, obj ? UJSON_OBJECT : UJSON_ARRAY);
- if (idx < 0)
- return -1;
- if (++p->depth > UJSON_MAX_DEPTH)
- return fail(p, "JSON nested too deeply");
- p->pos++; /* [ or { */
- skip_ws(p);
- if (p->pos < p->len && p->s[p->pos] == close) {
- p->pos++;
- p->depth--;
- return idx;
- }
- for (;;) {
- const char *key = 0;
- unsigned int klen;
- int child;
- skip_ws(p);
- if (obj) {
- if (p->pos >= p->len || p->s[p->pos] != '"')
- return fail(p, "expected member name");
- if (parse_string(p, &key, &klen))
- return -1;
- skip_ws(p);
- if (p->pos >= p->len || p->s[p->pos] != ':')
- return fail(p, "expected ':'");
- p->pos++;
- skip_ws(p);
- }
- child = parse_value(p);
- if (child < 0)
- return -1;
- p->j->nodes[child].key = key;
- if (last < 0)
- p->j->nodes[idx].child = child;
- else
- p->j->nodes[last].next = child;
- last = child;
- p->j->nodes[idx].len++;
- skip_ws(p);
- if (p->pos >= p->len)
- return fail(p, "unterminated container");
- if (p->s[p->pos] == ',') {
- p->pos++;
- continue;
- }
- if (p->s[p->pos] == close) {
- p->pos++;
- break;
- }
- return fail(p, obj ? "expected ',' or '}'" :
- "expected ',' or ']'");
- }
- p->depth--;
- return idx;
- }
- static int parse_value(struct pstate *p)
- {
- char c;
- int idx;
- skip_ws(p);
- if (p->pos >= p->len)
- return fail(p, "unexpected end of JSON");
- c = p->s[p->pos];
- if (c == '{')
- return parse_container(p, 1);
- if (c == '[')
- return parse_container(p, 0);
- if (c == '"') {
- const char *s;
- unsigned int n;
- idx = new_node(p, UJSON_STRING);
- if (idx < 0)
- return -1;
- if (parse_string(p, &s, &n))
- return -1;
- p->j->nodes[idx].str = s;
- p->j->nodes[idx].len = n;
- return idx;
- }
- if (c == '-' || (c >= '0' && c <= '9'))
- return parse_number(p);
- if (match_word(p, "true"))
- return new_node(p, UJSON_TRUE);
- if (match_word(p, "false"))
- return new_node(p, UJSON_FALSE);
- if (match_word(p, "null"))
- return new_node(p, UJSON_NULL);
- return fail(p, "unexpected character");
- }
- void ujson_init(struct ujson *j, struct ujson_node *nodes, unsigned int cap,
- char *sbuf, unsigned int scap)
- {
- j->nodes = nodes;
- j->cap = cap;
- j->n = 0;
- j->sbuf = sbuf;
- j->scap = scap;
- j->sn = 0;
- j->error = 0;
- j->error_pos = 0;
- }
- int ujson_parse(struct ujson *j, const char *src, unsigned int len)
- {
- struct pstate p;
- int root;
- j->n = 0;
- j->sn = 0;
- j->error = 0;
- j->error_pos = 0;
- p.j = j;
- p.s = src;
- p.len = len;
- p.pos = 0;
- p.depth = 0;
- /* Skip a UTF-8 byte order mark. */
- if (len >= 3 && (unsigned char)src[0] == 0xEF &&
- (unsigned char)src[1] == 0xBB && (unsigned char)src[2] == 0xBF)
- p.pos = 3;
- root = parse_value(&p);
- if (root < 0)
- return -1;
- skip_ws(&p);
- if (p.pos != p.len)
- return fail(&p, "trailing data after JSON");
- return root;
- }
- static int valid(const struct ujson *j, int idx)
- {
- return idx >= 0 && (unsigned int)idx < j->n;
- }
- int ujson_type(const struct ujson *j, int idx)
- {
- return valid(j, idx) ? j->nodes[idx].type : -1;
- }
- static int str_eq(const char *a, const char *b)
- {
- while (*a && *a == *b) {
- a++;
- b++;
- }
- return *a == *b;
- }
- int ujson_get(const struct ujson *j, int idx, const char *key)
- {
- int c;
- if (!valid(j, idx) || j->nodes[idx].type != UJSON_OBJECT)
- return -1;
- for (c = j->nodes[idx].child; c >= 0; c = j->nodes[c].next)
- if (j->nodes[c].key && str_eq(j->nodes[c].key, key))
- return c;
- return -1;
- }
- int ujson_first(const struct ujson *j, int idx)
- {
- if (!valid(j, idx) || (j->nodes[idx].type != UJSON_ARRAY &&
- j->nodes[idx].type != UJSON_OBJECT))
- return -1;
- return j->nodes[idx].child;
- }
- int ujson_next(const struct ujson *j, int idx)
- {
- return valid(j, idx) ? j->nodes[idx].next : -1;
- }
- unsigned int ujson_count(const struct ujson *j, int idx)
- {
- if (!valid(j, idx) || (j->nodes[idx].type != UJSON_ARRAY &&
- j->nodes[idx].type != UJSON_OBJECT))
- return 0;
- return j->nodes[idx].len;
- }
- const char *ujson_str(const struct ujson *j, int idx)
- {
- if (!valid(j, idx) || j->nodes[idx].type != UJSON_STRING)
- return "";
- return j->nodes[idx].str;
- }
- const char *ujson_get_str(const struct ujson *j, int obj, const char *key)
- {
- return ujson_str(j, ujson_get(j, obj, key));
- }
- long ujson_int(const struct ujson *j, int idx, long def)
- {
- const char *s;
- long v = 0;
- int neg = 0, digits = 0;
- if (!valid(j, idx))
- return def;
- if (j->nodes[idx].type != UJSON_NUMBER &&
- j->nodes[idx].type != UJSON_STRING)
- return def;
- s = j->nodes[idx].str;
- if (*s == '-') {
- neg = 1;
- s++;
- }
- while (*s >= '0' && *s <= '9') {
- v = v * 10 + (*s - '0');
- digits++;
- s++;
- }
- if (!digits)
- return def;
- return neg ? -v : v;
- }
- int ujson_truthy(const struct ujson *j, int idx)
- {
- if (!valid(j, idx))
- return 0;
- switch (j->nodes[idx].type) {
- case UJSON_TRUE:
- return 1;
- case UJSON_NUMBER:
- return ujson_int(j, idx, 0) != 0;
- case UJSON_STRING:
- return j->nodes[idx].len != 0;
- default:
- return 0;
- }
- }
- int ujson_escape(char *out, unsigned int cap, const char *s)
- {
- static const char hex[] = "0123456789abcdef";
- unsigned int n = 0;
- #define PUT(ch) do { if (n + 1 >= cap) return -1; out[n++] = (ch); } while (0)
- PUT('"');
- for (; *s; s++) {
- unsigned char c = (unsigned char)*s;
- if (c == '"' || c == '\\') {
- PUT('\\');
- PUT((char)c);
- } else if (c == '\n') {
- PUT('\\');
- PUT('n');
- } else if (c == '\r') {
- PUT('\\');
- PUT('r');
- } else if (c == '\t') {
- PUT('\\');
- PUT('t');
- } else if (c < 0x20) {
- PUT('\\');
- PUT('u');
- PUT('0');
- PUT('0');
- PUT(hex[c >> 4]);
- PUT(hex[c & 15]);
- } else {
- PUT((char)c);
- }
- }
- PUT('"');
- #undef PUT
- out[n] = '\0';
- return (int)n;
- }
|