| 1251 | } |
| 1252 | |
| 1253 | static int json_decode(lua_State *l) |
| 1254 | { |
| 1255 | json_parse_t json; |
| 1256 | json_token_t token; |
| 1257 | size_t json_len; |
| 1258 | |
| 1259 | luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); |
| 1260 | |
| 1261 | json.cfg = json_fetch_config(l); |
| 1262 | json.data = luaL_checklstring(l, 1, &json_len); |
| 1263 | json.current_depth = 0; |
| 1264 | json.ptr = json.data; |
| 1265 | |
| 1266 | /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) |
| 1267 | * |
| 1268 | * CJSON can support any simple data type, hence only the first |
| 1269 | * character is guaranteed to be ASCII (at worst: '"'). This is |
| 1270 | * still enough to detect whether the wrong encoding is in use. */ |
| 1271 | if (json_len >= 2 && (!json.data[0] || !json.data[1])) |
| 1272 | luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); |
| 1273 | |
| 1274 | /* Ensure the temporary buffer can hold the entire string. |
| 1275 | * This means we no longer need to do length checks since the decoded |
| 1276 | * string must be smaller than the entire json string */ |
| 1277 | json.tmp = strbuf_new(json_len); |
| 1278 | |
| 1279 | json_next_token(&json, &token); |
| 1280 | json_process_value(l, &json, &token); |
| 1281 | |
| 1282 | /* Ensure there is no more input left */ |
| 1283 | json_next_token(&json, &token); |
| 1284 | |
| 1285 | if (token.type != T_END) |
| 1286 | json_throw_parse_error(l, &json, "the end", &token); |
| 1287 | |
| 1288 | strbuf_free(json.tmp); |
| 1289 | |
| 1290 | return 1; |
| 1291 | } |
| 1292 | |
| 1293 | /* ===== INITIALISATION ===== */ |
| 1294 |
nothing calls this directly
no test coverage detected