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.107 by root, Wed Mar 17 01:45:43 2010 UTC

77 77
78enum { 78enum {
79 INCR_M_WS = 0, // initial whitespace skipping, must be 0 79 INCR_M_WS = 0, // initial whitespace skipping, must be 0
80 INCR_M_STR, // inside string 80 INCR_M_STR, // inside string
81 INCR_M_BS, // inside backslash 81 INCR_M_BS, // inside backslash
82 INCR_M_C0, // inside comment in initial whitespace sequence
83 INCR_M_C1, // inside comment in other places
82 INCR_M_JSON // outside anything, count nesting 84 INCR_M_JSON // outside anything, count nesting
83}; 85};
84 86
85#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) 87#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
86 88
185 return SvUTF8 (sv) 187 return SvUTF8 (sv)
186 ? utf8_distance (offset, SvPVX (sv)) 188 ? utf8_distance (offset, SvPVX (sv))
187 : offset - SvPVX (sv); 189 : offset - SvPVX (sv);
188} 190}
189 191
192/////////////////////////////////////////////////////////////////////////////
193// fp hell
194
195// scan a group of digits, and a trailing exponent
196static void
197json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
198{
199 UV uaccum = 0;
200 int eaccum = 0;
201
202 // if we recurse too deep, skip all remaining digits
203 // to avoid a stack overflow attack
204 if (expect_false (--maxdepth <= 0))
205 while (((U8)*s - '0') < 10)
206 ++s;
207
208 for (;;)
209 {
210 U8 dig = (U8)*s - '0';
211
212 if (expect_false (dig >= 10))
213 {
214 if (dig == (U8)((U8)'.' - (U8)'0'))
215 {
216 ++s;
217 json_atof_scan1 (s, accum, expo, 1, maxdepth);
218 }
219 else if ((dig | ' ') == 'e' - '0')
220 {
221 int exp2 = 0;
222 int neg = 0;
223
224 ++s;
225
226 if (*s == '-')
227 {
228 ++s;
229 neg = 1;
230 }
231 else if (*s == '+')
232 ++s;
233
234 while ((dig = (U8)*s - '0') < 10)
235 exp2 = exp2 * 10 + *s++ - '0';
236
237 *expo += neg ? -exp2 : exp2;
238 }
239
240 break;
241 }
242
243 ++s;
244
245 uaccum = uaccum * 10 + dig;
246 ++eaccum;
247
248 // if we have too many digits, then recurse for more
249 // we actually do this for rather few digits
250 if (uaccum >= (UV_MAX - 9) / 10)
251 {
252 if (postdp) *expo -= eaccum;
253 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
254 if (postdp) *expo += eaccum;
255
256 break;
257 }
258 }
259
260 // this relies greatly on the quality of the pow ()
261 // implementation of the platform, but a good
262 // implementation is hard to beat.
263 if (postdp) *expo -= eaccum;
264 *accum += uaccum * Perl_pow (10., *expo);
265 *expo += eaccum;
266}
267
268static NV
269json_atof (const char *s)
270{
271 NV accum = 0.;
272 int expo = 0;
273 int neg = 0;
274
275 if (*s == '-')
276 {
277 ++s;
278 neg = 1;
279 }
280
281 // a recursion depth of ten gives us >>500 bits
282 json_atof_scan1 (s, &accum, &expo, 0, 10);
283
284 return neg ? -accum : accum;
285}
190///////////////////////////////////////////////////////////////////////////// 286/////////////////////////////////////////////////////////////////////////////
191// encoder 287// encoder
192 288
193// structure used for encoding JSON 289// structure used for encoding JSON
194typedef struct 290typedef struct
1117 } 1213 }
1118 1214
1119 len -= *start == '-' ? 1 : 0; 1215 len -= *start == '-' ? 1 : 0;
1120 1216
1121 // does not fit into IV or UV, try NV 1217 // does not fit into IV or UV, try NV
1122 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1218 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 1219 // fits into NV without loss of precision
1128 return newSVnv (Atof (start)); 1220 return newSVnv (json_atof (start));
1129 1221
1130 // everything else fails, convert it to a string 1222 // everything else fails, convert it to a string
1131 return newSVpvn (start, dec->cur - start); 1223 return newSVpvn (start, dec->cur - start);
1132 } 1224 }
1133 1225
1134 // loss of precision here 1226 // loss of precision here
1135 return newSVnv (Atof (start)); 1227 return newSVnv (json_atof (start));
1136 1228
1137fail: 1229fail:
1138 return 0; 1230 return 0;
1139} 1231}
1140 1232
1309 dSP; 1401 dSP;
1310 int count; 1402 int count;
1311 1403
1312 ENTER; SAVETMPS; PUSHMARK (SP); 1404 ENTER; SAVETMPS; PUSHMARK (SP);
1313 XPUSHs (HeVAL (he)); 1405 XPUSHs (HeVAL (he));
1406 sv_2mortal (sv);
1314 1407
1315 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1408 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1316 1409
1317 if (count == 1) 1410 if (count == 1)
1318 { 1411 {
1319 sv = newSVsv (POPs); 1412 sv = newSVsv (POPs);
1320 FREETMPS; LEAVE; 1413 FREETMPS; LEAVE;
1321 return sv; 1414 return sv;
1322 } 1415 }
1323 1416
1417 SvREFCNT_inc (sv);
1324 FREETMPS; LEAVE; 1418 FREETMPS; LEAVE;
1325 } 1419 }
1326 } 1420 }
1327 1421
1328 if (dec->json.cb_object) 1422 if (dec->json.cb_object)
1526static void 1620static void
1527incr_parse (JSON *self) 1621incr_parse (JSON *self)
1528{ 1622{
1529 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1623 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1530 1624
1625 // the state machine here is a bit convoluted and could be simplified a lot
1626 // but this would make it slower, so...
1627
1531 for (;;) 1628 for (;;)
1532 { 1629 {
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 1630 //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) 1631 switch (self->incr_mode)
1535 { 1632 {
1536 // only used for intiial whitespace skipping 1633 // only used for initial whitespace skipping
1537 case INCR_M_WS: 1634 case INCR_M_WS:
1538 for (;;) 1635 for (;;)
1539 { 1636 {
1540 if (*p > 0x20) 1637 if (*p > 0x20)
1541 { 1638 {
1639 if (*p == '#')
1640 {
1641 self->incr_mode = INCR_M_C0;
1642 goto incr_m_c;
1643 }
1644 else
1645 {
1542 self->incr_mode = INCR_M_JSON; 1646 self->incr_mode = INCR_M_JSON;
1543 goto incr_m_json; 1647 goto incr_m_json;
1648 }
1544 } 1649 }
1545 else if (!*p) 1650 else if (!*p)
1546 goto interrupt; 1651 goto interrupt;
1547 1652
1548 ++p; 1653 ++p;
1554 goto interrupt; 1659 goto interrupt;
1555 1660
1556 ++p; 1661 ++p;
1557 self->incr_mode = INCR_M_STR; 1662 self->incr_mode = INCR_M_STR;
1558 goto incr_m_str; 1663 goto incr_m_str;
1664
1665 // inside #-style comments
1666 case INCR_M_C0:
1667 case INCR_M_C1:
1668 incr_m_c:
1669 for (;;)
1670 {
1671 if (*p == '\n')
1672 {
1673 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1674 break;
1675 }
1676 else if (!*p)
1677 goto interrupt;
1678
1679 ++p;
1680 }
1681
1682 break;
1559 1683
1560 // inside a string 1684 // inside a string
1561 case INCR_M_STR: 1685 case INCR_M_STR:
1562 incr_m_str: 1686 incr_m_str:
1563 for (;;) 1687 for (;;)
1622 1746
1623 case ']': 1747 case ']':
1624 case '}': 1748 case '}':
1625 if (--self->incr_nest <= 0) 1749 if (--self->incr_nest <= 0)
1626 goto interrupt; 1750 goto interrupt;
1751 break;
1752
1753 case '#':
1754 self->incr_mode = INCR_M_C1;
1755 goto incr_m_c;
1627 } 1756 }
1628 } 1757 }
1629 } 1758 }
1630 1759
1631 modechange: 1760 modechange:
1632 ; 1761 ;
1633 } 1762 }
1634 1763
1635interrupt: 1764interrupt:
1636 self->incr_pos = p - SvPVX (self->incr_text); 1765 self->incr_pos = p - SvPVX (self->incr_text);
1766 //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 1767 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1638} 1768}
1639 1769
1640///////////////////////////////////////////////////////////////////////////// 1770/////////////////////////////////////////////////////////////////////////////
1641// XS interface functions 1771// XS interface functions
1925 json_init (&json); 2055 json_init (&json);
1926 json.flags |= ix; 2056 json.flags |= ix;
1927 XPUSHs (decode_json (jsonstr, &json, 0)); 2057 XPUSHs (decode_json (jsonstr, &json, 0));
1928} 2058}
1929 2059
1930

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines