… | |
… | |
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 | |
179 | ///////////////////////////////////////////////////////////////////////////// |
192 | ///////////////////////////////////////////////////////////////////////////// |
180 | // encoder |
193 | // encoder |
181 | |
194 | |
182 | // structure used for encoding JSON |
195 | // structure used for encoding JSON |
183 | typedef struct |
196 | typedef struct |
… | |
… | |
193 | INLINE void |
206 | INLINE void |
194 | need (enc_t *enc, STRLEN len) |
207 | need (enc_t *enc, STRLEN len) |
195 | { |
208 | { |
196 | if (expect_false (enc->cur + len >= enc->end)) |
209 | if (expect_false (enc->cur + len >= enc->end)) |
197 | { |
210 | { |
198 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
211 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
199 | SvGROW (enc->sv, cur + len + 1); |
212 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
200 | enc->cur = SvPVX (enc->sv) + cur; |
213 | enc->cur = SvPVX (enc->sv) + cur; |
201 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
214 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
202 | } |
215 | } |
203 | } |
216 | } |
204 | |
217 | |
… | |
… | |
279 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
292 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
280 | enc->cur += 12; |
293 | enc->cur += 12; |
281 | } |
294 | } |
282 | else |
295 | else |
283 | { |
296 | { |
284 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
285 | need (enc, len += 5); |
297 | need (enc, len += 5); |
286 | *enc->cur++ = '\\'; |
298 | *enc->cur++ = '\\'; |
287 | *enc->cur++ = 'u'; |
299 | *enc->cur++ = 'u'; |
288 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
300 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
289 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
301 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
290 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
302 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
291 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
303 | *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
292 | } |
304 | } |
293 | |
305 | |
294 | str += clen; |
306 | str += clen; |
295 | } |
307 | } |
296 | else if (enc->json.flags & F_LATIN1) |
308 | else if (enc->json.flags & F_LATIN1) |
… | |
… | |
460 | |
472 | |
461 | encode_ch (enc, '{'); |
473 | encode_ch (enc, '{'); |
462 | |
474 | |
463 | // for canonical output we have to sort by keys first |
475 | // for canonical output we have to sort by keys first |
464 | // actually, this is mostly due to the stupid so-called |
476 | // actually, this is mostly due to the stupid so-called |
465 | // security workaround added somewhere in 5.8.x. |
477 | // security workaround added somewhere in 5.8.x |
466 | // that randomises hash orderings |
478 | // that randomises hash orderings |
467 | if (enc->json.flags & F_CANONICAL) |
479 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
468 | { |
480 | { |
469 | int count = hv_iterinit (hv); |
481 | int count = hv_iterinit (hv); |
470 | |
482 | |
471 | if (SvMAGICAL (hv)) |
483 | if (SvMAGICAL (hv)) |
472 | { |
484 | { |
… | |
… | |
750 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
762 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
751 | : 0x110000UL; |
763 | : 0x110000UL; |
752 | |
764 | |
753 | SvPOK_only (enc.sv); |
765 | SvPOK_only (enc.sv); |
754 | encode_sv (&enc, scalar); |
766 | encode_sv (&enc, scalar); |
|
|
767 | encode_nl (&enc); |
755 | |
768 | |
756 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
769 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
757 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
770 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
758 | |
771 | |
759 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
772 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
939 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
952 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
940 | *cur++ = ch; |
953 | *cur++ = ch; |
941 | else if (ch >= 0x80) |
954 | else if (ch >= 0x80) |
942 | { |
955 | { |
943 | STRLEN clen; |
956 | STRLEN clen; |
944 | UV uch; |
|
|
945 | |
957 | |
946 | --dec_cur; |
958 | --dec_cur; |
947 | |
959 | |
948 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
960 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
949 | if (clen == (STRLEN)-1) |
961 | if (clen == (STRLEN)-1) |
950 | ERR ("malformed UTF-8 character in JSON string"); |
962 | ERR ("malformed UTF-8 character in JSON string"); |
951 | |
963 | |
952 | do |
964 | do |
953 | *cur++ = *dec_cur++; |
965 | *cur++ = *dec_cur++; |
… | |
… | |
970 | { |
982 | { |
971 | STRLEN len = cur - buf; |
983 | STRLEN len = cur - buf; |
972 | |
984 | |
973 | if (sv) |
985 | if (sv) |
974 | { |
986 | { |
975 | SvGROW (sv, SvCUR (sv) + len + 1); |
987 | STRLEN cur = SvCUR (sv); |
|
|
988 | |
|
|
989 | if (SvLEN (sv) <= cur + len) |
|
|
990 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
991 | |
976 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
992 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
977 | SvCUR_set (sv, SvCUR (sv) + len); |
993 | SvCUR_set (sv, SvCUR (sv) + len); |
978 | } |
994 | } |
979 | else |
995 | else |
980 | sv = newSVpvn (buf, len); |
996 | sv = newSVpvn (buf, len); |
… | |
… | |
1071 | |
1087 | |
1072 | // special case the rather common 1..5-digit-int case |
1088 | // special case the rather common 1..5-digit-int case |
1073 | if (*start == '-') |
1089 | if (*start == '-') |
1074 | switch (len) |
1090 | switch (len) |
1075 | { |
1091 | { |
1076 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1092 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1077 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1093 | 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)); |
1094 | 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)); |
1095 | 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)); |
1096 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1081 | } |
1097 | } |
1082 | else |
1098 | else |
1083 | switch (len) |
1099 | switch (len) |
1084 | { |
1100 | { |
1085 | case 1: return newSViv ( start [0] - '0' * 1); |
1101 | case 1: return newSViv ( start [0] - '0' * 1); |
1086 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1102 | 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); |
1103 | 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); |
1104 | 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); |
1105 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1090 | } |
1106 | } |
1091 | |
1107 | |
1092 | { |
1108 | { |
1093 | UV uv; |
1109 | UV uv; |
1094 | int numtype = grok_number (start, len, &uv); |
1110 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1404 | fail: |
1420 | fail: |
1405 | return 0; |
1421 | return 0; |
1406 | } |
1422 | } |
1407 | |
1423 | |
1408 | static SV * |
1424 | static SV * |
1409 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1425 | decode_json (SV *string, JSON *json, char **offset_return) |
1410 | { |
1426 | { |
1411 | dec_t dec; |
1427 | dec_t dec; |
1412 | STRLEN offset; |
|
|
1413 | SV *sv; |
1428 | SV *sv; |
1414 | |
1429 | |
|
|
1430 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1431 | * will perl ignore the magic in subsequent accesses |
|
|
1432 | */ |
1415 | SvGETMAGIC (string); |
1433 | /*SvGETMAGIC (string);*/ |
|
|
1434 | if (SvMAGICAL (string)) |
|
|
1435 | string = sv_2mortal (newSVsv (string)); |
|
|
1436 | |
1416 | SvUPGRADE (string, SVt_PV); |
1437 | SvUPGRADE (string, SVt_PV); |
1417 | |
1438 | |
|
|
1439 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
1440 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
1441 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
1442 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1443 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1444 | * and hope for the best. |
|
|
1445 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
1446 | * assertion business is seriously broken, try yet another workaround |
|
|
1447 | * for the broken -DDEBUGGING. |
|
|
1448 | */ |
|
|
1449 | { |
|
|
1450 | #ifdef DEBUGGING |
|
|
1451 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
1452 | #else |
|
|
1453 | STRLEN offset = SvCUR (string); |
|
|
1454 | #endif |
|
|
1455 | |
1418 | if (SvCUR (string) > json->max_size && json->max_size) |
1456 | 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", |
1457 | 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); |
1458 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1459 | } |
1421 | |
1460 | |
1422 | if (json->flags & F_UTF8) |
1461 | if (json->flags & F_UTF8) |
1423 | sv_utf8_downgrade (string, 0); |
1462 | sv_utf8_downgrade (string, 0); |
1424 | else |
1463 | else |
1425 | sv_utf8_upgrade (string); |
1464 | sv_utf8_upgrade (string); |
… | |
… | |
1437 | |
1476 | |
1438 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1477 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1439 | |
1478 | |
1440 | decode_ws (&dec); |
1479 | decode_ws (&dec); |
1441 | sv = decode_sv (&dec); |
1480 | sv = decode_sv (&dec); |
|
|
1481 | |
|
|
1482 | if (offset_return) |
|
|
1483 | *offset_return = dec.cur; |
1442 | |
1484 | |
1443 | if (!(offset_return || !sv)) |
1485 | if (!(offset_return || !sv)) |
1444 | { |
1486 | { |
1445 | // check for trailing garbage |
1487 | // check for trailing garbage |
1446 | decode_ws (&dec); |
1488 | decode_ws (&dec); |
… | |
… | |
1449 | { |
1491 | { |
1450 | dec.err = "garbage after JSON object"; |
1492 | dec.err = "garbage after JSON object"; |
1451 | SvREFCNT_dec (sv); |
1493 | SvREFCNT_dec (sv); |
1452 | sv = 0; |
1494 | sv = 0; |
1453 | } |
1495 | } |
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 | } |
1496 | } |
1465 | |
1497 | |
1466 | if (!sv) |
1498 | if (!sv) |
1467 | { |
1499 | { |
1468 | SV *uni = sv_newmortal (); |
1500 | SV *uni = sv_newmortal (); |
… | |
… | |
1474 | SAVEVPTR (PL_curcop); |
1506 | SAVEVPTR (PL_curcop); |
1475 | PL_curcop = &cop; |
1507 | PL_curcop = &cop; |
1476 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1508 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1477 | LEAVE; |
1509 | LEAVE; |
1478 | |
1510 | |
1479 | croak ("%s, at character offset %d [\"%s\"]", |
1511 | croak ("%s, at character offset %d (before \"%s\")", |
1480 | dec.err, |
1512 | dec.err, |
1481 | (int)offset, |
1513 | ptr_to_index (string, dec.cur), |
1482 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1514 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1483 | } |
1515 | } |
1484 | |
1516 | |
1485 | sv = sv_2mortal (sv); |
1517 | sv = sv_2mortal (sv); |
1486 | |
1518 | |
… | |
… | |
1495 | |
1527 | |
1496 | static void |
1528 | static void |
1497 | incr_parse (JSON *self) |
1529 | incr_parse (JSON *self) |
1498 | { |
1530 | { |
1499 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1531 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1532 | |
|
|
1533 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1534 | // but this would make it slower, so... |
1500 | |
1535 | |
1501 | for (;;) |
1536 | for (;;) |
1502 | { |
1537 | { |
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 |
1538 | //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) |
1539 | switch (self->incr_mode) |
1505 | { |
1540 | { |
1506 | // only used for intiial whitespace skipping |
1541 | // only used for initial whitespace skipping |
1507 | case INCR_M_WS: |
1542 | case INCR_M_WS: |
1508 | for (;;) |
1543 | for (;;) |
1509 | { |
1544 | { |
1510 | if (*p > 0x20) |
1545 | if (*p > 0x20) |
1511 | { |
1546 | { |
|
|
1547 | if (*p == '#') |
|
|
1548 | { |
|
|
1549 | self->incr_mode = INCR_M_C0; |
|
|
1550 | goto incr_m_c; |
|
|
1551 | } |
|
|
1552 | else |
|
|
1553 | { |
1512 | self->incr_mode = INCR_M_JSON; |
1554 | self->incr_mode = INCR_M_JSON; |
1513 | goto incr_m_json; |
1555 | goto incr_m_json; |
|
|
1556 | } |
1514 | } |
1557 | } |
1515 | else if (!*p) |
1558 | else if (!*p) |
1516 | goto interrupt; |
1559 | goto interrupt; |
1517 | |
1560 | |
1518 | ++p; |
1561 | ++p; |
… | |
… | |
1524 | goto interrupt; |
1567 | goto interrupt; |
1525 | |
1568 | |
1526 | ++p; |
1569 | ++p; |
1527 | self->incr_mode = INCR_M_STR; |
1570 | self->incr_mode = INCR_M_STR; |
1528 | goto incr_m_str; |
1571 | goto incr_m_str; |
|
|
1572 | |
|
|
1573 | // inside #-style comments |
|
|
1574 | case INCR_M_C0: |
|
|
1575 | case INCR_M_C1: |
|
|
1576 | incr_m_c: |
|
|
1577 | for (;;) |
|
|
1578 | { |
|
|
1579 | if (*p == '\n') |
|
|
1580 | { |
|
|
1581 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1582 | break; |
|
|
1583 | } |
|
|
1584 | else if (!*p) |
|
|
1585 | goto interrupt; |
|
|
1586 | |
|
|
1587 | ++p; |
|
|
1588 | } |
|
|
1589 | |
|
|
1590 | break; |
1529 | |
1591 | |
1530 | // inside a string |
1592 | // inside a string |
1531 | case INCR_M_STR: |
1593 | case INCR_M_STR: |
1532 | incr_m_str: |
1594 | incr_m_str: |
1533 | for (;;) |
1595 | for (;;) |
… | |
… | |
1590 | croak (ERR_NESTING_EXCEEDED); |
1652 | croak (ERR_NESTING_EXCEEDED); |
1591 | break; |
1653 | break; |
1592 | |
1654 | |
1593 | case ']': |
1655 | case ']': |
1594 | case '}': |
1656 | case '}': |
1595 | if (!--self->incr_nest) |
1657 | if (--self->incr_nest <= 0) |
1596 | goto interrupt; |
1658 | goto interrupt; |
|
|
1659 | break; |
|
|
1660 | |
|
|
1661 | case '#': |
|
|
1662 | self->incr_mode = INCR_M_C1; |
|
|
1663 | goto incr_m_c; |
1597 | } |
1664 | } |
1598 | } |
1665 | } |
1599 | } |
1666 | } |
1600 | |
1667 | |
1601 | modechange: |
1668 | modechange: |
1602 | ; |
1669 | ; |
1603 | } |
1670 | } |
1604 | |
1671 | |
1605 | interrupt: |
1672 | interrupt: |
1606 | self->incr_pos = p - SvPVX (self->incr_text); |
1673 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1674 | //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 |
1675 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1608 | } |
1676 | } |
1609 | |
1677 | |
1610 | ///////////////////////////////////////////////////////////////////////////// |
1678 | ///////////////////////////////////////////////////////////////////////////// |
1611 | // XS interface functions |
1679 | // XS interface functions |
… | |
… | |
1626 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1694 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1627 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1695 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1628 | |
1696 | |
1629 | json_true = get_bool ("JSON::XS::true"); |
1697 | json_true = get_bool ("JSON::XS::true"); |
1630 | json_false = get_bool ("JSON::XS::false"); |
1698 | json_false = get_bool ("JSON::XS::false"); |
|
|
1699 | |
|
|
1700 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1631 | } |
1701 | } |
1632 | |
1702 | |
1633 | PROTOTYPES: DISABLE |
1703 | PROTOTYPES: DISABLE |
1634 | |
1704 | |
1635 | void CLONE (...) |
1705 | void CLONE (...) |
… | |
… | |
1755 | XPUSHs (decode_json (jsonstr, self, 0)); |
1825 | XPUSHs (decode_json (jsonstr, self, 0)); |
1756 | |
1826 | |
1757 | void decode_prefix (JSON *self, SV *jsonstr) |
1827 | void decode_prefix (JSON *self, SV *jsonstr) |
1758 | PPCODE: |
1828 | PPCODE: |
1759 | { |
1829 | { |
1760 | STRLEN offset; |
1830 | char *offset; |
1761 | EXTEND (SP, 2); |
1831 | EXTEND (SP, 2); |
1762 | PUSHs (decode_json (jsonstr, self, &offset)); |
1832 | PUSHs (decode_json (jsonstr, self, &offset)); |
1763 | PUSHs (sv_2mortal (newSVuv (offset))); |
1833 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1764 | } |
1834 | } |
1765 | |
1835 | |
1766 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1836 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1767 | PPCODE: |
1837 | PPCODE: |
1768 | { |
1838 | { |
… | |
… | |
1770 | self->incr_text = newSVpvn ("", 0); |
1840 | self->incr_text = newSVpvn ("", 0); |
1771 | |
1841 | |
1772 | // append data, if any |
1842 | // append data, if any |
1773 | if (jsonstr) |
1843 | if (jsonstr) |
1774 | { |
1844 | { |
1775 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1845 | if (SvUTF8 (jsonstr)) |
1776 | { |
1846 | { |
|
|
1847 | if (!SvUTF8 (self->incr_text)) |
|
|
1848 | { |
1777 | /* utf-8-ness differs, need to upgrade */ |
1849 | /* utf-8-ness differs, need to upgrade */ |
1778 | sv_utf8_upgrade (self->incr_text); |
1850 | sv_utf8_upgrade (self->incr_text); |
1779 | |
1851 | |
1780 | if (self->incr_pos) |
1852 | if (self->incr_pos) |
1781 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1853 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1782 | - (U8 *)SvPVX (self->incr_text); |
1854 | - (U8 *)SvPVX (self->incr_text); |
|
|
1855 | } |
1783 | } |
1856 | } |
|
|
1857 | else if (SvUTF8 (self->incr_text)) |
|
|
1858 | sv_utf8_upgrade (jsonstr); |
1784 | |
1859 | |
1785 | { |
1860 | { |
1786 | STRLEN len; |
1861 | STRLEN len; |
1787 | const char *str = SvPV (jsonstr, len); |
1862 | const char *str = SvPV (jsonstr, len); |
1788 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
1863 | STRLEN cur = SvCUR (self->incr_text); |
|
|
1864 | |
|
|
1865 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
1866 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1867 | |
1789 | Move (str, SvEND (self->incr_text), len, char); |
1868 | Move (str, SvEND (self->incr_text), len, char); |
1790 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1869 | 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 |
1870 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1792 | } |
1871 | } |
1793 | } |
1872 | } |
1794 | |
1873 | |
1795 | if (GIMME_V != G_VOID) |
1874 | if (GIMME_V != G_VOID) |
1796 | do |
1875 | do |
1797 | { |
1876 | { |
1798 | STRLEN offset; |
1877 | char *offset; |
1799 | |
1878 | |
1800 | if (!INCR_DONE (self)) |
1879 | if (!INCR_DONE (self)) |
1801 | { |
1880 | { |
1802 | incr_parse (self); |
1881 | incr_parse (self); |
1803 | |
1882 | |
… | |
… | |
1809 | break; |
1888 | break; |
1810 | } |
1889 | } |
1811 | |
1890 | |
1812 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1891 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1813 | |
1892 | |
1814 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1815 | self->incr_pos -= offset; |
1893 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1816 | self->incr_nest = 0; |
1894 | self->incr_nest = 0; |
1817 | self->incr_mode = 0; |
1895 | self->incr_mode = 0; |
|
|
1896 | |
|
|
1897 | sv_chop (self->incr_text, offset); |
1818 | } |
1898 | } |
1819 | while (GIMME_V == G_ARRAY); |
1899 | while (GIMME_V == G_ARRAY); |
1820 | } |
1900 | } |
1821 | |
1901 | |
1822 | SV *incr_text (JSON *self) |
1902 | SV *incr_text (JSON *self) |