… | |
… | |
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 |
… | |
… | |
76 | |
77 | |
77 | enum { |
78 | enum { |
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_STR, // inside string |
80 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
81 | INCR_M_BS, // inside backslash |
|
|
82 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
83 | INCR_M_C1, // inside comment in other places |
81 | INCR_M_JSON // outside anything, count nesting |
84 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
85 | }; |
83 | |
86 | |
84 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
87 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
88 | |
86 | typedef struct { |
89 | typedef struct { |
87 | U32 flags; |
90 | U32 flags; |
88 | U32 max_depth; |
91 | U32 max_depth; |
89 | STRLEN max_size; |
92 | STRLEN max_size; |
… | |
… | |
92 | HV *cb_sk_object; |
95 | HV *cb_sk_object; |
93 | |
96 | |
94 | // for the incremental parser |
97 | // for the incremental parser |
95 | SV *incr_text; // the source text so far |
98 | SV *incr_text; // the source text so far |
96 | STRLEN incr_pos; // the current offset into the text |
99 | STRLEN incr_pos; // the current offset into the text |
97 | unsigned char incr_nest; // {[]}-nesting level |
100 | int incr_nest; // {[]}-nesting level |
98 | unsigned char incr_mode; |
101 | unsigned char incr_mode; |
99 | } JSON; |
102 | } JSON; |
100 | |
103 | |
101 | INLINE void |
104 | INLINE void |
102 | json_init (JSON *json) |
105 | json_init (JSON *json) |
… | |
… | |
121 | |
124 | |
122 | INLINE void |
125 | INLINE void |
123 | shrink (SV *sv) |
126 | shrink (SV *sv) |
124 | { |
127 | { |
125 | sv_utf8_downgrade (sv, 1); |
128 | sv_utf8_downgrade (sv, 1); |
|
|
129 | |
126 | if (SvLEN (sv) > SvCUR (sv) + 1) |
130 | if (SvLEN (sv) > SvCUR (sv) + 1) |
127 | { |
131 | { |
128 | #ifdef SvPV_shrink_to_cur |
132 | #ifdef SvPV_shrink_to_cur |
129 | SvPV_shrink_to_cur (sv); |
133 | SvPV_shrink_to_cur (sv); |
130 | #elif defined (SvPV_renew) |
134 | #elif defined (SvPV_renew) |
… | |
… | |
174 | *s++ = 0x80 | ( ch & 0x3f); |
178 | *s++ = 0x80 | ( ch & 0x3f); |
175 | |
179 | |
176 | return s; |
180 | return s; |
177 | } |
181 | } |
178 | |
182 | |
|
|
183 | // convert offset pointer to character index, sv must be string |
|
|
184 | static STRLEN |
|
|
185 | ptr_to_index (SV *sv, char *offset) |
|
|
186 | { |
|
|
187 | return SvUTF8 (sv) |
|
|
188 | ? utf8_distance (offset, SvPVX (sv)) |
|
|
189 | : offset - SvPVX (sv); |
|
|
190 | } |
|
|
191 | |
|
|
192 | ///////////////////////////////////////////////////////////////////////////// |
|
|
193 | // fp hell |
|
|
194 | |
|
|
195 | // scan a group of digits, and a trailing exponent |
|
|
196 | static void |
|
|
197 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp) |
|
|
198 | { |
|
|
199 | UV uaccum = 0; |
|
|
200 | int eaccum = 0; |
|
|
201 | |
|
|
202 | for (;;) |
|
|
203 | { |
|
|
204 | U8 dig = (U8)*s - '0'; |
|
|
205 | |
|
|
206 | if (expect_false (dig >= 10)) |
|
|
207 | { |
|
|
208 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
209 | { |
|
|
210 | ++s; |
|
|
211 | json_atof_scan1 (s, accum, expo, 1); |
|
|
212 | } |
|
|
213 | else if ((dig | ' ') == 'e' - '0') |
|
|
214 | { |
|
|
215 | int exp2 = 0; |
|
|
216 | int neg = 0; |
|
|
217 | |
|
|
218 | ++s; |
|
|
219 | |
|
|
220 | if (*s == '-') |
|
|
221 | { |
|
|
222 | ++s; |
|
|
223 | neg = 1; |
|
|
224 | } |
|
|
225 | else if (*s == '+') |
|
|
226 | ++s; |
|
|
227 | |
|
|
228 | while ((dig = (U8)*s - '0') < 10) |
|
|
229 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
230 | |
|
|
231 | *expo += neg ? -exp2 : exp2; |
|
|
232 | } |
|
|
233 | |
|
|
234 | break; |
|
|
235 | } |
|
|
236 | |
|
|
237 | ++s; |
|
|
238 | |
|
|
239 | uaccum = uaccum * 10 + dig; |
|
|
240 | ++eaccum; |
|
|
241 | |
|
|
242 | // if we have too many digits, then recurse for more |
|
|
243 | // we actually do this for rather few digits |
|
|
244 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
245 | { |
|
|
246 | if (postdp) *expo -= eaccum; |
|
|
247 | json_atof_scan1 (s, accum, expo, postdp); |
|
|
248 | if (postdp) *expo += eaccum; |
|
|
249 | |
|
|
250 | break; |
|
|
251 | } |
|
|
252 | } |
|
|
253 | |
|
|
254 | if (postdp) *expo -= eaccum; |
|
|
255 | *accum += uaccum * pow (10., *expo); |
|
|
256 | *expo += eaccum; |
|
|
257 | } |
|
|
258 | |
|
|
259 | static NV |
|
|
260 | json_atof (const char *s) |
|
|
261 | { |
|
|
262 | NV accum = 0.; |
|
|
263 | int expo = 0; |
|
|
264 | int neg = 0; |
|
|
265 | |
|
|
266 | if (*s == '-') |
|
|
267 | { |
|
|
268 | ++s; |
|
|
269 | neg = 1; |
|
|
270 | } |
|
|
271 | |
|
|
272 | json_atof_scan1 (s, &accum, &expo, 0); |
|
|
273 | |
|
|
274 | return neg ? -accum : accum; |
|
|
275 | } |
179 | ///////////////////////////////////////////////////////////////////////////// |
276 | ///////////////////////////////////////////////////////////////////////////// |
180 | // encoder |
277 | // encoder |
181 | |
278 | |
182 | // structure used for encoding JSON |
279 | // structure used for encoding JSON |
183 | typedef struct |
280 | typedef struct |
… | |
… | |
193 | INLINE void |
290 | INLINE void |
194 | need (enc_t *enc, STRLEN len) |
291 | need (enc_t *enc, STRLEN len) |
195 | { |
292 | { |
196 | if (expect_false (enc->cur + len >= enc->end)) |
293 | if (expect_false (enc->cur + len >= enc->end)) |
197 | { |
294 | { |
198 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
295 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
199 | SvGROW (enc->sv, cur + len + 1); |
296 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
200 | enc->cur = SvPVX (enc->sv) + cur; |
297 | enc->cur = SvPVX (enc->sv) + cur; |
201 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
298 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
202 | } |
299 | } |
203 | } |
300 | } |
204 | |
301 | |
… | |
… | |
279 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
376 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
280 | enc->cur += 12; |
377 | enc->cur += 12; |
281 | } |
378 | } |
282 | else |
379 | else |
283 | { |
380 | { |
284 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
285 | need (enc, len += 5); |
381 | need (enc, len += 5); |
286 | *enc->cur++ = '\\'; |
382 | *enc->cur++ = '\\'; |
287 | *enc->cur++ = 'u'; |
383 | *enc->cur++ = 'u'; |
288 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
384 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
289 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
385 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
290 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
386 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
291 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
387 | *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
292 | } |
388 | } |
293 | |
389 | |
294 | str += clen; |
390 | str += clen; |
295 | } |
391 | } |
296 | else if (enc->json.flags & F_LATIN1) |
392 | else if (enc->json.flags & F_LATIN1) |
… | |
… | |
460 | |
556 | |
461 | encode_ch (enc, '{'); |
557 | encode_ch (enc, '{'); |
462 | |
558 | |
463 | // for canonical output we have to sort by keys first |
559 | // for canonical output we have to sort by keys first |
464 | // actually, this is mostly due to the stupid so-called |
560 | // actually, this is mostly due to the stupid so-called |
465 | // security workaround added somewhere in 5.8.x. |
561 | // security workaround added somewhere in 5.8.x |
466 | // that randomises hash orderings |
562 | // that randomises hash orderings |
467 | if (enc->json.flags & F_CANONICAL) |
563 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
468 | { |
564 | { |
469 | int count = hv_iterinit (hv); |
565 | int count = hv_iterinit (hv); |
470 | |
566 | |
471 | if (SvMAGICAL (hv)) |
567 | if (SvMAGICAL (hv)) |
472 | { |
568 | { |
… | |
… | |
750 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
846 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
751 | : 0x110000UL; |
847 | : 0x110000UL; |
752 | |
848 | |
753 | SvPOK_only (enc.sv); |
849 | SvPOK_only (enc.sv); |
754 | encode_sv (&enc, scalar); |
850 | encode_sv (&enc, scalar); |
|
|
851 | encode_nl (&enc); |
755 | |
852 | |
756 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
853 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
757 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
854 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
758 | |
855 | |
759 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
856 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
939 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1036 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
940 | *cur++ = ch; |
1037 | *cur++ = ch; |
941 | else if (ch >= 0x80) |
1038 | else if (ch >= 0x80) |
942 | { |
1039 | { |
943 | STRLEN clen; |
1040 | STRLEN clen; |
944 | UV uch; |
|
|
945 | |
1041 | |
946 | --dec_cur; |
1042 | --dec_cur; |
947 | |
1043 | |
948 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1044 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
949 | if (clen == (STRLEN)-1) |
1045 | if (clen == (STRLEN)-1) |
950 | ERR ("malformed UTF-8 character in JSON string"); |
1046 | ERR ("malformed UTF-8 character in JSON string"); |
951 | |
1047 | |
952 | do |
1048 | do |
953 | *cur++ = *dec_cur++; |
1049 | *cur++ = *dec_cur++; |
… | |
… | |
970 | { |
1066 | { |
971 | STRLEN len = cur - buf; |
1067 | STRLEN len = cur - buf; |
972 | |
1068 | |
973 | if (sv) |
1069 | if (sv) |
974 | { |
1070 | { |
975 | SvGROW (sv, SvCUR (sv) + len + 1); |
1071 | STRLEN cur = SvCUR (sv); |
|
|
1072 | |
|
|
1073 | if (SvLEN (sv) <= cur + len) |
|
|
1074 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1075 | |
976 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1076 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
977 | SvCUR_set (sv, SvCUR (sv) + len); |
1077 | SvCUR_set (sv, SvCUR (sv) + len); |
978 | } |
1078 | } |
979 | else |
1079 | else |
980 | sv = newSVpvn (buf, len); |
1080 | sv = newSVpvn (buf, len); |
… | |
… | |
1071 | |
1171 | |
1072 | // special case the rather common 1..5-digit-int case |
1172 | // special case the rather common 1..5-digit-int case |
1073 | if (*start == '-') |
1173 | if (*start == '-') |
1074 | switch (len) |
1174 | switch (len) |
1075 | { |
1175 | { |
1076 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1176 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1077 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1177 | 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)); |
1178 | 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)); |
1179 | 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)); |
1180 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1081 | } |
1181 | } |
1082 | else |
1182 | else |
1083 | switch (len) |
1183 | switch (len) |
1084 | { |
1184 | { |
1085 | case 1: return newSViv ( start [0] - '0' * 1); |
1185 | case 1: return newSViv ( start [0] - '0' * 1); |
1086 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1186 | 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); |
1187 | 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); |
1188 | 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); |
1189 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1090 | } |
1190 | } |
1091 | |
1191 | |
1092 | { |
1192 | { |
1093 | UV uv; |
1193 | UV uv; |
1094 | int numtype = grok_number (start, len, &uv); |
1194 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1103 | } |
1203 | } |
1104 | |
1204 | |
1105 | len -= *start == '-' ? 1 : 0; |
1205 | len -= *start == '-' ? 1 : 0; |
1106 | |
1206 | |
1107 | // does not fit into IV or UV, try NV |
1207 | // does not fit into IV or UV, try NV |
1108 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1208 | 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 |
1209 | // fits into NV without loss of precision |
1114 | return newSVnv (Atof (start)); |
1210 | return newSVnv (json_atof (start)); |
1115 | |
1211 | |
1116 | // everything else fails, convert it to a string |
1212 | // everything else fails, convert it to a string |
1117 | return newSVpvn (start, dec->cur - start); |
1213 | return newSVpvn (start, dec->cur - start); |
1118 | } |
1214 | } |
1119 | |
1215 | |
1120 | // loss of precision here |
1216 | // loss of precision here |
1121 | return newSVnv (Atof (start)); |
1217 | return newSVnv (json_atof (start)); |
1122 | |
1218 | |
1123 | fail: |
1219 | fail: |
1124 | return 0; |
1220 | return 0; |
1125 | } |
1221 | } |
1126 | |
1222 | |
… | |
… | |
1404 | fail: |
1500 | fail: |
1405 | return 0; |
1501 | return 0; |
1406 | } |
1502 | } |
1407 | |
1503 | |
1408 | static SV * |
1504 | static SV * |
1409 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1505 | decode_json (SV *string, JSON *json, char **offset_return) |
1410 | { |
1506 | { |
1411 | dec_t dec; |
1507 | dec_t dec; |
1412 | STRLEN offset; |
|
|
1413 | SV *sv; |
1508 | SV *sv; |
1414 | |
1509 | |
|
|
1510 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1511 | * will perl ignore the magic in subsequent accesses |
|
|
1512 | */ |
1415 | SvGETMAGIC (string); |
1513 | /*SvGETMAGIC (string);*/ |
|
|
1514 | if (SvMAGICAL (string)) |
|
|
1515 | string = sv_2mortal (newSVsv (string)); |
|
|
1516 | |
1416 | SvUPGRADE (string, SVt_PV); |
1517 | SvUPGRADE (string, SVt_PV); |
1417 | |
1518 | |
|
|
1519 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
1520 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
1521 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
1522 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1523 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1524 | * and hope for the best. |
|
|
1525 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
1526 | * assertion business is seriously broken, try yet another workaround |
|
|
1527 | * for the broken -DDEBUGGING. |
|
|
1528 | */ |
|
|
1529 | { |
|
|
1530 | #ifdef DEBUGGING |
|
|
1531 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
1532 | #else |
|
|
1533 | STRLEN offset = SvCUR (string); |
|
|
1534 | #endif |
|
|
1535 | |
1418 | if (SvCUR (string) > json->max_size && json->max_size) |
1536 | if (offset > json->max_size && json->max_size) |
1419 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1537 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1420 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1538 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1539 | } |
1421 | |
1540 | |
1422 | if (json->flags & F_UTF8) |
1541 | if (json->flags & F_UTF8) |
1423 | sv_utf8_downgrade (string, 0); |
1542 | sv_utf8_downgrade (string, 0); |
1424 | else |
1543 | else |
1425 | sv_utf8_upgrade (string); |
1544 | sv_utf8_upgrade (string); |
… | |
… | |
1437 | |
1556 | |
1438 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1557 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1439 | |
1558 | |
1440 | decode_ws (&dec); |
1559 | decode_ws (&dec); |
1441 | sv = decode_sv (&dec); |
1560 | sv = decode_sv (&dec); |
|
|
1561 | |
|
|
1562 | if (offset_return) |
|
|
1563 | *offset_return = dec.cur; |
1442 | |
1564 | |
1443 | if (!(offset_return || !sv)) |
1565 | if (!(offset_return || !sv)) |
1444 | { |
1566 | { |
1445 | // check for trailing garbage |
1567 | // check for trailing garbage |
1446 | decode_ws (&dec); |
1568 | decode_ws (&dec); |
… | |
… | |
1449 | { |
1571 | { |
1450 | dec.err = "garbage after JSON object"; |
1572 | dec.err = "garbage after JSON object"; |
1451 | SvREFCNT_dec (sv); |
1573 | SvREFCNT_dec (sv); |
1452 | sv = 0; |
1574 | sv = 0; |
1453 | } |
1575 | } |
1454 | } |
|
|
1455 | |
|
|
1456 | if (offset_return || !sv) |
|
|
1457 | { |
|
|
1458 | offset = dec.json.flags & F_UTF8 |
|
|
1459 | ? dec.cur - SvPVX (string) |
|
|
1460 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1461 | |
|
|
1462 | if (offset_return) |
|
|
1463 | *offset_return = offset; |
|
|
1464 | } |
1576 | } |
1465 | |
1577 | |
1466 | if (!sv) |
1578 | if (!sv) |
1467 | { |
1579 | { |
1468 | SV *uni = sv_newmortal (); |
1580 | SV *uni = sv_newmortal (); |
… | |
… | |
1474 | SAVEVPTR (PL_curcop); |
1586 | SAVEVPTR (PL_curcop); |
1475 | PL_curcop = &cop; |
1587 | PL_curcop = &cop; |
1476 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1588 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1477 | LEAVE; |
1589 | LEAVE; |
1478 | |
1590 | |
1479 | croak ("%s, at character offset %d [\"%s\"]", |
1591 | croak ("%s, at character offset %d (before \"%s\")", |
1480 | dec.err, |
1592 | dec.err, |
1481 | (int)offset, |
1593 | ptr_to_index (string, dec.cur), |
1482 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1594 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1483 | } |
1595 | } |
1484 | |
1596 | |
1485 | sv = sv_2mortal (sv); |
1597 | sv = sv_2mortal (sv); |
1486 | |
1598 | |
… | |
… | |
1495 | |
1607 | |
1496 | static void |
1608 | static void |
1497 | incr_parse (JSON *self) |
1609 | incr_parse (JSON *self) |
1498 | { |
1610 | { |
1499 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1611 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1612 | |
|
|
1613 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1614 | // but this would make it slower, so... |
1500 | |
1615 | |
1501 | for (;;) |
1616 | for (;;) |
1502 | { |
1617 | { |
1503 | //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 |
1618 | //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 |
1504 | switch (self->incr_mode) |
1619 | switch (self->incr_mode) |
1505 | { |
1620 | { |
1506 | // only used for intiial whitespace skipping |
1621 | // only used for initial whitespace skipping |
1507 | case INCR_M_WS: |
1622 | case INCR_M_WS: |
1508 | for (;;) |
1623 | for (;;) |
1509 | { |
1624 | { |
1510 | if (*p > 0x20) |
1625 | if (*p > 0x20) |
1511 | { |
1626 | { |
|
|
1627 | if (*p == '#') |
|
|
1628 | { |
|
|
1629 | self->incr_mode = INCR_M_C0; |
|
|
1630 | goto incr_m_c; |
|
|
1631 | } |
|
|
1632 | else |
|
|
1633 | { |
1512 | self->incr_mode = INCR_M_JSON; |
1634 | self->incr_mode = INCR_M_JSON; |
1513 | goto incr_m_json; |
1635 | goto incr_m_json; |
|
|
1636 | } |
1514 | } |
1637 | } |
1515 | else if (!*p) |
1638 | else if (!*p) |
1516 | goto interrupt; |
1639 | goto interrupt; |
1517 | |
1640 | |
1518 | ++p; |
1641 | ++p; |
… | |
… | |
1524 | goto interrupt; |
1647 | goto interrupt; |
1525 | |
1648 | |
1526 | ++p; |
1649 | ++p; |
1527 | self->incr_mode = INCR_M_STR; |
1650 | self->incr_mode = INCR_M_STR; |
1528 | goto incr_m_str; |
1651 | goto incr_m_str; |
|
|
1652 | |
|
|
1653 | // inside #-style comments |
|
|
1654 | case INCR_M_C0: |
|
|
1655 | case INCR_M_C1: |
|
|
1656 | incr_m_c: |
|
|
1657 | for (;;) |
|
|
1658 | { |
|
|
1659 | if (*p == '\n') |
|
|
1660 | { |
|
|
1661 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1662 | break; |
|
|
1663 | } |
|
|
1664 | else if (!*p) |
|
|
1665 | goto interrupt; |
|
|
1666 | |
|
|
1667 | ++p; |
|
|
1668 | } |
|
|
1669 | |
|
|
1670 | break; |
1529 | |
1671 | |
1530 | // inside a string |
1672 | // inside a string |
1531 | case INCR_M_STR: |
1673 | case INCR_M_STR: |
1532 | incr_m_str: |
1674 | incr_m_str: |
1533 | for (;;) |
1675 | for (;;) |
… | |
… | |
1590 | croak (ERR_NESTING_EXCEEDED); |
1732 | croak (ERR_NESTING_EXCEEDED); |
1591 | break; |
1733 | break; |
1592 | |
1734 | |
1593 | case ']': |
1735 | case ']': |
1594 | case '}': |
1736 | case '}': |
1595 | if (!--self->incr_nest) |
1737 | if (--self->incr_nest <= 0) |
1596 | goto interrupt; |
1738 | goto interrupt; |
|
|
1739 | break; |
|
|
1740 | |
|
|
1741 | case '#': |
|
|
1742 | self->incr_mode = INCR_M_C1; |
|
|
1743 | goto incr_m_c; |
1597 | } |
1744 | } |
1598 | } |
1745 | } |
1599 | } |
1746 | } |
1600 | |
1747 | |
1601 | modechange: |
1748 | modechange: |
1602 | ; |
1749 | ; |
1603 | } |
1750 | } |
1604 | |
1751 | |
1605 | interrupt: |
1752 | interrupt: |
1606 | self->incr_pos = p - SvPVX (self->incr_text); |
1753 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1754 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1607 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1755 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1608 | } |
1756 | } |
1609 | |
1757 | |
1610 | ///////////////////////////////////////////////////////////////////////////// |
1758 | ///////////////////////////////////////////////////////////////////////////// |
1611 | // XS interface functions |
1759 | // XS interface functions |
… | |
… | |
1626 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1774 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1627 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1775 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1628 | |
1776 | |
1629 | json_true = get_bool ("JSON::XS::true"); |
1777 | json_true = get_bool ("JSON::XS::true"); |
1630 | json_false = get_bool ("JSON::XS::false"); |
1778 | json_false = get_bool ("JSON::XS::false"); |
|
|
1779 | |
|
|
1780 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1631 | } |
1781 | } |
1632 | |
1782 | |
1633 | PROTOTYPES: DISABLE |
1783 | PROTOTYPES: DISABLE |
1634 | |
1784 | |
1635 | void CLONE (...) |
1785 | void CLONE (...) |
… | |
… | |
1755 | XPUSHs (decode_json (jsonstr, self, 0)); |
1905 | XPUSHs (decode_json (jsonstr, self, 0)); |
1756 | |
1906 | |
1757 | void decode_prefix (JSON *self, SV *jsonstr) |
1907 | void decode_prefix (JSON *self, SV *jsonstr) |
1758 | PPCODE: |
1908 | PPCODE: |
1759 | { |
1909 | { |
1760 | STRLEN offset; |
1910 | char *offset; |
1761 | EXTEND (SP, 2); |
1911 | EXTEND (SP, 2); |
1762 | PUSHs (decode_json (jsonstr, self, &offset)); |
1912 | PUSHs (decode_json (jsonstr, self, &offset)); |
1763 | PUSHs (sv_2mortal (newSVuv (offset))); |
1913 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1764 | } |
1914 | } |
1765 | |
1915 | |
1766 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1916 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1767 | PPCODE: |
1917 | PPCODE: |
1768 | { |
1918 | { |
… | |
… | |
1770 | self->incr_text = newSVpvn ("", 0); |
1920 | self->incr_text = newSVpvn ("", 0); |
1771 | |
1921 | |
1772 | // append data, if any |
1922 | // append data, if any |
1773 | if (jsonstr) |
1923 | if (jsonstr) |
1774 | { |
1924 | { |
1775 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1925 | if (SvUTF8 (jsonstr)) |
1776 | { |
1926 | { |
|
|
1927 | if (!SvUTF8 (self->incr_text)) |
|
|
1928 | { |
1777 | /* utf-8-ness differs, need to upgrade */ |
1929 | /* utf-8-ness differs, need to upgrade */ |
1778 | sv_utf8_upgrade (self->incr_text); |
1930 | sv_utf8_upgrade (self->incr_text); |
1779 | |
1931 | |
1780 | if (self->incr_pos) |
1932 | if (self->incr_pos) |
1781 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1933 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1782 | - (U8 *)SvPVX (self->incr_text); |
1934 | - (U8 *)SvPVX (self->incr_text); |
|
|
1935 | } |
1783 | } |
1936 | } |
|
|
1937 | else if (SvUTF8 (self->incr_text)) |
|
|
1938 | sv_utf8_upgrade (jsonstr); |
1784 | |
1939 | |
1785 | { |
1940 | { |
1786 | STRLEN len; |
1941 | STRLEN len; |
1787 | const char *str = SvPV (jsonstr, len); |
1942 | const char *str = SvPV (jsonstr, len); |
1788 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
1943 | STRLEN cur = SvCUR (self->incr_text); |
|
|
1944 | |
|
|
1945 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
1946 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1947 | |
1789 | Move (str, SvEND (self->incr_text), len, char); |
1948 | Move (str, SvEND (self->incr_text), len, char); |
1790 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1949 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1791 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1950 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1792 | } |
1951 | } |
1793 | } |
1952 | } |
1794 | |
1953 | |
1795 | if (GIMME_V != G_VOID) |
1954 | if (GIMME_V != G_VOID) |
1796 | do |
1955 | do |
1797 | { |
1956 | { |
1798 | STRLEN offset; |
1957 | char *offset; |
1799 | |
1958 | |
1800 | if (!INCR_DONE (self)) |
1959 | if (!INCR_DONE (self)) |
1801 | { |
1960 | { |
1802 | incr_parse (self); |
1961 | incr_parse (self); |
1803 | |
1962 | |
… | |
… | |
1809 | break; |
1968 | break; |
1810 | } |
1969 | } |
1811 | |
1970 | |
1812 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1971 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1813 | |
1972 | |
1814 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1815 | self->incr_pos -= offset; |
1973 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1816 | self->incr_nest = 0; |
1974 | self->incr_nest = 0; |
1817 | self->incr_mode = 0; |
1975 | self->incr_mode = 0; |
|
|
1976 | |
|
|
1977 | sv_chop (self->incr_text, offset); |
1818 | } |
1978 | } |
1819 | while (GIMME_V == G_ARRAY); |
1979 | while (GIMME_V == G_ARRAY); |
1820 | } |
1980 | } |
1821 | |
1981 | |
1822 | SV *incr_text (JSON *self) |
1982 | SV *incr_text (JSON *self) |
… | |
… | |
1883 | json_init (&json); |
2043 | json_init (&json); |
1884 | json.flags |= ix; |
2044 | json.flags |= ix; |
1885 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2045 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1886 | } |
2046 | } |
1887 | |
2047 | |
1888 | |
|
|