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.103 by root, Wed Jan 6 08:01:39 2010 UTC vs.
Revision 1.111 by root, Wed Jul 27 15:53:40 2011 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))
187 return SvUTF8 (sv) 189 return SvUTF8 (sv)
188 ? utf8_distance (offset, SvPVX (sv)) 190 ? utf8_distance (offset, SvPVX (sv))
189 : offset - SvPVX (sv); 191 : offset - SvPVX (sv);
190} 192}
191 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}
192///////////////////////////////////////////////////////////////////////////// 289/////////////////////////////////////////////////////////////////////////////
193// encoder 290// encoder
194 291
195// structure used for encoding JSON 292// structure used for encoding JSON
196typedef struct 293typedef struct
1119 } 1216 }
1120 1217
1121 len -= *start == '-' ? 1 : 0; 1218 len -= *start == '-' ? 1 : 0;
1122 1219
1123 // does not fit into IV or UV, try NV 1220 // does not fit into IV or UV, try NV
1124 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1221 if (len <= NV_DIG)
1125 #if defined (LDBL_DIG)
1126 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1127 #endif
1128 )
1129 // fits into NV without loss of precision 1222 // fits into NV without loss of precision
1130 return newSVnv (Atof (start)); 1223 return newSVnv (json_atof (start));
1131 1224
1132 // everything else fails, convert it to a string 1225 // everything else fails, convert it to a string
1133 return newSVpvn (start, dec->cur - start); 1226 return newSVpvn (start, dec->cur - start);
1134 } 1227 }
1135 1228
1136 // loss of precision here 1229 // loss of precision here
1137 return newSVnv (Atof (start)); 1230 return newSVnv (json_atof (start));
1138 1231
1139fail: 1232fail:
1140 return 0; 1233 return 0;
1141} 1234}
1142 1235
1311 dSP; 1404 dSP;
1312 int count; 1405 int count;
1313 1406
1314 ENTER; SAVETMPS; PUSHMARK (SP); 1407 ENTER; SAVETMPS; PUSHMARK (SP);
1315 XPUSHs (HeVAL (he)); 1408 XPUSHs (HeVAL (he));
1409 sv_2mortal (sv);
1316 1410
1317 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1411 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1318 1412
1319 if (count == 1) 1413 if (count == 1)
1320 { 1414 {
1321 sv = newSVsv (POPs); 1415 sv = newSVsv (POPs);
1322 FREETMPS; LEAVE; 1416 FREETMPS; LEAVE;
1323 return sv; 1417 return sv;
1324 } 1418 }
1325 1419
1420 SvREFCNT_inc (sv);
1326 FREETMPS; LEAVE; 1421 FREETMPS; LEAVE;
1327 } 1422 }
1328 } 1423 }
1329 1424
1330 if (dec->json.cb_object) 1425 if (dec->json.cb_object)
1426{ 1521{
1427 dec_t dec; 1522 dec_t dec;
1428 SV *sv; 1523 SV *sv;
1429 1524
1430 /* work around bugs in 5.10 where manipulating magic values 1525 /* work around bugs in 5.10 where manipulating magic values
1431 * 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).
1432 */ 1529 */
1433 /*SvGETMAGIC (string);*/ 1530 /*SvGETMAGIC (string);*/
1434 if (SvMAGICAL (string)) 1531 if (SvMAGICAL (string) || !SvPOK (string))
1435 string = sv_2mortal (newSVsv (string)); 1532 string = sv_2mortal (newSVsv (string));
1436 1533
1437 SvUPGRADE (string, SVt_PV); 1534 SvUPGRADE (string, SVt_PV);
1438 1535
1439 /* 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
1456 if (offset > json->max_size && json->max_size) 1553 if (offset > json->max_size && json->max_size)
1457 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",
1458 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1555 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1459 } 1556 }
1460 1557
1461 if (json->flags & F_UTF8) 1558 if (DECODE_WANTS_OCTETS (json))
1462 sv_utf8_downgrade (string, 0); 1559 sv_utf8_downgrade (string, 0);
1463 else 1560 else
1464 sv_utf8_upgrade (string); 1561 sv_utf8_upgrade (string);
1465 1562
1466 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1563 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1837 PPCODE: 1934 PPCODE:
1838{ 1935{
1839 if (!self->incr_text) 1936 if (!self->incr_text)
1840 self->incr_text = newSVpvn ("", 0); 1937 self->incr_text = newSVpvn ("", 0);
1841 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
1842 // append data, if any 1958 // append data, if any
1843 if (jsonstr) 1959 if (jsonstr)
1844 { 1960 {
1961 /* make sure both strings have same encoding */
1962 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1845 if (SvUTF8 (jsonstr)) 1963 if (SvUTF8 (jsonstr))
1964 sv_utf8_downgrade (jsonstr, 0);
1846 { 1965 else
1847 if (!SvUTF8 (self->incr_text))
1848 {
1849 /* utf-8-ness differs, need to upgrade */
1850 sv_utf8_upgrade (self->incr_text);
1851
1852 if (self->incr_pos)
1853 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1854 - (U8 *)SvPVX (self->incr_text);
1855 }
1856 }
1857 else if (SvUTF8 (self->incr_text))
1858 sv_utf8_upgrade (jsonstr); 1966 sv_utf8_upgrade (jsonstr);
1859 1967
1968 /* and then just blindly append */
1860 { 1969 {
1861 STRLEN len; 1970 STRLEN len;
1862 const char *str = SvPV (jsonstr, len); 1971 const char *str = SvPV (jsonstr, len);
1863 STRLEN cur = SvCUR (self->incr_text); 1972 STRLEN cur = SvCUR (self->incr_text);
1864 1973
1883 if (self->incr_pos > self->max_size && self->max_size) 1992 if (self->incr_pos > self->max_size && self->max_size)
1884 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1993 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1885 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 1994 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1886 1995
1887 if (!INCR_DONE (self)) 1996 if (!INCR_DONE (self))
1997 {
1998 // as an optimisation, do not accumulate white space in the incr buffer
1999 if (self->incr_mode == INCR_M_WS)
2000 SvCUR_set (self->incr_text, 0);
2001
1888 break; 2002 break;
2003 }
1889 } 2004 }
1890 2005
1891 XPUSHs (decode_json (self->incr_text, self, &offset)); 2006 XPUSHs (decode_json (self->incr_text, self, &offset));
1892 2007
1893 self->incr_pos -= offset - SvPVX (self->incr_text); 2008 self->incr_pos -= offset - SvPVX (self->incr_text);
1963 json_init (&json); 2078 json_init (&json);
1964 json.flags |= ix; 2079 json.flags |= ix;
1965 XPUSHs (decode_json (jsonstr, &json, 0)); 2080 XPUSHs (decode_json (jsonstr, &json, 0));
1966} 2081}
1967 2082
1968

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines