… | |
… | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
14 | #endif |
15 | |
15 | |
16 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
20 | #endif |
20 | #endif |
21 | |
21 | |
|
|
22 | // three extra for rounding, sign, and end of string |
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
23 | |
24 | |
24 | #define F_ASCII 0x00000001UL |
25 | #define F_ASCII 0x00000001UL |
25 | #define F_LATIN1 0x00000002UL |
26 | #define F_LATIN1 0x00000002UL |
26 | #define F_UTF8 0x00000004UL |
27 | #define F_UTF8 0x00000004UL |
27 | #define F_INDENT 0x00000008UL |
28 | #define F_INDENT 0x00000008UL |
… | |
… | |
41 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
43 | |
44 | |
44 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
45 | |
46 | |
|
|
47 | #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
|
|
48 | |
46 | #define SB do { |
49 | #define SB do { |
47 | #define SE } while (0) |
50 | #define SE } while (0) |
48 | |
51 | |
49 | #if __GNUC__ >= 3 |
52 | #if __GNUC__ >= 3 |
50 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
53 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
… | |
… | |
76 | |
79 | |
77 | enum { |
80 | enum { |
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
81 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_STR, // inside string |
82 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
83 | INCR_M_BS, // inside backslash |
|
|
84 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
85 | INCR_M_C1, // inside comment in other places |
81 | INCR_M_JSON // outside anything, count nesting |
86 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
87 | }; |
83 | |
88 | |
84 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
89 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
90 | |
86 | typedef struct { |
91 | typedef struct { |
87 | U32 flags; |
92 | U32 flags; |
88 | U32 max_depth; |
93 | U32 max_depth; |
89 | STRLEN max_size; |
94 | STRLEN max_size; |
… | |
… | |
92 | HV *cb_sk_object; |
97 | HV *cb_sk_object; |
93 | |
98 | |
94 | // for the incremental parser |
99 | // for the incremental parser |
95 | SV *incr_text; // the source text so far |
100 | SV *incr_text; // the source text so far |
96 | STRLEN incr_pos; // the current offset into the text |
101 | STRLEN incr_pos; // the current offset into the text |
97 | unsigned char incr_nest; // {[]}-nesting level |
102 | int incr_nest; // {[]}-nesting level |
98 | unsigned char incr_mode; |
103 | unsigned char incr_mode; |
99 | } JSON; |
104 | } JSON; |
100 | |
105 | |
101 | INLINE void |
106 | INLINE void |
102 | json_init (JSON *json) |
107 | json_init (JSON *json) |
… | |
… | |
175 | *s++ = 0x80 | ( ch & 0x3f); |
180 | *s++ = 0x80 | ( ch & 0x3f); |
176 | |
181 | |
177 | return s; |
182 | return s; |
178 | } |
183 | } |
179 | |
184 | |
|
|
185 | // convert offset pointer to character index, sv must be string |
|
|
186 | static STRLEN |
|
|
187 | ptr_to_index (SV *sv, char *offset) |
|
|
188 | { |
|
|
189 | return SvUTF8 (sv) |
|
|
190 | ? utf8_distance (offset, SvPVX (sv)) |
|
|
191 | : offset - SvPVX (sv); |
|
|
192 | } |
|
|
193 | |
|
|
194 | ///////////////////////////////////////////////////////////////////////////// |
|
|
195 | // fp hell |
|
|
196 | |
|
|
197 | // scan a group of digits, and a trailing exponent |
|
|
198 | static void |
|
|
199 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
|
|
200 | { |
|
|
201 | UV uaccum = 0; |
|
|
202 | int eaccum = 0; |
|
|
203 | |
|
|
204 | // if we recurse too deep, skip all remaining digits |
|
|
205 | // to avoid a stack overflow attack |
|
|
206 | if (expect_false (--maxdepth <= 0)) |
|
|
207 | while (((U8)*s - '0') < 10) |
|
|
208 | ++s; |
|
|
209 | |
|
|
210 | for (;;) |
|
|
211 | { |
|
|
212 | U8 dig = (U8)*s - '0'; |
|
|
213 | |
|
|
214 | if (expect_false (dig >= 10)) |
|
|
215 | { |
|
|
216 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
217 | { |
|
|
218 | ++s; |
|
|
219 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
|
|
220 | } |
|
|
221 | else if ((dig | ' ') == 'e' - '0') |
|
|
222 | { |
|
|
223 | int exp2 = 0; |
|
|
224 | int neg = 0; |
|
|
225 | |
|
|
226 | ++s; |
|
|
227 | |
|
|
228 | if (*s == '-') |
|
|
229 | { |
|
|
230 | ++s; |
|
|
231 | neg = 1; |
|
|
232 | } |
|
|
233 | else if (*s == '+') |
|
|
234 | ++s; |
|
|
235 | |
|
|
236 | while ((dig = (U8)*s - '0') < 10) |
|
|
237 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
238 | |
|
|
239 | *expo += neg ? -exp2 : exp2; |
|
|
240 | } |
|
|
241 | |
|
|
242 | break; |
|
|
243 | } |
|
|
244 | |
|
|
245 | ++s; |
|
|
246 | |
|
|
247 | uaccum = uaccum * 10 + dig; |
|
|
248 | ++eaccum; |
|
|
249 | |
|
|
250 | // if we have too many digits, then recurse for more |
|
|
251 | // we actually do this for rather few digits |
|
|
252 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
253 | { |
|
|
254 | if (postdp) *expo -= eaccum; |
|
|
255 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
|
|
256 | if (postdp) *expo += eaccum; |
|
|
257 | |
|
|
258 | break; |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
|
|
262 | // this relies greatly on the quality of the pow () |
|
|
263 | // implementation of the platform, but a good |
|
|
264 | // implementation is hard to beat. |
|
|
265 | if (postdp) *expo -= eaccum; |
|
|
266 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
267 | *expo += eaccum; |
|
|
268 | } |
|
|
269 | |
|
|
270 | static NV |
|
|
271 | json_atof (const char *s) |
|
|
272 | { |
|
|
273 | NV accum = 0.; |
|
|
274 | int expo = 0; |
|
|
275 | int neg = 0; |
|
|
276 | |
|
|
277 | if (*s == '-') |
|
|
278 | { |
|
|
279 | ++s; |
|
|
280 | neg = 1; |
|
|
281 | } |
|
|
282 | |
|
|
283 | // a recursion depth of ten gives us >>500 bits |
|
|
284 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
285 | |
|
|
286 | return neg ? -accum : accum; |
|
|
287 | } |
180 | ///////////////////////////////////////////////////////////////////////////// |
288 | ///////////////////////////////////////////////////////////////////////////// |
181 | // encoder |
289 | // encoder |
182 | |
290 | |
183 | // structure used for encoding JSON |
291 | // structure used for encoding JSON |
184 | typedef struct |
292 | typedef struct |
… | |
… | |
194 | INLINE void |
302 | INLINE void |
195 | need (enc_t *enc, STRLEN len) |
303 | need (enc_t *enc, STRLEN len) |
196 | { |
304 | { |
197 | if (expect_false (enc->cur + len >= enc->end)) |
305 | if (expect_false (enc->cur + len >= enc->end)) |
198 | { |
306 | { |
199 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
307 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
200 | SvGROW (enc->sv, cur + len + 1); |
308 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
201 | enc->cur = SvPVX (enc->sv) + cur; |
309 | enc->cur = SvPVX (enc->sv) + cur; |
202 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
310 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
203 | } |
311 | } |
204 | } |
312 | } |
205 | |
313 | |
… | |
… | |
280 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
388 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
281 | enc->cur += 12; |
389 | enc->cur += 12; |
282 | } |
390 | } |
283 | else |
391 | else |
284 | { |
392 | { |
285 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
286 | need (enc, len += 5); |
393 | need (enc, len += 5); |
287 | *enc->cur++ = '\\'; |
394 | *enc->cur++ = '\\'; |
288 | *enc->cur++ = 'u'; |
395 | *enc->cur++ = 'u'; |
289 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
396 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
290 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
397 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
291 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
398 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
292 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
399 | *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
293 | } |
400 | } |
294 | |
401 | |
295 | str += clen; |
402 | str += clen; |
296 | } |
403 | } |
297 | else if (enc->json.flags & F_LATIN1) |
404 | else if (enc->json.flags & F_LATIN1) |
… | |
… | |
461 | |
568 | |
462 | encode_ch (enc, '{'); |
569 | encode_ch (enc, '{'); |
463 | |
570 | |
464 | // for canonical output we have to sort by keys first |
571 | // for canonical output we have to sort by keys first |
465 | // actually, this is mostly due to the stupid so-called |
572 | // actually, this is mostly due to the stupid so-called |
466 | // security workaround added somewhere in 5.8.x. |
573 | // security workaround added somewhere in 5.8.x |
467 | // that randomises hash orderings |
574 | // that randomises hash orderings |
468 | if (enc->json.flags & F_CANONICAL) |
575 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
469 | { |
576 | { |
470 | int count = hv_iterinit (hv); |
577 | int count = hv_iterinit (hv); |
471 | |
578 | |
472 | if (SvMAGICAL (hv)) |
579 | if (SvMAGICAL (hv)) |
473 | { |
580 | { |
… | |
… | |
751 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
858 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
752 | : 0x110000UL; |
859 | : 0x110000UL; |
753 | |
860 | |
754 | SvPOK_only (enc.sv); |
861 | SvPOK_only (enc.sv); |
755 | encode_sv (&enc, scalar); |
862 | encode_sv (&enc, scalar); |
|
|
863 | encode_nl (&enc); |
756 | |
864 | |
757 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
865 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
758 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
866 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
759 | |
867 | |
760 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
868 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
940 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1048 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
941 | *cur++ = ch; |
1049 | *cur++ = ch; |
942 | else if (ch >= 0x80) |
1050 | else if (ch >= 0x80) |
943 | { |
1051 | { |
944 | STRLEN clen; |
1052 | STRLEN clen; |
945 | UV uch; |
|
|
946 | |
1053 | |
947 | --dec_cur; |
1054 | --dec_cur; |
948 | |
1055 | |
949 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1056 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
950 | if (clen == (STRLEN)-1) |
1057 | if (clen == (STRLEN)-1) |
951 | ERR ("malformed UTF-8 character in JSON string"); |
1058 | ERR ("malformed UTF-8 character in JSON string"); |
952 | |
1059 | |
953 | do |
1060 | do |
954 | *cur++ = *dec_cur++; |
1061 | *cur++ = *dec_cur++; |
… | |
… | |
971 | { |
1078 | { |
972 | STRLEN len = cur - buf; |
1079 | STRLEN len = cur - buf; |
973 | |
1080 | |
974 | if (sv) |
1081 | if (sv) |
975 | { |
1082 | { |
976 | SvGROW (sv, SvCUR (sv) + len + 1); |
1083 | STRLEN cur = SvCUR (sv); |
|
|
1084 | |
|
|
1085 | if (SvLEN (sv) <= cur + len) |
|
|
1086 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1087 | |
977 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1088 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
978 | SvCUR_set (sv, SvCUR (sv) + len); |
1089 | SvCUR_set (sv, SvCUR (sv) + len); |
979 | } |
1090 | } |
980 | else |
1091 | else |
981 | sv = newSVpvn (buf, len); |
1092 | sv = newSVpvn (buf, len); |
… | |
… | |
1072 | |
1183 | |
1073 | // special case the rather common 1..5-digit-int case |
1184 | // special case the rather common 1..5-digit-int case |
1074 | if (*start == '-') |
1185 | if (*start == '-') |
1075 | switch (len) |
1186 | switch (len) |
1076 | { |
1187 | { |
1077 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1188 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1078 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1189 | case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
1079 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1190 | case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1080 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1191 | case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1081 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1192 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1082 | } |
1193 | } |
1083 | else |
1194 | else |
1084 | switch (len) |
1195 | switch (len) |
1085 | { |
1196 | { |
1086 | case 1: return newSViv ( start [0] - '0' * 1); |
1197 | case 1: return newSViv ( start [0] - '0' * 1); |
1087 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1198 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1088 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1199 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1089 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1200 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1090 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1201 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1091 | } |
1202 | } |
1092 | |
1203 | |
1093 | { |
1204 | { |
1094 | UV uv; |
1205 | UV uv; |
1095 | int numtype = grok_number (start, len, &uv); |
1206 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1104 | } |
1215 | } |
1105 | |
1216 | |
1106 | len -= *start == '-' ? 1 : 0; |
1217 | len -= *start == '-' ? 1 : 0; |
1107 | |
1218 | |
1108 | // does not fit into IV or UV, try NV |
1219 | // does not fit into IV or UV, try NV |
1109 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1220 | if (len <= NV_DIG) |
1110 | #if defined (LDBL_DIG) |
|
|
1111 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1112 | #endif |
|
|
1113 | ) |
|
|
1114 | // fits into NV without loss of precision |
1221 | // fits into NV without loss of precision |
1115 | return newSVnv (Atof (start)); |
1222 | return newSVnv (json_atof (start)); |
1116 | |
1223 | |
1117 | // everything else fails, convert it to a string |
1224 | // everything else fails, convert it to a string |
1118 | return newSVpvn (start, dec->cur - start); |
1225 | return newSVpvn (start, dec->cur - start); |
1119 | } |
1226 | } |
1120 | |
1227 | |
1121 | // loss of precision here |
1228 | // loss of precision here |
1122 | return newSVnv (Atof (start)); |
1229 | return newSVnv (json_atof (start)); |
1123 | |
1230 | |
1124 | fail: |
1231 | fail: |
1125 | return 0; |
1232 | return 0; |
1126 | } |
1233 | } |
1127 | |
1234 | |
… | |
… | |
1296 | dSP; |
1403 | dSP; |
1297 | int count; |
1404 | int count; |
1298 | |
1405 | |
1299 | ENTER; SAVETMPS; PUSHMARK (SP); |
1406 | ENTER; SAVETMPS; PUSHMARK (SP); |
1300 | XPUSHs (HeVAL (he)); |
1407 | XPUSHs (HeVAL (he)); |
|
|
1408 | sv_2mortal (sv); |
1301 | |
1409 | |
1302 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1410 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1303 | |
1411 | |
1304 | if (count == 1) |
1412 | if (count == 1) |
1305 | { |
1413 | { |
1306 | sv = newSVsv (POPs); |
1414 | sv = newSVsv (POPs); |
1307 | FREETMPS; LEAVE; |
1415 | FREETMPS; LEAVE; |
1308 | return sv; |
1416 | return sv; |
1309 | } |
1417 | } |
1310 | |
1418 | |
|
|
1419 | SvREFCNT_inc (sv); |
1311 | FREETMPS; LEAVE; |
1420 | FREETMPS; LEAVE; |
1312 | } |
1421 | } |
1313 | } |
1422 | } |
1314 | |
1423 | |
1315 | if (dec->json.cb_object) |
1424 | if (dec->json.cb_object) |
… | |
… | |
1405 | fail: |
1514 | fail: |
1406 | return 0; |
1515 | return 0; |
1407 | } |
1516 | } |
1408 | |
1517 | |
1409 | static SV * |
1518 | static SV * |
1410 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1519 | decode_json (SV *string, JSON *json, char **offset_return) |
1411 | { |
1520 | { |
1412 | dec_t dec; |
1521 | dec_t dec; |
1413 | STRLEN offset; |
|
|
1414 | SV *sv; |
1522 | SV *sv; |
1415 | |
1523 | |
|
|
1524 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1525 | * will perl ignore the magic in subsequent accesses |
|
|
1526 | */ |
1416 | SvGETMAGIC (string); |
1527 | /*SvGETMAGIC (string);*/ |
|
|
1528 | if (SvMAGICAL (string)) |
|
|
1529 | string = sv_2mortal (newSVsv (string)); |
|
|
1530 | |
1417 | SvUPGRADE (string, SVt_PV); |
1531 | SvUPGRADE (string, SVt_PV); |
1418 | |
1532 | |
1419 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1533 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1420 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1534 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1421 | * return the xpv_cur field which certainly exists after upgrading. |
1535 | * return the xpv_cur field which certainly exists after upgrading. |
1422 | * according to nicholas clark, calling SvPOK fixes this. |
1536 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1537 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1538 | * and hope for the best. |
|
|
1539 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
1540 | * assertion business is seriously broken, try yet another workaround |
|
|
1541 | * for the broken -DDEBUGGING. |
1423 | */ |
1542 | */ |
1424 | SvPOK (string); |
1543 | { |
|
|
1544 | #ifdef DEBUGGING |
|
|
1545 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
1546 | #else |
|
|
1547 | STRLEN offset = SvCUR (string); |
|
|
1548 | #endif |
1425 | |
1549 | |
1426 | if (SvCUR (string) > json->max_size && json->max_size) |
1550 | if (offset > json->max_size && json->max_size) |
1427 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1551 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1428 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1552 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1553 | } |
1429 | |
1554 | |
1430 | if (json->flags & F_UTF8) |
1555 | if (DECODE_WANTS_OCTETS (json)) |
1431 | sv_utf8_downgrade (string, 0); |
1556 | sv_utf8_downgrade (string, 0); |
1432 | else |
1557 | else |
1433 | sv_utf8_upgrade (string); |
1558 | sv_utf8_upgrade (string); |
1434 | |
1559 | |
1435 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1560 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1445 | |
1570 | |
1446 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1571 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1447 | |
1572 | |
1448 | decode_ws (&dec); |
1573 | decode_ws (&dec); |
1449 | sv = decode_sv (&dec); |
1574 | sv = decode_sv (&dec); |
|
|
1575 | |
|
|
1576 | if (offset_return) |
|
|
1577 | *offset_return = dec.cur; |
1450 | |
1578 | |
1451 | if (!(offset_return || !sv)) |
1579 | if (!(offset_return || !sv)) |
1452 | { |
1580 | { |
1453 | // check for trailing garbage |
1581 | // check for trailing garbage |
1454 | decode_ws (&dec); |
1582 | decode_ws (&dec); |
… | |
… | |
1457 | { |
1585 | { |
1458 | dec.err = "garbage after JSON object"; |
1586 | dec.err = "garbage after JSON object"; |
1459 | SvREFCNT_dec (sv); |
1587 | SvREFCNT_dec (sv); |
1460 | sv = 0; |
1588 | sv = 0; |
1461 | } |
1589 | } |
1462 | } |
|
|
1463 | |
|
|
1464 | if (offset_return || !sv) |
|
|
1465 | { |
|
|
1466 | offset = dec.json.flags & F_UTF8 |
|
|
1467 | ? dec.cur - SvPVX (string) |
|
|
1468 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1469 | |
|
|
1470 | if (offset_return) |
|
|
1471 | *offset_return = offset; |
|
|
1472 | } |
1590 | } |
1473 | |
1591 | |
1474 | if (!sv) |
1592 | if (!sv) |
1475 | { |
1593 | { |
1476 | SV *uni = sv_newmortal (); |
1594 | SV *uni = sv_newmortal (); |
… | |
… | |
1482 | SAVEVPTR (PL_curcop); |
1600 | SAVEVPTR (PL_curcop); |
1483 | PL_curcop = &cop; |
1601 | PL_curcop = &cop; |
1484 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1602 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1485 | LEAVE; |
1603 | LEAVE; |
1486 | |
1604 | |
1487 | croak ("%s, at character offset %d [\"%s\"]", |
1605 | croak ("%s, at character offset %d (before \"%s\")", |
1488 | dec.err, |
1606 | dec.err, |
1489 | (int)offset, |
1607 | ptr_to_index (string, dec.cur), |
1490 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1608 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1491 | } |
1609 | } |
1492 | |
1610 | |
1493 | sv = sv_2mortal (sv); |
1611 | sv = sv_2mortal (sv); |
1494 | |
1612 | |
… | |
… | |
1503 | |
1621 | |
1504 | static void |
1622 | static void |
1505 | incr_parse (JSON *self) |
1623 | incr_parse (JSON *self) |
1506 | { |
1624 | { |
1507 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1625 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1626 | |
|
|
1627 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1628 | // but this would make it slower, so... |
1508 | |
1629 | |
1509 | for (;;) |
1630 | for (;;) |
1510 | { |
1631 | { |
1511 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1632 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1512 | switch (self->incr_mode) |
1633 | switch (self->incr_mode) |
1513 | { |
1634 | { |
1514 | // only used for intiial whitespace skipping |
1635 | // only used for initial whitespace skipping |
1515 | case INCR_M_WS: |
1636 | case INCR_M_WS: |
1516 | for (;;) |
1637 | for (;;) |
1517 | { |
1638 | { |
1518 | if (*p > 0x20) |
1639 | if (*p > 0x20) |
1519 | { |
1640 | { |
|
|
1641 | if (*p == '#') |
|
|
1642 | { |
|
|
1643 | self->incr_mode = INCR_M_C0; |
|
|
1644 | goto incr_m_c; |
|
|
1645 | } |
|
|
1646 | else |
|
|
1647 | { |
1520 | self->incr_mode = INCR_M_JSON; |
1648 | self->incr_mode = INCR_M_JSON; |
1521 | goto incr_m_json; |
1649 | goto incr_m_json; |
|
|
1650 | } |
1522 | } |
1651 | } |
1523 | else if (!*p) |
1652 | else if (!*p) |
1524 | goto interrupt; |
1653 | goto interrupt; |
1525 | |
1654 | |
1526 | ++p; |
1655 | ++p; |
… | |
… | |
1532 | goto interrupt; |
1661 | goto interrupt; |
1533 | |
1662 | |
1534 | ++p; |
1663 | ++p; |
1535 | self->incr_mode = INCR_M_STR; |
1664 | self->incr_mode = INCR_M_STR; |
1536 | goto incr_m_str; |
1665 | goto incr_m_str; |
|
|
1666 | |
|
|
1667 | // inside #-style comments |
|
|
1668 | case INCR_M_C0: |
|
|
1669 | case INCR_M_C1: |
|
|
1670 | incr_m_c: |
|
|
1671 | for (;;) |
|
|
1672 | { |
|
|
1673 | if (*p == '\n') |
|
|
1674 | { |
|
|
1675 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1676 | break; |
|
|
1677 | } |
|
|
1678 | else if (!*p) |
|
|
1679 | goto interrupt; |
|
|
1680 | |
|
|
1681 | ++p; |
|
|
1682 | } |
|
|
1683 | |
|
|
1684 | break; |
1537 | |
1685 | |
1538 | // inside a string |
1686 | // inside a string |
1539 | case INCR_M_STR: |
1687 | case INCR_M_STR: |
1540 | incr_m_str: |
1688 | incr_m_str: |
1541 | for (;;) |
1689 | for (;;) |
… | |
… | |
1598 | croak (ERR_NESTING_EXCEEDED); |
1746 | croak (ERR_NESTING_EXCEEDED); |
1599 | break; |
1747 | break; |
1600 | |
1748 | |
1601 | case ']': |
1749 | case ']': |
1602 | case '}': |
1750 | case '}': |
1603 | if (!--self->incr_nest) |
1751 | if (--self->incr_nest <= 0) |
1604 | goto interrupt; |
1752 | goto interrupt; |
|
|
1753 | break; |
|
|
1754 | |
|
|
1755 | case '#': |
|
|
1756 | self->incr_mode = INCR_M_C1; |
|
|
1757 | goto incr_m_c; |
1605 | } |
1758 | } |
1606 | } |
1759 | } |
1607 | } |
1760 | } |
1608 | |
1761 | |
1609 | modechange: |
1762 | modechange: |
1610 | ; |
1763 | ; |
1611 | } |
1764 | } |
1612 | |
1765 | |
1613 | interrupt: |
1766 | interrupt: |
1614 | self->incr_pos = p - SvPVX (self->incr_text); |
1767 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1768 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1615 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1769 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1616 | } |
1770 | } |
1617 | |
1771 | |
1618 | ///////////////////////////////////////////////////////////////////////////// |
1772 | ///////////////////////////////////////////////////////////////////////////// |
1619 | // XS interface functions |
1773 | // XS interface functions |
… | |
… | |
1634 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1788 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1635 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1789 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1636 | |
1790 | |
1637 | json_true = get_bool ("JSON::XS::true"); |
1791 | json_true = get_bool ("JSON::XS::true"); |
1638 | json_false = get_bool ("JSON::XS::false"); |
1792 | json_false = get_bool ("JSON::XS::false"); |
|
|
1793 | |
|
|
1794 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1639 | } |
1795 | } |
1640 | |
1796 | |
1641 | PROTOTYPES: DISABLE |
1797 | PROTOTYPES: DISABLE |
1642 | |
1798 | |
1643 | void CLONE (...) |
1799 | void CLONE (...) |
… | |
… | |
1763 | XPUSHs (decode_json (jsonstr, self, 0)); |
1919 | XPUSHs (decode_json (jsonstr, self, 0)); |
1764 | |
1920 | |
1765 | void decode_prefix (JSON *self, SV *jsonstr) |
1921 | void decode_prefix (JSON *self, SV *jsonstr) |
1766 | PPCODE: |
1922 | PPCODE: |
1767 | { |
1923 | { |
1768 | STRLEN offset; |
1924 | char *offset; |
1769 | EXTEND (SP, 2); |
1925 | EXTEND (SP, 2); |
1770 | PUSHs (decode_json (jsonstr, self, &offset)); |
1926 | PUSHs (decode_json (jsonstr, self, &offset)); |
1771 | PUSHs (sv_2mortal (newSVuv (offset))); |
1927 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1772 | } |
1928 | } |
1773 | |
1929 | |
1774 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1930 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1775 | PPCODE: |
1931 | PPCODE: |
1776 | { |
1932 | { |
1777 | if (!self->incr_text) |
1933 | if (!self->incr_text) |
1778 | self->incr_text = newSVpvn ("", 0); |
1934 | self->incr_text = newSVpvn ("", 0); |
|
|
1935 | |
|
|
1936 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
1937 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
1938 | if (DECODE_WANTS_OCTETS (self)) |
|
|
1939 | { |
|
|
1940 | if (self->incr_pos) |
|
|
1941 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
1942 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
1943 | |
|
|
1944 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
1945 | } |
|
|
1946 | else |
|
|
1947 | { |
|
|
1948 | sv_utf8_upgrade (self->incr_text); |
|
|
1949 | |
|
|
1950 | if (self->incr_pos) |
|
|
1951 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1952 | - (U8 *)SvPVX (self->incr_text); |
|
|
1953 | } |
1779 | |
1954 | |
1780 | // append data, if any |
1955 | // append data, if any |
1781 | if (jsonstr) |
1956 | if (jsonstr) |
1782 | { |
1957 | { |
|
|
1958 | /* make sure both strings have same encoding */ |
1783 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1959 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
|
|
1960 | if (SvUTF8 (jsonstr)) |
|
|
1961 | sv_utf8_downgrade (jsonstr, 0); |
1784 | { |
1962 | else |
1785 | /* utf-8-ness differs, need to upgrade */ |
|
|
1786 | sv_utf8_upgrade (self->incr_text); |
1963 | sv_utf8_upgrade (jsonstr); |
1787 | |
1964 | |
1788 | if (self->incr_pos) |
1965 | /* and then just blindly append */ |
1789 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1790 | - (U8 *)SvPVX (self->incr_text); |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | { |
1966 | { |
1794 | STRLEN len; |
1967 | STRLEN len; |
1795 | const char *str = SvPV (jsonstr, len); |
1968 | const char *str = SvPV (jsonstr, len); |
1796 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
1969 | STRLEN cur = SvCUR (self->incr_text); |
|
|
1970 | |
|
|
1971 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
1972 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1973 | |
1797 | Move (str, SvEND (self->incr_text), len, char); |
1974 | Move (str, SvEND (self->incr_text), len, char); |
1798 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1975 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1799 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1976 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1800 | } |
1977 | } |
1801 | } |
1978 | } |
1802 | |
1979 | |
1803 | if (GIMME_V != G_VOID) |
1980 | if (GIMME_V != G_VOID) |
1804 | do |
1981 | do |
1805 | { |
1982 | { |
1806 | STRLEN offset; |
1983 | char *offset; |
1807 | |
1984 | |
1808 | if (!INCR_DONE (self)) |
1985 | if (!INCR_DONE (self)) |
1809 | { |
1986 | { |
1810 | incr_parse (self); |
1987 | incr_parse (self); |
1811 | |
1988 | |
… | |
… | |
1817 | break; |
1994 | break; |
1818 | } |
1995 | } |
1819 | |
1996 | |
1820 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1997 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1821 | |
1998 | |
1822 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1823 | self->incr_pos -= offset; |
1999 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1824 | self->incr_nest = 0; |
2000 | self->incr_nest = 0; |
1825 | self->incr_mode = 0; |
2001 | self->incr_mode = 0; |
|
|
2002 | |
|
|
2003 | sv_chop (self->incr_text, offset); |
1826 | } |
2004 | } |
1827 | while (GIMME_V == G_ARRAY); |
2005 | while (GIMME_V == G_ARRAY); |
1828 | } |
2006 | } |
1829 | |
2007 | |
1830 | SV *incr_text (JSON *self) |
2008 | SV *incr_text (JSON *self) |
… | |
… | |
1891 | json_init (&json); |
2069 | json_init (&json); |
1892 | json.flags |= ix; |
2070 | json.flags |= ix; |
1893 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2071 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1894 | } |
2072 | } |
1895 | |
2073 | |
1896 | |
|
|