ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.84 by root, Thu Mar 27 06:37:35 2008 UTC vs.
Revision 1.96 by root, Sat May 30 06:26:05 2009 UTC

32#define F_SHRINK 0x00000200UL 32#define F_SHRINK 0x00000200UL
33#define F_ALLOW_BLESSED 0x00000400UL 33#define F_ALLOW_BLESSED 0x00000400UL
34#define F_CONV_BLESSED 0x00000800UL 34#define F_CONV_BLESSED 0x00000800UL
35#define F_RELAXED 0x00001000UL 35#define F_RELAXED 0x00001000UL
36#define F_ALLOW_UNKNOWN 0x00002000UL 36#define F_ALLOW_UNKNOWN 0x00002000UL
37
38#define F_MAXDEPTH 0xf8000000UL
39#define S_MAXDEPTH 27
40#define F_MAXSIZE 0x01f00000UL
41#define S_MAXSIZE 20
42#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 37#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
43 38
44#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
45#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
46
47#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
48#define F_DEFAULT (9UL << S_MAXDEPTH)
49 40
50#define INIT_SIZE 32 // initial scalar size to be allocated 41#define INIT_SIZE 32 // initial scalar size to be allocated
51#define INDENT_STEP 3 // spaces per indentation level 42#define INDENT_STEP 3 // spaces per indentation level
52 43
53#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 44#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
68 59
69#define IN_RANGE_INC(type,val,beg,end) \ 60#define IN_RANGE_INC(type,val,beg,end) \
70 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ 61 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
71 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) 62 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
72 63
64#define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)"
65
73#ifdef USE_ITHREADS 66#ifdef USE_ITHREADS
74# define JSON_SLOW 1 67# define JSON_SLOW 1
75# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 68# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
76#else 69#else
77# define JSON_SLOW 0 70# define JSON_SLOW 0
86 INCR_M_STR, // inside string 79 INCR_M_STR, // inside string
87 INCR_M_BS, // inside backslash 80 INCR_M_BS, // inside backslash
88 INCR_M_JSON // outside anything, count nesting 81 INCR_M_JSON // outside anything, count nesting
89}; 82};
90 83
91#define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) 84#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
92 85
93typedef struct { 86typedef struct {
94 U32 flags; 87 U32 flags;
88 U32 max_depth;
89 STRLEN max_size;
90
95 SV *cb_object; 91 SV *cb_object;
96 HV *cb_sk_object; 92 HV *cb_sk_object;
97 93
98 // for the incremental parser 94 // for the incremental parser
99 SV *incr_text; // the source text so far 95 SV *incr_text; // the source text so far
100 STRLEN incr_pos; // the current offset into the text 96 STRLEN incr_pos; // the current offset into the text
101 int incr_nest; // {[]}-nesting level 97 int incr_nest; // {[]}-nesting level
102 int incr_mode; 98 unsigned char incr_mode;
103} JSON; 99} JSON;
100
101INLINE void
102json_init (JSON *json)
103{
104 Zero (json, 1, JSON);
105 json->max_depth = 512;
106}
104 107
105///////////////////////////////////////////////////////////////////////////// 108/////////////////////////////////////////////////////////////////////////////
106// utility functions 109// utility functions
107 110
108INLINE SV * 111INLINE SV *
118 121
119INLINE void 122INLINE void
120shrink (SV *sv) 123shrink (SV *sv)
121{ 124{
122 sv_utf8_downgrade (sv, 1); 125 sv_utf8_downgrade (sv, 1);
126
123 if (SvLEN (sv) > SvCUR (sv) + 1) 127 if (SvLEN (sv) > SvCUR (sv) + 1)
124 { 128 {
125#ifdef SvPV_shrink_to_cur 129#ifdef SvPV_shrink_to_cur
126 SvPV_shrink_to_cur (sv); 130 SvPV_shrink_to_cur (sv);
127#elif defined (SvPV_renew) 131#elif defined (SvPV_renew)
171 *s++ = 0x80 | ( ch & 0x3f); 175 *s++ = 0x80 | ( ch & 0x3f);
172 176
173 return s; 177 return s;
174} 178}
175 179
180// convert offset pointer to character index, sv must be string
181static STRLEN
182ptr_to_index (SV *sv, char *offset)
183{
184 return SvUTF8 (sv)
185 ? utf8_distance (offset, SvPVX (sv))
186 : offset - SvPVX (sv);
187}
188
176///////////////////////////////////////////////////////////////////////////// 189/////////////////////////////////////////////////////////////////////////////
177// encoder 190// encoder
178 191
179// structure used for encoding JSON 192// structure used for encoding JSON
180typedef struct 193typedef struct
182 char *cur; // SvPVX (sv) + current output position 195 char *cur; // SvPVX (sv) + current output position
183 char *end; // SvEND (sv) 196 char *end; // SvEND (sv)
184 SV *sv; // result scalar 197 SV *sv; // result scalar
185 JSON json; 198 JSON json;
186 U32 indent; // indentation level 199 U32 indent; // indentation level
187 U32 maxdepth; // max. indentation/recursion level
188 UV limit; // escape character values >= this value when encoding 200 UV limit; // escape character values >= this value when encoding
189} enc_t; 201} enc_t;
190 202
191INLINE void 203INLINE void
192need (enc_t *enc, STRLEN len) 204need (enc_t *enc, STRLEN len)
193{ 205{
194 if (expect_false (enc->cur + len >= enc->end)) 206 if (expect_false (enc->cur + len >= enc->end))
195 { 207 {
196 STRLEN cur = enc->cur - SvPVX (enc->sv); 208 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
197 SvGROW (enc->sv, cur + len + 1); 209 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
198 enc->cur = SvPVX (enc->sv) + cur; 210 enc->cur = SvPVX (enc->sv) + cur;
199 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 211 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
200 } 212 }
201} 213}
202 214
277 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 289 (int)((uch - 0x10000) % 0x400 + 0xDC00));
278 enc->cur += 12; 290 enc->cur += 12;
279 } 291 }
280 else 292 else
281 { 293 {
282 static char hexdigit [16] = "0123456789abcdef";
283 need (enc, len += 5); 294 need (enc, len += 5);
284 *enc->cur++ = '\\'; 295 *enc->cur++ = '\\';
285 *enc->cur++ = 'u'; 296 *enc->cur++ = 'u';
286 *enc->cur++ = hexdigit [ uch >> 12 ]; 297 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
287 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 298 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
288 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 299 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
289 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 300 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
290 } 301 }
291 302
292 str += clen; 303 str += clen;
293 } 304 }
294 else if (enc->json.flags & F_LATIN1) 305 else if (enc->json.flags & F_LATIN1)
365static void 376static void
366encode_av (enc_t *enc, AV *av) 377encode_av (enc_t *enc, AV *av)
367{ 378{
368 int i, len = av_len (av); 379 int i, len = av_len (av);
369 380
370 if (enc->indent >= enc->maxdepth) 381 if (enc->indent >= enc->json.max_depth)
371 croak ("data structure too deep (hit recursion limit)"); 382 croak (ERR_NESTING_EXCEEDED);
372 383
373 encode_ch (enc, '['); 384 encode_ch (enc, '[');
374 385
375 if (len >= 0) 386 if (len >= 0)
376 { 387 {
451static void 462static void
452encode_hv (enc_t *enc, HV *hv) 463encode_hv (enc_t *enc, HV *hv)
453{ 464{
454 HE *he; 465 HE *he;
455 466
456 if (enc->indent >= enc->maxdepth) 467 if (enc->indent >= enc->json.max_depth)
457 croak ("data structure too deep (hit recursion limit)"); 468 croak (ERR_NESTING_EXCEEDED);
458 469
459 encode_ch (enc, '{'); 470 encode_ch (enc, '{');
460 471
461 // for canonical output we have to sort by keys first 472 // for canonical output we have to sort by keys first
462 // actually, this is mostly due to the stupid so-called 473 // actually, this is mostly due to the stupid so-called
463 // security workaround added somewhere in 5.8.x. 474 // security workaround added somewhere in 5.8.x
464 // that randomises hash orderings 475 // that randomises hash orderings
465 if (enc->json.flags & F_CANONICAL) 476 if (enc->json.flags & F_CANONICAL)
466 { 477 {
467 int count = hv_iterinit (hv); 478 int count = hv_iterinit (hv);
468 479
742 enc.json = *json; 753 enc.json = *json;
743 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 754 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
744 enc.cur = SvPVX (enc.sv); 755 enc.cur = SvPVX (enc.sv);
745 enc.end = SvEND (enc.sv); 756 enc.end = SvEND (enc.sv);
746 enc.indent = 0; 757 enc.indent = 0;
747 enc.maxdepth = DEC_DEPTH (enc.json.flags);
748 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL 758 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
749 : enc.json.flags & F_LATIN1 ? 0x000100UL 759 : enc.json.flags & F_LATIN1 ? 0x000100UL
750 : 0x110000UL; 760 : 0x110000UL;
751 761
752 SvPOK_only (enc.sv); 762 SvPOK_only (enc.sv);
819 if (*dec->cur != ch) \ 829 if (*dec->cur != ch) \
820 ERR (# ch " expected"); \ 830 ERR (# ch " expected"); \
821 ++dec->cur; \ 831 ++dec->cur; \
822 SE 832 SE
823 833
824#define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") 834#define DEC_INC_DEPTH if (++dec->depth > dec->json.max_depth) ERR (ERR_NESTING_EXCEEDED)
825#define DEC_DEC_DEPTH --dec->depth 835#define DEC_DEC_DEPTH --dec->depth
826 836
827static SV *decode_sv (dec_t *dec); 837static SV *decode_sv (dec_t *dec);
828 838
829static signed char decode_hexdigit[256]; 839static signed char decode_hexdigit[256];
969 { 979 {
970 STRLEN len = cur - buf; 980 STRLEN len = cur - buf;
971 981
972 if (sv) 982 if (sv)
973 { 983 {
974 SvGROW (sv, SvCUR (sv) + len + 1); 984 STRLEN cur = SvCUR (sv);
985
986 if (SvLEN (sv) <= cur + len)
987 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
988
975 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 989 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
976 SvCUR_set (sv, SvCUR (sv) + len); 990 SvCUR_set (sv, SvCUR (sv) + len);
977 } 991 }
978 else 992 else
979 sv = newSVpvn (buf, len); 993 sv = newSVpvn (buf, len);
1403fail: 1417fail:
1404 return 0; 1418 return 0;
1405} 1419}
1406 1420
1407static SV * 1421static SV *
1408decode_json (SV *string, JSON *json, STRLEN *offset_return) 1422decode_json (SV *string, JSON *json, char **offset_return)
1409{ 1423{
1410 dec_t dec; 1424 dec_t dec;
1411 STRLEN offset;
1412 SV *sv; 1425 SV *sv;
1413 1426
1427 /* work around bugs in 5.10 where manipulating magic values
1428 * will perl ignore the magic in subsequent accesses
1429 */
1414 SvGETMAGIC (string); 1430 /*SvGETMAGIC (string);*/
1431 if (SvMAGICAL (string))
1432 string = sv_2mortal (newSVsv (string));
1433
1415 SvUPGRADE (string, SVt_PV); 1434 SvUPGRADE (string, SVt_PV);
1416 1435
1417 if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) 1436 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1437 * assertion with -DDEBUGGING, although SvCUR is documented to
1438 * return the xpv_cur field which certainly exists after upgrading.
1439 * according to nicholas clark, calling SvPOK fixes this.
1440 * But it doesn't fix it, so try another workaround, call SvPV_nolen
1441 * and hope for the best.
1442 * Damnit, SvPV_nolen still trips over yet another assertion. This
1443 * assertion business is seriously broken, try yet another workaround
1444 * for the broken -DDEBUGGING.
1445 */
1446 {
1447#ifdef DEBUGGING
1448 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1449#else
1450 STRLEN offset = SvCUR (string);
1451#endif
1452
1453 if (offset > json->max_size && json->max_size)
1418 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1454 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1419 (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); 1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 }
1420 1457
1421 if (json->flags & F_UTF8) 1458 if (json->flags & F_UTF8)
1422 sv_utf8_downgrade (string, 0); 1459 sv_utf8_downgrade (string, 0);
1423 else 1460 else
1424 sv_utf8_upgrade (string); 1461 sv_utf8_upgrade (string);
1425 1462
1426 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1463 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1427 1464
1428 dec.json = *json; 1465 dec.json = *json;
1429 dec.cur = SvPVX (string); 1466 dec.cur = SvPVX (string);
1430 dec.end = SvEND (string); 1467 dec.end = SvEND (string);
1431 dec.err = 0; 1468 dec.err = 0;
1432 dec.depth = 0; 1469 dec.depth = 0;
1433 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1434 1470
1435 if (dec.json.cb_object || dec.json.cb_sk_object) 1471 if (dec.json.cb_object || dec.json.cb_sk_object)
1436 dec.json.flags |= F_HOOK; 1472 dec.json.flags |= F_HOOK;
1437 1473
1438 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1474 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1439 1475
1440 decode_ws (&dec); 1476 decode_ws (&dec);
1441 sv = decode_sv (&dec); 1477 sv = decode_sv (&dec);
1478
1479 if (offset_return)
1480 *offset_return = dec.cur;
1442 1481
1443 if (!(offset_return || !sv)) 1482 if (!(offset_return || !sv))
1444 { 1483 {
1445 // check for trailing garbage 1484 // check for trailing garbage
1446 decode_ws (&dec); 1485 decode_ws (&dec);
1449 { 1488 {
1450 dec.err = "garbage after JSON object"; 1489 dec.err = "garbage after JSON object";
1451 SvREFCNT_dec (sv); 1490 SvREFCNT_dec (sv);
1452 sv = 0; 1491 sv = 0;
1453 } 1492 }
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 } 1493 }
1465 1494
1466 if (!sv) 1495 if (!sv)
1467 { 1496 {
1468 SV *uni = sv_newmortal (); 1497 SV *uni = sv_newmortal ();
1474 SAVEVPTR (PL_curcop); 1503 SAVEVPTR (PL_curcop);
1475 PL_curcop = &cop; 1504 PL_curcop = &cop;
1476 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1505 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1477 LEAVE; 1506 LEAVE;
1478 1507
1479 croak ("%s, at character offset %d [\"%s\"]", 1508 croak ("%s, at character offset %d (before \"%s\")",
1480 dec.err, 1509 dec.err,
1481 (int)offset, 1510 ptr_to_index (string, dec.cur),
1482 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1511 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1483 } 1512 }
1484 1513
1485 sv = sv_2mortal (sv); 1514 sv = sv_2mortal (sv);
1486 1515
1584 self->incr_mode = INCR_M_STR; 1613 self->incr_mode = INCR_M_STR;
1585 goto incr_m_str; 1614 goto incr_m_str;
1586 1615
1587 case '[': 1616 case '[':
1588 case '{': 1617 case '{':
1589 ++self->incr_nest; 1618 if (++self->incr_nest > self->max_depth)
1619 croak (ERR_NESTING_EXCEEDED);
1590 break; 1620 break;
1591 1621
1592 case ']': 1622 case ']':
1593 case '}': 1623 case '}':
1594 if (!--self->incr_nest) 1624 if (--self->incr_nest <= 0)
1595 goto interrupt; 1625 goto interrupt;
1596 } 1626 }
1597 } 1627 }
1598 } 1628 }
1599 1629
1639void new (char *klass) 1669void new (char *klass)
1640 PPCODE: 1670 PPCODE:
1641{ 1671{
1642 SV *pv = NEWSV (0, sizeof (JSON)); 1672 SV *pv = NEWSV (0, sizeof (JSON));
1643 SvPOK_only (pv); 1673 SvPOK_only (pv);
1644 Zero (SvPVX (pv), 1, JSON); 1674 json_init ((JSON *)SvPVX (pv));
1645 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1646 XPUSHs (sv_2mortal (sv_bless ( 1675 XPUSHs (sv_2mortal (sv_bless (
1647 newRV_noinc (pv), 1676 newRV_noinc (pv),
1648 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) 1677 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1649 ))); 1678 )));
1650} 1679}
1691 get_relaxed = F_RELAXED 1720 get_relaxed = F_RELAXED
1692 get_allow_unknown = F_ALLOW_UNKNOWN 1721 get_allow_unknown = F_ALLOW_UNKNOWN
1693 PPCODE: 1722 PPCODE:
1694 XPUSHs (boolSV (self->flags & ix)); 1723 XPUSHs (boolSV (self->flags & ix));
1695 1724
1696void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1725void max_depth (JSON *self, U32 max_depth = 0x80000000UL)
1697 PPCODE: 1726 PPCODE:
1698{ 1727 self->max_depth = max_depth;
1699 UV log2 = 0;
1700
1701 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1702
1703 while ((1UL << log2) < max_depth)
1704 ++log2;
1705
1706 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1707
1708 XPUSHs (ST (0)); 1728 XPUSHs (ST (0));
1709}
1710 1729
1711U32 get_max_depth (JSON *self) 1730U32 get_max_depth (JSON *self)
1712 CODE: 1731 CODE:
1713 RETVAL = DEC_DEPTH (self->flags); 1732 RETVAL = self->max_depth;
1714 OUTPUT: 1733 OUTPUT:
1715 RETVAL 1734 RETVAL
1716 1735
1717void max_size (JSON *self, UV max_size = 0) 1736void max_size (JSON *self, U32 max_size = 0)
1718 PPCODE: 1737 PPCODE:
1719{ 1738 self->max_size = max_size;
1720 UV log2 = 0;
1721
1722 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1723 if (max_size == 1) max_size = 2;
1724
1725 while ((1UL << log2) < max_size)
1726 ++log2;
1727
1728 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1729
1730 XPUSHs (ST (0)); 1739 XPUSHs (ST (0));
1731}
1732 1740
1733int get_max_size (JSON *self) 1741int get_max_size (JSON *self)
1734 CODE: 1742 CODE:
1735 RETVAL = DEC_SIZE (self->flags); 1743 RETVAL = self->max_size;
1736 OUTPUT: 1744 OUTPUT:
1737 RETVAL 1745 RETVAL
1738 1746
1739void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1747void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1740 PPCODE: 1748 PPCODE:
1776 XPUSHs (decode_json (jsonstr, self, 0)); 1784 XPUSHs (decode_json (jsonstr, self, 0));
1777 1785
1778void decode_prefix (JSON *self, SV *jsonstr) 1786void decode_prefix (JSON *self, SV *jsonstr)
1779 PPCODE: 1787 PPCODE:
1780{ 1788{
1781 STRLEN offset; 1789 char *offset;
1782 EXTEND (SP, 2); 1790 EXTEND (SP, 2);
1783 PUSHs (decode_json (jsonstr, self, &offset)); 1791 PUSHs (decode_json (jsonstr, self, &offset));
1784 PUSHs (sv_2mortal (newSVuv (offset))); 1792 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1785} 1793}
1786 1794
1787void incr_parse (JSON *self, SV *jsonstr = 0) 1795void incr_parse (JSON *self, SV *jsonstr = 0)
1788 PPCODE: 1796 PPCODE:
1789{ 1797{
1791 self->incr_text = newSVpvn ("", 0); 1799 self->incr_text = newSVpvn ("", 0);
1792 1800
1793 // append data, if any 1801 // append data, if any
1794 if (jsonstr) 1802 if (jsonstr)
1795 { 1803 {
1796 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1804 if (SvUTF8 (jsonstr))
1797 { 1805 {
1806 if (!SvUTF8 (self->incr_text))
1807 {
1798 /* utf-8-ness differs, need to upgrade */ 1808 /* utf-8-ness differs, need to upgrade */
1799 sv_utf8_upgrade (self->incr_text); 1809 sv_utf8_upgrade (self->incr_text);
1800 1810
1801 if (self->incr_pos) 1811 if (self->incr_pos)
1802 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1812 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1803 - (U8 *)SvPVX (self->incr_text); 1813 - (U8 *)SvPVX (self->incr_text);
1814 }
1804 } 1815 }
1816 else if (SvUTF8 (self->incr_text))
1817 sv_utf8_upgrade (jsonstr);
1805 1818
1806 { 1819 {
1807 STRLEN len; 1820 STRLEN len;
1808 const char *str = SvPV (jsonstr, len); 1821 const char *str = SvPV (jsonstr, len);
1809 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1822 STRLEN cur = SvCUR (self->incr_text);
1823
1824 if (SvLEN (self->incr_text) <= cur + len)
1825 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1826
1810 Move (str, SvEND (self->incr_text), len, char); 1827 Move (str, SvEND (self->incr_text), len, char);
1811 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1828 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1812 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1829 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1813 } 1830 }
1814 } 1831 }
1815 1832
1816 if (GIMME_V != G_VOID) 1833 if (GIMME_V != G_VOID)
1817 do 1834 do
1818 { 1835 {
1819 STRLEN offset; 1836 char *offset;
1820 1837
1821 if (!INCR_DONE (self)) 1838 if (!INCR_DONE (self))
1822 { 1839 {
1823 incr_parse (self); 1840 incr_parse (self);
1841
1842 if (self->incr_pos > self->max_size && self->max_size)
1843 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1844 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1845
1824 if (!INCR_DONE (self)) 1846 if (!INCR_DONE (self))
1825 break; 1847 break;
1826 } 1848 }
1827 1849
1828 XPUSHs (decode_json (self->incr_text, self, &offset)); 1850 XPUSHs (decode_json (self->incr_text, self, &offset));
1829 1851
1830 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1831 self->incr_pos -= offset; 1852 self->incr_pos -= offset - SvPVX (self->incr_text);
1832 self->incr_nest = 0; 1853 self->incr_nest = 0;
1833 self->incr_mode = 0; 1854 self->incr_mode = 0;
1855
1856 sv_chop (self->incr_text, offset);
1834 } 1857 }
1835 while (GIMME_V == G_ARRAY); 1858 while (GIMME_V == G_ARRAY);
1836} 1859}
1837 1860
1838SV *incr_text (JSON *self) 1861SV *incr_text (JSON *self)
1857 self->incr_nest = 0; 1880 self->incr_nest = 0;
1858 self->incr_mode = 0; 1881 self->incr_mode = 0;
1859 } 1882 }
1860} 1883}
1861 1884
1885void incr_reset (JSON *self)
1886 CODE:
1887{
1888 SvREFCNT_dec (self->incr_text);
1889 self->incr_text = 0;
1890 self->incr_pos = 0;
1891 self->incr_nest = 0;
1892 self->incr_mode = 0;
1893}
1894
1862void DESTROY (JSON *self) 1895void DESTROY (JSON *self)
1863 CODE: 1896 CODE:
1864 SvREFCNT_dec (self->cb_sk_object); 1897 SvREFCNT_dec (self->cb_sk_object);
1865 SvREFCNT_dec (self->cb_object); 1898 SvREFCNT_dec (self->cb_object);
1866 SvREFCNT_dec (self->incr_text); 1899 SvREFCNT_dec (self->incr_text);
1871 ALIAS: 1904 ALIAS:
1872 to_json_ = 0 1905 to_json_ = 0
1873 encode_json = F_UTF8 1906 encode_json = F_UTF8
1874 PPCODE: 1907 PPCODE:
1875{ 1908{
1876 JSON json = { F_DEFAULT | ix }; 1909 JSON json;
1910 json_init (&json);
1911 json.flags |= ix;
1877 XPUSHs (encode_json (scalar, &json)); 1912 XPUSHs (encode_json (scalar, &json));
1878} 1913}
1879 1914
1880void decode_json (SV *jsonstr) 1915void decode_json (SV *jsonstr)
1881 ALIAS: 1916 ALIAS:
1882 from_json_ = 0 1917 from_json_ = 0
1883 decode_json = F_UTF8 1918 decode_json = F_UTF8
1884 PPCODE: 1919 PPCODE:
1885{ 1920{
1886 JSON json = { F_DEFAULT | ix }; 1921 JSON json;
1922 json_init (&json);
1923 json.flags |= ix;
1887 XPUSHs (decode_json (jsonstr, &json, 0)); 1924 XPUSHs (decode_json (jsonstr, &json, 0));
1888} 1925}
1889 1926
1890 1927

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines