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.85 by root, Sat Apr 5 18:15:46 2008 UTC vs.
Revision 1.96 by root, Sat May 30 06:26:05 2009 UTC

79 INCR_M_STR, // inside string 79 INCR_M_STR, // inside string
80 INCR_M_BS, // inside backslash 80 INCR_M_BS, // inside backslash
81 INCR_M_JSON // outside anything, count nesting 81 INCR_M_JSON // outside anything, count nesting
82}; 82};
83 83
84#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)
85 85
86typedef struct { 86typedef struct {
87 U32 flags; 87 U32 flags;
88 U32 max_depth; 88 U32 max_depth;
89 STRLEN max_size; 89 STRLEN max_size;
92 HV *cb_sk_object; 92 HV *cb_sk_object;
93 93
94 // for the incremental parser 94 // for the incremental parser
95 SV *incr_text; // the source text so far 95 SV *incr_text; // the source text so far
96 STRLEN incr_pos; // the current offset into the text 96 STRLEN incr_pos; // the current offset into the text
97 unsigned char incr_nest; // {[]}-nesting level 97 int incr_nest; // {[]}-nesting level
98 unsigned char incr_mode; 98 unsigned char incr_mode;
99} JSON; 99} JSON;
100 100
101INLINE void 101INLINE void
102json_init (JSON *json) 102json_init (JSON *json)
121 121
122INLINE void 122INLINE void
123shrink (SV *sv) 123shrink (SV *sv)
124{ 124{
125 sv_utf8_downgrade (sv, 1); 125 sv_utf8_downgrade (sv, 1);
126
126 if (SvLEN (sv) > SvCUR (sv) + 1) 127 if (SvLEN (sv) > SvCUR (sv) + 1)
127 { 128 {
128#ifdef SvPV_shrink_to_cur 129#ifdef SvPV_shrink_to_cur
129 SvPV_shrink_to_cur (sv); 130 SvPV_shrink_to_cur (sv);
130#elif defined (SvPV_renew) 131#elif defined (SvPV_renew)
174 *s++ = 0x80 | ( ch & 0x3f); 175 *s++ = 0x80 | ( ch & 0x3f);
175 176
176 return s; 177 return s;
177} 178}
178 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
179///////////////////////////////////////////////////////////////////////////// 189/////////////////////////////////////////////////////////////////////////////
180// encoder 190// encoder
181 191
182// structure used for encoding JSON 192// structure used for encoding JSON
183typedef struct 193typedef struct
193INLINE void 203INLINE void
194need (enc_t *enc, STRLEN len) 204need (enc_t *enc, STRLEN len)
195{ 205{
196 if (expect_false (enc->cur + len >= enc->end)) 206 if (expect_false (enc->cur + len >= enc->end))
197 { 207 {
198 STRLEN cur = enc->cur - SvPVX (enc->sv); 208 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
199 SvGROW (enc->sv, cur + len + 1); 209 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
200 enc->cur = SvPVX (enc->sv) + cur; 210 enc->cur = SvPVX (enc->sv) + cur;
201 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 211 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
202 } 212 }
203} 213}
204 214
279 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 289 (int)((uch - 0x10000) % 0x400 + 0xDC00));
280 enc->cur += 12; 290 enc->cur += 12;
281 } 291 }
282 else 292 else
283 { 293 {
284 static char hexdigit [16] = "0123456789abcdef";
285 need (enc, len += 5); 294 need (enc, len += 5);
286 *enc->cur++ = '\\'; 295 *enc->cur++ = '\\';
287 *enc->cur++ = 'u'; 296 *enc->cur++ = 'u';
288 *enc->cur++ = hexdigit [ uch >> 12 ]; 297 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
289 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 298 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
290 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 299 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
291 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 300 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
292 } 301 }
293 302
294 str += clen; 303 str += clen;
295 } 304 }
296 else if (enc->json.flags & F_LATIN1) 305 else if (enc->json.flags & F_LATIN1)
460 469
461 encode_ch (enc, '{'); 470 encode_ch (enc, '{');
462 471
463 // for canonical output we have to sort by keys first 472 // for canonical output we have to sort by keys first
464 // actually, this is mostly due to the stupid so-called 473 // actually, this is mostly due to the stupid so-called
465 // security workaround added somewhere in 5.8.x. 474 // security workaround added somewhere in 5.8.x
466 // that randomises hash orderings 475 // that randomises hash orderings
467 if (enc->json.flags & F_CANONICAL) 476 if (enc->json.flags & F_CANONICAL)
468 { 477 {
469 int count = hv_iterinit (hv); 478 int count = hv_iterinit (hv);
470 479
970 { 979 {
971 STRLEN len = cur - buf; 980 STRLEN len = cur - buf;
972 981
973 if (sv) 982 if (sv)
974 { 983 {
975 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
976 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 989 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
977 SvCUR_set (sv, SvCUR (sv) + len); 990 SvCUR_set (sv, SvCUR (sv) + len);
978 } 991 }
979 else 992 else
980 sv = newSVpvn (buf, len); 993 sv = newSVpvn (buf, len);
1404fail: 1417fail:
1405 return 0; 1418 return 0;
1406} 1419}
1407 1420
1408static SV * 1421static SV *
1409decode_json (SV *string, JSON *json, STRLEN *offset_return) 1422decode_json (SV *string, JSON *json, char **offset_return)
1410{ 1423{
1411 dec_t dec; 1424 dec_t dec;
1412 STRLEN offset;
1413 SV *sv; 1425 SV *sv;
1414 1426
1427 /* work around bugs in 5.10 where manipulating magic values
1428 * will perl ignore the magic in subsequent accesses
1429 */
1415 SvGETMAGIC (string); 1430 /*SvGETMAGIC (string);*/
1431 if (SvMAGICAL (string))
1432 string = sv_2mortal (newSVsv (string));
1433
1416 SvUPGRADE (string, SVt_PV); 1434 SvUPGRADE (string, SVt_PV);
1417 1435
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
1418 if (SvCUR (string) > json->max_size && json->max_size) 1453 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", 1454 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); 1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 }
1421 1457
1422 if (json->flags & F_UTF8) 1458 if (json->flags & F_UTF8)
1423 sv_utf8_downgrade (string, 0); 1459 sv_utf8_downgrade (string, 0);
1424 else 1460 else
1425 sv_utf8_upgrade (string); 1461 sv_utf8_upgrade (string);
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
1590 croak (ERR_NESTING_EXCEEDED); 1619 croak (ERR_NESTING_EXCEEDED);
1591 break; 1620 break;
1592 1621
1593 case ']': 1622 case ']':
1594 case '}': 1623 case '}':
1595 if (!--self->incr_nest) 1624 if (--self->incr_nest <= 0)
1596 goto interrupt; 1625 goto interrupt;
1597 } 1626 }
1598 } 1627 }
1599 } 1628 }
1600 1629
1755 XPUSHs (decode_json (jsonstr, self, 0)); 1784 XPUSHs (decode_json (jsonstr, self, 0));
1756 1785
1757void decode_prefix (JSON *self, SV *jsonstr) 1786void decode_prefix (JSON *self, SV *jsonstr)
1758 PPCODE: 1787 PPCODE:
1759{ 1788{
1760 STRLEN offset; 1789 char *offset;
1761 EXTEND (SP, 2); 1790 EXTEND (SP, 2);
1762 PUSHs (decode_json (jsonstr, self, &offset)); 1791 PUSHs (decode_json (jsonstr, self, &offset));
1763 PUSHs (sv_2mortal (newSVuv (offset))); 1792 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1764} 1793}
1765 1794
1766void incr_parse (JSON *self, SV *jsonstr = 0) 1795void incr_parse (JSON *self, SV *jsonstr = 0)
1767 PPCODE: 1796 PPCODE:
1768{ 1797{
1770 self->incr_text = newSVpvn ("", 0); 1799 self->incr_text = newSVpvn ("", 0);
1771 1800
1772 // append data, if any 1801 // append data, if any
1773 if (jsonstr) 1802 if (jsonstr)
1774 { 1803 {
1775 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1804 if (SvUTF8 (jsonstr))
1776 { 1805 {
1806 if (!SvUTF8 (self->incr_text))
1807 {
1777 /* utf-8-ness differs, need to upgrade */ 1808 /* utf-8-ness differs, need to upgrade */
1778 sv_utf8_upgrade (self->incr_text); 1809 sv_utf8_upgrade (self->incr_text);
1779 1810
1780 if (self->incr_pos) 1811 if (self->incr_pos)
1781 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)
1782 - (U8 *)SvPVX (self->incr_text); 1813 - (U8 *)SvPVX (self->incr_text);
1814 }
1783 } 1815 }
1816 else if (SvUTF8 (self->incr_text))
1817 sv_utf8_upgrade (jsonstr);
1784 1818
1785 { 1819 {
1786 STRLEN len; 1820 STRLEN len;
1787 const char *str = SvPV (jsonstr, len); 1821 const char *str = SvPV (jsonstr, len);
1788 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
1789 Move (str, SvEND (self->incr_text), len, char); 1827 Move (str, SvEND (self->incr_text), len, char);
1790 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1828 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 1829 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1792 } 1830 }
1793 } 1831 }
1794 1832
1795 if (GIMME_V != G_VOID) 1833 if (GIMME_V != G_VOID)
1796 do 1834 do
1797 { 1835 {
1798 STRLEN offset; 1836 char *offset;
1799 1837
1800 if (!INCR_DONE (self)) 1838 if (!INCR_DONE (self))
1801 { 1839 {
1802 incr_parse (self); 1840 incr_parse (self);
1803 1841
1809 break; 1847 break;
1810 } 1848 }
1811 1849
1812 XPUSHs (decode_json (self->incr_text, self, &offset)); 1850 XPUSHs (decode_json (self->incr_text, self, &offset));
1813 1851
1814 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1815 self->incr_pos -= offset; 1852 self->incr_pos -= offset - SvPVX (self->incr_text);
1816 self->incr_nest = 0; 1853 self->incr_nest = 0;
1817 self->incr_mode = 0; 1854 self->incr_mode = 0;
1855
1856 sv_chop (self->incr_text, offset);
1818 } 1857 }
1819 while (GIMME_V == G_ARRAY); 1858 while (GIMME_V == G_ARRAY);
1820} 1859}
1821 1860
1822SV *incr_text (JSON *self) 1861SV *incr_text (JSON *self)
1841 self->incr_nest = 0; 1880 self->incr_nest = 0;
1842 self->incr_mode = 0; 1881 self->incr_mode = 0;
1843 } 1882 }
1844} 1883}
1845 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
1846void DESTROY (JSON *self) 1895void DESTROY (JSON *self)
1847 CODE: 1896 CODE:
1848 SvREFCNT_dec (self->cb_sk_object); 1897 SvREFCNT_dec (self->cb_sk_object);
1849 SvREFCNT_dec (self->cb_object); 1898 SvREFCNT_dec (self->cb_object);
1850 SvREFCNT_dec (self->incr_text); 1899 SvREFCNT_dec (self->incr_text);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines