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.104 by root, Tue Jan 19 00:31:13 2010 UTC vs.
Revision 1.113 by root, Thu Aug 11 21:37:41 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))
192///////////////////////////////////////////////////////////////////////////// 194/////////////////////////////////////////////////////////////////////////////
193// fp hell 195// fp hell
194 196
195// scan a group of digits, and a trailing exponent 197// scan a group of digits, and a trailing exponent
196static void 198static void
197json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp) 199json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
198{ 200{
199 UV uaccum = 0; 201 UV uaccum = 0;
200 int eaccum = 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;
201 209
202 for (;;) 210 for (;;)
203 { 211 {
204 U8 dig = (U8)*s - '0'; 212 U8 dig = (U8)*s - '0';
205 213
206 if (expect_false (dig >= 10)) 214 if (expect_false (dig >= 10))
207 { 215 {
208 if (dig == (U8)((U8)'.' - (U8)'0')) 216 if (dig == (U8)((U8)'.' - (U8)'0'))
209 { 217 {
210 ++s; 218 ++s;
211 json_atof_scan1 (s, accum, expo, 1); 219 json_atof_scan1 (s, accum, expo, 1, maxdepth);
212 } 220 }
213 else if ((dig | ' ') == 'e' - '0') 221 else if ((dig | ' ') == 'e' - '0')
214 { 222 {
215 int exp2 = 0; 223 int exp2 = 0;
216 int neg = 0; 224 int neg = 0;
242 // if we have too many digits, then recurse for more 250 // if we have too many digits, then recurse for more
243 // we actually do this for rather few digits 251 // we actually do this for rather few digits
244 if (uaccum >= (UV_MAX - 9) / 10) 252 if (uaccum >= (UV_MAX - 9) / 10)
245 { 253 {
246 if (postdp) *expo -= eaccum; 254 if (postdp) *expo -= eaccum;
247 json_atof_scan1 (s, accum, expo, postdp); 255 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
248 if (postdp) *expo += eaccum; 256 if (postdp) *expo += eaccum;
249 257
250 break; 258 break;
251 } 259 }
252 } 260 }
253 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)
254 if (postdp) *expo -= eaccum; 266 if (postdp) *expo -= eaccum;
255 *accum += uaccum * pow (10., *expo); 267 *accum += uaccum * Perl_pow (10., *expo);
256 *expo += eaccum; 268 *expo += eaccum;
257} 269}
258 270
259static NV 271static NV
260json_atof (const char *s) 272json_atof (const char *s)
267 { 279 {
268 ++s; 280 ++s;
269 neg = 1; 281 neg = 1;
270 } 282 }
271 283
284 // a recursion depth of ten gives us >>500 bits
272 json_atof_scan1 (s, &accum, &expo, 0); 285 json_atof_scan1 (s, &accum, &expo, 0, 10);
273 286
274 return neg ? -accum : accum; 287 return neg ? -accum : accum;
275} 288}
276///////////////////////////////////////////////////////////////////////////// 289/////////////////////////////////////////////////////////////////////////////
277// encoder 290// encoder
824 encode_rv (enc, SvRV (sv)); 837 encode_rv (enc, SvRV (sv));
825 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 838 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
826 encode_str (enc, "null", 4, 0); 839 encode_str (enc, "null", 4, 0);
827 else 840 else
828 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 841 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
829 SvPV_nolen (sv), SvFLAGS (sv)); 842 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
830} 843}
831 844
832static SV * 845static SV *
833encode_json (SV *scalar, JSON *json) 846encode_json (SV *scalar, JSON *json)
834{ 847{
1391 dSP; 1404 dSP;
1392 int count; 1405 int count;
1393 1406
1394 ENTER; SAVETMPS; PUSHMARK (SP); 1407 ENTER; SAVETMPS; PUSHMARK (SP);
1395 XPUSHs (HeVAL (he)); 1408 XPUSHs (HeVAL (he));
1409 sv_2mortal (sv);
1396 1410
1397 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1411 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1398 1412
1399 if (count == 1) 1413 if (count == 1)
1400 { 1414 {
1401 sv = newSVsv (POPs); 1415 sv = newSVsv (POPs);
1402 FREETMPS; LEAVE; 1416 FREETMPS; LEAVE;
1403 return sv; 1417 return sv;
1404 } 1418 }
1405 1419
1420 SvREFCNT_inc (sv);
1406 FREETMPS; LEAVE; 1421 FREETMPS; LEAVE;
1407 } 1422 }
1408 } 1423 }
1409 1424
1410 if (dec->json.cb_object) 1425 if (dec->json.cb_object)
1506{ 1521{
1507 dec_t dec; 1522 dec_t dec;
1508 SV *sv; 1523 SV *sv;
1509 1524
1510 /* work around bugs in 5.10 where manipulating magic values 1525 /* work around bugs in 5.10 where manipulating magic values
1511 * 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).
1512 */ 1529 */
1513 /*SvGETMAGIC (string);*/ 1530 /*SvGETMAGIC (string);*/
1514 if (SvMAGICAL (string)) 1531 if (SvMAGICAL (string) || !SvPOK (string))
1515 string = sv_2mortal (newSVsv (string)); 1532 string = sv_2mortal (newSVsv (string));
1516 1533
1517 SvUPGRADE (string, SVt_PV); 1534 SvUPGRADE (string, SVt_PV);
1518 1535
1519 /* 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
1536 if (offset > json->max_size && json->max_size) 1553 if (offset > json->max_size && json->max_size)
1537 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",
1538 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1555 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1539 } 1556 }
1540 1557
1541 if (json->flags & F_UTF8) 1558 if (DECODE_WANTS_OCTETS (json))
1542 sv_utf8_downgrade (string, 0); 1559 sv_utf8_downgrade (string, 0);
1543 else 1560 else
1544 sv_utf8_upgrade (string); 1561 sv_utf8_upgrade (string);
1545 1562
1546 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1563 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1588 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1605 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1589 LEAVE; 1606 LEAVE;
1590 1607
1591 croak ("%s, at character offset %d (before \"%s\")", 1608 croak ("%s, at character offset %d (before \"%s\")",
1592 dec.err, 1609 dec.err,
1593 ptr_to_index (string, dec.cur), 1610 (int)ptr_to_index (string, dec.cur),
1594 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1611 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1595 } 1612 }
1596 1613
1597 sv = sv_2mortal (sv); 1614 sv = sv_2mortal (sv);
1598 1615
1917 PPCODE: 1934 PPCODE:
1918{ 1935{
1919 if (!self->incr_text) 1936 if (!self->incr_text)
1920 self->incr_text = newSVpvn ("", 0); 1937 self->incr_text = newSVpvn ("", 0);
1921 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
1922 // append data, if any 1958 // append data, if any
1923 if (jsonstr) 1959 if (jsonstr)
1924 { 1960 {
1961 /* make sure both strings have same encoding */
1962 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1925 if (SvUTF8 (jsonstr)) 1963 if (SvUTF8 (jsonstr))
1964 sv_utf8_downgrade (jsonstr, 0);
1926 { 1965 else
1927 if (!SvUTF8 (self->incr_text))
1928 {
1929 /* utf-8-ness differs, need to upgrade */
1930 sv_utf8_upgrade (self->incr_text);
1931
1932 if (self->incr_pos)
1933 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1934 - (U8 *)SvPVX (self->incr_text);
1935 }
1936 }
1937 else if (SvUTF8 (self->incr_text))
1938 sv_utf8_upgrade (jsonstr); 1966 sv_utf8_upgrade (jsonstr);
1939 1967
1968 /* and then just blindly append */
1940 { 1969 {
1941 STRLEN len; 1970 STRLEN len;
1942 const char *str = SvPV (jsonstr, len); 1971 const char *str = SvPV (jsonstr, len);
1943 STRLEN cur = SvCUR (self->incr_text); 1972 STRLEN cur = SvCUR (self->incr_text);
1944 1973
1963 if (self->incr_pos > self->max_size && self->max_size) 1992 if (self->incr_pos > self->max_size && self->max_size)
1964 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",
1965 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 1994 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1966 1995
1967 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 && self->incr_pos)
2000 {
2001 self->incr_pos = 0;
2002 SvCUR_set (self->incr_text, 0);
2003 }
2004
1968 break; 2005 break;
2006 }
1969 } 2007 }
1970 2008
1971 XPUSHs (decode_json (self->incr_text, self, &offset)); 2009 XPUSHs (decode_json (self->incr_text, self, &offset));
1972 2010
1973 self->incr_pos -= offset - SvPVX (self->incr_text); 2011 self->incr_pos -= offset - SvPVX (self->incr_text);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines