diff options
-rw-r--r-- | lua_cjson.c | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index 5f7d6fb..9ec69cf 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
@@ -731,18 +731,14 @@ static void json_append_data(lua_State *l, json_config_t *cfg, | |||
731 | 731 | ||
732 | static int json_encode(lua_State *l) | 732 | static int json_encode(lua_State *l) |
733 | { | 733 | { |
734 | json_config_t *cfg; | 734 | json_config_t *cfg = json_fetch_config(l); |
735 | strbuf_t local_encode_buf; | 735 | strbuf_t local_encode_buf; |
736 | strbuf_t *encode_buf; | 736 | strbuf_t *encode_buf; |
737 | char *json; | 737 | char *json; |
738 | int len; | 738 | int len; |
739 | 739 | ||
740 | /* Can't use json_verify_arg_count() since we need to ensure | ||
741 | * there is only 1 argument */ | ||
742 | luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); | 740 | luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); |
743 | 741 | ||
744 | cfg = json_fetch_config(l); | ||
745 | |||
746 | if (!cfg->encode_keep_buffer) { | 742 | if (!cfg->encode_keep_buffer) { |
747 | /* Use private buffer */ | 743 | /* Use private buffer */ |
748 | encode_buf = &local_encode_buf; | 744 | encode_buf = &local_encode_buf; |
@@ -1282,17 +1278,27 @@ static void json_process_value(lua_State *l, json_parse_t *json, | |||
1282 | } | 1278 | } |
1283 | } | 1279 | } |
1284 | 1280 | ||
1285 | /* json_text must be null terminated string */ | 1281 | static int json_decode(lua_State *l) |
1286 | static void lua_json_decode(lua_State *l, const char *json_text, int json_len) | ||
1287 | { | 1282 | { |
1288 | json_parse_t json; | 1283 | json_parse_t json; |
1289 | json_token_t token; | 1284 | json_token_t token; |
1285 | size_t json_len; | ||
1286 | |||
1287 | luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); | ||
1290 | 1288 | ||
1291 | json.cfg = json_fetch_config(l); | 1289 | json.cfg = json_fetch_config(l); |
1290 | json.data = luaL_checklstring(l, 1, &json_len); | ||
1292 | json.current_depth = 0; | 1291 | json.current_depth = 0; |
1293 | json.data = json_text; | ||
1294 | json.ptr = json.data; | 1292 | json.ptr = json.data; |
1295 | 1293 | ||
1294 | /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) | ||
1295 | * | ||
1296 | * CJSON can support any simple data type, hence only the first | ||
1297 | * character is guaranteed to be ASCII (at worst: '"'). This is | ||
1298 | * still enough to detect whether the wrong encoding is in use. */ | ||
1299 | if (json_len >= 2 && (!json.data[0] || !json.data[1])) | ||
1300 | luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); | ||
1301 | |||
1296 | /* Ensure the temporary buffer can hold the entire string. | 1302 | /* Ensure the temporary buffer can hold the entire string. |
1297 | * This means we no longer need to do length checks since the decoded | 1303 | * This means we no longer need to do length checks since the decoded |
1298 | * string must be smaller than the entire json string */ | 1304 | * string must be smaller than the entire json string */ |
@@ -1308,26 +1314,6 @@ static void lua_json_decode(lua_State *l, const char *json_text, int json_len) | |||
1308 | json_throw_parse_error(l, &json, "the end", &token); | 1314 | json_throw_parse_error(l, &json, "the end", &token); |
1309 | 1315 | ||
1310 | strbuf_free(json.tmp); | 1316 | strbuf_free(json.tmp); |
1311 | } | ||
1312 | |||
1313 | static int json_decode(lua_State *l) | ||
1314 | { | ||
1315 | const char *json; | ||
1316 | size_t len; | ||
1317 | |||
1318 | json_verify_arg_count(l, 1); | ||
1319 | |||
1320 | json = luaL_checklstring(l, 1, &len); | ||
1321 | |||
1322 | /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) | ||
1323 | * | ||
1324 | * CJSON can support any simple data type, hence only the first | ||
1325 | * character is guaranteed to be ASCII (at worst: '"'). This is | ||
1326 | * still enough to detect whether the wrong encoding is in use. */ | ||
1327 | if (len >= 2 && (!json[0] || !json[1])) | ||
1328 | luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); | ||
1329 | |||
1330 | lua_json_decode(l, json, len); | ||
1331 | 1317 | ||
1332 | return 1; | 1318 | return 1; |
1333 | } | 1319 | } |