… | |
… | |
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 |
196 | static void |
198 | static void |
197 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp) |
199 | json_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 | |
254 | // this relies greatly on the quality of the pow () |
262 | // this relies greatly on the quality of the pow () |
255 | // implementation of the platform, but a good |
263 | // implementation of the platform, but a good |
256 | // implementation is hard to beat. |
264 | // implementation is hard to beat. |
|
|
265 | // (IEEE 754 conformant ones are required to be exact) |
257 | if (postdp) *expo -= eaccum; |
266 | if (postdp) *expo -= eaccum; |
258 | *accum += uaccum * Perl_pow (10., *expo); |
267 | *accum += uaccum * Perl_pow (10., *expo); |
259 | *expo += eaccum; |
268 | *expo += eaccum; |
260 | } |
269 | } |
261 | |
270 | |
… | |
… | |
270 | { |
279 | { |
271 | ++s; |
280 | ++s; |
272 | neg = 1; |
281 | neg = 1; |
273 | } |
282 | } |
274 | |
283 | |
|
|
284 | // a recursion depth of ten gives us >>500 bits |
275 | json_atof_scan1 (s, &accum, &expo, 0); |
285 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
276 | |
286 | |
277 | return neg ? -accum : accum; |
287 | return neg ? -accum : accum; |
278 | } |
288 | } |
279 | ///////////////////////////////////////////////////////////////////////////// |
289 | ///////////////////////////////////////////////////////////////////////////// |
280 | // encoder |
290 | // encoder |
… | |
… | |
1394 | dSP; |
1404 | dSP; |
1395 | int count; |
1405 | int count; |
1396 | |
1406 | |
1397 | ENTER; SAVETMPS; PUSHMARK (SP); |
1407 | ENTER; SAVETMPS; PUSHMARK (SP); |
1398 | XPUSHs (HeVAL (he)); |
1408 | XPUSHs (HeVAL (he)); |
|
|
1409 | sv_2mortal (sv); |
1399 | |
1410 | |
1400 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1411 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1401 | |
1412 | |
1402 | if (count == 1) |
1413 | if (count == 1) |
1403 | { |
1414 | { |
1404 | sv = newSVsv (POPs); |
1415 | sv = newSVsv (POPs); |
1405 | FREETMPS; LEAVE; |
1416 | FREETMPS; LEAVE; |
1406 | return sv; |
1417 | return sv; |
1407 | } |
1418 | } |
1408 | |
1419 | |
|
|
1420 | SvREFCNT_inc (sv); |
1409 | FREETMPS; LEAVE; |
1421 | FREETMPS; LEAVE; |
1410 | } |
1422 | } |
1411 | } |
1423 | } |
1412 | |
1424 | |
1413 | if (dec->json.cb_object) |
1425 | if (dec->json.cb_object) |
… | |
… | |
1509 | { |
1521 | { |
1510 | dec_t dec; |
1522 | dec_t dec; |
1511 | SV *sv; |
1523 | SV *sv; |
1512 | |
1524 | |
1513 | /* work around bugs in 5.10 where manipulating magic values |
1525 | /* work around bugs in 5.10 where manipulating magic values |
1514 | * 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). |
1515 | */ |
1529 | */ |
1516 | /*SvGETMAGIC (string);*/ |
1530 | /*SvGETMAGIC (string);*/ |
1517 | if (SvMAGICAL (string)) |
1531 | if (SvMAGICAL (string) || !SvPOK (string)) |
1518 | string = sv_2mortal (newSVsv (string)); |
1532 | string = sv_2mortal (newSVsv (string)); |
1519 | |
1533 | |
1520 | SvUPGRADE (string, SVt_PV); |
1534 | SvUPGRADE (string, SVt_PV); |
1521 | |
1535 | |
1522 | /* 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 |
… | |
… | |
1539 | if (offset > json->max_size && json->max_size) |
1553 | 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", |
1554 | 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); |
1555 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1542 | } |
1556 | } |
1543 | |
1557 | |
1544 | if (json->flags & F_UTF8) |
1558 | if (DECODE_WANTS_OCTETS (json)) |
1545 | sv_utf8_downgrade (string, 0); |
1559 | sv_utf8_downgrade (string, 0); |
1546 | else |
1560 | else |
1547 | sv_utf8_upgrade (string); |
1561 | sv_utf8_upgrade (string); |
1548 | |
1562 | |
1549 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1563 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1920 | PPCODE: |
1934 | PPCODE: |
1921 | { |
1935 | { |
1922 | if (!self->incr_text) |
1936 | if (!self->incr_text) |
1923 | self->incr_text = newSVpvn ("", 0); |
1937 | self->incr_text = newSVpvn ("", 0); |
1924 | |
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 | |
1925 | // append data, if any |
1958 | // append data, if any |
1926 | if (jsonstr) |
1959 | if (jsonstr) |
1927 | { |
1960 | { |
|
|
1961 | /* make sure both strings have same encoding */ |
|
|
1962 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1928 | if (SvUTF8 (jsonstr)) |
1963 | if (SvUTF8 (jsonstr)) |
|
|
1964 | sv_utf8_downgrade (jsonstr, 0); |
1929 | { |
1965 | 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); |
1966 | sv_utf8_upgrade (jsonstr); |
1942 | |
1967 | |
|
|
1968 | /* and then just blindly append */ |
1943 | { |
1969 | { |
1944 | STRLEN len; |
1970 | STRLEN len; |
1945 | const char *str = SvPV (jsonstr, len); |
1971 | const char *str = SvPV (jsonstr, len); |
1946 | STRLEN cur = SvCUR (self->incr_text); |
1972 | STRLEN cur = SvCUR (self->incr_text); |
1947 | |
1973 | |