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.89 by root, Sat Jul 19 04:21:32 2008 UTC vs.
Revision 1.102 by root, Sat Oct 10 01:48:50 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// 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
175 *s++ = 0x80 | ( ch & 0x3f); 176 *s++ = 0x80 | ( ch & 0x3f);
176 177
177 return s; 178 return s;
178} 179}
179 180
181// convert offset pointer to character index, sv must be string
182static STRLEN
183ptr_to_index (SV *sv, char *offset)
184{
185 return SvUTF8 (sv)
186 ? utf8_distance (offset, SvPVX (sv))
187 : offset - SvPVX (sv);
188}
189
180///////////////////////////////////////////////////////////////////////////// 190/////////////////////////////////////////////////////////////////////////////
181// encoder 191// encoder
182 192
183// structure used for encoding JSON 193// structure used for encoding JSON
184typedef struct 194typedef struct
194INLINE void 204INLINE void
195need (enc_t *enc, STRLEN len) 205need (enc_t *enc, STRLEN len)
196{ 206{
197 if (expect_false (enc->cur + len >= enc->end)) 207 if (expect_false (enc->cur + len >= enc->end))
198 { 208 {
199 STRLEN cur = enc->cur - SvPVX (enc->sv); 209 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 210 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 211 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 212 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 213 }
204} 214}
205 215
280 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 290 (int)((uch - 0x10000) % 0x400 + 0xDC00));
281 enc->cur += 12; 291 enc->cur += 12;
282 } 292 }
283 else 293 else
284 { 294 {
285 static char hexdigit [16] = "0123456789abcdef";
286 need (enc, len += 5); 295 need (enc, len += 5);
287 *enc->cur++ = '\\'; 296 *enc->cur++ = '\\';
288 *enc->cur++ = 'u'; 297 *enc->cur++ = 'u';
289 *enc->cur++ = hexdigit [ uch >> 12 ]; 298 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
290 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 299 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
291 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 300 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
292 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 301 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
293 } 302 }
294 303
295 str += clen; 304 str += clen;
296 } 305 }
297 else if (enc->json.flags & F_LATIN1) 306 else if (enc->json.flags & F_LATIN1)
461 470
462 encode_ch (enc, '{'); 471 encode_ch (enc, '{');
463 472
464 // for canonical output we have to sort by keys first 473 // for canonical output we have to sort by keys first
465 // actually, this is mostly due to the stupid so-called 474 // actually, this is mostly due to the stupid so-called
466 // security workaround added somewhere in 5.8.x. 475 // security workaround added somewhere in 5.8.x
467 // that randomises hash orderings 476 // that randomises hash orderings
468 if (enc->json.flags & F_CANONICAL) 477 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
469 { 478 {
470 int count = hv_iterinit (hv); 479 int count = hv_iterinit (hv);
471 480
472 if (SvMAGICAL (hv)) 481 if (SvMAGICAL (hv))
473 { 482 {
751 : enc.json.flags & F_LATIN1 ? 0x000100UL 760 : enc.json.flags & F_LATIN1 ? 0x000100UL
752 : 0x110000UL; 761 : 0x110000UL;
753 762
754 SvPOK_only (enc.sv); 763 SvPOK_only (enc.sv);
755 encode_sv (&enc, scalar); 764 encode_sv (&enc, scalar);
765 encode_nl (&enc);
756 766
757 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 767 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
758 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 768 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
759 769
760 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 770 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
940 else if (expect_true (ch >= 0x20 && ch < 0x80)) 950 else if (expect_true (ch >= 0x20 && ch < 0x80))
941 *cur++ = ch; 951 *cur++ = ch;
942 else if (ch >= 0x80) 952 else if (ch >= 0x80)
943 { 953 {
944 STRLEN clen; 954 STRLEN clen;
945 UV uch;
946 955
947 --dec_cur; 956 --dec_cur;
948 957
949 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 958 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
950 if (clen == (STRLEN)-1) 959 if (clen == (STRLEN)-1)
951 ERR ("malformed UTF-8 character in JSON string"); 960 ERR ("malformed UTF-8 character in JSON string");
952 961
953 do 962 do
954 *cur++ = *dec_cur++; 963 *cur++ = *dec_cur++;
971 { 980 {
972 STRLEN len = cur - buf; 981 STRLEN len = cur - buf;
973 982
974 if (sv) 983 if (sv)
975 { 984 {
976 SvGROW (sv, SvCUR (sv) + len + 1); 985 STRLEN cur = SvCUR (sv);
986
987 if (SvLEN (sv) <= cur + len)
988 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
989
977 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 990 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
978 SvCUR_set (sv, SvCUR (sv) + len); 991 SvCUR_set (sv, SvCUR (sv) + len);
979 } 992 }
980 else 993 else
981 sv = newSVpvn (buf, len); 994 sv = newSVpvn (buf, len);
1072 1085
1073 // special case the rather common 1..5-digit-int case 1086 // special case the rather common 1..5-digit-int case
1074 if (*start == '-') 1087 if (*start == '-')
1075 switch (len) 1088 switch (len)
1076 { 1089 {
1077 case 2: return newSViv (-( start [1] - '0' * 1)); 1090 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1078 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1091 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)); 1092 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)); 1093 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)); 1094 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1082 } 1095 }
1083 else 1096 else
1084 switch (len) 1097 switch (len)
1085 { 1098 {
1086 case 1: return newSViv ( start [0] - '0' * 1); 1099 case 1: return newSViv ( start [0] - '0' * 1);
1087 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1100 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); 1101 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); 1102 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); 1103 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1091 } 1104 }
1092 1105
1093 { 1106 {
1094 UV uv; 1107 UV uv;
1095 int numtype = grok_number (start, len, &uv); 1108 int numtype = grok_number (start, len, &uv);
1405fail: 1418fail:
1406 return 0; 1419 return 0;
1407} 1420}
1408 1421
1409static SV * 1422static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1423decode_json (SV *string, JSON *json, char **offset_return)
1411{ 1424{
1412 dec_t dec; 1425 dec_t dec;
1413 STRLEN offset;
1414 SV *sv; 1426 SV *sv;
1415 1427
1428 /* work around bugs in 5.10 where manipulating magic values
1429 * will perl ignore the magic in subsequent accesses
1430 */
1416 SvGETMAGIC (string); 1431 /*SvGETMAGIC (string);*/
1432 if (SvMAGICAL (string))
1433 string = sv_2mortal (newSVsv (string));
1434
1417 SvUPGRADE (string, SVt_PV); 1435 SvUPGRADE (string, SVt_PV);
1418 1436
1419 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1437 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1420 * assertion with -DDEBUGGING, although SvCUR is documented to 1438 * assertion with -DDEBUGGING, although SvCUR is documented to
1421 * return the xpv_cur field which certainly exists after upgrading. 1439 * return the xpv_cur field which certainly exists after upgrading.
1422 * according to nicholas clark, calling SvPOK fixes this. 1440 * according to nicholas clark, calling SvPOK fixes this.
1423 * But it doesn't fix it, so try another workaround, call SvPV_nolen 1441 * But it doesn't fix it, so try another workaround, call SvPV_nolen
1424 * and hope for the best. 1442 * and hope for the best.
1443 * Damnit, SvPV_nolen still trips over yet another assertion. This
1444 * assertion business is seriously broken, try yet another workaround
1445 * for the broken -DDEBUGGING.
1425 */ 1446 */
1447 {
1426#ifdef DEBUGGING 1448#ifdef DEBUGGING
1427 SvPV_nolen (string); 1449 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1450#else
1451 STRLEN offset = SvCUR (string);
1428#endif 1452#endif
1429 1453
1430 if (SvCUR (string) > json->max_size && json->max_size) 1454 if (offset > json->max_size && json->max_size)
1431 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1455 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1432 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1456 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1457 }
1433 1458
1434 if (json->flags & F_UTF8) 1459 if (json->flags & F_UTF8)
1435 sv_utf8_downgrade (string, 0); 1460 sv_utf8_downgrade (string, 0);
1436 else 1461 else
1437 sv_utf8_upgrade (string); 1462 sv_utf8_upgrade (string);
1449 1474
1450 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1475 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1451 1476
1452 decode_ws (&dec); 1477 decode_ws (&dec);
1453 sv = decode_sv (&dec); 1478 sv = decode_sv (&dec);
1479
1480 if (offset_return)
1481 *offset_return = dec.cur;
1454 1482
1455 if (!(offset_return || !sv)) 1483 if (!(offset_return || !sv))
1456 { 1484 {
1457 // check for trailing garbage 1485 // check for trailing garbage
1458 decode_ws (&dec); 1486 decode_ws (&dec);
1461 { 1489 {
1462 dec.err = "garbage after JSON object"; 1490 dec.err = "garbage after JSON object";
1463 SvREFCNT_dec (sv); 1491 SvREFCNT_dec (sv);
1464 sv = 0; 1492 sv = 0;
1465 } 1493 }
1466 }
1467
1468 if (offset_return || !sv)
1469 {
1470 offset = dec.json.flags & F_UTF8
1471 ? dec.cur - SvPVX (string)
1472 : utf8_distance (dec.cur, SvPVX (string));
1473
1474 if (offset_return)
1475 *offset_return = offset;
1476 } 1494 }
1477 1495
1478 if (!sv) 1496 if (!sv)
1479 { 1497 {
1480 SV *uni = sv_newmortal (); 1498 SV *uni = sv_newmortal ();
1486 SAVEVPTR (PL_curcop); 1504 SAVEVPTR (PL_curcop);
1487 PL_curcop = &cop; 1505 PL_curcop = &cop;
1488 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1506 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1489 LEAVE; 1507 LEAVE;
1490 1508
1491 croak ("%s, at character offset %d [\"%s\"]", 1509 croak ("%s, at character offset %d (before \"%s\")",
1492 dec.err, 1510 dec.err,
1493 (int)offset, 1511 ptr_to_index (string, dec.cur),
1494 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1512 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1495 } 1513 }
1496 1514
1497 sv = sv_2mortal (sv); 1515 sv = sv_2mortal (sv);
1498 1516
1638 json_stash = gv_stashpv ("JSON::XS" , 1); 1656 json_stash = gv_stashpv ("JSON::XS" , 1);
1639 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1657 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1640 1658
1641 json_true = get_bool ("JSON::XS::true"); 1659 json_true = get_bool ("JSON::XS::true");
1642 json_false = get_bool ("JSON::XS::false"); 1660 json_false = get_bool ("JSON::XS::false");
1661
1662 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1643} 1663}
1644 1664
1645PROTOTYPES: DISABLE 1665PROTOTYPES: DISABLE
1646 1666
1647void CLONE (...) 1667void CLONE (...)
1767 XPUSHs (decode_json (jsonstr, self, 0)); 1787 XPUSHs (decode_json (jsonstr, self, 0));
1768 1788
1769void decode_prefix (JSON *self, SV *jsonstr) 1789void decode_prefix (JSON *self, SV *jsonstr)
1770 PPCODE: 1790 PPCODE:
1771{ 1791{
1772 STRLEN offset; 1792 char *offset;
1773 EXTEND (SP, 2); 1793 EXTEND (SP, 2);
1774 PUSHs (decode_json (jsonstr, self, &offset)); 1794 PUSHs (decode_json (jsonstr, self, &offset));
1775 PUSHs (sv_2mortal (newSVuv (offset))); 1795 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1776} 1796}
1777 1797
1778void incr_parse (JSON *self, SV *jsonstr = 0) 1798void incr_parse (JSON *self, SV *jsonstr = 0)
1779 PPCODE: 1799 PPCODE:
1780{ 1800{
1782 self->incr_text = newSVpvn ("", 0); 1802 self->incr_text = newSVpvn ("", 0);
1783 1803
1784 // append data, if any 1804 // append data, if any
1785 if (jsonstr) 1805 if (jsonstr)
1786 { 1806 {
1787 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1807 if (SvUTF8 (jsonstr))
1788 { 1808 {
1809 if (!SvUTF8 (self->incr_text))
1810 {
1789 /* utf-8-ness differs, need to upgrade */ 1811 /* utf-8-ness differs, need to upgrade */
1790 sv_utf8_upgrade (self->incr_text); 1812 sv_utf8_upgrade (self->incr_text);
1791 1813
1792 if (self->incr_pos) 1814 if (self->incr_pos)
1793 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1815 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1794 - (U8 *)SvPVX (self->incr_text); 1816 - (U8 *)SvPVX (self->incr_text);
1817 }
1795 } 1818 }
1819 else if (SvUTF8 (self->incr_text))
1820 sv_utf8_upgrade (jsonstr);
1796 1821
1797 { 1822 {
1798 STRLEN len; 1823 STRLEN len;
1799 const char *str = SvPV (jsonstr, len); 1824 const char *str = SvPV (jsonstr, len);
1800 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1825 STRLEN cur = SvCUR (self->incr_text);
1826
1827 if (SvLEN (self->incr_text) <= cur + len)
1828 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1829
1801 Move (str, SvEND (self->incr_text), len, char); 1830 Move (str, SvEND (self->incr_text), len, char);
1802 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1831 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1803 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1832 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1804 } 1833 }
1805 } 1834 }
1806 1835
1807 if (GIMME_V != G_VOID) 1836 if (GIMME_V != G_VOID)
1808 do 1837 do
1809 { 1838 {
1810 STRLEN offset; 1839 char *offset;
1811 1840
1812 if (!INCR_DONE (self)) 1841 if (!INCR_DONE (self))
1813 { 1842 {
1814 incr_parse (self); 1843 incr_parse (self);
1815 1844
1821 break; 1850 break;
1822 } 1851 }
1823 1852
1824 XPUSHs (decode_json (self->incr_text, self, &offset)); 1853 XPUSHs (decode_json (self->incr_text, self, &offset));
1825 1854
1826 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1827 self->incr_pos -= offset; 1855 self->incr_pos -= offset - SvPVX (self->incr_text);
1828 self->incr_nest = 0; 1856 self->incr_nest = 0;
1829 self->incr_mode = 0; 1857 self->incr_mode = 0;
1858
1859 sv_chop (self->incr_text, offset);
1830 } 1860 }
1831 while (GIMME_V == G_ARRAY); 1861 while (GIMME_V == G_ARRAY);
1832} 1862}
1833 1863
1834SV *incr_text (JSON *self) 1864SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines