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.94 by root, Thu Nov 20 03:59:53 2008 UTC vs.
Revision 1.100 by root, Sat Sep 5 23:00:56 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)
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
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 - (char *)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
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 && !SvRMAGICAL (hv))
468 { 477 {
469 int count = hv_iterinit (hv); 478 int count = hv_iterinit (hv);
470 479
471 if (SvMAGICAL (hv)) 480 if (SvMAGICAL (hv))
472 { 481 {
750 : enc.json.flags & F_LATIN1 ? 0x000100UL 759 : enc.json.flags & F_LATIN1 ? 0x000100UL
751 : 0x110000UL; 760 : 0x110000UL;
752 761
753 SvPOK_only (enc.sv); 762 SvPOK_only (enc.sv);
754 encode_sv (&enc, scalar); 763 encode_sv (&enc, scalar);
764 encode_nl (&enc);
755 765
756 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 766 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
757 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 767 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
758 768
759 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 769 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
970 { 980 {
971 STRLEN len = cur - buf; 981 STRLEN len = cur - buf;
972 982
973 if (sv) 983 if (sv)
974 { 984 {
975 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
976 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 990 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
977 SvCUR_set (sv, SvCUR (sv) + len); 991 SvCUR_set (sv, SvCUR (sv) + len);
978 } 992 }
979 else 993 else
980 sv = newSVpvn (buf, len); 994 sv = newSVpvn (buf, len);
1071 1085
1072 // special case the rather common 1..5-digit-int case 1086 // special case the rather common 1..5-digit-int case
1073 if (*start == '-') 1087 if (*start == '-')
1074 switch (len) 1088 switch (len)
1075 { 1089 {
1076 case 2: return newSViv (-( start [1] - '0' * 1)); 1090 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1077 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1091 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)); 1092 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)); 1093 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)); 1094 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1081 } 1095 }
1082 else 1096 else
1083 switch (len) 1097 switch (len)
1084 { 1098 {
1085 case 1: return newSViv ( start [0] - '0' * 1); 1099 case 1: return newSViv ( start [0] - '0' * 1);
1086 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1100 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); 1101 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); 1102 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); 1103 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1090 } 1104 }
1091 1105
1092 { 1106 {
1093 UV uv; 1107 UV uv;
1094 int numtype = grok_number (start, len, &uv); 1108 int numtype = grok_number (start, len, &uv);
1404fail: 1418fail:
1405 return 0; 1419 return 0;
1406} 1420}
1407 1421
1408static SV * 1422static SV *
1409decode_json (SV *string, JSON *json, STRLEN *offset_return) 1423decode_json (SV *string, JSON *json, char **offset_return)
1410{ 1424{
1411 dec_t dec; 1425 dec_t dec;
1412 STRLEN offset;
1413 SV *sv; 1426 SV *sv;
1414 1427
1415 /* work around bugs in 5.10 where manipulating magic values 1428 /* work around bugs in 5.10 where manipulating magic values
1416 * will perl ignore the magic in subsequent accesses 1429 * will perl ignore the magic in subsequent accesses
1417 */ 1430 */
1429 * and hope for the best. 1442 * and hope for the best.
1430 * Damnit, SvPV_nolen still trips over yet another assertion. This 1443 * Damnit, SvPV_nolen still trips over yet another assertion. This
1431 * assertion business is seriously broken, try yet another workaround 1444 * assertion business is seriously broken, try yet another workaround
1432 * for the broken -DDEBUGGING. 1445 * for the broken -DDEBUGGING.
1433 */ 1446 */
1447 {
1434#ifdef DEBUGGING 1448#ifdef DEBUGGING
1435 offset = SvOK (string) ? sv_len (string) : 0; 1449 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1436#else 1450#else
1437 offset = SvCUR (string); 1451 STRLEN offset = SvCUR (string);
1438#endif 1452#endif
1439 1453
1440 if (offset > json->max_size && json->max_size) 1454 if (offset > json->max_size && json->max_size)
1441 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",
1442 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1456 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1457 }
1443 1458
1444 if (json->flags & F_UTF8) 1459 if (json->flags & F_UTF8)
1445 sv_utf8_downgrade (string, 0); 1460 sv_utf8_downgrade (string, 0);
1446 else 1461 else
1447 sv_utf8_upgrade (string); 1462 sv_utf8_upgrade (string);
1459 1474
1460 *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
1461 1476
1462 decode_ws (&dec); 1477 decode_ws (&dec);
1463 sv = decode_sv (&dec); 1478 sv = decode_sv (&dec);
1479
1480 if (offset_return)
1481 *offset_return = dec.cur;
1464 1482
1465 if (!(offset_return || !sv)) 1483 if (!(offset_return || !sv))
1466 { 1484 {
1467 // check for trailing garbage 1485 // check for trailing garbage
1468 decode_ws (&dec); 1486 decode_ws (&dec);
1471 { 1489 {
1472 dec.err = "garbage after JSON object"; 1490 dec.err = "garbage after JSON object";
1473 SvREFCNT_dec (sv); 1491 SvREFCNT_dec (sv);
1474 sv = 0; 1492 sv = 0;
1475 } 1493 }
1476 }
1477
1478 if (offset_return || !sv)
1479 {
1480 offset = dec.json.flags & F_UTF8
1481 ? dec.cur - SvPVX (string)
1482 : utf8_distance (dec.cur, SvPVX (string));
1483
1484 if (offset_return)
1485 *offset_return = offset;
1486 } 1494 }
1487 1495
1488 if (!sv) 1496 if (!sv)
1489 { 1497 {
1490 SV *uni = sv_newmortal (); 1498 SV *uni = sv_newmortal ();
1496 SAVEVPTR (PL_curcop); 1504 SAVEVPTR (PL_curcop);
1497 PL_curcop = &cop; 1505 PL_curcop = &cop;
1498 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);
1499 LEAVE; 1507 LEAVE;
1500 1508
1501 croak ("%s, at character offset %d [\"%s\"]", 1509 croak ("%s, at character offset %d (before \"%s\")",
1502 dec.err, 1510 dec.err,
1503 (int)offset, 1511 ptr_to_index (string, dec.cur),
1504 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1512 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1505 } 1513 }
1506 1514
1507 sv = sv_2mortal (sv); 1515 sv = sv_2mortal (sv);
1508 1516
1648 json_stash = gv_stashpv ("JSON::XS" , 1); 1656 json_stash = gv_stashpv ("JSON::XS" , 1);
1649 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1657 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1650 1658
1651 json_true = get_bool ("JSON::XS::true"); 1659 json_true = get_bool ("JSON::XS::true");
1652 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 */
1653} 1663}
1654 1664
1655PROTOTYPES: DISABLE 1665PROTOTYPES: DISABLE
1656 1666
1657void CLONE (...) 1667void CLONE (...)
1777 XPUSHs (decode_json (jsonstr, self, 0)); 1787 XPUSHs (decode_json (jsonstr, self, 0));
1778 1788
1779void decode_prefix (JSON *self, SV *jsonstr) 1789void decode_prefix (JSON *self, SV *jsonstr)
1780 PPCODE: 1790 PPCODE:
1781{ 1791{
1782 STRLEN offset; 1792 char *offset;
1783 EXTEND (SP, 2); 1793 EXTEND (SP, 2);
1784 PUSHs (decode_json (jsonstr, self, &offset)); 1794 PUSHs (decode_json (jsonstr, self, &offset));
1785 PUSHs (sv_2mortal (newSVuv (offset))); 1795 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1786} 1796}
1787 1797
1788void incr_parse (JSON *self, SV *jsonstr = 0) 1798void incr_parse (JSON *self, SV *jsonstr = 0)
1789 PPCODE: 1799 PPCODE:
1790{ 1800{
1792 self->incr_text = newSVpvn ("", 0); 1802 self->incr_text = newSVpvn ("", 0);
1793 1803
1794 // append data, if any 1804 // append data, if any
1795 if (jsonstr) 1805 if (jsonstr)
1796 { 1806 {
1797 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1807 if (SvUTF8 (jsonstr))
1798 { 1808 {
1809 if (!SvUTF8 (self->incr_text))
1810 {
1799 /* utf-8-ness differs, need to upgrade */ 1811 /* utf-8-ness differs, need to upgrade */
1800 sv_utf8_upgrade (self->incr_text); 1812 sv_utf8_upgrade (self->incr_text);
1801 1813
1802 if (self->incr_pos) 1814 if (self->incr_pos)
1803 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)
1804 - (U8 *)SvPVX (self->incr_text); 1816 - (U8 *)SvPVX (self->incr_text);
1817 }
1805 } 1818 }
1819 else if (SvUTF8 (self->incr_text))
1820 sv_utf8_upgrade (jsonstr);
1806 1821
1807 { 1822 {
1808 STRLEN len; 1823 STRLEN len;
1809 const char *str = SvPV (jsonstr, len); 1824 const char *str = SvPV (jsonstr, len);
1810 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
1811 Move (str, SvEND (self->incr_text), len, char); 1830 Move (str, SvEND (self->incr_text), len, char);
1812 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1831 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1813 *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
1814 } 1833 }
1815 } 1834 }
1816 1835
1817 if (GIMME_V != G_VOID) 1836 if (GIMME_V != G_VOID)
1818 do 1837 do
1819 { 1838 {
1820 STRLEN offset; 1839 char *offset;
1821 1840
1822 if (!INCR_DONE (self)) 1841 if (!INCR_DONE (self))
1823 { 1842 {
1824 incr_parse (self); 1843 incr_parse (self);
1825 1844
1831 break; 1850 break;
1832 } 1851 }
1833 1852
1834 XPUSHs (decode_json (self->incr_text, self, &offset)); 1853 XPUSHs (decode_json (self->incr_text, self, &offset));
1835 1854
1836 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1837 self->incr_pos -= offset; 1855 self->incr_pos -= offset - SvPVX (self->incr_text);
1838 self->incr_nest = 0; 1856 self->incr_nest = 0;
1839 self->incr_mode = 0; 1857 self->incr_mode = 0;
1858
1859 sv_chop (self->incr_text, offset);
1840 } 1860 }
1841 while (GIMME_V == G_ARRAY); 1861 while (GIMME_V == G_ARRAY);
1842} 1862}
1843 1863
1844SV *incr_text (JSON *self) 1864SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines