| 1287 | } |
| 1288 | |
| 1289 | static int json_decode(lua_State *l) |
| 1290 | { |
| 1291 | json_parse_t json; |
| 1292 | json_token_t token; |
| 1293 | size_t json_len; |
| 1294 | |
| 1295 | luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); |
| 1296 | |
| 1297 | json.cfg = json_fetch_config(l); |
| 1298 | json.data = luaL_checklstring(l, 1, &json_len); |
| 1299 | json.current_depth = 0; |
| 1300 | json.ptr = json.data; |
| 1301 | |
| 1302 | /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) |
| 1303 | * |
| 1304 | * CJSON can support any simple data type, hence only the first |
| 1305 | * character is guaranteed to be ASCII (at worst: '"'). This is |
| 1306 | * still enough to detect whether the wrong encoding is in use. */ |
| 1307 | if (json_len >= 2 && (!json.data[0] || !json.data[1])) |
| 1308 | luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); |
| 1309 | |
| 1310 | /* Ensure the temporary buffer can hold the entire string. |
| 1311 | * This means we no longer need to do length checks since the decoded |
| 1312 | * string must be smaller than the entire json string */ |
| 1313 | json.tmp = strbuf_new(json_len); |
| 1314 | |
| 1315 | json_next_token(&json, &token); |
| 1316 | json_process_value(l, &json, &token); |
| 1317 | |
| 1318 | /* Ensure there is no more input left */ |
| 1319 | json_next_token(&json, &token); |
| 1320 | |
| 1321 | if (token.type != T_END) |
| 1322 | json_throw_parse_error(l, &json, "the end", &token); |
| 1323 | |
| 1324 | strbuf_free(json.tmp); |
| 1325 | |
| 1326 | return 1; |
| 1327 | } |
| 1328 | |
| 1329 | /* ===== INITIALISATION ===== */ |
| 1330 |
nothing calls this directly
no test coverage detected