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.105 by root, Tue Jan 19 01:07:27 2010 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))
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 }
270 { 278 {
271 ++s; 279 ++s;
272 neg = 1; 280 neg = 1;
273 } 281 }
274 282
283 // a recursion depth of ten gives us >>500 bits
275 json_atof_scan1 (s, &accum, &expo, 0); 284 json_atof_scan1 (s, &accum, &expo, 0, 10);
276 285
277 return neg ? -accum : accum; 286 return neg ? -accum : accum;
278} 287}
279///////////////////////////////////////////////////////////////////////////// 288/////////////////////////////////////////////////////////////////////////////
280// encoder 289// encoder
1394 dSP; 1403 dSP;
1395 int count; 1404 int count;
1396 1405
1397 ENTER; SAVETMPS; PUSHMARK (SP); 1406 ENTER; SAVETMPS; PUSHMARK (SP);
1398 XPUSHs (HeVAL (he)); 1407 XPUSHs (HeVAL (he));
1408 sv_2mortal (sv);
1399 1409
1400 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1410 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1401 1411
1402 if (count == 1) 1412 if (count == 1)
1403 { 1413 {
1404 sv = newSVsv (POPs); 1414 sv = newSVsv (POPs);
1405 FREETMPS; LEAVE; 1415 FREETMPS; LEAVE;
1406 return sv; 1416 return sv;
1407 } 1417 }
1408 1418
1419 SvREFCNT_inc (sv);
1409 FREETMPS; LEAVE; 1420 FREETMPS; LEAVE;
1410 } 1421 }
1411 } 1422 }
1412 1423
1413 if (dec->json.cb_object) 1424 if (dec->json.cb_object)
1539 if (offset > json->max_size && json->max_size) 1550 if (offset > json->max_size && json->max_size)
1540 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",
1541 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1552 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1542 } 1553 }
1543 1554
1544 if (json->flags & F_UTF8) 1555 if (DECODE_WANTS_OCTETS (json))
1545 sv_utf8_downgrade (string, 0); 1556 sv_utf8_downgrade (string, 0);
1546 else 1557 else
1547 sv_utf8_upgrade (string); 1558 sv_utf8_upgrade (string);
1548 1559
1549 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1560 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1920 PPCODE: 1931 PPCODE:
1921{ 1932{
1922 if (!self->incr_text) 1933 if (!self->incr_text)
1923 self->incr_text = newSVpvn ("", 0); 1934 self->incr_text = newSVpvn ("", 0);
1924 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
1925 // append data, if any 1955 // append data, if any
1926 if (jsonstr) 1956 if (jsonstr)
1927 { 1957 {
1958 /* make sure both strings have same encoding */
1959 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1928 if (SvUTF8 (jsonstr)) 1960 if (SvUTF8 (jsonstr))
1961 sv_utf8_downgrade (jsonstr, 0);
1929 { 1962 else
1930 if (!SvUTF8 (self->incr_text))
1931 {
1932 /* utf-8-ness differs, need to upgrade */
1933 sv_utf8_upgrade (self->incr_text);
1934
1935 if (self->incr_pos)
1936 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1937 - (U8 *)SvPVX (self->incr_text);
1938 }
1939 }
1940 else if (SvUTF8 (self->incr_text))
1941 sv_utf8_upgrade (jsonstr); 1963 sv_utf8_upgrade (jsonstr);
1942 1964
1965 /* and then just blindly append */
1943 { 1966 {
1944 STRLEN len; 1967 STRLEN len;
1945 const char *str = SvPV (jsonstr, len); 1968 const char *str = SvPV (jsonstr, len);
1946 STRLEN cur = SvCUR (self->incr_text); 1969 STRLEN cur = SvCUR (self->incr_text);
1947 1970

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines