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.95 by root, Sun Feb 22 06:55:28 2009 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
173 *s++ = 0x80 | ((ch >> 12) & 0x3f), 174 *s++ = 0x80 | ((ch >> 12) & 0x3f),
174 *s++ = 0x80 | ((ch >> 6) & 0x3f), 175 *s++ = 0x80 | ((ch >> 6) & 0x3f),
175 *s++ = 0x80 | ( ch & 0x3f); 176 *s++ = 0x80 | ( ch & 0x3f);
176 177
177 return s; 178 return s;
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);
178} 188}
179 189
180///////////////////////////////////////////////////////////////////////////// 190/////////////////////////////////////////////////////////////////////////////
181// encoder 191// encoder
182 192
462 472
463 // for canonical output we have to sort by keys first 473 // for canonical output we have to sort by keys first
464 // actually, this is mostly due to the stupid so-called 474 // actually, this is mostly due to the stupid so-called
465 // security workaround added somewhere in 5.8.x 475 // security workaround added somewhere in 5.8.x
466 // that randomises hash orderings 476 // that randomises hash orderings
467 if (enc->json.flags & F_CANONICAL) 477 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
468 { 478 {
469 int count = hv_iterinit (hv); 479 int count = hv_iterinit (hv);
470 480
471 if (SvMAGICAL (hv)) 481 if (SvMAGICAL (hv))
472 { 482 {
750 : enc.json.flags & F_LATIN1 ? 0x000100UL 760 : enc.json.flags & F_LATIN1 ? 0x000100UL
751 : 0x110000UL; 761 : 0x110000UL;
752 762
753 SvPOK_only (enc.sv); 763 SvPOK_only (enc.sv);
754 encode_sv (&enc, scalar); 764 encode_sv (&enc, scalar);
765 encode_nl (&enc);
755 766
756 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 767 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
757 *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
758 769
759 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 770 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
939 else if (expect_true (ch >= 0x20 && ch < 0x80)) 950 else if (expect_true (ch >= 0x20 && ch < 0x80))
940 *cur++ = ch; 951 *cur++ = ch;
941 else if (ch >= 0x80) 952 else if (ch >= 0x80)
942 { 953 {
943 STRLEN clen; 954 STRLEN clen;
944 UV uch;
945 955
946 --dec_cur; 956 --dec_cur;
947 957
948 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 958 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
949 if (clen == (STRLEN)-1) 959 if (clen == (STRLEN)-1)
950 ERR ("malformed UTF-8 character in JSON string"); 960 ERR ("malformed UTF-8 character in JSON string");
951 961
952 do 962 do
953 *cur++ = *dec_cur++; 963 *cur++ = *dec_cur++;
1075 1085
1076 // special case the rather common 1..5-digit-int case 1086 // special case the rather common 1..5-digit-int case
1077 if (*start == '-') 1087 if (*start == '-')
1078 switch (len) 1088 switch (len)
1079 { 1089 {
1080 case 2: return newSViv (-( start [1] - '0' * 1)); 1090 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1081 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1091 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
1082 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));
1083 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));
1084 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));
1085 } 1095 }
1086 else 1096 else
1087 switch (len) 1097 switch (len)
1088 { 1098 {
1089 case 1: return newSViv ( start [0] - '0' * 1); 1099 case 1: return newSViv ( start [0] - '0' * 1);
1090 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1100 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1091 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);
1092 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);
1093 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);
1094 } 1104 }
1095 1105
1096 { 1106 {
1097 UV uv; 1107 UV uv;
1098 int numtype = grok_number (start, len, &uv); 1108 int numtype = grok_number (start, len, &uv);
1408fail: 1418fail:
1409 return 0; 1419 return 0;
1410} 1420}
1411 1421
1412static SV * 1422static SV *
1413decode_json (SV *string, JSON *json, STRLEN *offset_return) 1423decode_json (SV *string, JSON *json, char **offset_return)
1414{ 1424{
1415 dec_t dec; 1425 dec_t dec;
1416 STRLEN offset;
1417 SV *sv; 1426 SV *sv;
1418 1427
1419 /* work around bugs in 5.10 where manipulating magic values 1428 /* work around bugs in 5.10 where manipulating magic values
1420 * will perl ignore the magic in subsequent accesses 1429 * will perl ignore the magic in subsequent accesses
1421 */ 1430 */
1433 * and hope for the best. 1442 * and hope for the best.
1434 * Damnit, SvPV_nolen still trips over yet another assertion. This 1443 * Damnit, SvPV_nolen still trips over yet another assertion. This
1435 * assertion business is seriously broken, try yet another workaround 1444 * assertion business is seriously broken, try yet another workaround
1436 * for the broken -DDEBUGGING. 1445 * for the broken -DDEBUGGING.
1437 */ 1446 */
1447 {
1438#ifdef DEBUGGING 1448#ifdef DEBUGGING
1439 offset = SvOK (string) ? sv_len (string) : 0; 1449 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1440#else 1450#else
1441 offset = SvCUR (string); 1451 STRLEN offset = SvCUR (string);
1442#endif 1452#endif
1443 1453
1444 if (offset > json->max_size && json->max_size) 1454 if (offset > json->max_size && json->max_size)
1445 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",
1446 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1456 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1457 }
1447 1458
1448 if (json->flags & F_UTF8) 1459 if (json->flags & F_UTF8)
1449 sv_utf8_downgrade (string, 0); 1460 sv_utf8_downgrade (string, 0);
1450 else 1461 else
1451 sv_utf8_upgrade (string); 1462 sv_utf8_upgrade (string);
1463 1474
1464 *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
1465 1476
1466 decode_ws (&dec); 1477 decode_ws (&dec);
1467 sv = decode_sv (&dec); 1478 sv = decode_sv (&dec);
1479
1480 if (offset_return)
1481 *offset_return = dec.cur;
1468 1482
1469 if (!(offset_return || !sv)) 1483 if (!(offset_return || !sv))
1470 { 1484 {
1471 // check for trailing garbage 1485 // check for trailing garbage
1472 decode_ws (&dec); 1486 decode_ws (&dec);
1475 { 1489 {
1476 dec.err = "garbage after JSON object"; 1490 dec.err = "garbage after JSON object";
1477 SvREFCNT_dec (sv); 1491 SvREFCNT_dec (sv);
1478 sv = 0; 1492 sv = 0;
1479 } 1493 }
1480 }
1481
1482 if (offset_return || !sv)
1483 {
1484 offset = dec.json.flags & F_UTF8
1485 ? dec.cur - SvPVX (string)
1486 : utf8_distance (dec.cur, SvPVX (string));
1487
1488 if (offset_return)
1489 *offset_return = offset;
1490 } 1494 }
1491 1495
1492 if (!sv) 1496 if (!sv)
1493 { 1497 {
1494 SV *uni = sv_newmortal (); 1498 SV *uni = sv_newmortal ();
1500 SAVEVPTR (PL_curcop); 1504 SAVEVPTR (PL_curcop);
1501 PL_curcop = &cop; 1505 PL_curcop = &cop;
1502 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);
1503 LEAVE; 1507 LEAVE;
1504 1508
1505 croak ("%s, at character offset %d [\"%s\"]", 1509 croak ("%s, at character offset %d (before \"%s\")",
1506 dec.err, 1510 dec.err,
1507 (int)offset, 1511 ptr_to_index (string, dec.cur),
1508 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1512 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1509 } 1513 }
1510 1514
1511 sv = sv_2mortal (sv); 1515 sv = sv_2mortal (sv);
1512 1516
1652 json_stash = gv_stashpv ("JSON::XS" , 1); 1656 json_stash = gv_stashpv ("JSON::XS" , 1);
1653 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1657 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1654 1658
1655 json_true = get_bool ("JSON::XS::true"); 1659 json_true = get_bool ("JSON::XS::true");
1656 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 */
1657} 1663}
1658 1664
1659PROTOTYPES: DISABLE 1665PROTOTYPES: DISABLE
1660 1666
1661void CLONE (...) 1667void CLONE (...)
1781 XPUSHs (decode_json (jsonstr, self, 0)); 1787 XPUSHs (decode_json (jsonstr, self, 0));
1782 1788
1783void decode_prefix (JSON *self, SV *jsonstr) 1789void decode_prefix (JSON *self, SV *jsonstr)
1784 PPCODE: 1790 PPCODE:
1785{ 1791{
1786 STRLEN offset; 1792 char *offset;
1787 EXTEND (SP, 2); 1793 EXTEND (SP, 2);
1788 PUSHs (decode_json (jsonstr, self, &offset)); 1794 PUSHs (decode_json (jsonstr, self, &offset));
1789 PUSHs (sv_2mortal (newSVuv (offset))); 1795 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1790} 1796}
1791 1797
1792void incr_parse (JSON *self, SV *jsonstr = 0) 1798void incr_parse (JSON *self, SV *jsonstr = 0)
1793 PPCODE: 1799 PPCODE:
1794{ 1800{
1796 self->incr_text = newSVpvn ("", 0); 1802 self->incr_text = newSVpvn ("", 0);
1797 1803
1798 // append data, if any 1804 // append data, if any
1799 if (jsonstr) 1805 if (jsonstr)
1800 { 1806 {
1801 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1807 if (SvUTF8 (jsonstr))
1802 { 1808 {
1809 if (!SvUTF8 (self->incr_text))
1810 {
1803 /* utf-8-ness differs, need to upgrade */ 1811 /* utf-8-ness differs, need to upgrade */
1804 sv_utf8_upgrade (self->incr_text); 1812 sv_utf8_upgrade (self->incr_text);
1805 1813
1806 if (self->incr_pos) 1814 if (self->incr_pos)
1807 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)
1808 - (U8 *)SvPVX (self->incr_text); 1816 - (U8 *)SvPVX (self->incr_text);
1817 }
1809 } 1818 }
1819 else if (SvUTF8 (self->incr_text))
1820 sv_utf8_upgrade (jsonstr);
1810 1821
1811 { 1822 {
1812 STRLEN len; 1823 STRLEN len;
1813 const char *str = SvPV (jsonstr, len); 1824 const char *str = SvPV (jsonstr, len);
1814 STRLEN cur = SvCUR (self->incr_text); 1825 STRLEN cur = SvCUR (self->incr_text);
1823 } 1834 }
1824 1835
1825 if (GIMME_V != G_VOID) 1836 if (GIMME_V != G_VOID)
1826 do 1837 do
1827 { 1838 {
1828 STRLEN offset; 1839 char *offset;
1829 1840
1830 if (!INCR_DONE (self)) 1841 if (!INCR_DONE (self))
1831 { 1842 {
1832 incr_parse (self); 1843 incr_parse (self);
1833 1844
1839 break; 1850 break;
1840 } 1851 }
1841 1852
1842 XPUSHs (decode_json (self->incr_text, self, &offset)); 1853 XPUSHs (decode_json (self->incr_text, self, &offset));
1843 1854
1844 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1845 self->incr_pos -= offset; 1855 self->incr_pos -= offset - SvPVX (self->incr_text);
1846 self->incr_nest = 0; 1856 self->incr_nest = 0;
1847 self->incr_mode = 0; 1857 self->incr_mode = 0;
1858
1859 sv_chop (self->incr_text, offset);
1848 } 1860 }
1849 while (GIMME_V == G_ARRAY); 1861 while (GIMME_V == G_ARRAY);
1850} 1862}
1851 1863
1852SV *incr_text (JSON *self) 1864SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines