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.87 by root, Tue Jun 3 06:43:45 2008 UTC vs.
Revision 1.99 by root, Sat Aug 8 10:06:02 2009 UTC

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#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) 22#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2)
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)
175 *s++ = 0x80 | ( ch & 0x3f); 175 *s++ = 0x80 | ( ch & 0x3f);
176 176
177 return s; 177 return s;
178} 178}
179 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
180///////////////////////////////////////////////////////////////////////////// 189/////////////////////////////////////////////////////////////////////////////
181// encoder 190// encoder
182 191
183// structure used for encoding JSON 192// structure used for encoding JSON
184typedef struct 193typedef struct
194INLINE void 203INLINE void
195need (enc_t *enc, STRLEN len) 204need (enc_t *enc, STRLEN len)
196{ 205{
197 if (expect_false (enc->cur + len >= enc->end)) 206 if (expect_false (enc->cur + len >= enc->end))
198 { 207 {
199 STRLEN cur = enc->cur - SvPVX (enc->sv); 208 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 209 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 210 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 211 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 212 }
204} 213}
205 214
280 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 289 (int)((uch - 0x10000) % 0x400 + 0xDC00));
281 enc->cur += 12; 290 enc->cur += 12;
282 } 291 }
283 else 292 else
284 { 293 {
285 static char hexdigit [16] = "0123456789abcdef";
286 need (enc, len += 5); 294 need (enc, len += 5);
287 *enc->cur++ = '\\'; 295 *enc->cur++ = '\\';
288 *enc->cur++ = 'u'; 296 *enc->cur++ = 'u';
289 *enc->cur++ = hexdigit [ uch >> 12 ]; 297 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
290 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 298 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
291 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 299 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
292 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 300 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
293 } 301 }
294 302
295 str += clen; 303 str += clen;
296 } 304 }
297 else if (enc->json.flags & F_LATIN1) 305 else if (enc->json.flags & F_LATIN1)
461 469
462 encode_ch (enc, '{'); 470 encode_ch (enc, '{');
463 471
464 // for canonical output we have to sort by keys first 472 // for canonical output we have to sort by keys first
465 // actually, this is mostly due to the stupid so-called 473 // actually, this is mostly due to the stupid so-called
466 // security workaround added somewhere in 5.8.x. 474 // security workaround added somewhere in 5.8.x
467 // that randomises hash orderings 475 // that randomises hash orderings
468 if (enc->json.flags & F_CANONICAL) 476 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
469 { 477 {
470 int count = hv_iterinit (hv); 478 int count = hv_iterinit (hv);
471 479
472 if (SvMAGICAL (hv)) 480 if (SvMAGICAL (hv))
473 { 481 {
971 { 979 {
972 STRLEN len = cur - buf; 980 STRLEN len = cur - buf;
973 981
974 if (sv) 982 if (sv)
975 { 983 {
976 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
977 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 989 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
978 SvCUR_set (sv, SvCUR (sv) + len); 990 SvCUR_set (sv, SvCUR (sv) + len);
979 } 991 }
980 else 992 else
981 sv = newSVpvn (buf, len); 993 sv = newSVpvn (buf, len);
1072 1084
1073 // special case the rather common 1..5-digit-int case 1085 // special case the rather common 1..5-digit-int case
1074 if (*start == '-') 1086 if (*start == '-')
1075 switch (len) 1087 switch (len)
1076 { 1088 {
1077 case 2: return newSViv (-( start [1] - '0' * 1)); 1089 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1078 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1090 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
1079 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1091 case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
1080 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1092 case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1081 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); 1093 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1082 } 1094 }
1083 else 1095 else
1084 switch (len) 1096 switch (len)
1085 { 1097 {
1086 case 1: return newSViv ( start [0] - '0' * 1); 1098 case 1: return newSViv ( start [0] - '0' * 1);
1087 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1099 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1088 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1100 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
1089 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1101 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1090 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); 1102 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1091 } 1103 }
1092 1104
1093 { 1105 {
1094 UV uv; 1106 UV uv;
1095 int numtype = grok_number (start, len, &uv); 1107 int numtype = grok_number (start, len, &uv);
1405fail: 1417fail:
1406 return 0; 1418 return 0;
1407} 1419}
1408 1420
1409static SV * 1421static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1422decode_json (SV *string, JSON *json, char **offset_return)
1411{ 1423{
1412 dec_t dec; 1424 dec_t dec;
1413 STRLEN offset;
1414 SV *sv; 1425 SV *sv;
1415 1426
1427 /* work around bugs in 5.10 where manipulating magic values
1428 * will perl ignore the magic in subsequent accesses
1429 */
1416 SvGETMAGIC (string); 1430 /*SvGETMAGIC (string);*/
1431 if (SvMAGICAL (string))
1432 string = sv_2mortal (newSVsv (string));
1433
1417 SvUPGRADE (string, SVt_PV); 1434 SvUPGRADE (string, SVt_PV);
1418 1435
1419 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1436 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1420 * assertion with -DDEBUGGING, although SvCUR is documented to 1437 * assertion with -DDEBUGGING, although SvCUR is documented to
1421 * return the xpv_cur field which certainly exists after upgrading. 1438 * return the xpv_cur field which certainly exists after upgrading.
1422 * according to nicholas clark, calling SvPOK fixes this. 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.
1423 */ 1445 */
1424 SvPOK (string); 1446 {
1447#ifdef DEBUGGING
1448 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1449#else
1450 STRLEN offset = SvCUR (string);
1451#endif
1425 1452
1426 if (SvCUR (string) > json->max_size && json->max_size) 1453 if (offset > json->max_size && json->max_size)
1427 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",
1428 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 }
1429 1457
1430 if (json->flags & F_UTF8) 1458 if (json->flags & F_UTF8)
1431 sv_utf8_downgrade (string, 0); 1459 sv_utf8_downgrade (string, 0);
1432 else 1460 else
1433 sv_utf8_upgrade (string); 1461 sv_utf8_upgrade (string);
1445 1473
1446 *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
1447 1475
1448 decode_ws (&dec); 1476 decode_ws (&dec);
1449 sv = decode_sv (&dec); 1477 sv = decode_sv (&dec);
1478
1479 if (offset_return)
1480 *offset_return = dec.cur;
1450 1481
1451 if (!(offset_return || !sv)) 1482 if (!(offset_return || !sv))
1452 { 1483 {
1453 // check for trailing garbage 1484 // check for trailing garbage
1454 decode_ws (&dec); 1485 decode_ws (&dec);
1457 { 1488 {
1458 dec.err = "garbage after JSON object"; 1489 dec.err = "garbage after JSON object";
1459 SvREFCNT_dec (sv); 1490 SvREFCNT_dec (sv);
1460 sv = 0; 1491 sv = 0;
1461 } 1492 }
1462 }
1463
1464 if (offset_return || !sv)
1465 {
1466 offset = dec.json.flags & F_UTF8
1467 ? dec.cur - SvPVX (string)
1468 : utf8_distance (dec.cur, SvPVX (string));
1469
1470 if (offset_return)
1471 *offset_return = offset;
1472 } 1493 }
1473 1494
1474 if (!sv) 1495 if (!sv)
1475 { 1496 {
1476 SV *uni = sv_newmortal (); 1497 SV *uni = sv_newmortal ();
1482 SAVEVPTR (PL_curcop); 1503 SAVEVPTR (PL_curcop);
1483 PL_curcop = &cop; 1504 PL_curcop = &cop;
1484 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);
1485 LEAVE; 1506 LEAVE;
1486 1507
1487 croak ("%s, at character offset %d [\"%s\"]", 1508 croak ("%s, at character offset %d (before \"%s\")",
1488 dec.err, 1509 dec.err,
1489 (int)offset, 1510 ptr_to_index (string, dec.cur),
1490 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1511 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1491 } 1512 }
1492 1513
1493 sv = sv_2mortal (sv); 1514 sv = sv_2mortal (sv);
1494 1515
1598 croak (ERR_NESTING_EXCEEDED); 1619 croak (ERR_NESTING_EXCEEDED);
1599 break; 1620 break;
1600 1621
1601 case ']': 1622 case ']':
1602 case '}': 1623 case '}':
1603 if (!--self->incr_nest) 1624 if (--self->incr_nest <= 0)
1604 goto interrupt; 1625 goto interrupt;
1605 } 1626 }
1606 } 1627 }
1607 } 1628 }
1608 1629
1634 json_stash = gv_stashpv ("JSON::XS" , 1); 1655 json_stash = gv_stashpv ("JSON::XS" , 1);
1635 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1656 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1636 1657
1637 json_true = get_bool ("JSON::XS::true"); 1658 json_true = get_bool ("JSON::XS::true");
1638 json_false = get_bool ("JSON::XS::false"); 1659 json_false = get_bool ("JSON::XS::false");
1660
1661 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1639} 1662}
1640 1663
1641PROTOTYPES: DISABLE 1664PROTOTYPES: DISABLE
1642 1665
1643void CLONE (...) 1666void CLONE (...)
1763 XPUSHs (decode_json (jsonstr, self, 0)); 1786 XPUSHs (decode_json (jsonstr, self, 0));
1764 1787
1765void decode_prefix (JSON *self, SV *jsonstr) 1788void decode_prefix (JSON *self, SV *jsonstr)
1766 PPCODE: 1789 PPCODE:
1767{ 1790{
1768 STRLEN offset; 1791 char *offset;
1769 EXTEND (SP, 2); 1792 EXTEND (SP, 2);
1770 PUSHs (decode_json (jsonstr, self, &offset)); 1793 PUSHs (decode_json (jsonstr, self, &offset));
1771 PUSHs (sv_2mortal (newSVuv (offset))); 1794 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1772} 1795}
1773 1796
1774void incr_parse (JSON *self, SV *jsonstr = 0) 1797void incr_parse (JSON *self, SV *jsonstr = 0)
1775 PPCODE: 1798 PPCODE:
1776{ 1799{
1778 self->incr_text = newSVpvn ("", 0); 1801 self->incr_text = newSVpvn ("", 0);
1779 1802
1780 // append data, if any 1803 // append data, if any
1781 if (jsonstr) 1804 if (jsonstr)
1782 { 1805 {
1783 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1806 if (SvUTF8 (jsonstr))
1784 { 1807 {
1808 if (!SvUTF8 (self->incr_text))
1809 {
1785 /* utf-8-ness differs, need to upgrade */ 1810 /* utf-8-ness differs, need to upgrade */
1786 sv_utf8_upgrade (self->incr_text); 1811 sv_utf8_upgrade (self->incr_text);
1787 1812
1788 if (self->incr_pos) 1813 if (self->incr_pos)
1789 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1814 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1790 - (U8 *)SvPVX (self->incr_text); 1815 - (U8 *)SvPVX (self->incr_text);
1816 }
1791 } 1817 }
1818 else if (SvUTF8 (self->incr_text))
1819 sv_utf8_upgrade (jsonstr);
1792 1820
1793 { 1821 {
1794 STRLEN len; 1822 STRLEN len;
1795 const char *str = SvPV (jsonstr, len); 1823 const char *str = SvPV (jsonstr, len);
1796 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1824 STRLEN cur = SvCUR (self->incr_text);
1825
1826 if (SvLEN (self->incr_text) <= cur + len)
1827 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1828
1797 Move (str, SvEND (self->incr_text), len, char); 1829 Move (str, SvEND (self->incr_text), len, char);
1798 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1830 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1799 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1831 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1800 } 1832 }
1801 } 1833 }
1802 1834
1803 if (GIMME_V != G_VOID) 1835 if (GIMME_V != G_VOID)
1804 do 1836 do
1805 { 1837 {
1806 STRLEN offset; 1838 char *offset;
1807 1839
1808 if (!INCR_DONE (self)) 1840 if (!INCR_DONE (self))
1809 { 1841 {
1810 incr_parse (self); 1842 incr_parse (self);
1811 1843
1817 break; 1849 break;
1818 } 1850 }
1819 1851
1820 XPUSHs (decode_json (self->incr_text, self, &offset)); 1852 XPUSHs (decode_json (self->incr_text, self, &offset));
1821 1853
1822 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1823 self->incr_pos -= offset; 1854 self->incr_pos -= offset - SvPVX (self->incr_text);
1824 self->incr_nest = 0; 1855 self->incr_nest = 0;
1825 self->incr_mode = 0; 1856 self->incr_mode = 0;
1857
1858 sv_chop (self->incr_text, offset);
1826 } 1859 }
1827 while (GIMME_V == G_ARRAY); 1860 while (GIMME_V == G_ARRAY);
1828} 1861}
1829 1862
1830SV *incr_text (JSON *self) 1863SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines