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.102 by root, Sat Oct 10 01:48:50 2009 UTC vs.
Revision 1.108 by root, Tue Aug 17 23:27:36 2010 UTC

42#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
43#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
44 44
45#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
46 46
47#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
48
47#define SB do { 49#define SB do {
48#define SE } while (0) 50#define SE } while (0)
49 51
50#if __GNUC__ >= 3 52#if __GNUC__ >= 3
51# define expect(expr,value) __builtin_expect ((expr), (value)) 53# define expect(expr,value) __builtin_expect ((expr), (value))
77 79
78enum { 80enum {
79 INCR_M_WS = 0, // initial whitespace skipping, must be 0 81 INCR_M_WS = 0, // initial whitespace skipping, must be 0
80 INCR_M_STR, // inside string 82 INCR_M_STR, // inside string
81 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
82 INCR_M_JSON // outside anything, count nesting 86 INCR_M_JSON // outside anything, count nesting
83}; 87};
84 88
85#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)
86 90
185 return SvUTF8 (sv) 189 return SvUTF8 (sv)
186 ? utf8_distance (offset, SvPVX (sv)) 190 ? utf8_distance (offset, SvPVX (sv))
187 : offset - SvPVX (sv); 191 : offset - SvPVX (sv);
188} 192}
189 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 if (postdp) *expo -= eaccum;
266 *accum += uaccum * Perl_pow (10., *expo);
267 *expo += eaccum;
268}
269
270static NV
271json_atof (const char *s)
272{
273 NV accum = 0.;
274 int expo = 0;
275 int neg = 0;
276
277 if (*s == '-')
278 {
279 ++s;
280 neg = 1;
281 }
282
283 // a recursion depth of ten gives us >>500 bits
284 json_atof_scan1 (s, &accum, &expo, 0, 10);
285
286 return neg ? -accum : accum;
287}
190///////////////////////////////////////////////////////////////////////////// 288/////////////////////////////////////////////////////////////////////////////
191// encoder 289// encoder
192 290
193// structure used for encoding JSON 291// structure used for encoding JSON
194typedef struct 292typedef struct
1117 } 1215 }
1118 1216
1119 len -= *start == '-' ? 1 : 0; 1217 len -= *start == '-' ? 1 : 0;
1120 1218
1121 // does not fit into IV or UV, try NV 1219 // does not fit into IV or UV, try NV
1122 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1220 if (len <= NV_DIG)
1123 #if defined (LDBL_DIG)
1124 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1125 #endif
1126 )
1127 // fits into NV without loss of precision 1221 // fits into NV without loss of precision
1128 return newSVnv (Atof (start)); 1222 return newSVnv (json_atof (start));
1129 1223
1130 // everything else fails, convert it to a string 1224 // everything else fails, convert it to a string
1131 return newSVpvn (start, dec->cur - start); 1225 return newSVpvn (start, dec->cur - start);
1132 } 1226 }
1133 1227
1134 // loss of precision here 1228 // loss of precision here
1135 return newSVnv (Atof (start)); 1229 return newSVnv (json_atof (start));
1136 1230
1137fail: 1231fail:
1138 return 0; 1232 return 0;
1139} 1233}
1140 1234
1309 dSP; 1403 dSP;
1310 int count; 1404 int count;
1311 1405
1312 ENTER; SAVETMPS; PUSHMARK (SP); 1406 ENTER; SAVETMPS; PUSHMARK (SP);
1313 XPUSHs (HeVAL (he)); 1407 XPUSHs (HeVAL (he));
1408 sv_2mortal (sv);
1314 1409
1315 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1410 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1316 1411
1317 if (count == 1) 1412 if (count == 1)
1318 { 1413 {
1319 sv = newSVsv (POPs); 1414 sv = newSVsv (POPs);
1320 FREETMPS; LEAVE; 1415 FREETMPS; LEAVE;
1321 return sv; 1416 return sv;
1322 } 1417 }
1323 1418
1419 SvREFCNT_inc (sv);
1324 FREETMPS; LEAVE; 1420 FREETMPS; LEAVE;
1325 } 1421 }
1326 } 1422 }
1327 1423
1328 if (dec->json.cb_object) 1424 if (dec->json.cb_object)
1454 if (offset > json->max_size && json->max_size) 1550 if (offset > json->max_size && json->max_size)
1455 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1551 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1456 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1552 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1457 } 1553 }
1458 1554
1459 if (json->flags & F_UTF8) 1555 if (DECODE_WANTS_OCTETS (json))
1460 sv_utf8_downgrade (string, 0); 1556 sv_utf8_downgrade (string, 0);
1461 else 1557 else
1462 sv_utf8_upgrade (string); 1558 sv_utf8_upgrade (string);
1463 1559
1464 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1560 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1526static void 1622static void
1527incr_parse (JSON *self) 1623incr_parse (JSON *self)
1528{ 1624{
1529 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1625 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1530 1626
1627 // the state machine here is a bit convoluted and could be simplified a lot
1628 // but this would make it slower, so...
1629
1531 for (;;) 1630 for (;;)
1532 { 1631 {
1533 //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 1632 //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
1534 switch (self->incr_mode) 1633 switch (self->incr_mode)
1535 { 1634 {
1536 // only used for intiial whitespace skipping 1635 // only used for initial whitespace skipping
1537 case INCR_M_WS: 1636 case INCR_M_WS:
1538 for (;;) 1637 for (;;)
1539 { 1638 {
1540 if (*p > 0x20) 1639 if (*p > 0x20)
1541 { 1640 {
1641 if (*p == '#')
1642 {
1643 self->incr_mode = INCR_M_C0;
1644 goto incr_m_c;
1645 }
1646 else
1647 {
1542 self->incr_mode = INCR_M_JSON; 1648 self->incr_mode = INCR_M_JSON;
1543 goto incr_m_json; 1649 goto incr_m_json;
1650 }
1544 } 1651 }
1545 else if (!*p) 1652 else if (!*p)
1546 goto interrupt; 1653 goto interrupt;
1547 1654
1548 ++p; 1655 ++p;
1554 goto interrupt; 1661 goto interrupt;
1555 1662
1556 ++p; 1663 ++p;
1557 self->incr_mode = INCR_M_STR; 1664 self->incr_mode = INCR_M_STR;
1558 goto incr_m_str; 1665 goto incr_m_str;
1666
1667 // inside #-style comments
1668 case INCR_M_C0:
1669 case INCR_M_C1:
1670 incr_m_c:
1671 for (;;)
1672 {
1673 if (*p == '\n')
1674 {
1675 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1676 break;
1677 }
1678 else if (!*p)
1679 goto interrupt;
1680
1681 ++p;
1682 }
1683
1684 break;
1559 1685
1560 // inside a string 1686 // inside a string
1561 case INCR_M_STR: 1687 case INCR_M_STR:
1562 incr_m_str: 1688 incr_m_str:
1563 for (;;) 1689 for (;;)
1622 1748
1623 case ']': 1749 case ']':
1624 case '}': 1750 case '}':
1625 if (--self->incr_nest <= 0) 1751 if (--self->incr_nest <= 0)
1626 goto interrupt; 1752 goto interrupt;
1753 break;
1754
1755 case '#':
1756 self->incr_mode = INCR_M_C1;
1757 goto incr_m_c;
1627 } 1758 }
1628 } 1759 }
1629 } 1760 }
1630 1761
1631 modechange: 1762 modechange:
1632 ; 1763 ;
1633 } 1764 }
1634 1765
1635interrupt: 1766interrupt:
1636 self->incr_pos = p - SvPVX (self->incr_text); 1767 self->incr_pos = p - SvPVX (self->incr_text);
1768 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
1637 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D 1769 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1638} 1770}
1639 1771
1640///////////////////////////////////////////////////////////////////////////// 1772/////////////////////////////////////////////////////////////////////////////
1641// XS interface functions 1773// XS interface functions
1799 PPCODE: 1931 PPCODE:
1800{ 1932{
1801 if (!self->incr_text) 1933 if (!self->incr_text)
1802 self->incr_text = newSVpvn ("", 0); 1934 self->incr_text = newSVpvn ("", 0);
1803 1935
1936 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
1937 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
1938 if (DECODE_WANTS_OCTETS (self))
1939 {
1940 if (self->incr_pos)
1941 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
1942 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
1943
1944 sv_utf8_downgrade (self->incr_text, 0);
1945 }
1946 else
1947 {
1948 sv_utf8_upgrade (self->incr_text);
1949
1950 if (self->incr_pos)
1951 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1952 - (U8 *)SvPVX (self->incr_text);
1953 }
1954
1804 // append data, if any 1955 // append data, if any
1805 if (jsonstr) 1956 if (jsonstr)
1806 { 1957 {
1958 /* make sure both strings have same encoding */
1959 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1807 if (SvUTF8 (jsonstr)) 1960 if (SvUTF8 (jsonstr))
1961 sv_utf8_downgrade (jsonstr, 0);
1808 { 1962 else
1809 if (!SvUTF8 (self->incr_text))
1810 {
1811 /* utf-8-ness differs, need to upgrade */
1812 sv_utf8_upgrade (self->incr_text);
1813
1814 if (self->incr_pos)
1815 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1816 - (U8 *)SvPVX (self->incr_text);
1817 }
1818 }
1819 else if (SvUTF8 (self->incr_text))
1820 sv_utf8_upgrade (jsonstr); 1963 sv_utf8_upgrade (jsonstr);
1821 1964
1965 /* and then just blindly append */
1822 { 1966 {
1823 STRLEN len; 1967 STRLEN len;
1824 const char *str = SvPV (jsonstr, len); 1968 const char *str = SvPV (jsonstr, len);
1825 STRLEN cur = SvCUR (self->incr_text); 1969 STRLEN cur = SvCUR (self->incr_text);
1826 1970
1925 json_init (&json); 2069 json_init (&json);
1926 json.flags |= ix; 2070 json.flags |= ix;
1927 XPUSHs (decode_json (jsonstr, &json, 0)); 2071 XPUSHs (decode_json (jsonstr, &json, 0));
1928} 2072}
1929 2073
1930

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines