mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-09 11:20:30 +00:00
make ljson more strict
- add new context ARRAY_SINGLE for [singleton] without commas - add new context ARRAY_END for [] without commas - add new context OBJECT_END for {} without commas - call Parse() once before looping for array or object so that the empty case is handled easily ([,] should now be wrong) - narrow use of OBJECT_KEY -- now it is only used to confirm that the key is a string - rename ARRAY_VAL to AFTER_VALUE -- a comma will only be present after a value ie either [1,2] or {"a":1,"b":2}, so the context for comma is always after value - reset context when AFTER_VALUE and OBJECT_VAL to avoid extra ,: - use bitfields instead of equality to check the context value - TOP_LEVEL is used to define the maximum depth, and now the depth is compared to TOP_LEVEL for the base case
This commit is contained in:
parent
30cc2c8dc1
commit
f3a76cde5d
1 changed files with 29 additions and 21 deletions
|
@ -31,10 +31,14 @@
|
||||||
#include "third_party/lua/lua.h"
|
#include "third_party/lua/lua.h"
|
||||||
#include "tool/net/ljson.h"
|
#include "tool/net/ljson.h"
|
||||||
|
|
||||||
#define TOP_LEVEL 0
|
#define AFTER_VALUE 0x01u
|
||||||
#define ARRAY_VAL 1
|
#define ARRAY_SINGLE 0x02u
|
||||||
#define OBJECT_KEY 2
|
#define ARRAY_END 0x04u
|
||||||
#define OBJECT_VAL 3
|
#define OBJECT_KEY 0x10u
|
||||||
|
#define OBJECT_VAL 0x20u
|
||||||
|
#define OBJECT_END 0x40u
|
||||||
|
|
||||||
|
#define TOP_LEVEL 64
|
||||||
|
|
||||||
static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
const char *e, int context, int depth) {
|
const char *e, int context, int depth) {
|
||||||
|
@ -58,7 +62,8 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ',': // present in list and object
|
case ',': // present in list and object
|
||||||
if (context == ARRAY_VAL || context == OBJECT_KEY) {
|
if (0 != (context & AFTER_VALUE)) {
|
||||||
|
context = 0;
|
||||||
a = p;
|
a = p;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
@ -66,7 +71,8 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
}
|
}
|
||||||
|
|
||||||
case ':': // present only in object after key
|
case ':': // present only in object after key
|
||||||
if (context == OBJECT_VAL) {
|
if (0 != (context & OBJECT_VAL)) {
|
||||||
|
context = 0;
|
||||||
a = p;
|
a = p;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
@ -74,7 +80,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'n': // null
|
case 'n': // null
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
if (p + 3 <= e && READ32LE(p - 1) == READ32LE("null")) {
|
if (p + 3 <= e && READ32LE(p - 1) == READ32LE("null")) {
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
return (struct DecodeJson){1, p + 3};
|
return (struct DecodeJson){1, p + 3};
|
||||||
|
@ -83,7 +89,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'f': // false
|
case 'f': // false
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
if (p + 4 <= e && READ32LE(p) == READ32LE("alse")) {
|
if (p + 4 <= e && READ32LE(p) == READ32LE("alse")) {
|
||||||
lua_pushboolean(L, false);
|
lua_pushboolean(L, false);
|
||||||
return (struct DecodeJson){1, p + 4};
|
return (struct DecodeJson){1, p + 4};
|
||||||
|
@ -92,7 +98,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
}
|
}
|
||||||
|
|
||||||
case 't': // true
|
case 't': // true
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
if (p + 3 <= e && READ32LE(p - 1) == READ32LE("true")) {
|
if (p + 3 <= e && READ32LE(p - 1) == READ32LE("true")) {
|
||||||
lua_pushboolean(L, true);
|
lua_pushboolean(L, true);
|
||||||
return (struct DecodeJson){1, p + 3};
|
return (struct DecodeJson){1, p + 3};
|
||||||
|
@ -104,7 +110,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
return (struct DecodeJson){-1, "object key must be string"};
|
return (struct DecodeJson){-1, "object key must be string"};
|
||||||
|
|
||||||
case '-': // negative
|
case '-': // negative
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
if (p < e && isdigit(*p)) {
|
if (p < e && isdigit(*p)) {
|
||||||
d = -1;
|
d = -1;
|
||||||
break;
|
break;
|
||||||
|
@ -113,7 +119,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
}
|
}
|
||||||
|
|
||||||
case '0': // zero or number
|
case '0': // zero or number
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
if (p < e) {
|
if (p < e) {
|
||||||
if ((*p == '.' || *p == 'e' || *p == 'E')) {
|
if ((*p == '.' || *p == 'e' || *p == 'E')) {
|
||||||
goto UseDubble;
|
goto UseDubble;
|
||||||
|
@ -125,7 +131,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
return (struct DecodeJson){1, p};
|
return (struct DecodeJson){1, p};
|
||||||
|
|
||||||
case '1' ... '9': // integer
|
case '1' ... '9': // integer
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
for (x = (c - '0') * d; p < e; ++p) {
|
for (x = (c - '0') * d; p < e; ++p) {
|
||||||
c = *p & 255;
|
c = *p & 255;
|
||||||
if (isdigit(c)) {
|
if (isdigit(c)) {
|
||||||
|
@ -148,11 +154,11 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
return (struct DecodeJson){1, a + c};
|
return (struct DecodeJson){1, a + c};
|
||||||
|
|
||||||
case '[': // Array
|
case '[': // Array
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
i = 0;
|
i = 0;
|
||||||
|
r = Parse(L, p, e, ARRAY_SINGLE | ARRAY_END, depth - 1);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
r = Parse(L, p, e, ARRAY_VAL, depth - 1);
|
|
||||||
if (UNLIKELY(r.rc == -1)) {
|
if (UNLIKELY(r.rc == -1)) {
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
return r;
|
return r;
|
||||||
|
@ -162,6 +168,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
lua_rawseti(L, -2, i++ + 1);
|
lua_rawseti(L, -2, i++ + 1);
|
||||||
|
r = Parse(L, p, e, AFTER_VALUE | ARRAY_END, depth - 1);
|
||||||
}
|
}
|
||||||
if (!i) {
|
if (!i) {
|
||||||
// we need this kludge so `[]` won't round-trip as `{}`
|
// we need this kludge so `[]` won't round-trip as `{}`
|
||||||
|
@ -171,24 +178,24 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
return (struct DecodeJson){1, p};
|
return (struct DecodeJson){1, p};
|
||||||
|
|
||||||
case ']':
|
case ']':
|
||||||
if (context == ARRAY_VAL) {
|
if (0 != (context & ARRAY_END)) {
|
||||||
return (struct DecodeJson){0, p};
|
return (struct DecodeJson){0, p};
|
||||||
} else {
|
} else {
|
||||||
return (struct DecodeJson){-1, "unexpected ']'"};
|
return (struct DecodeJson){-1, "unexpected ']'"};
|
||||||
}
|
}
|
||||||
|
|
||||||
case '}':
|
case '}':
|
||||||
if (context == OBJECT_KEY) {
|
if (0 != (context & OBJECT_END)) {
|
||||||
return (struct DecodeJson){0, p};
|
return (struct DecodeJson){0, p};
|
||||||
} else {
|
} else {
|
||||||
return (struct DecodeJson){-1, "unexpected '}'"};
|
return (struct DecodeJson){-1, "unexpected '}'"};
|
||||||
}
|
}
|
||||||
|
|
||||||
case '{': // Object
|
case '{': // Object
|
||||||
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
|
if (UNLIKELY(0 != (context & OBJECT_KEY))) goto BadObjectKey;
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
|
r = Parse(L, p, e, OBJECT_KEY | OBJECT_END, depth - 1);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
r = Parse(L, p, e, OBJECT_KEY, depth - 1);
|
|
||||||
if (r.rc == -1) {
|
if (r.rc == -1) {
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
return r;
|
return r;
|
||||||
|
@ -208,6 +215,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
}
|
}
|
||||||
p = r.p;
|
p = r.p;
|
||||||
lua_settable(L, -3);
|
lua_settable(L, -3);
|
||||||
|
r = Parse(L, p, e, OBJECT_KEY | AFTER_VALUE | OBJECT_END, depth - 1);
|
||||||
}
|
}
|
||||||
return (struct DecodeJson){1, p};
|
return (struct DecodeJson){1, p};
|
||||||
|
|
||||||
|
@ -355,7 +363,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
return (struct DecodeJson){-1, "illegal character"};
|
return (struct DecodeJson){-1, "illegal character"};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (UNLIKELY(context == TOP_LEVEL)) {
|
if (UNLIKELY(depth == TOP_LEVEL)) {
|
||||||
return (struct DecodeJson){0, 0};
|
return (struct DecodeJson){0, 0};
|
||||||
} else {
|
} else {
|
||||||
return (struct DecodeJson){-1, "unexpected eof"};
|
return (struct DecodeJson){-1, "unexpected eof"};
|
||||||
|
@ -386,10 +394,10 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p,
|
||||||
* @return r.p is string describing error if `rc < 0`
|
* @return r.p is string describing error if `rc < 0`
|
||||||
*/
|
*/
|
||||||
struct DecodeJson DecodeJson(struct lua_State *L, const char *p, size_t n) {
|
struct DecodeJson DecodeJson(struct lua_State *L, const char *p, size_t n) {
|
||||||
int depth = 64;
|
int depth = TOP_LEVEL;
|
||||||
if (n == -1) n = p ? strlen(p) : 0;
|
if (n == -1) n = p ? strlen(p) : 0;
|
||||||
if (lua_checkstack(L, depth * 4)) {
|
if (lua_checkstack(L, depth * 4)) {
|
||||||
return Parse(L, p, p + n, TOP_LEVEL, depth);
|
return Parse(L, p, p + n, 0, depth);
|
||||||
} else {
|
} else {
|
||||||
return (struct DecodeJson){-1, "can't set stack depth"};
|
return (struct DecodeJson){-1, "can't set stack depth"};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue