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.98 by root, Fri Jul 17 14:33:45 2009 UTC vs.
Revision 1.110 by root, Wed Jun 1 13:01:09 2011 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
41#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
42#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
43 44
44#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
45 46
47#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
48
46#define SB do { 49#define SB do {
47#define SE } while (0) 50#define SE } while (0)
48 51
49#if __GNUC__ >= 3 52#if __GNUC__ >= 3
50# define expect(expr,value) __builtin_expect ((expr), (value)) 53# define expect(expr,value) __builtin_expect ((expr), (value))
76 79
77enum { 80enum {
78 INCR_M_WS = 0, // initial whitespace skipping, must be 0 81 INCR_M_WS = 0, // initial whitespace skipping, must be 0
79 INCR_M_STR, // inside string 82 INCR_M_STR, // inside string
80 INCR_M_BS, // inside backslash 83 INCR_M_BS, // inside backslash
84 INCR_M_C0, // inside comment in initial whitespace sequence
85 INCR_M_C1, // inside comment in other places
81 INCR_M_JSON // outside anything, count nesting 86 INCR_M_JSON // outside anything, count nesting
82}; 87};
83 88
84#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) 89#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
85 90
184 return SvUTF8 (sv) 189 return SvUTF8 (sv)
185 ? utf8_distance (offset, SvPVX (sv)) 190 ? utf8_distance (offset, SvPVX (sv))
186 : offset - SvPVX (sv); 191 : offset - SvPVX (sv);
187} 192}
188 193
194/////////////////////////////////////////////////////////////////////////////
195// fp hell
196
197// scan a group of digits, and a trailing exponent
198static void
199json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
200{
201 UV uaccum = 0;
202 int eaccum = 0;
203
204 // if we recurse too deep, skip all remaining digits
205 // to avoid a stack overflow attack
206 if (expect_false (--maxdepth <= 0))
207 while (((U8)*s - '0') < 10)
208 ++s;
209
210 for (;;)
211 {
212 U8 dig = (U8)*s - '0';
213
214 if (expect_false (dig >= 10))
215 {
216 if (dig == (U8)((U8)'.' - (U8)'0'))
217 {
218 ++s;
219 json_atof_scan1 (s, accum, expo, 1, maxdepth);
220 }
221 else if ((dig | ' ') == 'e' - '0')
222 {
223 int exp2 = 0;
224 int neg = 0;
225
226 ++s;
227
228 if (*s == '-')
229 {
230 ++s;
231 neg = 1;
232 }
233 else if (*s == '+')
234 ++s;
235
236 while ((dig = (U8)*s - '0') < 10)
237 exp2 = exp2 * 10 + *s++ - '0';
238
239 *expo += neg ? -exp2 : exp2;
240 }
241
242 break;
243 }
244
245 ++s;
246
247 uaccum = uaccum * 10 + dig;
248 ++eaccum;
249
250 // if we have too many digits, then recurse for more
251 // we actually do this for rather few digits
252 if (uaccum >= (UV_MAX - 9) / 10)
253 {
254 if (postdp) *expo -= eaccum;
255 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
256 if (postdp) *expo += eaccum;
257
258 break;
259 }
260 }
261
262 // this relies greatly on the quality of the pow ()
263 // implementation of the platform, but a good
264 // implementation is hard to beat.
265 // (IEEE 754 conformant ones are required to be exact)
266 if (postdp) *expo -= eaccum;
267 *accum += uaccum * Perl_pow (10., *expo);
268 *expo += eaccum;
269}
270
271static NV
272json_atof (const char *s)
273{
274 NV accum = 0.;
275 int expo = 0;
276 int neg = 0;
277
278 if (*s == '-')
279 {
280 ++s;
281 neg = 1;
282 }
283
284 // a recursion depth of ten gives us >>500 bits
285 json_atof_scan1 (s, &accum, &expo, 0, 10);
286
287 return neg ? -accum : accum;
288}
189///////////////////////////////////////////////////////////////////////////// 289/////////////////////////////////////////////////////////////////////////////
190// encoder 290// encoder
191 291
192// structure used for encoding JSON 292// structure used for encoding JSON
193typedef struct 293typedef struct
759 : enc.json.flags & F_LATIN1 ? 0x000100UL 859 : enc.json.flags & F_LATIN1 ? 0x000100UL
760 : 0x110000UL; 860 : 0x110000UL;
761 861
762 SvPOK_only (enc.sv); 862 SvPOK_only (enc.sv);
763 encode_sv (&enc, scalar); 863 encode_sv (&enc, scalar);
864 encode_nl (&enc);
764 865
765 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 866 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
766 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 867 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
767 868
768 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 869 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
948 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1049 else if (expect_true (ch >= 0x20 && ch < 0x80))
949 *cur++ = ch; 1050 *cur++ = ch;
950 else if (ch >= 0x80) 1051 else if (ch >= 0x80)
951 { 1052 {
952 STRLEN clen; 1053 STRLEN clen;
953 UV uch;
954 1054
955 --dec_cur; 1055 --dec_cur;
956 1056
957 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1057 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
958 if (clen == (STRLEN)-1) 1058 if (clen == (STRLEN)-1)
959 ERR ("malformed UTF-8 character in JSON string"); 1059 ERR ("malformed UTF-8 character in JSON string");
960 1060
961 do 1061 do
962 *cur++ = *dec_cur++; 1062 *cur++ = *dec_cur++;
1116 } 1216 }
1117 1217
1118 len -= *start == '-' ? 1 : 0; 1218 len -= *start == '-' ? 1 : 0;
1119 1219
1120 // does not fit into IV or UV, try NV 1220 // does not fit into IV or UV, try NV
1121 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1221 if (len <= NV_DIG)
1122 #if defined (LDBL_DIG)
1123 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1124 #endif
1125 )
1126 // fits into NV without loss of precision 1222 // fits into NV without loss of precision
1127 return newSVnv (Atof (start)); 1223 return newSVnv (json_atof (start));
1128 1224
1129 // everything else fails, convert it to a string 1225 // everything else fails, convert it to a string
1130 return newSVpvn (start, dec->cur - start); 1226 return newSVpvn (start, dec->cur - start);
1131 } 1227 }
1132 1228
1133 // loss of precision here 1229 // loss of precision here
1134 return newSVnv (Atof (start)); 1230 return newSVnv (json_atof (start));
1135 1231
1136fail: 1232fail:
1137 return 0; 1233 return 0;
1138} 1234}
1139 1235
1308 dSP; 1404 dSP;
1309 int count; 1405 int count;
1310 1406
1311 ENTER; SAVETMPS; PUSHMARK (SP); 1407 ENTER; SAVETMPS; PUSHMARK (SP);
1312 XPUSHs (HeVAL (he)); 1408 XPUSHs (HeVAL (he));
1409 sv_2mortal (sv);
1313 1410
1314 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1411 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1315 1412
1316 if (count == 1) 1413 if (count == 1)
1317 { 1414 {
1318 sv = newSVsv (POPs); 1415 sv = newSVsv (POPs);
1319 FREETMPS; LEAVE; 1416 FREETMPS; LEAVE;
1320 return sv; 1417 return sv;
1321 } 1418 }
1322 1419
1420 SvREFCNT_inc (sv);
1323 FREETMPS; LEAVE; 1421 FREETMPS; LEAVE;
1324 } 1422 }
1325 } 1423 }
1326 1424
1327 if (dec->json.cb_object) 1425 if (dec->json.cb_object)
1423{ 1521{
1424 dec_t dec; 1522 dec_t dec;
1425 SV *sv; 1523 SV *sv;
1426 1524
1427 /* work around bugs in 5.10 where manipulating magic values 1525 /* work around bugs in 5.10 where manipulating magic values
1428 * will perl ignore the magic in subsequent accesses 1526 * will perl ignore the magic in subsequent accesses.
1527 * also make a copy of non-PV values, to get them into a clean
1528 * state (SvPV should do that, but it's buggy, see below).
1429 */ 1529 */
1430 /*SvGETMAGIC (string);*/ 1530 /*SvGETMAGIC (string);*/
1431 if (SvMAGICAL (string)) 1531 if (SvMAGICAL (string) || !SvPOK (string))
1432 string = sv_2mortal (newSVsv (string)); 1532 string = sv_2mortal (newSVsv (string));
1433 1533
1434 SvUPGRADE (string, SVt_PV); 1534 SvUPGRADE (string, SVt_PV);
1435 1535
1436 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1536 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1453 if (offset > json->max_size && json->max_size) 1553 if (offset > json->max_size && json->max_size)
1454 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1554 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1555 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 } 1556 }
1457 1557
1458 if (json->flags & F_UTF8) 1558 if (DECODE_WANTS_OCTETS (json))
1459 sv_utf8_downgrade (string, 0); 1559 sv_utf8_downgrade (string, 0);
1460 else 1560 else
1461 sv_utf8_upgrade (string); 1561 sv_utf8_upgrade (string);
1462 1562
1463 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1563 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1525static void 1625static void
1526incr_parse (JSON *self) 1626incr_parse (JSON *self)
1527{ 1627{
1528 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1628 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1529 1629
1630 // the state machine here is a bit convoluted and could be simplified a lot
1631 // but this would make it slower, so...
1632
1530 for (;;) 1633 for (;;)
1531 { 1634 {
1532 //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D 1635 //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D
1533 switch (self->incr_mode) 1636 switch (self->incr_mode)
1534 { 1637 {
1535 // only used for intiial whitespace skipping 1638 // only used for initial whitespace skipping
1536 case INCR_M_WS: 1639 case INCR_M_WS:
1537 for (;;) 1640 for (;;)
1538 { 1641 {
1539 if (*p > 0x20) 1642 if (*p > 0x20)
1540 { 1643 {
1644 if (*p == '#')
1645 {
1646 self->incr_mode = INCR_M_C0;
1647 goto incr_m_c;
1648 }
1649 else
1650 {
1541 self->incr_mode = INCR_M_JSON; 1651 self->incr_mode = INCR_M_JSON;
1542 goto incr_m_json; 1652 goto incr_m_json;
1653 }
1543 } 1654 }
1544 else if (!*p) 1655 else if (!*p)
1545 goto interrupt; 1656 goto interrupt;
1546 1657
1547 ++p; 1658 ++p;
1553 goto interrupt; 1664 goto interrupt;
1554 1665
1555 ++p; 1666 ++p;
1556 self->incr_mode = INCR_M_STR; 1667 self->incr_mode = INCR_M_STR;
1557 goto incr_m_str; 1668 goto incr_m_str;
1669
1670 // inside #-style comments
1671 case INCR_M_C0:
1672 case INCR_M_C1:
1673 incr_m_c:
1674 for (;;)
1675 {
1676 if (*p == '\n')
1677 {
1678 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1679 break;
1680 }
1681 else if (!*p)
1682 goto interrupt;
1683
1684 ++p;
1685 }
1686
1687 break;
1558 1688
1559 // inside a string 1689 // inside a string
1560 case INCR_M_STR: 1690 case INCR_M_STR:
1561 incr_m_str: 1691 incr_m_str:
1562 for (;;) 1692 for (;;)
1621 1751
1622 case ']': 1752 case ']':
1623 case '}': 1753 case '}':
1624 if (--self->incr_nest <= 0) 1754 if (--self->incr_nest <= 0)
1625 goto interrupt; 1755 goto interrupt;
1756 break;
1757
1758 case '#':
1759 self->incr_mode = INCR_M_C1;
1760 goto incr_m_c;
1626 } 1761 }
1627 } 1762 }
1628 } 1763 }
1629 1764
1630 modechange: 1765 modechange:
1631 ; 1766 ;
1632 } 1767 }
1633 1768
1634interrupt: 1769interrupt:
1635 self->incr_pos = p - SvPVX (self->incr_text); 1770 self->incr_pos = p - SvPVX (self->incr_text);
1771 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
1636 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D 1772 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1637} 1773}
1638 1774
1639///////////////////////////////////////////////////////////////////////////// 1775/////////////////////////////////////////////////////////////////////////////
1640// XS interface functions 1776// XS interface functions
1655 json_stash = gv_stashpv ("JSON::XS" , 1); 1791 json_stash = gv_stashpv ("JSON::XS" , 1);
1656 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1792 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1657 1793
1658 json_true = get_bool ("JSON::XS::true"); 1794 json_true = get_bool ("JSON::XS::true");
1659 json_false = get_bool ("JSON::XS::false"); 1795 json_false = get_bool ("JSON::XS::false");
1796
1797 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1660} 1798}
1661 1799
1662PROTOTYPES: DISABLE 1800PROTOTYPES: DISABLE
1663 1801
1664void CLONE (...) 1802void CLONE (...)
1796 PPCODE: 1934 PPCODE:
1797{ 1935{
1798 if (!self->incr_text) 1936 if (!self->incr_text)
1799 self->incr_text = newSVpvn ("", 0); 1937 self->incr_text = newSVpvn ("", 0);
1800 1938
1939 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
1940 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
1941 if (DECODE_WANTS_OCTETS (self))
1942 {
1943 if (self->incr_pos)
1944 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
1945 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
1946
1947 sv_utf8_downgrade (self->incr_text, 0);
1948 }
1949 else
1950 {
1951 sv_utf8_upgrade (self->incr_text);
1952
1953 if (self->incr_pos)
1954 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1955 - (U8 *)SvPVX (self->incr_text);
1956 }
1957
1801 // append data, if any 1958 // append data, if any
1802 if (jsonstr) 1959 if (jsonstr)
1803 { 1960 {
1961 /* make sure both strings have same encoding */
1962 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1804 if (SvUTF8 (jsonstr)) 1963 if (SvUTF8 (jsonstr))
1964 sv_utf8_downgrade (jsonstr, 0);
1805 { 1965 else
1806 if (!SvUTF8 (self->incr_text))
1807 {
1808 /* utf-8-ness differs, need to upgrade */
1809 sv_utf8_upgrade (self->incr_text);
1810
1811 if (self->incr_pos)
1812 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1813 - (U8 *)SvPVX (self->incr_text);
1814 }
1815 }
1816 else if (SvUTF8 (self->incr_text))
1817 sv_utf8_upgrade (jsonstr); 1966 sv_utf8_upgrade (jsonstr);
1818 1967
1968 /* and then just blindly append */
1819 { 1969 {
1820 STRLEN len; 1970 STRLEN len;
1821 const char *str = SvPV (jsonstr, len); 1971 const char *str = SvPV (jsonstr, len);
1822 STRLEN cur = SvCUR (self->incr_text); 1972 STRLEN cur = SvCUR (self->incr_text);
1823 1973
1922 json_init (&json); 2072 json_init (&json);
1923 json.flags |= ix; 2073 json.flags |= ix;
1924 XPUSHs (decode_json (jsonstr, &json, 0)); 2074 XPUSHs (decode_json (jsonstr, &json, 0));
1925} 2075}
1926 2076
1927

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines