… | |
… | |
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 <= 0 && (json)->incr_mode == INCR_M_JSON) |
89 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
90 | |
… | |
… | |
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 | // (IEEE 754 conformant ones are required to be exact) |
|
|
266 | if (postdp) *expo -= eaccum; |
|
|
267 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
268 | *expo += eaccum; |
|
|
269 | } |
|
|
270 | |
|
|
271 | static NV |
|
|
272 | json_atof (const char *s) |
|
|
273 | { |
|
|
274 | NV accum = 0.; |
|
|
275 | int expo = 0; |
|
|
276 | int neg = 0; |
|
|
277 | |
|
|
278 | if (*s == '-') |
|
|
279 | { |
|
|
280 | ++s; |
|
|
281 | neg = 1; |
|
|
282 | } |
|
|
283 | |
|
|
284 | // a recursion depth of ten gives us >>500 bits |
|
|
285 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
286 | |
|
|
287 | return neg ? -accum : accum; |
|
|
288 | } |
180 | ///////////////////////////////////////////////////////////////////////////// |
289 | ///////////////////////////////////////////////////////////////////////////// |
181 | // encoder |
290 | // encoder |
182 | |
291 | |
183 | // structure used for encoding JSON |
292 | // structure used for encoding JSON |
184 | typedef struct |
293 | typedef struct |
… | |
… | |
194 | INLINE void |
303 | INLINE void |
195 | need (enc_t *enc, STRLEN len) |
304 | need (enc_t *enc, STRLEN len) |
196 | { |
305 | { |
197 | if (expect_false (enc->cur + len >= enc->end)) |
306 | if (expect_false (enc->cur + len >= enc->end)) |
198 | { |
307 | { |
199 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
308 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
200 | SvGROW (enc->sv, cur + len + 1); |
309 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
201 | enc->cur = SvPVX (enc->sv) + cur; |
310 | enc->cur = SvPVX (enc->sv) + cur; |
202 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
311 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
203 | } |
312 | } |
204 | } |
313 | } |
205 | |
314 | |
… | |
… | |
462 | |
571 | |
463 | // for canonical output we have to sort by keys first |
572 | // for canonical output we have to sort by keys first |
464 | // actually, this is mostly due to the stupid so-called |
573 | // actually, this is mostly due to the stupid so-called |
465 | // security workaround added somewhere in 5.8.x |
574 | // security workaround added somewhere in 5.8.x |
466 | // that randomises hash orderings |
575 | // that randomises hash orderings |
467 | if (enc->json.flags & F_CANONICAL) |
576 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
468 | { |
577 | { |
469 | int count = hv_iterinit (hv); |
578 | int count = hv_iterinit (hv); |
470 | |
579 | |
471 | if (SvMAGICAL (hv)) |
580 | if (SvMAGICAL (hv)) |
472 | { |
581 | { |
… | |
… | |
750 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
859 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
751 | : 0x110000UL; |
860 | : 0x110000UL; |
752 | |
861 | |
753 | SvPOK_only (enc.sv); |
862 | SvPOK_only (enc.sv); |
754 | encode_sv (&enc, scalar); |
863 | encode_sv (&enc, scalar); |
|
|
864 | encode_nl (&enc); |
755 | |
865 | |
756 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
866 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
757 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
867 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
758 | |
868 | |
759 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
869 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
939 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1049 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
940 | *cur++ = ch; |
1050 | *cur++ = ch; |
941 | else if (ch >= 0x80) |
1051 | else if (ch >= 0x80) |
942 | { |
1052 | { |
943 | STRLEN clen; |
1053 | STRLEN clen; |
944 | UV uch; |
|
|
945 | |
1054 | |
946 | --dec_cur; |
1055 | --dec_cur; |
947 | |
1056 | |
948 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1057 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
949 | if (clen == (STRLEN)-1) |
1058 | if (clen == (STRLEN)-1) |
950 | ERR ("malformed UTF-8 character in JSON string"); |
1059 | ERR ("malformed UTF-8 character in JSON string"); |
951 | |
1060 | |
952 | do |
1061 | do |
953 | *cur++ = *dec_cur++; |
1062 | *cur++ = *dec_cur++; |
… | |
… | |
970 | { |
1079 | { |
971 | STRLEN len = cur - buf; |
1080 | STRLEN len = cur - buf; |
972 | |
1081 | |
973 | if (sv) |
1082 | if (sv) |
974 | { |
1083 | { |
975 | SvGROW (sv, SvCUR (sv) + len + 1); |
1084 | STRLEN cur = SvCUR (sv); |
|
|
1085 | |
|
|
1086 | if (SvLEN (sv) <= cur + len) |
|
|
1087 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1088 | |
976 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1089 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
977 | SvCUR_set (sv, SvCUR (sv) + len); |
1090 | SvCUR_set (sv, SvCUR (sv) + len); |
978 | } |
1091 | } |
979 | else |
1092 | else |
980 | sv = newSVpvn (buf, len); |
1093 | sv = newSVpvn (buf, len); |
… | |
… | |
1071 | |
1184 | |
1072 | // special case the rather common 1..5-digit-int case |
1185 | // special case the rather common 1..5-digit-int case |
1073 | if (*start == '-') |
1186 | if (*start == '-') |
1074 | switch (len) |
1187 | switch (len) |
1075 | { |
1188 | { |
1076 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1189 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1077 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1190 | case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
1078 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1191 | case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1079 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1192 | case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1080 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1193 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1081 | } |
1194 | } |
1082 | else |
1195 | else |
1083 | switch (len) |
1196 | switch (len) |
1084 | { |
1197 | { |
1085 | case 1: return newSViv ( start [0] - '0' * 1); |
1198 | case 1: return newSViv ( start [0] - '0' * 1); |
1086 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1199 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1087 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1200 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1088 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1201 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1089 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1202 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1090 | } |
1203 | } |
1091 | |
1204 | |
1092 | { |
1205 | { |
1093 | UV uv; |
1206 | UV uv; |
1094 | int numtype = grok_number (start, len, &uv); |
1207 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1103 | } |
1216 | } |
1104 | |
1217 | |
1105 | len -= *start == '-' ? 1 : 0; |
1218 | len -= *start == '-' ? 1 : 0; |
1106 | |
1219 | |
1107 | // does not fit into IV or UV, try NV |
1220 | // does not fit into IV or UV, try NV |
1108 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1221 | if (len <= NV_DIG) |
1109 | #if defined (LDBL_DIG) |
|
|
1110 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1111 | #endif |
|
|
1112 | ) |
|
|
1113 | // fits into NV without loss of precision |
1222 | // fits into NV without loss of precision |
1114 | return newSVnv (Atof (start)); |
1223 | return newSVnv (json_atof (start)); |
1115 | |
1224 | |
1116 | // everything else fails, convert it to a string |
1225 | // everything else fails, convert it to a string |
1117 | return newSVpvn (start, dec->cur - start); |
1226 | return newSVpvn (start, dec->cur - start); |
1118 | } |
1227 | } |
1119 | |
1228 | |
1120 | // loss of precision here |
1229 | // loss of precision here |
1121 | return newSVnv (Atof (start)); |
1230 | return newSVnv (json_atof (start)); |
1122 | |
1231 | |
1123 | fail: |
1232 | fail: |
1124 | return 0; |
1233 | return 0; |
1125 | } |
1234 | } |
1126 | |
1235 | |
… | |
… | |
1295 | dSP; |
1404 | dSP; |
1296 | int count; |
1405 | int count; |
1297 | |
1406 | |
1298 | ENTER; SAVETMPS; PUSHMARK (SP); |
1407 | ENTER; SAVETMPS; PUSHMARK (SP); |
1299 | XPUSHs (HeVAL (he)); |
1408 | XPUSHs (HeVAL (he)); |
|
|
1409 | sv_2mortal (sv); |
1300 | |
1410 | |
1301 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1411 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1302 | |
1412 | |
1303 | if (count == 1) |
1413 | if (count == 1) |
1304 | { |
1414 | { |
1305 | sv = newSVsv (POPs); |
1415 | sv = newSVsv (POPs); |
1306 | FREETMPS; LEAVE; |
1416 | FREETMPS; LEAVE; |
1307 | return sv; |
1417 | return sv; |
1308 | } |
1418 | } |
1309 | |
1419 | |
|
|
1420 | SvREFCNT_inc (sv); |
1310 | FREETMPS; LEAVE; |
1421 | FREETMPS; LEAVE; |
1311 | } |
1422 | } |
1312 | } |
1423 | } |
1313 | |
1424 | |
1314 | if (dec->json.cb_object) |
1425 | if (dec->json.cb_object) |
… | |
… | |
1404 | fail: |
1515 | fail: |
1405 | return 0; |
1516 | return 0; |
1406 | } |
1517 | } |
1407 | |
1518 | |
1408 | static SV * |
1519 | static SV * |
1409 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1520 | decode_json (SV *string, JSON *json, char **offset_return) |
1410 | { |
1521 | { |
1411 | dec_t dec; |
1522 | dec_t dec; |
1412 | STRLEN offset; |
|
|
1413 | SV *sv; |
1523 | SV *sv; |
1414 | |
1524 | |
|
|
1525 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1526 | * will perl ignore the magic in subsequent accesses. |
|
|
1527 | * also make a copy of non-PV values, to get them into a clean |
|
|
1528 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1529 | */ |
1415 | SvGETMAGIC (string); |
1530 | /*SvGETMAGIC (string);*/ |
|
|
1531 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
1532 | string = sv_2mortal (newSVsv (string)); |
|
|
1533 | |
1416 | SvUPGRADE (string, SVt_PV); |
1534 | SvUPGRADE (string, SVt_PV); |
1417 | |
1535 | |
1418 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1536 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1419 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1537 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1420 | * return the xpv_cur field which certainly exists after upgrading. |
1538 | * return the xpv_cur field which certainly exists after upgrading. |
… | |
… | |
1423 | * and hope for the best. |
1541 | * and hope for the best. |
1424 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
1542 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
1425 | * assertion business is seriously broken, try yet another workaround |
1543 | * assertion business is seriously broken, try yet another workaround |
1426 | * for the broken -DDEBUGGING. |
1544 | * for the broken -DDEBUGGING. |
1427 | */ |
1545 | */ |
|
|
1546 | { |
1428 | #ifdef DEBUGGING |
1547 | #ifdef DEBUGGING |
1429 | offset = SvOK (string) ? sv_len (string) : 0; |
1548 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
1430 | #else |
1549 | #else |
1431 | offset = SvCUR (string); |
1550 | STRLEN offset = SvCUR (string); |
1432 | #endif |
1551 | #endif |
1433 | |
1552 | |
1434 | if (offset > json->max_size && json->max_size) |
1553 | if (offset > json->max_size && json->max_size) |
1435 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1554 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1436 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1555 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1556 | } |
1437 | |
1557 | |
1438 | if (json->flags & F_UTF8) |
1558 | if (DECODE_WANTS_OCTETS (json)) |
1439 | sv_utf8_downgrade (string, 0); |
1559 | sv_utf8_downgrade (string, 0); |
1440 | else |
1560 | else |
1441 | sv_utf8_upgrade (string); |
1561 | sv_utf8_upgrade (string); |
1442 | |
1562 | |
1443 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1563 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1453 | |
1573 | |
1454 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1574 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1455 | |
1575 | |
1456 | decode_ws (&dec); |
1576 | decode_ws (&dec); |
1457 | sv = decode_sv (&dec); |
1577 | sv = decode_sv (&dec); |
|
|
1578 | |
|
|
1579 | if (offset_return) |
|
|
1580 | *offset_return = dec.cur; |
1458 | |
1581 | |
1459 | if (!(offset_return || !sv)) |
1582 | if (!(offset_return || !sv)) |
1460 | { |
1583 | { |
1461 | // check for trailing garbage |
1584 | // check for trailing garbage |
1462 | decode_ws (&dec); |
1585 | decode_ws (&dec); |
… | |
… | |
1465 | { |
1588 | { |
1466 | dec.err = "garbage after JSON object"; |
1589 | dec.err = "garbage after JSON object"; |
1467 | SvREFCNT_dec (sv); |
1590 | SvREFCNT_dec (sv); |
1468 | sv = 0; |
1591 | sv = 0; |
1469 | } |
1592 | } |
1470 | } |
|
|
1471 | |
|
|
1472 | if (offset_return || !sv) |
|
|
1473 | { |
|
|
1474 | offset = dec.json.flags & F_UTF8 |
|
|
1475 | ? dec.cur - SvPVX (string) |
|
|
1476 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1477 | |
|
|
1478 | if (offset_return) |
|
|
1479 | *offset_return = offset; |
|
|
1480 | } |
1593 | } |
1481 | |
1594 | |
1482 | if (!sv) |
1595 | if (!sv) |
1483 | { |
1596 | { |
1484 | SV *uni = sv_newmortal (); |
1597 | SV *uni = sv_newmortal (); |
… | |
… | |
1490 | SAVEVPTR (PL_curcop); |
1603 | SAVEVPTR (PL_curcop); |
1491 | PL_curcop = &cop; |
1604 | PL_curcop = &cop; |
1492 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1605 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1493 | LEAVE; |
1606 | LEAVE; |
1494 | |
1607 | |
1495 | croak ("%s, at character offset %d [\"%s\"]", |
1608 | croak ("%s, at character offset %d (before \"%s\")", |
1496 | dec.err, |
1609 | dec.err, |
1497 | (int)offset, |
1610 | ptr_to_index (string, dec.cur), |
1498 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1611 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1499 | } |
1612 | } |
1500 | |
1613 | |
1501 | sv = sv_2mortal (sv); |
1614 | sv = sv_2mortal (sv); |
1502 | |
1615 | |
… | |
… | |
1511 | |
1624 | |
1512 | static void |
1625 | static void |
1513 | incr_parse (JSON *self) |
1626 | incr_parse (JSON *self) |
1514 | { |
1627 | { |
1515 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1628 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1629 | |
|
|
1630 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1631 | // but this would make it slower, so... |
1516 | |
1632 | |
1517 | for (;;) |
1633 | for (;;) |
1518 | { |
1634 | { |
1519 | //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 |
1635 | //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 |
1520 | switch (self->incr_mode) |
1636 | switch (self->incr_mode) |
1521 | { |
1637 | { |
1522 | // only used for intiial whitespace skipping |
1638 | // only used for initial whitespace skipping |
1523 | case INCR_M_WS: |
1639 | case INCR_M_WS: |
1524 | for (;;) |
1640 | for (;;) |
1525 | { |
1641 | { |
1526 | if (*p > 0x20) |
1642 | if (*p > 0x20) |
1527 | { |
1643 | { |
|
|
1644 | if (*p == '#') |
|
|
1645 | { |
|
|
1646 | self->incr_mode = INCR_M_C0; |
|
|
1647 | goto incr_m_c; |
|
|
1648 | } |
|
|
1649 | else |
|
|
1650 | { |
1528 | self->incr_mode = INCR_M_JSON; |
1651 | self->incr_mode = INCR_M_JSON; |
1529 | goto incr_m_json; |
1652 | goto incr_m_json; |
|
|
1653 | } |
1530 | } |
1654 | } |
1531 | else if (!*p) |
1655 | else if (!*p) |
1532 | goto interrupt; |
1656 | goto interrupt; |
1533 | |
1657 | |
1534 | ++p; |
1658 | ++p; |
… | |
… | |
1540 | goto interrupt; |
1664 | goto interrupt; |
1541 | |
1665 | |
1542 | ++p; |
1666 | ++p; |
1543 | self->incr_mode = INCR_M_STR; |
1667 | self->incr_mode = INCR_M_STR; |
1544 | goto incr_m_str; |
1668 | goto incr_m_str; |
|
|
1669 | |
|
|
1670 | // inside #-style comments |
|
|
1671 | case INCR_M_C0: |
|
|
1672 | case INCR_M_C1: |
|
|
1673 | incr_m_c: |
|
|
1674 | for (;;) |
|
|
1675 | { |
|
|
1676 | if (*p == '\n') |
|
|
1677 | { |
|
|
1678 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1679 | break; |
|
|
1680 | } |
|
|
1681 | else if (!*p) |
|
|
1682 | goto interrupt; |
|
|
1683 | |
|
|
1684 | ++p; |
|
|
1685 | } |
|
|
1686 | |
|
|
1687 | break; |
1545 | |
1688 | |
1546 | // inside a string |
1689 | // inside a string |
1547 | case INCR_M_STR: |
1690 | case INCR_M_STR: |
1548 | incr_m_str: |
1691 | incr_m_str: |
1549 | for (;;) |
1692 | for (;;) |
… | |
… | |
1608 | |
1751 | |
1609 | case ']': |
1752 | case ']': |
1610 | case '}': |
1753 | case '}': |
1611 | if (--self->incr_nest <= 0) |
1754 | if (--self->incr_nest <= 0) |
1612 | goto interrupt; |
1755 | goto interrupt; |
|
|
1756 | break; |
|
|
1757 | |
|
|
1758 | case '#': |
|
|
1759 | self->incr_mode = INCR_M_C1; |
|
|
1760 | goto incr_m_c; |
1613 | } |
1761 | } |
1614 | } |
1762 | } |
1615 | } |
1763 | } |
1616 | |
1764 | |
1617 | modechange: |
1765 | modechange: |
1618 | ; |
1766 | ; |
1619 | } |
1767 | } |
1620 | |
1768 | |
1621 | interrupt: |
1769 | interrupt: |
1622 | self->incr_pos = p - SvPVX (self->incr_text); |
1770 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1771 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1623 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1772 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1624 | } |
1773 | } |
1625 | |
1774 | |
1626 | ///////////////////////////////////////////////////////////////////////////// |
1775 | ///////////////////////////////////////////////////////////////////////////// |
1627 | // XS interface functions |
1776 | // XS interface functions |
… | |
… | |
1642 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1791 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1643 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1792 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1644 | |
1793 | |
1645 | json_true = get_bool ("JSON::XS::true"); |
1794 | json_true = get_bool ("JSON::XS::true"); |
1646 | json_false = get_bool ("JSON::XS::false"); |
1795 | json_false = get_bool ("JSON::XS::false"); |
|
|
1796 | |
|
|
1797 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1647 | } |
1798 | } |
1648 | |
1799 | |
1649 | PROTOTYPES: DISABLE |
1800 | PROTOTYPES: DISABLE |
1650 | |
1801 | |
1651 | void CLONE (...) |
1802 | void CLONE (...) |
… | |
… | |
1771 | XPUSHs (decode_json (jsonstr, self, 0)); |
1922 | XPUSHs (decode_json (jsonstr, self, 0)); |
1772 | |
1923 | |
1773 | void decode_prefix (JSON *self, SV *jsonstr) |
1924 | void decode_prefix (JSON *self, SV *jsonstr) |
1774 | PPCODE: |
1925 | PPCODE: |
1775 | { |
1926 | { |
1776 | STRLEN offset; |
1927 | char *offset; |
1777 | EXTEND (SP, 2); |
1928 | EXTEND (SP, 2); |
1778 | PUSHs (decode_json (jsonstr, self, &offset)); |
1929 | PUSHs (decode_json (jsonstr, self, &offset)); |
1779 | PUSHs (sv_2mortal (newSVuv (offset))); |
1930 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1780 | } |
1931 | } |
1781 | |
1932 | |
1782 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1933 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1783 | PPCODE: |
1934 | PPCODE: |
1784 | { |
1935 | { |
1785 | if (!self->incr_text) |
1936 | if (!self->incr_text) |
1786 | self->incr_text = newSVpvn ("", 0); |
1937 | self->incr_text = newSVpvn ("", 0); |
|
|
1938 | |
|
|
1939 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
1940 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
1941 | if (DECODE_WANTS_OCTETS (self)) |
|
|
1942 | { |
|
|
1943 | if (self->incr_pos) |
|
|
1944 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
1945 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
1946 | |
|
|
1947 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
1948 | } |
|
|
1949 | else |
|
|
1950 | { |
|
|
1951 | sv_utf8_upgrade (self->incr_text); |
|
|
1952 | |
|
|
1953 | if (self->incr_pos) |
|
|
1954 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1955 | - (U8 *)SvPVX (self->incr_text); |
|
|
1956 | } |
1787 | |
1957 | |
1788 | // append data, if any |
1958 | // append data, if any |
1789 | if (jsonstr) |
1959 | if (jsonstr) |
1790 | { |
1960 | { |
|
|
1961 | /* make sure both strings have same encoding */ |
1791 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1962 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
|
|
1963 | if (SvUTF8 (jsonstr)) |
|
|
1964 | sv_utf8_downgrade (jsonstr, 0); |
1792 | { |
1965 | else |
1793 | /* utf-8-ness differs, need to upgrade */ |
|
|
1794 | sv_utf8_upgrade (self->incr_text); |
1966 | sv_utf8_upgrade (jsonstr); |
1795 | |
1967 | |
1796 | if (self->incr_pos) |
1968 | /* and then just blindly append */ |
1797 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1798 | - (U8 *)SvPVX (self->incr_text); |
|
|
1799 | } |
|
|
1800 | |
|
|
1801 | { |
1969 | { |
1802 | STRLEN len; |
1970 | STRLEN len; |
1803 | const char *str = SvPV (jsonstr, len); |
1971 | const char *str = SvPV (jsonstr, len); |
1804 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
1972 | STRLEN cur = SvCUR (self->incr_text); |
|
|
1973 | |
|
|
1974 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
1975 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1976 | |
1805 | Move (str, SvEND (self->incr_text), len, char); |
1977 | Move (str, SvEND (self->incr_text), len, char); |
1806 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1978 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1807 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1979 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1808 | } |
1980 | } |
1809 | } |
1981 | } |
1810 | |
1982 | |
1811 | if (GIMME_V != G_VOID) |
1983 | if (GIMME_V != G_VOID) |
1812 | do |
1984 | do |
1813 | { |
1985 | { |
1814 | STRLEN offset; |
1986 | char *offset; |
1815 | |
1987 | |
1816 | if (!INCR_DONE (self)) |
1988 | if (!INCR_DONE (self)) |
1817 | { |
1989 | { |
1818 | incr_parse (self); |
1990 | incr_parse (self); |
1819 | |
1991 | |
1820 | if (self->incr_pos > self->max_size && self->max_size) |
1992 | if (self->incr_pos > self->max_size && self->max_size) |
1821 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1993 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1822 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1994 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1823 | |
1995 | |
1824 | if (!INCR_DONE (self)) |
1996 | if (!INCR_DONE (self)) |
|
|
1997 | { |
|
|
1998 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
1999 | if (self->incr_mode == INCR_M_WS) |
|
|
2000 | SvCUR_set (self->incr_text, 0); |
|
|
2001 | |
1825 | break; |
2002 | break; |
|
|
2003 | } |
1826 | } |
2004 | } |
1827 | |
2005 | |
1828 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2006 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1829 | |
2007 | |
1830 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1831 | self->incr_pos -= offset; |
2008 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1832 | self->incr_nest = 0; |
2009 | self->incr_nest = 0; |
1833 | self->incr_mode = 0; |
2010 | self->incr_mode = 0; |
|
|
2011 | |
|
|
2012 | sv_chop (self->incr_text, offset); |
1834 | } |
2013 | } |
1835 | while (GIMME_V == G_ARRAY); |
2014 | while (GIMME_V == G_ARRAY); |
1836 | } |
2015 | } |
1837 | |
2016 | |
1838 | SV *incr_text (JSON *self) |
2017 | SV *incr_text (JSON *self) |
… | |
… | |
1899 | json_init (&json); |
2078 | json_init (&json); |
1900 | json.flags |= ix; |
2079 | json.flags |= ix; |
1901 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2080 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1902 | } |
2081 | } |
1903 | |
2082 | |
1904 | |
|
|