json.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * Copyright (C) 2009-2011 Vincent Hanquez <vincent@snarc.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation; version 2.1 or version 3.0 only.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * SPDX-License-Identifier: GPL-3.0+
  14. * License-Filename: LICENSE
  15. */
  16. /*
  17. * the class, states and state transition tables has been inspired by the JSON_parser.c
  18. * available at http://json.org, but are quite different on the way that the
  19. * parser handles its parse buffer and contains significant differences that affect
  20. * the JSON compliance.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include "json.h"
  27. #ifdef _MSC_VER
  28. #define inline _inline
  29. #endif
  30. #ifdef TRACING_ENABLE
  31. #include <stdio.h>
  32. #define TRACING(fmt, ...) fprintf(stderr, "tracing: " fmt, ##__VA_ARGS__)
  33. #else
  34. #define TRACING(fmt, ...) ((void) 0)
  35. #endif
  36. enum classes {
  37. C_SPACE, /* space */
  38. C_NL, /* newline */
  39. C_WHITE, /* tab, CR */
  40. C_LCURB, C_RCURB, /* object opening/closing */
  41. C_LSQRB, C_RSQRB, /* array opening/closing */
  42. /* syntax symbols */
  43. C_COLON,
  44. C_COMMA,
  45. C_QUOTE, /* " */
  46. C_BACKS, /* \ */
  47. C_SLASH, /* / */
  48. C_PLUS,
  49. C_MINUS,
  50. C_DOT,
  51. C_ZERO, C_DIGIT, /* digits */
  52. C_a, C_b, C_c, C_d, C_e, C_f, C_l, C_n, C_r, C_s, C_t, C_u, /* nocaps letters */
  53. C_ABCDF, C_E, /* caps letters */
  54. C_OTHER, /* all other */
  55. C_STAR, /* star in C style comment */
  56. C_HASH, /* # for YAML comment */
  57. C_ERROR = 0xfe,
  58. };
  59. /* map from character < 128 to classes. from 128 to 256 all C_OTHER */
  60. static uint8_t character_class[128] = {
  61. C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR,
  62. C_ERROR, C_WHITE, C_NL, C_ERROR, C_ERROR, C_WHITE, C_ERROR, C_ERROR,
  63. C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR,
  64. C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR, C_ERROR,
  65. C_SPACE, C_OTHER, C_QUOTE, C_HASH, C_OTHER, C_OTHER, C_OTHER, C_OTHER,
  66. C_OTHER, C_OTHER, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_DOT, C_SLASH,
  67. C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
  68. C_DIGIT, C_DIGIT, C_COLON, C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER,
  69. C_OTHER, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_OTHER,
  70. C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER,
  71. C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_OTHER,
  72. C_OTHER, C_OTHER, C_OTHER, C_LSQRB, C_BACKS, C_RSQRB, C_OTHER, C_OTHER,
  73. C_OTHER, C_a, C_b, C_c, C_d, C_e, C_f, C_OTHER,
  74. C_OTHER, C_OTHER, C_OTHER, C_OTHER, C_l, C_OTHER, C_n, C_OTHER,
  75. C_OTHER, C_OTHER, C_r, C_s, C_t, C_u, C_OTHER, C_OTHER,
  76. C_OTHER, C_OTHER, C_OTHER, C_LCURB, C_OTHER, C_RCURB, C_OTHER, C_OTHER
  77. };
  78. /* only the first 36 ascii characters need an escape */
  79. static char const *character_escape[] = {
  80. "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007", /* 0-7 */
  81. "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f", /* 8-f */
  82. "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014", "\\u0015", "\\u0016", "\\u0017", /* 10-17 */
  83. "\\u0018", "\\u0019", "\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f", /* 18-1f */
  84. "\x20", "\x21", "\\\"", "\x23", "\x24", "\x25", "\x26", "\x27", /* 20-27 */
  85. "\x28", "\x29", "\x2a", "\x2b", "\x2c", "\x2d", "\x2e", "\x2f", /* 28-2f */
  86. "\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", /* 30-37 */
  87. "\x38", "\x39", "\x3a", "\x3b", "\x3c", "\x3d", "\x3e", "\x3f", /* 38-3f */
  88. "\x40", "\x41", "\x42", "\x43", "\x44", "\x45", "\x46", "\x47", /* 40-47 */
  89. "\x48", "\x49", "\x4a", "\x4b", "\x4c", "\x4d", "\x4e", "\x4f", /* 48-4f */
  90. "\x50", "\x51", "\x52", "\x53", "\x54", "\x55", "\x56", "\x57", /* 50-57 */
  91. "\x58", "\x59", "\x5a", "\x5b", "\\\\", "\x5d", "\x5e", "\x5f", /* 58-5f */
  92. "\x60", "\x61", "\x62", "\x63", "\x64", "\x65", "\x66", "\x67", /* 60-67 */
  93. "\x68", "\x69", "\x6a", "\x6b", "\x6c", "\x6d", "\x6e", "\x6f", /* 68-6f */
  94. "\x70", "\x71", "\x72", "\x73", "\x74", "\x75", "\x76", "\x77", /* 70-77 */
  95. "\x78", "\x79", "\x7a", "\x7b", "\x7c", "\x7d", "\x7e", "\\u007f", /* 78-7f */
  96. "\\u0080", "\\u0081", "\\u0082", "\\u0083", "\\u0084", "\\u0085", "\\u0086", "\\u0087", /* 80-87 */
  97. "\\u0088", "\\u0089", "\\u008a", "\\u008b", "\\u008c", "\\u008d", "\\u008e", "\\u008f", /* 88-8f */
  98. "\\u0090", "\\u0091", "\\u0092", "\\u0093", "\\u0094", "\\u0095", "\\u0096", "\\u0097", /* 90-97 */
  99. "\\u0098", "\\u0099", "\\u009a", "\\u009b", "\\u009c", "\\u009d", "\\u009e", "\\u009f", /* 98-9f */
  100. "\\u00a0", "\\u00a1", "\\u00a2", "\\u00a3", "\\u00a4", "\\u00a5", "\\u00a6", "\\u00a7", /* a0-a7 */
  101. "\\u00a8", "\\u00a9", "\\u00aa", "\\u00ab", "\\u00ac", "\\u00ad", "\\u00ae", "\\u00af", /* a8-af */
  102. "\\u00b0", "\\u00b1", "\\u00b2", "\\u00b3", "\\u00b4", "\\u00b5", "\\u00b6", "\\u00b7", /* b0-b7 */
  103. "\\u00b8", "\\u00b9", "\\u00ba", "\\u00bb", "\\u00bc", "\\u00bd", "\\u00be", "\\u00bf", /* b8-bf */
  104. "\\u00c0", "\\u00c1", "\\u00c2", "\\u00c3", "\\u00c4", "\\u00c5", "\\u00c6", "\\u00c7", /* c0-c7 */
  105. "\\u00c8", "\\u00c9", "\\u00ca", "\\u00cb", "\\u00cc", "\\u00cd", "\\u00ce", "\\u00cf", /* c8-cf */
  106. "\\u00d0", "\\u00d1", "\\u00d2", "\\u00d3", "\\u00d4", "\\u00d5", "\\u00d6", "\\u00d7", /* d0-d7 */
  107. "\\u00d8", "\\u00d9", "\\u00da", "\\u00db", "\\u00dc", "\\u00dd", "\\u00de", "\\u00df", /* d8-df */
  108. "\\u00e0", "\\u00e1", "\\u00e2", "\\u00e3", "\\u00e4", "\\u00e5", "\\u00e6", "\\u00e7", /* e0-e7 */
  109. "\\u00e8", "\\u00e9", "\\u00ea", "\\u00eb", "\\u00ec", "\\u00ed", "\\u00ee", "\\u00ef", /* e8-ef */
  110. "\\u00f0", "\\u00f1", "\\u00f2", "\\u00f3", "\\u00f4", "\\u00f5", "\\u00f6", "\\u00f7", /* f0-f7 */
  111. "\\u00f8", "\\u00f9", "\\u00fa", "\\u00fb", "\\u00fc", "\\u00fd", "\\u00fe", "\\u00ff", /* f8-ff */
  112. };
  113. /* define all states and actions that will be taken on each transition.
  114. *
  115. * states are defined first because of the fact they are use as index in the
  116. * transitions table. they usually contains either a number or a prefix _
  117. * for simple state like string, object, value ...
  118. *
  119. * actions are defined starting from 0x80. state error is defined as 0xff
  120. */
  121. enum states {
  122. STATE_GO, /* start */
  123. STATE_OK, /* ok */
  124. STATE__O, /* object */
  125. STATE__K, /* key */
  126. STATE_CO, /* colon */
  127. STATE__V, /* value */
  128. STATE__A, /* array */
  129. STATE__S, /* string */
  130. STATE_E0, /* escape */
  131. STATE_U1, STATE_U2, STATE_U3, STATE_U4, /* unicode states */
  132. STATE_M0, STATE_Z0, STATE_I0, /* number states */
  133. STATE_R1, STATE_R2, /* real states (after-dot digits) */
  134. STATE_X1, STATE_X2, STATE_X3, /* exponant states */
  135. STATE_T1, STATE_T2, STATE_T3, /* true constant states */
  136. STATE_F1, STATE_F2, STATE_F3, STATE_F4, /* false constant states */
  137. STATE_N1, STATE_N2, STATE_N3, /* null constant states */
  138. STATE_C1, STATE_C2, STATE_C3, /* C-comment states */
  139. STATE_Y1, /* YAML-comment state */
  140. STATE_D1, STATE_D2, /* multi unicode states */
  141. };
  142. /* the following are actions that need to be taken */
  143. enum actions {
  144. STATE_KS = 0x80, /* key separator */
  145. STATE_SP, /* comma separator */
  146. STATE_AB, /* array begin */
  147. STATE_AE, /* array ending */
  148. STATE_OB, /* object begin */
  149. STATE_OE, /* object end */
  150. STATE_CB, /* C-comment begin */
  151. STATE_YB, /* YAML-comment begin */
  152. STATE_CE, /* YAML/C comment end */
  153. STATE_FA, /* false */
  154. STATE_TR, /* true */
  155. STATE_NU, /* null */
  156. STATE_DE, /* double detected by exponent */
  157. STATE_DF, /* double detected by . */
  158. STATE_SE, /* string end */
  159. STATE_MX, /* integer detected by minus */
  160. STATE_ZX, /* integer detected by zero */
  161. STATE_IX, /* integer detected by 1-9 */
  162. STATE_UC, /* Unicode character read */
  163. };
  164. /* error state */
  165. #define STATE___ 0xff
  166. #define NR_STATES (STATE_D2 + 1)
  167. #define NR_CLASSES (C_HASH + 1)
  168. #define IS_STATE_ACTION(s) ((s) & 0x80)
  169. #define S(x) STATE_##x
  170. #define PT_(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a1,b1,c1,d1,e1,f1,g1,h1) \
  171. { S(a),S(b),S(c),S(d),S(e),S(f),S(g),S(h),S(i),S(j),S(k),S(l),S(m),S(n), \
  172. S(o),S(p),S(q),S(r),S(s),S(t),S(u),S(v),S(w),S(x),S(y),S(z),S(a1),S(b1), \
  173. S(c1),S(d1),S(e1),S(f1),S(g1),S(h1) }
  174. /* map from the (previous state+new character class) to the next parser transition */
  175. static const uint8_t state_transition_table[NR_STATES][NR_CLASSES] = {
  176. /* white ABCDF other */
  177. /* sp nl | { } [ ] : , " \ / + - . 0 19 a b c d e f l n r s t u | E | * # */
  178. /*GO*/ PT_(GO, GO, GO, OB, __, AB, __, __, __, __, __, CB, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  179. __, __, __, __, __, YB),
  180. /*OK*/ PT_(OK, OK, OK, __, OE, __, AE, __, SP, __, __, CB, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  181. __, __, __, __, __, YB),
  182. /*_O*/ PT_(_O, _O, _O, __, OE, __, __, __, __, _S, __, CB, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  183. __, __, __, __, __, YB),
  184. /*_K*/ PT_(_K, _K, _K, __, __, __, __, __, __, _S, __, CB, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  185. __, __, __, __, __, YB),
  186. /*CO*/ PT_(CO, CO, CO, __, __, __, __, KS, __, __, __, CB, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  187. __, __, __, __, __, YB),
  188. /*_V*/ PT_(_V, _V, _V, OB, __, AB, __, __, __, _S, __, CB, __, MX, __, ZX, IX, __, __, __, __, __, F1, __, N1, __, __, T1,
  189. __, __, __, __, __, YB),
  190. /*_A*/ PT_(_A, _A, _A, OB, __, AB, AE, __, __, _S, __, CB, __, MX, __, ZX, IX, __, __, __, __, __, F1, __, N1, __, __, T1,
  191. __, __, __, __, __, YB),
  192. /****************************************************************************************************************/
  193. /*_S*/ PT_(_S, __, __, _S, _S, _S, _S, _S, _S, SE, E0, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S, _S,
  194. _S, _S, _S, _S, _S, _S),
  195. /*E0*/ PT_(__, __, __, __, __, __, __, __, __, _S, _S, _S, __, __, __, __, __, __, _S, __, __, __, _S, __, _S, _S, __, _S,
  196. U1, __, __, __, __, __),
  197. /*U1*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, U2, U2, U2, U2, U2, U2, U2, U2, __, __, __, __, __,
  198. __, U2, U2, __, __, __),
  199. /*U2*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, U3, U3, U3, U3, U3, U3, U3, U3, __, __, __, __, __,
  200. __, U3, U3, __, __, __),
  201. /*U3*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, U4, U4, U4, U4, U4, U4, U4, U4, __, __, __, __, __,
  202. __, U4, U4, __, __, __),
  203. /*U4*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, UC, UC, UC, UC, UC, UC, UC, UC, __, __, __, __, __,
  204. __, UC, UC, __, __, __),
  205. /****************************************************************************************************************/
  206. /*M0*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, Z0, I0, __, __, __, __, __, __, __, __, __, __, __,
  207. __, __, __, __, __, __),
  208. /*Z0*/ PT_(OK, OK, OK, __, OE, __, AE, __, SP, __, __, CB, __, __, DF, __, __, __, __, __, __, __, __, __, __, __, __, __,
  209. __, __, __, __, __, YB),
  210. /*I0*/ PT_(OK, OK, OK, __, OE, __, AE, __, SP, __, __, CB, __, __, DF, I0, I0, __, __, __, __, DE, __, __, __, __, __, __,
  211. __, __, DE, __, __, YB),
  212. /*R1*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, R2, R2, __, __, __, __, __, __, __, __, __, __, __,
  213. __, __, __, __, __, __),
  214. /*R2*/ PT_(OK, OK, OK, __, OE, __, AE, __, SP, __, __, CB, __, __, __, R2, R2, __, __, __, __, X1, __, __, __, __, __, __,
  215. __, __, X1, __, __, YB),
  216. /*X1*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, X2, X2, __, X3, X3, __, __, __, __, __, __, __, __, __, __, __,
  217. __, __, __, __, __, __),
  218. /*X2*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, X3, X3, __, __, __, __, __, __, __, __, __, __, __,
  219. __, __, __, __, __, __),
  220. /*X3*/ PT_(OK, OK, OK, __, OE, __, AE, __, SP, __, __, __, __, __, __, X3, X3, __, __, __, __, __, __, __, __, __, __, __,
  221. __, __, __, __, __, __),
  222. /****************************************************************************************************************/
  223. /*T1*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, T2, __, __,
  224. __, __, __, __, __, __),
  225. /*T2*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  226. T3, __, __, __, __, __),
  227. /*T3*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, TR, __, __, __, __, __, __,
  228. __, __, __, __, __, __),
  229. /*F1*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, F2, __, __, __, __, __, __, __, __, __, __,
  230. __, __, __, __, __, __),
  231. /*F2*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, F3, __, __, __, __,
  232. __, __, __, __, __, __),
  233. /*F3*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, F4, __,
  234. __, __, __, __, __, __),
  235. /*F4*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, FA, __, __, __, __, __, __,
  236. __, __, __, __, __, __),
  237. /*N1*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  238. N2, __, __, __, __, __),
  239. /*N2*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, N3, __, __, __, __,
  240. __, __, __, __, __, __),
  241. /*N3*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, NU, __, __, __, __,
  242. __, __, __, __, __, __),
  243. /****************************************************************************************************************/
  244. /*C1*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  245. __, __, __, __, C2, __),
  246. /*C2*/ PT_(C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2,
  247. C2, C2, C2, C2, C3, C2),
  248. /*C3*/ PT_(C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, CE, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2, C2,
  249. C2, C2, C2, C2, C3, C2),
  250. /*Y1*/ PT_(Y1, CE, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1, Y1,
  251. Y1, Y1, Y1, Y1, Y1, Y1),
  252. /*D1*/ PT_(__, __, __, __, __, __, __, __, __, __, D2, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  253. __, __, __, __, __, __),
  254. /*D2*/ PT_(__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  255. U1, __, __, __, __, __),
  256. };
  257. #undef S
  258. #undef PT_
  259. /* map from (previous state+new character class) to the buffer policy. ignore=0/append=1/escape=2 */
  260. static const uint8_t buffer_policy_table[NR_STATES][NR_CLASSES] = {
  261. /* white ABCDF other */
  262. /* sp nl | { } [ ] : , " \ / + - . 0 19 a b c d e f l n r s t u | E | * # */
  263. /*GO*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  264. /*OK*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  265. /*_O*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  266. /*_K*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  267. /*CO*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  268. /*_V*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  269. /*_A*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  270. /**************************************************************************************************************/
  271. /*_S*/ { 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  272. /*E0*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0 },
  273. /*U1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
  274. /*U2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
  275. /*U3*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
  276. /*U4*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
  277. /**************************************************************************************************************/
  278. /*M0*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  279. /*Z0*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  280. /*I0*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
  281. /*R1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  282. /*R2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
  283. /*X1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  284. /*X2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  285. /*X3*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  286. /**************************************************************************************************************/
  287. /*T1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  288. /*T2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  289. /*T3*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  290. /*F1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  291. /*F2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  292. /*F3*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  293. /*F4*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  294. /*N1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  295. /*N2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  296. /*N3*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  297. /**************************************************************************************************************/
  298. /*C1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  299. /*C2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  300. /*C3*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  301. /*Y1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  302. /*D1*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  303. /*D2*/ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  304. };
  305. #define __ 0xff
  306. static const uint8_t utf8_header_table[256] = {
  307. /* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  308. /* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  309. /* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  310. /* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  311. /* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  312. /* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  313. /* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  314. /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  315. /* 80 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  316. /* 90 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  317. /* a0 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  318. /* b0 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  319. /* c0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  320. /* d0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  321. /* e0 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  322. /* f0 */ 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, __, __,
  323. };
  324. static const uint8_t utf8_continuation_table[256] = {
  325. /*__0 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  326. /* 10 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  327. /* 20 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  328. /* 30 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  329. /* 40 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  330. /* 50 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  331. /* 60 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  332. /* 70 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  333. /* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  334. /* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  335. /* a0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  336. /* b0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  337. /* c0 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  338. /* d0 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  339. /* e0 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  340. /* f0 */ __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  341. };
  342. #undef __
  343. #define MODE_ARRAY 0
  344. #define MODE_OBJECT 1
  345. static inline void *memory_realloc(void *(*realloc_fct)(void *, size_t), void *ptr, size_t size)
  346. {
  347. if (realloc_fct) {
  348. return (realloc_fct(ptr, size));
  349. } else {
  350. return (realloc(ptr, size));
  351. }
  352. }
  353. static inline void *memory_calloc(void *(*calloc_fct)(size_t, size_t), size_t nmemb, size_t size)
  354. {
  355. if (calloc_fct) {
  356. return (calloc_fct(nmemb, size));
  357. } else {
  358. return (calloc(nmemb, size));
  359. }
  360. }
  361. static inline void memory_free(void (*free_fct)(void *ptr), void *ptr)
  362. {
  363. if (free_fct) {
  364. free_fct(ptr);
  365. } else {
  366. free(ptr);
  367. }
  368. }
  369. #define parser_free(parser, n) memory_free(parser->config.user_free, n)
  370. #define parser_calloc(parser, n, s) memory_calloc(parser->config.user_calloc, n, s)
  371. #define parser_realloc(parser, n, s) memory_realloc(parser->config.user_realloc, n, s)
  372. static int state_grow(json_parser * parser)
  373. {
  374. uint32_t newsize = parser->stack_size * 2;
  375. void *ptr = NULL;
  376. if (parser->config.max_nesting != 0)
  377. return JSON_ERROR_NESTING_LIMIT;
  378. ptr = parser_realloc(parser, parser->stack, newsize * sizeof(uint8_t));
  379. if (!ptr)
  380. return JSON_ERROR_NO_MEMORY;
  381. parser->stack = ptr;
  382. parser->stack_size = newsize;
  383. return 0;
  384. }
  385. static int state_push(json_parser * parser, int mode)
  386. {
  387. if (parser->stack_offset >= parser->stack_size) {
  388. int ret = state_grow(parser);
  389. if (ret)
  390. return ret;
  391. }
  392. parser->stack[parser->stack_offset++] = mode;
  393. return 0;
  394. }
  395. static int state_pop(json_parser * parser, int mode)
  396. {
  397. if (parser->stack_offset == 0)
  398. return JSON_ERROR_POP_EMPTY;
  399. parser->stack_offset--;
  400. if (parser->stack[parser->stack_offset] != mode)
  401. return JSON_ERROR_POP_UNEXPECTED_MODE;
  402. return 0;
  403. }
  404. static int buffer_grow(json_parser * parser)
  405. {
  406. uint32_t newsize;
  407. void *ptr;
  408. uint32_t max = parser->config.max_data;
  409. if (max > 0 && parser->buffer_size == max)
  410. return JSON_ERROR_DATA_LIMIT;
  411. newsize = parser->buffer_size * 2;
  412. if (max > 0 && newsize > max)
  413. newsize = max;
  414. ptr = parser_realloc(parser, parser->buffer, newsize * sizeof(char));
  415. if (!ptr)
  416. return JSON_ERROR_NO_MEMORY;
  417. parser->buffer = ptr;
  418. parser->buffer_size = newsize;
  419. return 0;
  420. }
  421. static int buffer_push(json_parser * parser, unsigned char c)
  422. {
  423. int ret;
  424. if (parser->buffer_offset + 1 >= parser->buffer_size) {
  425. ret = buffer_grow(parser);
  426. if (ret)
  427. return ret;
  428. }
  429. parser->buffer[parser->buffer_offset++] = c;
  430. return 0;
  431. }
  432. static int do_callback_withbuf(json_parser * parser, int type)
  433. {
  434. if (!parser->callback)
  435. return 0;
  436. parser->buffer[parser->buffer_offset] = '\0';
  437. return (*parser->callback) (parser->userdata, type, parser->buffer, parser->buffer_offset);
  438. }
  439. static int do_callback(json_parser * parser, int type)
  440. {
  441. if (!parser->callback)
  442. return 0;
  443. return (*parser->callback) (parser->userdata, type, NULL, 0);
  444. }
  445. static int do_buffer(json_parser * parser)
  446. {
  447. int ret = 0;
  448. switch (parser->type) {
  449. case JSON_KEY:
  450. case JSON_STRING:
  451. case JSON_FLOAT:
  452. case JSON_INT:
  453. case JSON_NULL:
  454. case JSON_TRUE:
  455. case JSON_FALSE:
  456. ret = do_callback_withbuf(parser, parser->type);
  457. if (ret)
  458. return ret;
  459. break;
  460. default:
  461. break;
  462. }
  463. parser->buffer_offset = 0;
  464. return ret;
  465. }
  466. static const uint8_t hextable[] = {
  467. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  468. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  469. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  470. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
  471. 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  472. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  473. 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  474. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  475. };
  476. #define hex(c) (hextable[(uint8_t) c])
  477. /* high surrogate range from d800 to dbff */
  478. /* low surrogate range dc00 to dfff */
  479. #define IS_HIGH_SURROGATE(uc) (((uc) & 0xfc00) == 0xd800)
  480. #define IS_LOW_SURROGATE(uc) (((uc) & 0xfc00) == 0xdc00)
  481. /* transform an unicode [0-9A-Fa-f]{4} sequence into a proper value */
  482. static int decode_unicode_char(json_parser * parser)
  483. {
  484. uint32_t uval;
  485. char *b = parser->buffer;
  486. int offset = parser->buffer_offset;
  487. uval = (hex(b[offset - 4]) << 12) | (hex(b[offset - 3]) << 8)
  488. | (hex(b[offset - 2]) << 4) | hex(b[offset - 1]);
  489. parser->buffer_offset -= 4;
  490. /* fast case */
  491. if (!parser->unicode_multi && uval < 0x80) {
  492. b[parser->buffer_offset++] = (char)uval;
  493. return 0;
  494. }
  495. if (parser->unicode_multi) {
  496. if (!IS_LOW_SURROGATE(uval))
  497. return JSON_ERROR_UNICODE_MISSING_LOW_SURROGATE;
  498. uval = 0x10000 + ((parser->unicode_multi & 0x3ff) << 10) + (uval & 0x3ff);
  499. b[parser->buffer_offset++] = (char)((uval >> 18) | 0xf0);
  500. b[parser->buffer_offset++] = (char)(((uval >> 12) & 0x3f) | 0x80);
  501. b[parser->buffer_offset++] = (char)(((uval >> 6) & 0x3f) | 0x80);
  502. b[parser->buffer_offset++] = (char)((uval & 0x3f) | 0x80);
  503. parser->unicode_multi = 0;
  504. return 0;
  505. }
  506. if (IS_LOW_SURROGATE(uval))
  507. return JSON_ERROR_UNICODE_UNEXPECTED_LOW_SURROGATE;
  508. if (IS_HIGH_SURROGATE(uval)) {
  509. parser->unicode_multi = uval;
  510. return 0;
  511. }
  512. if (uval < 0x800) {
  513. b[parser->buffer_offset++] = (char)((uval >> 6) | 0xc0);
  514. b[parser->buffer_offset++] = (char)((uval & 0x3f) | 0x80);
  515. } else {
  516. b[parser->buffer_offset++] = (char)((uval >> 12) | 0xe0);
  517. b[parser->buffer_offset++] = (char)(((uval >> 6) & 0x3f) | 0x80);
  518. b[parser->buffer_offset++] = (char)(((uval >> 0) & 0x3f) | 0x80);
  519. }
  520. return 0;
  521. }
  522. static int buffer_push_escape(json_parser * parser, unsigned char next)
  523. {
  524. char c = '\0';
  525. switch (next) {
  526. case 'b':
  527. c = '\b';
  528. break;
  529. case 'f':
  530. c = '\f';
  531. break;
  532. case 'n':
  533. c = '\n';
  534. break;
  535. case 'r':
  536. c = '\r';
  537. break;
  538. case 't':
  539. c = '\t';
  540. break;
  541. case '"':
  542. c = '"';
  543. break;
  544. case '/':
  545. c = '/';
  546. break;
  547. case '\\':
  548. c = '\\';
  549. break;
  550. }
  551. /* push the escaped character */
  552. return buffer_push(parser, c);
  553. }
  554. #define CHK(f) do { ret = f; if (ret) return ret; } while(0)
  555. static int act_uc(json_parser * parser)
  556. {
  557. int ret;
  558. CHK(decode_unicode_char(parser));
  559. parser->state = (parser->unicode_multi) ? STATE_D1 : STATE__S;
  560. return 0;
  561. }
  562. static int act_yb(json_parser * parser)
  563. {
  564. if (!parser->config.allow_yaml_comments)
  565. return JSON_ERROR_COMMENT_NOT_ALLOWED;
  566. parser->save_state = parser->state;
  567. return 0;
  568. }
  569. static int act_cb(json_parser * parser)
  570. {
  571. if (!parser->config.allow_c_comments)
  572. return JSON_ERROR_COMMENT_NOT_ALLOWED;
  573. parser->save_state = parser->state;
  574. return 0;
  575. }
  576. static int act_ce(json_parser * parser)
  577. {
  578. parser->state = (parser->save_state > STATE__A) ? STATE_OK : parser->save_state;
  579. return 0;
  580. }
  581. static int act_ob(json_parser * parser)
  582. {
  583. int ret;
  584. CHK(do_callback(parser, JSON_OBJECT_BEGIN));
  585. CHK(state_push(parser, MODE_OBJECT));
  586. parser->expecting_key = 1;
  587. return 0;
  588. }
  589. static int act_oe(json_parser * parser)
  590. {
  591. int ret;
  592. CHK(state_pop(parser, MODE_OBJECT));
  593. CHK(do_callback(parser, JSON_OBJECT_END));
  594. parser->expecting_key = 0;
  595. return 0;
  596. }
  597. static int act_ab(json_parser * parser)
  598. {
  599. int ret;
  600. CHK(do_callback(parser, JSON_ARRAY_BEGIN));
  601. CHK(state_push(parser, MODE_ARRAY));
  602. return 0;
  603. }
  604. static int act_ae(json_parser * parser)
  605. {
  606. int ret;
  607. CHK(state_pop(parser, MODE_ARRAY));
  608. CHK(do_callback(parser, JSON_ARRAY_END));
  609. return 0;
  610. }
  611. static int act_se(json_parser * parser)
  612. {
  613. int ret;
  614. CHK(do_callback_withbuf(parser, (parser->expecting_key) ? JSON_KEY : JSON_STRING));
  615. parser->buffer_offset = 0;
  616. parser->state = (parser->expecting_key) ? STATE_CO : STATE_OK;
  617. parser->expecting_key = 0;
  618. return 0;
  619. }
  620. static int act_sp(json_parser * parser)
  621. {
  622. if (parser->stack_offset == 0)
  623. return JSON_ERROR_COMMA_OUT_OF_STRUCTURE;
  624. if (parser->stack[parser->stack_offset - 1] == MODE_OBJECT) {
  625. parser->expecting_key = 1;
  626. parser->state = STATE__K;
  627. } else
  628. parser->state = STATE__V;
  629. return 0;
  630. }
  631. struct action_descr {
  632. int (*call)(json_parser * parser);
  633. uint8_t type;
  634. uint8_t state; /* 0 if we let the callback set the value it want */
  635. uint8_t dobuffer;
  636. };
  637. static struct action_descr actions_map[] = {
  638. { NULL, JSON_NONE, STATE__V, 0 }, /* KS */
  639. { act_sp, JSON_NONE, 0, 1 }, /* SP */
  640. { act_ab, JSON_NONE, STATE__A, 0 }, /* AB */
  641. { act_ae, JSON_NONE, STATE_OK, 1 }, /* AE */
  642. { act_ob, JSON_NONE, STATE__O, 0 }, /* OB */
  643. { act_oe, JSON_NONE, STATE_OK, 1 }, /* OE */
  644. { act_cb, JSON_NONE, STATE_C1, 1 }, /* CB */
  645. { act_yb, JSON_NONE, STATE_Y1, 1 }, /* YB */
  646. { act_ce, JSON_NONE, 0, 0 }, /* CE */
  647. { NULL, JSON_FALSE, STATE_OK, 0 }, /* FA */
  648. { NULL, JSON_TRUE, STATE_OK, 0 }, /* TR */
  649. { NULL, JSON_NULL, STATE_OK, 0 }, /* NU */
  650. { NULL, JSON_FLOAT, STATE_X1, 0 }, /* DE */
  651. { NULL, JSON_FLOAT, STATE_R1, 0 }, /* DF */
  652. { act_se, JSON_NONE, 0, 0 }, /* SE */
  653. { NULL, JSON_INT, STATE_M0, 0 }, /* MX */
  654. { NULL, JSON_INT, STATE_Z0, 0 }, /* ZX */
  655. { NULL, JSON_INT, STATE_I0, 0 }, /* IX */
  656. { act_uc, JSON_NONE, 0, 0 }, /* UC */
  657. };
  658. static int do_action(json_parser * parser, int next_state)
  659. {
  660. struct action_descr *descr = &actions_map[next_state & ~0x80];
  661. if (descr->call) {
  662. int ret;
  663. if (descr->dobuffer)
  664. CHK(do_buffer(parser));
  665. CHK((descr->call) (parser));
  666. }
  667. if (descr->state)
  668. parser->state = descr->state;
  669. parser->type = descr->type;
  670. return 0;
  671. }
  672. /** json_parser_init initialize a parser structure taking a config,
  673. * a config and its userdata.
  674. * return JSON_ERROR_NO_MEMORY if memory allocation failed or SUCCESS.
  675. */
  676. int json_parser_init(json_parser * parser, json_config * config, json_parser_callback callback, void *userdata)
  677. {
  678. memset(parser, 0, sizeof(*parser));
  679. if (config)
  680. memcpy(&parser->config, config, sizeof(json_config));
  681. parser->callback = callback;
  682. parser->userdata = userdata;
  683. /* initialise parsing stack and state */
  684. parser->stack_offset = 0;
  685. parser->state = STATE_GO;
  686. /* initialize the parse stack */
  687. parser->stack_size = (parser->config.max_nesting > 0)
  688. ? parser->config.max_nesting : LIBJSON_DEFAULT_STACK_SIZE;
  689. parser->stack = parser_calloc(parser, parser->stack_size, sizeof(parser->stack[0]));
  690. if (!parser->stack) { /* gcc-10 -fanalyzer sys memory leak here */
  691. return JSON_ERROR_NO_MEMORY;
  692. }
  693. /* initialize the parse buffer */
  694. parser->buffer_size = (parser->config.buffer_initial_size > 0)
  695. ? parser->config.buffer_initial_size : LIBJSON_DEFAULT_BUFFER_SIZE;
  696. if (parser->config.max_data > 0 && parser->buffer_size > parser->config.max_data)
  697. parser->buffer_size = parser->config.max_data;
  698. parser->buffer = parser_calloc(parser, parser->buffer_size, sizeof(char));
  699. if (!parser->buffer) {
  700. parser_free(parser, parser->stack);
  701. return JSON_ERROR_NO_MEMORY;
  702. }
  703. return 0;
  704. }
  705. /** json_parser_free freed memory structure allocated by the parser */
  706. int json_parser_free(json_parser * parser)
  707. {
  708. if (!parser)
  709. return 0;
  710. parser_free(parser, parser->stack);
  711. parser_free(parser, parser->buffer);
  712. parser->stack = NULL;
  713. parser->buffer = NULL;
  714. return 0;
  715. }
  716. /** json_parser_is_done return 0 is the parser isn't in a finish state. !0 if it is */
  717. int json_parser_is_done(json_parser * parser)
  718. {
  719. /* need to compare the state to !GO to not accept empty document */
  720. return parser->stack_offset == 0 && parser->state != STATE_GO;
  721. }
  722. /** json_parser_string append a string s with a specific length to the parser
  723. * return 0 if everything went ok, a JSON_ERROR_* otherwise.
  724. * the user can supplied a valid processed pointer that will
  725. * be fill with the number of processed characters before returning */
  726. int json_parser_string(json_parser * parser, const char *s, uint32_t length, uint32_t * processed)
  727. {
  728. int ret;
  729. int next_class, next_state;
  730. int buffer_policy;
  731. uint32_t i;
  732. ret = 0;
  733. for (i = 0; i < length; i++) {
  734. unsigned char ch = s[i];
  735. ret = 0;
  736. if (parser->utf8_multibyte_left > 0) {
  737. if (utf8_continuation_table[ch] != 0) {
  738. ret = JSON_ERROR_UTF8;
  739. break;
  740. }
  741. next_class = C_OTHER;
  742. parser->utf8_multibyte_left--;
  743. } else {
  744. parser->utf8_multibyte_left = utf8_header_table[ch];
  745. if (parser->utf8_multibyte_left == 0xff) {
  746. ret = JSON_ERROR_UTF8;
  747. break;
  748. }
  749. next_class = (parser->utf8_multibyte_left > 0) ? C_OTHER : character_class[ch];
  750. if (next_class == C_ERROR) {
  751. ret = JSON_ERROR_BAD_CHAR;
  752. break;
  753. }
  754. }
  755. next_state = state_transition_table[parser->state][next_class];
  756. buffer_policy = buffer_policy_table[parser->state][next_class];
  757. TRACING("addchar %d (current-state=%d, next-state=%d, buf-policy=%d)\n", ch, parser->state, next_state, buffer_policy);
  758. if (next_state == STATE___) {
  759. ret = JSON_ERROR_UNEXPECTED_CHAR;
  760. break;
  761. }
  762. /* add char to buffer */
  763. if (buffer_policy) {
  764. ret = (buffer_policy == 2)
  765. ? buffer_push_escape(parser, ch)
  766. : buffer_push(parser, ch);
  767. if (ret)
  768. break;
  769. }
  770. /* move to the next level */
  771. if (IS_STATE_ACTION(next_state))
  772. ret = do_action(parser, next_state);
  773. else
  774. parser->state = next_state;
  775. if (ret)
  776. break;
  777. }
  778. if (processed)
  779. *processed = i;
  780. return ret;
  781. }
  782. /** json_parser_char append one single char to the parser
  783. * return 0 if everything went ok, a JSON_ERROR_* otherwise */
  784. int json_parser_char(json_parser * parser, unsigned char ch)
  785. {
  786. return json_parser_string(parser, (char *)&ch, 1, NULL);
  787. }
  788. /** json_print_init initialize a printer context. always succeed */
  789. int json_print_init(json_printer * printer, json_printer_callback callback, void *userdata)
  790. {
  791. memset(printer, '\0', sizeof(*printer));
  792. printer->callback = callback;
  793. printer->userdata = userdata;
  794. printer->indentstr = "\t";
  795. printer->indentlevel = 0;
  796. printer->enter_object = 1;
  797. printer->first = 1;
  798. return 0;
  799. }
  800. /** json_print_free free a printer context
  801. * doesn't do anything now, but in future print_init could allocate memory */
  802. int json_print_free(json_printer * printer)
  803. {
  804. memset(printer, '\0', sizeof(*printer));
  805. return 0;
  806. }
  807. /* escape a C string to be a JSON valid string on the wire.
  808. * XXX: it doesn't do unicode verification. yet?. */
  809. static int print_string(json_printer * printer, const char *data, uint32_t length)
  810. {
  811. uint32_t i;
  812. if (printer->callback == NULL) {
  813. printf("%s(): nil printer->callback\n", __func__);
  814. return (0);
  815. }
  816. printer->callback(printer->userdata, "\"", 1);
  817. for (i = 0; i < length; i++) {
  818. unsigned char c = data[i];
  819. if (c < 36) {
  820. char const *esc = character_escape[c];
  821. printer->callback(printer->userdata, esc, strlen(esc));
  822. } else if (c == '\\') {
  823. printer->callback(printer->userdata, "\\\\", 2);
  824. } else
  825. printer->callback(printer->userdata, data + i, 1);
  826. }
  827. printer->callback(printer->userdata, "\"", 1);
  828. return 0;
  829. }
  830. static int print_binary_string(json_printer * printer, const char *data, uint32_t length)
  831. {
  832. uint32_t i;
  833. printer->callback(printer->userdata, "\"", 1);
  834. for (i = 0; i < length; i++) {
  835. unsigned char c = data[i];
  836. char const *esc = character_escape[c];
  837. printer->callback(printer->userdata, esc, strlen(esc));
  838. }
  839. printer->callback(printer->userdata, "\"", 1);
  840. return 0;
  841. }
  842. static int print_indent(json_printer * printer)
  843. {
  844. int i;
  845. printer->callback(printer->userdata, "\n", 1);
  846. for (i = 0; i < printer->indentlevel; i++)
  847. printer->callback(printer->userdata, printer->indentstr, strlen(printer->indentstr));
  848. return 0;
  849. }
  850. static int json_print_mode(json_printer * printer, int type, const char *data, uint32_t length, int pretty)
  851. {
  852. int enterobj = printer->enter_object;
  853. if (!enterobj && !printer->afterkey && (type != JSON_ARRAY_END && type != JSON_OBJECT_END)) {
  854. printer->callback(printer->userdata, ",", 1);
  855. if (pretty)
  856. print_indent(printer);
  857. }
  858. if (pretty && (enterobj && !printer->first && (type != JSON_ARRAY_END && type != JSON_OBJECT_END))) {
  859. print_indent(printer);
  860. }
  861. printer->first = 0;
  862. printer->enter_object = 0;
  863. printer->afterkey = 0;
  864. switch (type) {
  865. case JSON_ARRAY_BEGIN:
  866. printer->callback(printer->userdata, "[", 1);
  867. printer->indentlevel++;
  868. printer->enter_object = 1;
  869. break;
  870. case JSON_OBJECT_BEGIN:
  871. printer->callback(printer->userdata, "{", 1);
  872. printer->indentlevel++;
  873. printer->enter_object = 1;
  874. break;
  875. case JSON_ARRAY_END:
  876. case JSON_OBJECT_END:
  877. printer->indentlevel--;
  878. if (pretty && !enterobj)
  879. print_indent(printer);
  880. printer->callback(printer->userdata, (type == JSON_OBJECT_END) ? "}" : "]", 1);
  881. break;
  882. case JSON_INT:
  883. printer->callback(printer->userdata, data, length);
  884. break;
  885. case JSON_FLOAT:
  886. printer->callback(printer->userdata, data, length);
  887. break;
  888. case JSON_NULL:
  889. printer->callback(printer->userdata, "null", 4);
  890. break;
  891. case JSON_TRUE:
  892. printer->callback(printer->userdata, "true", 4);
  893. break;
  894. case JSON_FALSE:
  895. printer->callback(printer->userdata, "false", 5);
  896. break;
  897. case JSON_KEY:
  898. print_string(printer, data, length);
  899. printer->callback(printer->userdata, ": ", (pretty) ? 2 : 1);
  900. printer->afterkey = 1;
  901. break;
  902. case JSON_STRING:
  903. print_string(printer, data, length);
  904. break;
  905. case JSON_BSTRING:
  906. print_binary_string(printer, data, length);
  907. break;
  908. default:
  909. break;
  910. }
  911. return 0;
  912. }
  913. /** json_print_pretty pretty print the passed argument (type/data/length). */
  914. int json_print_pretty(json_printer * printer, int type, const char *data, uint32_t length)
  915. {
  916. if (data == NULL) {
  917. printf("%s(): nil data\n", __func__);
  918. return (0);
  919. }
  920. if (length == 0) {
  921. printf("%s(): zero length\n", __func__);
  922. return (0);
  923. }
  924. return json_print_mode(printer, type, data, length, 1);
  925. }
  926. /** json_print_raw prints without eye candy the passed argument (type/data/length). */
  927. int json_print_raw(json_printer * printer, int type, const char *data, uint32_t length)
  928. {
  929. return json_print_mode(printer, type, data, length, 0);
  930. }
  931. /** json_print_args takes multiple types and pass them to the printer function */
  932. int json_print_args(json_printer * printer, int (*f)(json_printer *, int, const char *, uint32_t), ...)
  933. {
  934. va_list ap;
  935. char *data;
  936. int32_t length;
  937. int type, ret;
  938. ret = 0;
  939. va_start(ap, f);
  940. while ((type = va_arg(ap, int)) != -1) {
  941. switch (type) {
  942. case JSON_ARRAY_BEGIN:
  943. case JSON_ARRAY_END:
  944. case JSON_OBJECT_BEGIN:
  945. case JSON_OBJECT_END:
  946. case JSON_NULL:
  947. case JSON_TRUE:
  948. case JSON_FALSE:
  949. ret = (*f) (printer, type, NULL, 0);
  950. break;
  951. case JSON_INT:
  952. case JSON_FLOAT:
  953. case JSON_KEY:
  954. case JSON_STRING:
  955. data = va_arg(ap, char *);
  956. length = va_arg(ap, int32_t);
  957. if (length == -1)
  958. length = strlen(data);
  959. ret = (*f) (printer, type, data, length);
  960. break;
  961. }
  962. if (ret)
  963. break;
  964. }
  965. va_end(ap);
  966. return ret;
  967. }
  968. static int dom_push(struct json_parser_dom *ctx, void *val)
  969. {
  970. if (ctx->stack_offset == ctx->stack_size) {
  971. void *ptr;
  972. uint32_t newsize = ctx->stack_size * 2;
  973. ptr = memory_realloc(ctx->user_realloc, ctx->stack, newsize);
  974. if (!ptr)
  975. return JSON_ERROR_NO_MEMORY;
  976. ctx->stack = ptr;
  977. ctx->stack_size = newsize;
  978. }
  979. ctx->stack[ctx->stack_offset].val = val;
  980. ctx->stack[ctx->stack_offset].key = NULL;
  981. ctx->stack[ctx->stack_offset].key_length = 0;
  982. ctx->stack_offset++;
  983. return 0;
  984. }
  985. static int dom_pop(struct json_parser_dom *ctx, void **val)
  986. {
  987. ctx->stack_offset--;
  988. *val = ctx->stack[ctx->stack_offset].val;
  989. return 0;
  990. }
  991. /* XXX todo should use parser_calloc and parser_free also below */
  992. int json_parser_dom_init(json_parser_dom * dom,
  993. json_parser_dom_create_structure create_structure,
  994. json_parser_dom_create_data create_data, json_parser_dom_append append)
  995. {
  996. memset(dom, 0, sizeof(*dom));
  997. dom->stack_size = 1024;
  998. dom->stack_offset = 0;
  999. dom->stack = memory_calloc(dom->user_calloc, dom->stack_size, sizeof(*(dom->stack)));
  1000. if (!dom->stack) { /* gcc-10 -fanalyzer memory leak here */
  1001. return JSON_ERROR_NO_MEMORY;
  1002. }
  1003. dom->append = append;
  1004. dom->create_structure = create_structure;
  1005. dom->create_data = create_data;
  1006. return 0;
  1007. }
  1008. int json_parser_dom_free(json_parser_dom * dom)
  1009. {
  1010. free(dom->stack);
  1011. return 0;
  1012. }
  1013. int json_parser_dom_callback(void *userdata, int type, const char *data, uint32_t length)
  1014. {
  1015. struct json_parser_dom *ctx = userdata;
  1016. void *v;
  1017. struct stack_elem *stack = NULL;
  1018. switch (type) {
  1019. case JSON_ARRAY_BEGIN:
  1020. case JSON_OBJECT_BEGIN:
  1021. v = ctx->create_structure(ctx->stack_offset, type == JSON_OBJECT_BEGIN);
  1022. if (!v)
  1023. return JSON_ERROR_CALLBACK;
  1024. dom_push(ctx, v);
  1025. break;
  1026. case JSON_OBJECT_END:
  1027. case JSON_ARRAY_END:
  1028. dom_pop(ctx, &v);
  1029. if (ctx->stack_offset > 0) {
  1030. stack = &(ctx->stack[ctx->stack_offset - 1]);
  1031. ctx->append(stack->val, stack->key, stack->key_length, v);
  1032. free(stack->key);
  1033. } else
  1034. ctx->root_structure = v;
  1035. break;
  1036. case JSON_KEY:
  1037. stack = &(ctx->stack[ctx->stack_offset - 1]);
  1038. stack->key = memory_calloc(ctx->user_calloc, length + 1, sizeof(char));
  1039. if (!stack->key)
  1040. return JSON_ERROR_NO_MEMORY;
  1041. stack->key_length = length;
  1042. memcpy(stack->key, data, length);
  1043. break;
  1044. case JSON_STRING:
  1045. case JSON_INT:
  1046. case JSON_FLOAT:
  1047. case JSON_NULL:
  1048. case JSON_TRUE:
  1049. case JSON_FALSE:
  1050. stack = &(ctx->stack[ctx->stack_offset - 1]);
  1051. v = ctx->create_data(type, data, length);
  1052. if (!v)
  1053. return JSON_ERROR_CALLBACK;
  1054. if (ctx->append(stack->val, stack->key, stack->key_length, v))
  1055. return JSON_ERROR_CALLBACK;
  1056. free(stack->key);
  1057. break;
  1058. }
  1059. return 0;
  1060. }
  1061. /* end. */