cjson.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. /* Lua CJSON - JSON support for Lua
  2. *
  3. * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /* Caveats:
  25. * - JSON "null" values are represented as lightuserdata since Lua
  26. * tables cannot contain "nil". Compare with cjson.null.
  27. * - Invalid UTF-8 characters are not detected and will be passed
  28. * untouched. If required, UTF-8 error checking should be done
  29. * outside this library.
  30. * - Javascript comments are not part of the JSON spec, and are not
  31. * currently supported.
  32. *
  33. * Note: Decoding is slower than encoding. Lua spends significant
  34. * time (30%) managing tables when parsing JSON since it is
  35. * difficult to know object/array sizes ahead of time.
  36. */
  37. // #include <assert.h>
  38. #include "module.h"
  39. #include "c_string.h"
  40. #include "c_math.h"
  41. #include "c_limits.h"
  42. #include "lauxlib.h"
  43. #include "flash_api.h"
  44. #include "ctype.h"
  45. #include "strbuf.h"
  46. #include "cjson_mem.h"
  47. #define FPCONV_G_FMT_BUFSIZE 32
  48. #define fpconv_strtod c_strtod
  49. #define fpconv_init() ((void)0)
  50. #ifndef CJSON_MODNAME
  51. #define CJSON_MODNAME "cjson"
  52. #endif
  53. #ifndef CJSON_VERSION
  54. #define CJSON_VERSION "2.1devel"
  55. #endif
  56. /* Workaround for Solaris platforms missing isinf() */
  57. #if !defined(isinf) && (defined(USE_INTERNAL_ISINF) || defined(MISSING_ISINF))
  58. #define isinf(x) (!isnan(x) && isnan((x) - (x)))
  59. #endif
  60. #define DEFAULT_SPARSE_CONVERT 0
  61. #define DEFAULT_SPARSE_RATIO 2
  62. #define DEFAULT_SPARSE_SAFE 10
  63. #define DEFAULT_ENCODE_MAX_DEPTH 1000
  64. #define DEFAULT_DECODE_MAX_DEPTH 1000
  65. #define DEFAULT_ENCODE_INVALID_NUMBERS 0
  66. #define DEFAULT_DECODE_INVALID_NUMBERS 1
  67. #define DEFAULT_ENCODE_KEEP_BUFFER 0
  68. #define DEFAULT_ENCODE_NUMBER_PRECISION 14
  69. #ifdef DISABLE_INVALID_NUMBERS
  70. #undef DEFAULT_DECODE_INVALID_NUMBERS
  71. #define DEFAULT_DECODE_INVALID_NUMBERS 0
  72. #endif
  73. typedef enum {
  74. T_OBJ_BEGIN,
  75. T_OBJ_END,
  76. T_ARR_BEGIN,
  77. T_ARR_END,
  78. T_STRING,
  79. T_NUMBER,
  80. T_BOOLEAN,
  81. T_NULL,
  82. T_COLON,
  83. T_COMMA,
  84. T_END,
  85. T_WHITESPACE,
  86. T_ERROR,
  87. T_UNKNOWN
  88. } json_token_type_t;
  89. #if 0
  90. static const char *json_token_type_name[] = {
  91. "T_OBJ_BEGIN",
  92. "T_OBJ_END",
  93. "T_ARR_BEGIN",
  94. "T_ARR_END",
  95. "T_STRING",
  96. "T_NUMBER",
  97. "T_BOOLEAN",
  98. "T_NULL",
  99. "T_COLON",
  100. "T_COMMA",
  101. "T_END",
  102. "T_WHITESPACE",
  103. "T_ERROR",
  104. "T_UNKNOWN",
  105. NULL
  106. };
  107. #endif
  108. static const char json_token_type_name[14][16] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = {
  109. {'T','_','O','B','J','_','B','E','G','I','N',0},
  110. {'T','_','O','B','J','_','E','N','D',0},
  111. {'T','_','A','R','R','_','B','E','G','I','N',0},
  112. {'T','_','A','R','R','_','E','N','D',0},
  113. {'T','_','S','T','R','I','N','G',0},
  114. {'T','_','N','U','M','B','E','R',0},
  115. {'T','_','B','O','O','L','E','A','N',0},
  116. {'T','_','N','U','L','L',0},
  117. {'T','_','C','O','L','O','N',0},
  118. {'T','_','C','O','M','M','A',0},
  119. {'T','_','E','N','D',0},
  120. {'T','_','W','H','I','T','E','S','P','A','C','E',0},
  121. {'T','_','E','R','R','O','R',0},
  122. {'T','_','U','N','K','N','O','W','N',0}
  123. };
  124. typedef struct {
  125. // json_token_type_t ch2token[256]; // 256*4 = 1024 byte
  126. // char escape2char[256]; /* Decoding */
  127. /* encode_buf is only allocated and used when
  128. * encode_keep_buffer is set */
  129. strbuf_t encode_buf;
  130. int encode_sparse_convert;
  131. int encode_sparse_ratio;
  132. int encode_sparse_safe;
  133. int encode_max_depth;
  134. int encode_invalid_numbers; /* 2 => Encode as "null" */
  135. int encode_number_precision;
  136. int encode_keep_buffer;
  137. int decode_invalid_numbers;
  138. int decode_max_depth;
  139. } json_config_t;
  140. typedef struct {
  141. const char *data;
  142. const char *ptr;
  143. strbuf_t *tmp; /* Temporary storage for strings */
  144. json_config_t *cfg;
  145. int current_depth;
  146. } json_parse_t;
  147. typedef struct {
  148. json_token_type_t type;
  149. int index;
  150. union {
  151. const char *string;
  152. double number;
  153. int boolean;
  154. } value;
  155. int string_len;
  156. } json_token_t;
  157. #if 0
  158. static const char *char2escape[256] = {
  159. "\\u0000", "\\u0001", "\\u0002", "\\u0003",
  160. "\\u0004", "\\u0005", "\\u0006", "\\u0007",
  161. "\\b", "\\t", "\\n", "\\u000b",
  162. "\\f", "\\r", "\\u000e", "\\u000f",
  163. "\\u0010", "\\u0011", "\\u0012", "\\u0013",
  164. "\\u0014", "\\u0015", "\\u0016", "\\u0017",
  165. "\\u0018", "\\u0019", "\\u001a", "\\u001b",
  166. "\\u001c", "\\u001d", "\\u001e", "\\u001f",
  167. NULL, NULL, "\\\"", NULL, NULL, NULL, NULL, NULL,
  168. NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\/",
  169. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  170. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  171. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  172. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  173. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  174. NULL, NULL, NULL, NULL, "\\\\", NULL, NULL, NULL,
  175. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  176. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  177. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  178. NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\u007f",
  179. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  180. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  181. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  182. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  183. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  184. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  185. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  186. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  187. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  188. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  189. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  190. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  191. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  192. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  193. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  194. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  195. };
  196. #endif
  197. /* ===== HELPER FUNCTION ===== */
  198. static const char escape_array[36][8] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = {
  199. {'\\','u','0','0','0','0','\0','\0'},
  200. {'\\','u','0','0','0','1','\0','\0'},
  201. {'\\','u','0','0','0','2','\0','\0'},
  202. {'\\','u','0','0','0','3','\0','\0'},
  203. {'\\','u','0','0','0','4','\0','\0'},
  204. {'\\','u','0','0','0','5','\0','\0'},
  205. {'\\','u','0','0','0','6','\0','\0'},
  206. {'\\','u','0','0','0','7','\0','\0'},
  207. {'\\','b','\0','\0','\0','\0','\0','\0'},
  208. {'\\','t','\0','\0','\0','\0','\0','\0'},
  209. {'\\','n','\0','\0','\0','\0','\0','\0'},
  210. {'\\','u','0','0','0','b','\0','\0'},
  211. {'\\','f','\0','\0','\0','\0','\0','\0'},
  212. {'\\','r','\0','\0','\0','\0','\0','\0'},
  213. {'\\','u','0','0','0','e','\0','\0'},
  214. {'\\','u','0','0','0','f','\0','\0'},
  215. {'\\','u','0','0','1','0','\0','\0'},
  216. {'\\','u','0','0','1','1','\0','\0'},
  217. {'\\','u','0','0','1','2','\0','\0'},
  218. {'\\','u','0','0','1','3','\0','\0'},
  219. {'\\','u','0','0','1','4','\0','\0'},
  220. {'\\','u','0','0','1','5','\0','\0'},
  221. {'\\','u','0','0','1','6','\0','\0'},
  222. {'\\','u','0','0','1','7','\0','\0'},
  223. {'\\','u','0','0','1','8','\0','\0'},
  224. {'\\','u','0','0','1','9','\0','\0'},
  225. {'\\','u','0','0','1','a','\0','\0'},
  226. {'\\','u','0','0','1','b','\0','\0'},
  227. {'\\','u','0','0','1','c','\0','\0'},
  228. {'\\','u','0','0','1','d','\0','\0'},
  229. {'\\','u','0','0','1','e','\0','\0'},
  230. {'\\','u','0','0','1','f','\0','\0'},
  231. {'\\','\"','\0','\0','\0','\0','\0','\0'},
  232. {'\\','/','\0','\0','\0','\0','\0','\0'},
  233. {'\\','\\','\0','\0','\0','\0','\0','\0'},
  234. {'\\','u','0','0','7','f','\0','\0'}
  235. };
  236. static const char *char2escape(unsigned char c){
  237. if(c<32) return escape_array[c];
  238. switch(c){
  239. case 34: return escape_array[32];
  240. case 47: return escape_array[33];
  241. case 92: return escape_array[34];
  242. case 127: return escape_array[35];
  243. default:
  244. return NULL;
  245. }
  246. }
  247. static json_token_type_t ch2token(unsigned char c){
  248. switch(c){
  249. case '{': return T_OBJ_BEGIN;
  250. case '}': return T_OBJ_END;
  251. case '[': return T_ARR_BEGIN;
  252. case ']': return T_ARR_END;
  253. case ',': return T_COMMA;
  254. case ':': return T_COLON;
  255. case '\0': return T_END;
  256. case ' ': return T_WHITESPACE;
  257. case '\t': return T_WHITESPACE;
  258. case '\n': return T_WHITESPACE;
  259. case '\r': return T_WHITESPACE;
  260. /* Update characters that require further processing */
  261. case 'f': case 'i': case 'I': case 'n': case 'N': case 't': case '"': case '+': case '-':
  262. case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  263. return T_UNKNOWN;
  264. default:
  265. return T_ERROR;
  266. }
  267. }
  268. static char escape2char(unsigned char c){
  269. switch(c){
  270. case '"': return '"';
  271. case '\\': return '\\';
  272. case '/': return '/';
  273. case 'b': return '\b';
  274. case 't': return '\t';
  275. case 'n': return '\n';
  276. case 'f': return '\f';
  277. case 'r': return '\r';
  278. case 'u': return 'u';
  279. default:
  280. return 0;
  281. }
  282. }
  283. /* ===== CONFIGURATION ===== */
  284. #if 0
  285. static json_config_t *json_fetch_config(lua_State *l)
  286. {
  287. json_config_t *cfg;
  288. cfg = lua_touserdata(l, lua_upvalueindex(1));
  289. if (!cfg)
  290. luaL_error(l, "BUG: Unable to fetch CJSON configuration");
  291. return cfg;
  292. }
  293. /* Ensure the correct number of arguments have been provided.
  294. * Pad with nil to allow other functions to simply check arg[i]
  295. * to find whether an argument was provided */
  296. static json_config_t *json_arg_init(lua_State *l, int args)
  297. {
  298. luaL_argcheck(l, lua_gettop(l) <= args, args + 1,
  299. "found too many arguments");
  300. while (lua_gettop(l) < args)
  301. lua_pushnil(l);
  302. return json_fetch_config(l);
  303. }
  304. /* Process integer options for configuration functions */
  305. static int json_integer_option(lua_State *l, int optindex, int *setting,
  306. int min, int max)
  307. {
  308. char errmsg[64];
  309. int value;
  310. if (!lua_isnil(l, optindex)) {
  311. value = luaL_checkinteger(l, optindex);
  312. c_sprintf(errmsg, "expected integer between %d and %d", min, max);
  313. luaL_argcheck(l, min <= value && value <= max, 1, errmsg);
  314. *setting = value;
  315. }
  316. lua_pushinteger(l, *setting);
  317. return 1;
  318. }
  319. /* Process enumerated arguments for a configuration function */
  320. static int json_enum_option(lua_State *l, int optindex, int *setting,
  321. const char **options, int bool_true)
  322. {
  323. static const char *bool_options[] = { "off", "on", NULL };
  324. if (!options) {
  325. options = bool_options;
  326. bool_true = 1;
  327. }
  328. if (!lua_isnil(l, optindex)) {
  329. if (bool_true && lua_isboolean(l, optindex))
  330. *setting = lua_toboolean(l, optindex) * bool_true;
  331. else
  332. *setting = luaL_checkoption(l, optindex, NULL, options);
  333. }
  334. if (bool_true && (*setting == 0 || *setting == bool_true))
  335. lua_pushboolean(l, *setting);
  336. else
  337. lua_pushstring(l, options[*setting]);
  338. return 1;
  339. }
  340. /* Configures handling of extremely sparse arrays:
  341. * convert: Convert extremely sparse arrays into objects? Otherwise error.
  342. * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio
  343. * safe: Always use an array when the max index <= safe */
  344. static int json_cfg_encode_sparse_array(lua_State *l)
  345. {
  346. json_config_t *cfg = json_arg_init(l, 3);
  347. json_enum_option(l, 1, &cfg->encode_sparse_convert, NULL, 1);
  348. json_integer_option(l, 2, &cfg->encode_sparse_ratio, 0, INT_MAX);
  349. json_integer_option(l, 3, &cfg->encode_sparse_safe, 0, INT_MAX);
  350. return 3;
  351. }
  352. /* Configures the maximum number of nested arrays/objects allowed when
  353. * encoding */
  354. static int json_cfg_encode_max_depth(lua_State *l)
  355. {
  356. json_config_t *cfg = json_arg_init(l, 1);
  357. return json_integer_option(l, 1, &cfg->encode_max_depth, 1, INT_MAX);
  358. }
  359. /* Configures the maximum number of nested arrays/objects allowed when
  360. * encoding */
  361. static int json_cfg_decode_max_depth(lua_State *l)
  362. {
  363. json_config_t *cfg = json_arg_init(l, 1);
  364. return json_integer_option(l, 1, &cfg->decode_max_depth, 1, INT_MAX);
  365. }
  366. /* Configures number precision when converting doubles to text */
  367. static int json_cfg_encode_number_precision(lua_State *l)
  368. {
  369. json_config_t *cfg = json_arg_init(l, 1);
  370. return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14);
  371. }
  372. /* Configures JSON encoding buffer persistence */
  373. static int json_cfg_encode_keep_buffer(lua_State *l)
  374. {
  375. json_config_t *cfg = json_arg_init(l, 1);
  376. int old_value;
  377. old_value = cfg->encode_keep_buffer;
  378. json_enum_option(l, 1, &cfg->encode_keep_buffer, NULL, 1);
  379. /* Init / free the buffer if the setting has changed */
  380. if (old_value ^ cfg->encode_keep_buffer) {
  381. if (cfg->encode_keep_buffer){
  382. if(-1==strbuf_init(&cfg->encode_buf, 0))
  383. return luaL_error(l, "not enough memory");
  384. }
  385. else
  386. strbuf_free(&cfg->encode_buf);
  387. }
  388. return 1;
  389. }
  390. #if defined(DISABLE_INVALID_NUMBERS) && !defined(USE_INTERNAL_FPCONV)
  391. void json_verify_invalid_number_setting(lua_State *l, int *setting)
  392. {
  393. if (*setting == 1) {
  394. *setting = 0;
  395. luaL_error(l, "Infinity, NaN, and/or hexadecimal numbers are not supported.");
  396. }
  397. }
  398. #else
  399. #define json_verify_invalid_number_setting(l, s) do { } while(0)
  400. #endif
  401. static int json_cfg_encode_invalid_numbers(lua_State *l)
  402. {
  403. static const char *options[] = { "off", "on", "null", NULL };
  404. json_config_t *cfg = json_arg_init(l, 1);
  405. json_enum_option(l, 1, &cfg->encode_invalid_numbers, options, 1);
  406. json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers);
  407. return 1;
  408. }
  409. static int json_cfg_decode_invalid_numbers(lua_State *l)
  410. {
  411. json_config_t *cfg = json_arg_init(l, 1);
  412. json_enum_option(l, 1, &cfg->decode_invalid_numbers, NULL, 1);
  413. json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers);
  414. return 1;
  415. }
  416. static int json_destroy_config(lua_State *l)
  417. {
  418. json_config_t *cfg;
  419. cfg = lua_touserdata(l, 1);
  420. if (cfg)
  421. strbuf_free(&cfg->encode_buf);
  422. cfg = NULL;
  423. return 0;
  424. }
  425. static void json_create_config(lua_State *l)
  426. {
  427. json_config_t *cfg;
  428. int i;
  429. cfg = lua_newuserdata(l, sizeof(*cfg));
  430. /* Create GC method to clean up strbuf */
  431. lua_newtable(l);
  432. lua_pushcfunction(l, json_destroy_config);
  433. lua_setfield(l, -2, "__gc");
  434. lua_setmetatable(l, -2);
  435. cfg->encode_sparse_convert = DEFAULT_SPARSE_CONVERT;
  436. cfg->encode_sparse_ratio = DEFAULT_SPARSE_RATIO;
  437. cfg->encode_sparse_safe = DEFAULT_SPARSE_SAFE;
  438. cfg->encode_max_depth = DEFAULT_ENCODE_MAX_DEPTH;
  439. cfg->decode_max_depth = DEFAULT_DECODE_MAX_DEPTH;
  440. cfg->encode_invalid_numbers = DEFAULT_ENCODE_INVALID_NUMBERS;
  441. cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS;
  442. cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER;
  443. cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
  444. #if DEFAULT_ENCODE_KEEP_BUFFER > 0
  445. strbuf_init(&cfg->encode_buf, 0);
  446. #endif
  447. /* Decoding init */
  448. /* Tag all characters as an error */
  449. for (i = 0; i < 256; i++)
  450. cfg->ch2token[i] = T_ERROR;
  451. /* Set tokens that require no further processing */
  452. cfg->ch2token['{'] = T_OBJ_BEGIN;
  453. cfg->ch2token['}'] = T_OBJ_END;
  454. cfg->ch2token['['] = T_ARR_BEGIN;
  455. cfg->ch2token[']'] = T_ARR_END;
  456. cfg->ch2token[','] = T_COMMA;
  457. cfg->ch2token[':'] = T_COLON;
  458. cfg->ch2token['\0'] = T_END;
  459. cfg->ch2token[' '] = T_WHITESPACE;
  460. cfg->ch2token['\t'] = T_WHITESPACE;
  461. cfg->ch2token['\n'] = T_WHITESPACE;
  462. cfg->ch2token['\r'] = T_WHITESPACE;
  463. /* Update characters that require further processing */
  464. cfg->ch2token['f'] = T_UNKNOWN; /* false? */
  465. cfg->ch2token['i'] = T_UNKNOWN; /* inf, ininity? */
  466. cfg->ch2token['I'] = T_UNKNOWN;
  467. cfg->ch2token['n'] = T_UNKNOWN; /* null, nan? */
  468. cfg->ch2token['N'] = T_UNKNOWN;
  469. cfg->ch2token['t'] = T_UNKNOWN; /* true? */
  470. cfg->ch2token['"'] = T_UNKNOWN; /* string? */
  471. cfg->ch2token['+'] = T_UNKNOWN; /* number? */
  472. cfg->ch2token['-'] = T_UNKNOWN;
  473. for (i = 0; i < 10; i++)
  474. cfg->ch2token['0' + i] = T_UNKNOWN;
  475. /* Lookup table for parsing escape characters */
  476. for (i = 0; i < 256; i++)
  477. cfg->escape2char[i] = 0; /* String error */
  478. cfg->escape2char['"'] = '"';
  479. cfg->escape2char['\\'] = '\\';
  480. cfg->escape2char['/'] = '/';
  481. cfg->escape2char['b'] = '\b';
  482. cfg->escape2char['t'] = '\t';
  483. cfg->escape2char['n'] = '\n';
  484. cfg->escape2char['f'] = '\f';
  485. cfg->escape2char['r'] = '\r';
  486. cfg->escape2char['u'] = 'u'; /* Unicode parsing required */
  487. }
  488. #endif
  489. json_config_t _cfg;
  490. static json_config_t *json_fetch_config(lua_State *l)
  491. {
  492. return &_cfg;
  493. }
  494. static int cfg_init(json_config_t *cfg){
  495. cfg->encode_sparse_convert = DEFAULT_SPARSE_CONVERT;
  496. cfg->encode_sparse_ratio = DEFAULT_SPARSE_RATIO;
  497. cfg->encode_sparse_safe = DEFAULT_SPARSE_SAFE;
  498. cfg->encode_max_depth = DEFAULT_ENCODE_MAX_DEPTH;
  499. cfg->decode_max_depth = DEFAULT_DECODE_MAX_DEPTH;
  500. cfg->encode_invalid_numbers = DEFAULT_ENCODE_INVALID_NUMBERS;
  501. cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS;
  502. cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER;
  503. cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
  504. #if DEFAULT_ENCODE_KEEP_BUFFER > 0
  505. if(-1==strbuf_init(&cfg->encode_buf, 0)){
  506. NODE_ERR("not enough memory\n");
  507. return -1;
  508. }
  509. #endif
  510. return 0;
  511. }
  512. /* ===== ENCODING ===== */
  513. static void json_encode_exception(lua_State *l, json_config_t *cfg, strbuf_t *json, int lindex,
  514. const char *reason)
  515. {
  516. if (!cfg->encode_keep_buffer)
  517. strbuf_free(json);
  518. luaL_error(l, "Cannot serialise %s: %s",
  519. lua_typename(l, lua_type(l, lindex)), reason);
  520. }
  521. /* json_append_string args:
  522. * - lua_State
  523. * - JSON strbuf
  524. * - String (Lua stack index)
  525. *
  526. * Returns nothing. Doesn't remove string from Lua stack */
  527. static void json_append_string(lua_State *l, strbuf_t *json, int lindex)
  528. {
  529. const char *escstr;
  530. int i;
  531. const char *str;
  532. size_t len;
  533. str = lua_tolstring(l, lindex, &len);
  534. /* Worst case is len * 6 (all unicode escapes).
  535. * This buffer is reused constantly for small strings
  536. * If there are any excess pages, they won't be hit anyway.
  537. * This gains ~5% speedup. */
  538. strbuf_ensure_empty_length(json, len * 6 + 2);
  539. strbuf_append_char_unsafe(json, '\"');
  540. for (i = 0; i < len; i++) {
  541. escstr = char2escape((unsigned char)str[i]);
  542. if (escstr){
  543. int i;
  544. char temp[8]; // for now, 8-bytes is enough.
  545. for (i=0; i < 8; ++i)
  546. {
  547. temp[i] = byte_of_aligned_array(escstr, i);
  548. if(temp[i]==0) break;
  549. }
  550. escstr = temp;
  551. strbuf_append_string(json, escstr);
  552. }
  553. else
  554. strbuf_append_char_unsafe(json, str[i]);
  555. }
  556. strbuf_append_char_unsafe(json, '\"');
  557. }
  558. /* Find the size of the array on the top of the Lua stack
  559. * -1 object (not a pure array)
  560. * >=0 elements in array
  561. */
  562. static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)
  563. {
  564. double k;
  565. int max;
  566. int items;
  567. max = 0;
  568. items = 0;
  569. lua_pushnil(l);
  570. /* table, startkey */
  571. while (lua_next(l, -2) != 0) {
  572. /* table, key, value */
  573. if (lua_type(l, -2) == LUA_TNUMBER &&
  574. (k = lua_tonumber(l, -2))) {
  575. /* Integer >= 1 ? */
  576. if (floor(k) == k && k >= 1) {
  577. if (k > max)
  578. max = k;
  579. items++;
  580. lua_pop(l, 1);
  581. continue;
  582. }
  583. }
  584. /* Must not be an array (non integer key) */
  585. lua_pop(l, 2);
  586. return -1;
  587. }
  588. /* Encode excessively sparse arrays as objects (if enabled) */
  589. if (cfg->encode_sparse_ratio > 0 &&
  590. max > items * cfg->encode_sparse_ratio &&
  591. max > cfg->encode_sparse_safe) {
  592. if (!cfg->encode_sparse_convert)
  593. json_encode_exception(l, cfg, json, -1, "excessively sparse array");
  594. return -1;
  595. }
  596. return max;
  597. }
  598. static void json_check_encode_depth(lua_State *l, json_config_t *cfg,
  599. int current_depth, strbuf_t *json)
  600. {
  601. /* Ensure there are enough slots free to traverse a table (key,
  602. * value) and push a string for a potential error message.
  603. *
  604. * Unlike "decode", the key and value are still on the stack when
  605. * lua_checkstack() is called. Hence an extra slot for luaL_error()
  606. * below is required just in case the next check to lua_checkstack()
  607. * fails.
  608. *
  609. * While this won't cause a crash due to the EXTRA_STACK reserve
  610. * slots, it would still be an improper use of the API. */
  611. if (current_depth <= cfg->encode_max_depth && lua_checkstack(l, 3))
  612. return;
  613. if (!cfg->encode_keep_buffer)
  614. strbuf_free(json);
  615. luaL_error(l, "Cannot serialise, excessive nesting (%d)",
  616. current_depth);
  617. }
  618. static void json_append_data(lua_State *l, json_config_t *cfg,
  619. int current_depth, strbuf_t *json);
  620. /* json_append_array args:
  621. * - lua_State
  622. * - JSON strbuf
  623. * - Size of passwd Lua array (top of stack) */
  624. static void json_append_array(lua_State *l, json_config_t *cfg, int current_depth,
  625. strbuf_t *json, int array_length)
  626. {
  627. int comma, i;
  628. strbuf_append_char(json, '[');
  629. comma = 0;
  630. for (i = 1; i <= array_length; i++) {
  631. if (comma)
  632. strbuf_append_char(json, ',');
  633. else
  634. comma = 1;
  635. lua_rawgeti(l, -1, i);
  636. json_append_data(l, cfg, current_depth, json);
  637. lua_pop(l, 1);
  638. }
  639. strbuf_append_char(json, ']');
  640. }
  641. static void json_append_number(lua_State *l, json_config_t *cfg,
  642. strbuf_t *json, int lindex)
  643. {
  644. double num = lua_tonumber(l, lindex);
  645. int len;
  646. if (cfg->encode_invalid_numbers == 0) {
  647. /* Prevent encoding invalid numbers */
  648. if (isinf(num) || isnan(num))
  649. json_encode_exception(l, cfg, json, lindex,
  650. "must not be NaN or Infinity");
  651. } else if (cfg->encode_invalid_numbers == 1) {
  652. /* Encode NaN/Infinity separately to ensure Javascript compatible
  653. * values are used. */
  654. if (isnan(num)) {
  655. strbuf_append_mem(json, "NaN", 3);
  656. return;
  657. }
  658. if (isinf(num)) {
  659. if (num < 0)
  660. strbuf_append_mem(json, "-Infinity", 9);
  661. else
  662. strbuf_append_mem(json, "Infinity", 8);
  663. return;
  664. }
  665. } else {
  666. /* Encode invalid numbers as "null" */
  667. if (isinf(num) || isnan(num)) {
  668. strbuf_append_mem(json, "null", 4);
  669. return;
  670. }
  671. }
  672. strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE);
  673. // len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision);
  674. c_sprintf(strbuf_empty_ptr(json), LUA_NUMBER_FMT, (LUA_NUMBER)num);
  675. len = c_strlen(strbuf_empty_ptr(json));
  676. strbuf_extend_length(json, len);
  677. }
  678. static void json_append_object(lua_State *l, json_config_t *cfg,
  679. int current_depth, strbuf_t *json)
  680. {
  681. int comma, keytype;
  682. /* Object */
  683. strbuf_append_char(json, '{');
  684. lua_pushnil(l);
  685. /* table, startkey */
  686. comma = 0;
  687. while (lua_next(l, -2) != 0) {
  688. if (comma)
  689. strbuf_append_char(json, ',');
  690. else
  691. comma = 1;
  692. /* table, key, value */
  693. keytype = lua_type(l, -2);
  694. if (keytype == LUA_TNUMBER) {
  695. strbuf_append_char(json, '"');
  696. json_append_number(l, cfg, json, -2);
  697. strbuf_append_mem(json, "\":", 2);
  698. } else if (keytype == LUA_TSTRING) {
  699. json_append_string(l, json, -2);
  700. strbuf_append_char(json, ':');
  701. } else {
  702. json_encode_exception(l, cfg, json, -2,
  703. "table key must be a number or string");
  704. /* never returns */
  705. }
  706. /* table, key, value */
  707. json_append_data(l, cfg, current_depth, json);
  708. lua_pop(l, 1);
  709. /* table, key */
  710. }
  711. strbuf_append_char(json, '}');
  712. }
  713. /* Serialise Lua data into JSON string. */
  714. static void json_append_data(lua_State *l, json_config_t *cfg,
  715. int current_depth, strbuf_t *json)
  716. {
  717. int len;
  718. switch (lua_type(l, -1)) {
  719. case LUA_TSTRING:
  720. json_append_string(l, json, -1);
  721. break;
  722. case LUA_TNUMBER:
  723. json_append_number(l, cfg, json, -1);
  724. break;
  725. case LUA_TBOOLEAN:
  726. if (lua_toboolean(l, -1))
  727. strbuf_append_mem(json, "true", 4);
  728. else
  729. strbuf_append_mem(json, "false", 5);
  730. break;
  731. case LUA_TTABLE:
  732. current_depth++;
  733. json_check_encode_depth(l, cfg, current_depth, json);
  734. len = lua_array_length(l, cfg, json);
  735. if (len > 0)
  736. json_append_array(l, cfg, current_depth, json, len);
  737. else
  738. json_append_object(l, cfg, current_depth, json);
  739. break;
  740. case LUA_TNIL:
  741. strbuf_append_mem(json, "null", 4);
  742. break;
  743. case LUA_TLIGHTUSERDATA:
  744. if (lua_touserdata(l, -1) == NULL) {
  745. strbuf_append_mem(json, "null", 4);
  746. break;
  747. }
  748. default:
  749. /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD,
  750. * and LUA_TLIGHTUSERDATA) cannot be serialised */
  751. json_encode_exception(l, cfg, json, -1, "type not supported");
  752. /* never returns */
  753. }
  754. }
  755. static int json_encode(lua_State *l)
  756. {
  757. json_config_t *cfg = json_fetch_config(l);
  758. strbuf_t local_encode_buf;
  759. strbuf_t *encode_buf;
  760. char *json;
  761. int len;
  762. luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
  763. if (!cfg->encode_keep_buffer) {
  764. /* Use private buffer */
  765. encode_buf = &local_encode_buf;
  766. if(-1==strbuf_init(encode_buf, 0))
  767. return luaL_error(l, "not enough memory");
  768. } else {
  769. /* Reuse existing buffer */
  770. encode_buf = &cfg->encode_buf;
  771. strbuf_reset(encode_buf);
  772. }
  773. json_append_data(l, cfg, 0, encode_buf);
  774. json = strbuf_string(encode_buf, &len);
  775. lua_pushlstring(l, json, len);
  776. if (!cfg->encode_keep_buffer)
  777. strbuf_free(encode_buf);
  778. return 1;
  779. }
  780. /* ===== DECODING ===== */
  781. static void json_process_value(lua_State *l, json_parse_t *json,
  782. json_token_t *token);
  783. static int hexdigit2int(char hex)
  784. {
  785. if ('0' <= hex && hex <= '9')
  786. return hex - '0';
  787. /* Force lowercase */
  788. hex |= 0x20;
  789. if ('a' <= hex && hex <= 'f')
  790. return 10 + hex - 'a';
  791. return -1;
  792. }
  793. static int decode_hex4(const char *hex)
  794. {
  795. int digit[4];
  796. int i;
  797. /* Convert ASCII hex digit to numeric digit
  798. * Note: this returns an error for invalid hex digits, including
  799. * NULL */
  800. for (i = 0; i < 4; i++) {
  801. digit[i] = hexdigit2int(hex[i]);
  802. if (digit[i] < 0) {
  803. return -1;
  804. }
  805. }
  806. return (digit[0] << 12) +
  807. (digit[1] << 8) +
  808. (digit[2] << 4) +
  809. digit[3];
  810. }
  811. /* Converts a Unicode codepoint to UTF-8.
  812. * Returns UTF-8 string length, and up to 4 bytes in *utf8 */
  813. static int codepoint_to_utf8(char *utf8, int codepoint)
  814. {
  815. /* 0xxxxxxx */
  816. if (codepoint <= 0x7F) {
  817. utf8[0] = codepoint;
  818. return 1;
  819. }
  820. /* 110xxxxx 10xxxxxx */
  821. if (codepoint <= 0x7FF) {
  822. utf8[0] = (codepoint >> 6) | 0xC0;
  823. utf8[1] = (codepoint & 0x3F) | 0x80;
  824. return 2;
  825. }
  826. /* 1110xxxx 10xxxxxx 10xxxxxx */
  827. if (codepoint <= 0xFFFF) {
  828. utf8[0] = (codepoint >> 12) | 0xE0;
  829. utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80;
  830. utf8[2] = (codepoint & 0x3F) | 0x80;
  831. return 3;
  832. }
  833. /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
  834. if (codepoint <= 0x1FFFFF) {
  835. utf8[0] = (codepoint >> 18) | 0xF0;
  836. utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80;
  837. utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80;
  838. utf8[3] = (codepoint & 0x3F) | 0x80;
  839. return 4;
  840. }
  841. return 0;
  842. }
  843. /* Called when index pointing to beginning of UTF-16 code escape: \uXXXX
  844. * \u is guaranteed to exist, but the remaining hex characters may be
  845. * missing.
  846. * Translate to UTF-8 and append to temporary token string.
  847. * Must advance index to the next character to be processed.
  848. * Returns: 0 success
  849. * -1 error
  850. */
  851. static int json_append_unicode_escape(json_parse_t *json)
  852. {
  853. char utf8[4]; /* Surrogate pairs require 4 UTF-8 bytes */
  854. int codepoint;
  855. int surrogate_low;
  856. int len;
  857. int escape_len = 6;
  858. /* Fetch UTF-16 code unit */
  859. codepoint = decode_hex4(json->ptr + 2);
  860. if (codepoint < 0)
  861. return -1;
  862. /* UTF-16 surrogate pairs take the following 2 byte form:
  863. * 11011 x yyyyyyyyyy
  864. * When x = 0: y is the high 10 bits of the codepoint
  865. * x = 1: y is the low 10 bits of the codepoint
  866. *
  867. * Check for a surrogate pair (high or low) */
  868. if ((codepoint & 0xF800) == 0xD800) {
  869. /* Error if the 1st surrogate is not high */
  870. if (codepoint & 0x400)
  871. return -1;
  872. /* Ensure the next code is a unicode escape */
  873. if (*(json->ptr + escape_len) != '\\' ||
  874. *(json->ptr + escape_len + 1) != 'u') {
  875. return -1;
  876. }
  877. /* Fetch the next codepoint */
  878. surrogate_low = decode_hex4(json->ptr + 2 + escape_len);
  879. if (surrogate_low < 0)
  880. return -1;
  881. /* Error if the 2nd code is not a low surrogate */
  882. if ((surrogate_low & 0xFC00) != 0xDC00)
  883. return -1;
  884. /* Calculate Unicode codepoint */
  885. codepoint = (codepoint & 0x3FF) << 10;
  886. surrogate_low &= 0x3FF;
  887. codepoint = (codepoint | surrogate_low) + 0x10000;
  888. escape_len = 12;
  889. }
  890. /* Convert codepoint to UTF-8 */
  891. len = codepoint_to_utf8(utf8, codepoint);
  892. if (!len)
  893. return -1;
  894. /* Append bytes and advance parse index */
  895. strbuf_append_mem_unsafe(json->tmp, utf8, len);
  896. json->ptr += escape_len;
  897. return 0;
  898. }
  899. static void json_set_token_error(json_token_t *token, json_parse_t *json,
  900. const char *errtype)
  901. {
  902. token->type = T_ERROR;
  903. token->index = json->ptr - json->data;
  904. token->value.string = errtype;
  905. }
  906. static void json_next_string_token(json_parse_t *json, json_token_t *token)
  907. {
  908. // char *escape2char = json->cfg->escape2char;
  909. char ch;
  910. /* Caller must ensure a string is next */
  911. if(!(*json->ptr == '"')) return;
  912. /* Skip " */
  913. json->ptr++;
  914. /* json->tmp is the temporary strbuf used to accumulate the
  915. * decoded string value.
  916. * json->tmp is sized to handle JSON containing only a string value.
  917. */
  918. strbuf_reset(json->tmp);
  919. while ((ch = *json->ptr) != '"') {
  920. if (!ch) {
  921. /* Premature end of the string */
  922. json_set_token_error(token, json, "unexpected end of string");
  923. return;
  924. }
  925. /* Handle escapes */
  926. if (ch == '\\') {
  927. /* Fetch escape character */
  928. ch = *(json->ptr + 1);
  929. /* Translate escape code and append to tmp string */
  930. ch = escape2char((unsigned char)ch);
  931. if (ch == 'u') {
  932. if (json_append_unicode_escape(json) == 0)
  933. continue;
  934. json_set_token_error(token, json,
  935. "invalid unicode escape code");
  936. return;
  937. }
  938. if (!ch) {
  939. json_set_token_error(token, json, "invalid escape code");
  940. return;
  941. }
  942. /* Skip '\' */
  943. json->ptr++;
  944. }
  945. /* Append normal character or translated single character
  946. * Unicode escapes are handled above */
  947. strbuf_append_char_unsafe(json->tmp, ch);
  948. json->ptr++;
  949. }
  950. json->ptr++; /* Eat final quote (") */
  951. strbuf_ensure_null(json->tmp);
  952. token->type = T_STRING;
  953. token->value.string = strbuf_string(json->tmp, &token->string_len);
  954. }
  955. /* JSON numbers should take the following form:
  956. * -?(0|[1-9]|[1-9][0-9]+)(.[0-9]+)?([eE][-+]?[0-9]+)?
  957. *
  958. * json_next_number_token() uses strtod() which allows other forms:
  959. * - numbers starting with '+'
  960. * - NaN, -NaN, infinity, -infinity
  961. * - hexadecimal numbers
  962. * - numbers with leading zeros
  963. *
  964. * json_is_invalid_number() detects "numbers" which may pass strtod()'s
  965. * error checking, but should not be allowed with strict JSON.
  966. *
  967. * json_is_invalid_number() may pass numbers which cause strtod()
  968. * to generate an error.
  969. */
  970. static int json_is_invalid_number(json_parse_t *json)
  971. {
  972. const char *p = json->ptr;
  973. /* Reject numbers starting with + */
  974. if (*p == '+')
  975. return 1;
  976. /* Skip minus sign if it exists */
  977. if (*p == '-')
  978. p++;
  979. /* Reject numbers starting with 0x, or leading zeros */
  980. if (*p == '0') {
  981. int ch2 = *(p + 1);
  982. if ((ch2 | 0x20) == 'x' || /* Hex */
  983. ('0' <= ch2 && ch2 <= '9')) /* Leading zero */
  984. return 1;
  985. return 0;
  986. } else if (*p <= '9') {
  987. return 0; /* Ordinary number */
  988. }
  989. char tmp[4]; // conv to lower. because c_strncasecmp == c_strcmp
  990. int i;
  991. for (i = 0; i < 3; ++i)
  992. {
  993. if(p[i]!=0)
  994. tmp[i] = tolower(p[i]);
  995. else
  996. tmp[i] = 0;
  997. }
  998. tmp[3] = 0;
  999. /* Reject inf/nan */
  1000. if (!c_strncasecmp(tmp, "inf", 3))
  1001. return 1;
  1002. if (!c_strncasecmp(tmp, "nan", 3))
  1003. return 1;
  1004. /* Pass all other numbers which may still be invalid, but
  1005. * strtod() will catch them. */
  1006. return 0;
  1007. }
  1008. static void json_next_number_token(json_parse_t *json, json_token_t *token)
  1009. {
  1010. char *endptr;
  1011. token->type = T_NUMBER;
  1012. token->value.number = fpconv_strtod(json->ptr, &endptr);
  1013. if (json->ptr == endptr)
  1014. json_set_token_error(token, json, "invalid number");
  1015. else
  1016. json->ptr = endptr; /* Skip the processed number */
  1017. return;
  1018. }
  1019. /* Fills in the token struct.
  1020. * T_STRING will return a pointer to the json_parse_t temporary string
  1021. * T_ERROR will leave the json->ptr pointer at the error.
  1022. */
  1023. static void json_next_token(json_parse_t *json, json_token_t *token)
  1024. {
  1025. // const json_token_type_t *ch2token = json->cfg->ch2token;
  1026. int ch;
  1027. /* Eat whitespace. */
  1028. while (1) {
  1029. ch = (unsigned char)*(json->ptr);
  1030. token->type = ch2token(ch);
  1031. if (token->type != T_WHITESPACE)
  1032. break;
  1033. json->ptr++;
  1034. }
  1035. /* Store location of new token. Required when throwing errors
  1036. * for unexpected tokens (syntax errors). */
  1037. token->index = json->ptr - json->data;
  1038. /* Don't advance the pointer for an error or the end */
  1039. if (token->type == T_ERROR) {
  1040. json_set_token_error(token, json, "invalid token");
  1041. return;
  1042. }
  1043. if (token->type == T_END) {
  1044. return;
  1045. }
  1046. /* Found a known single character token, advance index and return */
  1047. if (token->type != T_UNKNOWN) {
  1048. json->ptr++;
  1049. return;
  1050. }
  1051. /* Process characters which triggered T_UNKNOWN
  1052. *
  1053. * Must use strncmp() to match the front of the JSON string.
  1054. * JSON identifier must be lowercase.
  1055. * When strict_numbers if disabled, either case is allowed for
  1056. * Infinity/NaN (since we are no longer following the spec..) */
  1057. if (ch == '"') {
  1058. json_next_string_token(json, token);
  1059. return;
  1060. } else if (ch == '-' || ('0' <= ch && ch <= '9')) {
  1061. if (!json->cfg->decode_invalid_numbers && json_is_invalid_number(json)) {
  1062. json_set_token_error(token, json, "invalid number");
  1063. return;
  1064. }
  1065. json_next_number_token(json, token);
  1066. return;
  1067. } else if (!c_strncmp(json->ptr, "true", 4)) {
  1068. token->type = T_BOOLEAN;
  1069. token->value.boolean = 1;
  1070. json->ptr += 4;
  1071. return;
  1072. } else if (!c_strncmp(json->ptr, "false", 5)) {
  1073. token->type = T_BOOLEAN;
  1074. token->value.boolean = 0;
  1075. json->ptr += 5;
  1076. return;
  1077. } else if (!c_strncmp(json->ptr, "null", 4)) {
  1078. token->type = T_NULL;
  1079. json->ptr += 4;
  1080. return;
  1081. } else if (json->cfg->decode_invalid_numbers &&
  1082. json_is_invalid_number(json)) {
  1083. /* When decode_invalid_numbers is enabled, only attempt to process
  1084. * numbers we know are invalid JSON (Inf, NaN, hex)
  1085. * This is required to generate an appropriate token error,
  1086. * otherwise all bad tokens will register as "invalid number"
  1087. */
  1088. json_next_number_token(json, token);
  1089. return;
  1090. }
  1091. /* Token starts with t/f/n but isn't recognised above. */
  1092. json_set_token_error(token, json, "invalid token");
  1093. }
  1094. /* This function does not return.
  1095. * DO NOT CALL WITH DYNAMIC MEMORY ALLOCATED.
  1096. * The only supported exception is the temporary parser string
  1097. * json->tmp struct.
  1098. * json and token should exist on the stack somewhere.
  1099. * luaL_error() will long_jmp and release the stack */
  1100. static void json_throw_parse_error(lua_State *l, json_parse_t *json,
  1101. const char *exp, json_token_t *token)
  1102. {
  1103. const char *found;
  1104. char temp[16]; // for now, 16-bytes is enough.
  1105. strbuf_free(json->tmp);
  1106. if (token->type == T_ERROR)
  1107. found = token->value.string;
  1108. else
  1109. {
  1110. found = json_token_type_name[token->type];
  1111. int i;
  1112. for (i=0; i < 16; ++i)
  1113. {
  1114. temp[i] = byte_of_aligned_array(found, i);
  1115. if(temp[i]==0) break;
  1116. }
  1117. found = temp;
  1118. }
  1119. /* Note: token->index is 0 based, display starting from 1 */
  1120. luaL_error(l, "Expected %s but found %s at character %d",
  1121. exp, found, token->index + 1);
  1122. }
  1123. static inline void json_decode_ascend(json_parse_t *json)
  1124. {
  1125. json->current_depth--;
  1126. }
  1127. static void json_decode_descend(lua_State *l, json_parse_t *json, int slots)
  1128. {
  1129. json->current_depth++;
  1130. if (json->current_depth <= json->cfg->decode_max_depth &&
  1131. lua_checkstack(l, slots)) {
  1132. return;
  1133. }
  1134. strbuf_free(json->tmp);
  1135. luaL_error(l, "Found too many nested data structures (%d) at character %d",
  1136. json->current_depth, json->ptr - json->data);
  1137. }
  1138. static void json_parse_object_context(lua_State *l, json_parse_t *json)
  1139. {
  1140. json_token_t token;
  1141. /* 3 slots required:
  1142. * .., table, key, value */
  1143. json_decode_descend(l, json, 3);
  1144. lua_newtable(l);
  1145. json_next_token(json, &token);
  1146. /* Handle empty objects */
  1147. if (token.type == T_OBJ_END) {
  1148. json_decode_ascend(json);
  1149. return;
  1150. }
  1151. while (1) {
  1152. if (token.type != T_STRING)
  1153. json_throw_parse_error(l, json, "object key string", &token);
  1154. /* Push key */
  1155. lua_pushlstring(l, token.value.string, token.string_len);
  1156. json_next_token(json, &token);
  1157. if (token.type != T_COLON)
  1158. json_throw_parse_error(l, json, "colon", &token);
  1159. /* Fetch value */
  1160. json_next_token(json, &token);
  1161. json_process_value(l, json, &token);
  1162. /* Set key = value */
  1163. lua_rawset(l, -3);
  1164. json_next_token(json, &token);
  1165. if (token.type == T_OBJ_END) {
  1166. json_decode_ascend(json);
  1167. return;
  1168. }
  1169. if (token.type != T_COMMA)
  1170. json_throw_parse_error(l, json, "comma or object end", &token);
  1171. json_next_token(json, &token);
  1172. }
  1173. }
  1174. /* Handle the array context */
  1175. static void json_parse_array_context(lua_State *l, json_parse_t *json)
  1176. {
  1177. json_token_t token;
  1178. int i;
  1179. /* 2 slots required:
  1180. * .., table, value */
  1181. json_decode_descend(l, json, 2);
  1182. lua_newtable(l);
  1183. json_next_token(json, &token);
  1184. /* Handle empty arrays */
  1185. if (token.type == T_ARR_END) {
  1186. json_decode_ascend(json);
  1187. return;
  1188. }
  1189. for (i = 1; ; i++) {
  1190. json_process_value(l, json, &token);
  1191. lua_rawseti(l, -2, i); /* arr[i] = value */
  1192. json_next_token(json, &token);
  1193. if (token.type == T_ARR_END) {
  1194. json_decode_ascend(json);
  1195. return;
  1196. }
  1197. if (token.type != T_COMMA)
  1198. json_throw_parse_error(l, json, "comma or array end", &token);
  1199. json_next_token(json, &token);
  1200. }
  1201. }
  1202. /* Handle the "value" context */
  1203. static void json_process_value(lua_State *l, json_parse_t *json,
  1204. json_token_t *token)
  1205. {
  1206. switch (token->type) {
  1207. case T_STRING:
  1208. lua_pushlstring(l, token->value.string, token->string_len);
  1209. break;;
  1210. case T_NUMBER:
  1211. lua_pushnumber(l, token->value.number);
  1212. break;;
  1213. case T_BOOLEAN:
  1214. lua_pushboolean(l, token->value.boolean);
  1215. break;;
  1216. case T_OBJ_BEGIN:
  1217. json_parse_object_context(l, json);
  1218. break;;
  1219. case T_ARR_BEGIN:
  1220. json_parse_array_context(l, json);
  1221. break;;
  1222. case T_NULL:
  1223. /* In Lua, setting "t[k] = nil" will delete k from the table.
  1224. * Hence a NULL pointer lightuserdata object is used instead */
  1225. lua_pushlightuserdata(l, NULL);
  1226. break;;
  1227. default:
  1228. json_throw_parse_error(l, json, "value", token);
  1229. }
  1230. }
  1231. static int json_decode(lua_State *l)
  1232. {
  1233. json_parse_t json;
  1234. json_token_t token;
  1235. size_t json_len;
  1236. luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
  1237. json.cfg = json_fetch_config(l);
  1238. json.data = luaL_checklstring(l, 1, &json_len);
  1239. json.current_depth = 0;
  1240. json.ptr = json.data;
  1241. /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3)
  1242. *
  1243. * CJSON can support any simple data type, hence only the first
  1244. * character is guaranteed to be ASCII (at worst: '"'). This is
  1245. * still enough to detect whether the wrong encoding is in use. */
  1246. if (json_len >= 2 && (!json.data[0] || !json.data[1]))
  1247. luaL_error(l, "JSON parser does not support UTF-16 or UTF-32");
  1248. /* Ensure the temporary buffer can hold the entire string.
  1249. * This means we no longer need to do length checks since the decoded
  1250. * string must be smaller than the entire json string */
  1251. json.tmp = strbuf_new(json_len);
  1252. if(json.tmp == NULL){
  1253. return luaL_error(l, "not enough memory");
  1254. }
  1255. json_next_token(&json, &token);
  1256. json_process_value(l, &json, &token);
  1257. /* Ensure there is no more input left */
  1258. json_next_token(&json, &token);
  1259. if (token.type != T_END)
  1260. json_throw_parse_error(l, &json, "the end", &token);
  1261. strbuf_free(json.tmp);
  1262. return 1;
  1263. }
  1264. /* ===== INITIALISATION ===== */
  1265. #if 0
  1266. #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
  1267. /* Compatibility for Lua 5.1.
  1268. *
  1269. * luaL_setfuncs() is used to create a module table where the functions have
  1270. * json_config_t as their first upvalue. Code borrowed from Lua 5.2 source. */
  1271. static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
  1272. {
  1273. int i;
  1274. luaL_checkstack(l, nup, "too many upvalues");
  1275. for (; reg->name != NULL; reg++) { /* fill the table with given functions */
  1276. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  1277. lua_pushvalue(l, -nup);
  1278. lua_pushcclosure(l, reg->func, nup); /* closure with those upvalues */
  1279. lua_setfield(l, -(nup + 2), reg->name);
  1280. }
  1281. lua_pop(l, nup); /* remove upvalues */
  1282. }
  1283. #endif
  1284. /* Call target function in protected mode with all supplied args.
  1285. * Assumes target function only returns a single non-nil value.
  1286. * Convert and return thrown errors as: nil, "error message" */
  1287. static int json_protect_conversion(lua_State *l)
  1288. {
  1289. int err;
  1290. /* Deliberately throw an error for invalid arguments */
  1291. luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
  1292. /* pcall() the function stored as upvalue(1) */
  1293. lua_pushvalue(l, lua_upvalueindex(1));
  1294. lua_insert(l, 1);
  1295. err = lua_pcall(l, 1, 1, 0);
  1296. if (!err)
  1297. return 1;
  1298. if (err == LUA_ERRRUN) {
  1299. lua_pushnil(l);
  1300. lua_insert(l, -2);
  1301. return 2;
  1302. }
  1303. /* Since we are not using a custom error handler, the only remaining
  1304. * errors are memory related */
  1305. return luaL_error(l, "Memory allocation error in CJSON protected call");
  1306. }
  1307. /* Return cjson module table */
  1308. static int lua_cjson_new(lua_State *l)
  1309. {
  1310. /* Initialise number conversions */
  1311. fpconv_init();
  1312. /* cjson module table */
  1313. lua_newtable(l);
  1314. /* Register functions with config data as upvalue */
  1315. json_create_config(l);
  1316. luaL_setfuncs(l, reg, 1);
  1317. /* Set cjson.null */
  1318. lua_pushlightuserdata(l, NULL);
  1319. lua_setfield(l, -2, "null");
  1320. /* Set module name / version fields */
  1321. lua_pushliteral(l, CJSON_MODNAME);
  1322. lua_setfield(l, -2, "_NAME");
  1323. lua_pushliteral(l, CJSON_VERSION);
  1324. lua_setfield(l, -2, "_VERSION");
  1325. return 1;
  1326. }
  1327. /* Return cjson.safe module table */
  1328. static int lua_cjson_safe_new(lua_State *l)
  1329. {
  1330. const char *func[] = { "decode", "encode", NULL };
  1331. int i;
  1332. lua_cjson_new(l);
  1333. /* Fix new() method */
  1334. lua_pushcfunction(l, lua_cjson_safe_new);
  1335. lua_setfield(l, -2, "new");
  1336. for (i = 0; func[i]; i++) {
  1337. lua_getfield(l, -1, func[i]);
  1338. lua_pushcclosure(l, json_protect_conversion, 1);
  1339. lua_setfield(l, -2, func[i]);
  1340. }
  1341. return 1;
  1342. }
  1343. int luaopen_cjson(lua_State *l)
  1344. {
  1345. lua_cjson_new(l);
  1346. #ifdef ENABLE_CJSON_GLOBAL
  1347. /* Register a global "cjson" table. */
  1348. lua_pushvalue(l, -1);
  1349. lua_setglobal(l, CJSON_MODNAME);
  1350. #endif
  1351. /* Return cjson table */
  1352. return 1;
  1353. }
  1354. int luaopen_cjson_safe(lua_State *l)
  1355. {
  1356. lua_cjson_safe_new(l);
  1357. /* Return cjson.safe table */
  1358. return 1;
  1359. }
  1360. #endif
  1361. // Module function map
  1362. static const LUA_REG_TYPE cjson_map[] = {
  1363. { LSTRKEY( "encode" ), LFUNCVAL( json_encode ) },
  1364. { LSTRKEY( "decode" ), LFUNCVAL( json_decode ) },
  1365. //{ LSTRKEY( "encode_sparse_array" ), LFUNCVAL( json_cfg_encode_sparse_array ) },
  1366. //{ LSTRKEY( "encode_max_depth" ), LFUNCVAL( json_cfg_encode_max_depth ) },
  1367. //{ LSTRKEY( "decode_max_depth" ), LFUNCVAL( json_cfg_decode_max_depth ) },
  1368. //{ LSTRKEY( "encode_number_precision" ), LFUNCVAL( json_cfg_encode_number_precision ) },
  1369. //{ LSTRKEY( "encode_keep_buffer" ), LFUNCVAL( json_cfg_encode_keep_buffer ) },
  1370. //{ LSTRKEY( "encode_invalid_numbers" ), LFUNCVAL( json_cfg_encode_invalid_numbers ) },
  1371. //{ LSTRKEY( "decode_invalid_numbers" ), LFUNCVAL( json_cfg_decode_invalid_numbers ) },
  1372. //{ LSTRKEY( "new" ), LFUNCVAL( lua_cjson_new ) },
  1373. { LNILKEY, LNILVAL }
  1374. };
  1375. int luaopen_cjson( lua_State *L )
  1376. {
  1377. /* Initialise number conversions */
  1378. // fpconv_init(); // not needed for a specific cpu.
  1379. if(-1==cfg_init(&_cfg)){
  1380. return luaL_error(L, "BUG: Unable to init config for cjson");;
  1381. }
  1382. return 0;
  1383. }
  1384. NODEMCU_MODULE(CJSON, "cjson", cjson_map, luaopen_cjson);
  1385. /* vi:ai et sw=4 ts=4:
  1386. */