… | |
… | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
14 | #endif |
15 | |
15 | |
16 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
20 | #endif |
20 | #endif |
21 | |
21 | |
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
… | |
… | |
173 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
173 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
174 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
174 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
175 | *s++ = 0x80 | ( ch & 0x3f); |
175 | *s++ = 0x80 | ( ch & 0x3f); |
176 | |
176 | |
177 | return s; |
177 | return s; |
|
|
178 | } |
|
|
179 | |
|
|
180 | // convert offset pointer to character index, sv must be string |
|
|
181 | static STRLEN |
|
|
182 | ptr_to_index (SV *sv, char *offset) |
|
|
183 | { |
|
|
184 | return SvUTF8 (sv) |
|
|
185 | ? utf8_distance (offset, SvPVX (sv)) |
|
|
186 | : offset - SvPVX (sv); |
178 | } |
187 | } |
179 | |
188 | |
180 | ///////////////////////////////////////////////////////////////////////////// |
189 | ///////////////////////////////////////////////////////////////////////////// |
181 | // encoder |
190 | // encoder |
182 | |
191 | |
… | |
… | |
462 | |
471 | |
463 | // for canonical output we have to sort by keys first |
472 | // for canonical output we have to sort by keys first |
464 | // actually, this is mostly due to the stupid so-called |
473 | // actually, this is mostly due to the stupid so-called |
465 | // security workaround added somewhere in 5.8.x |
474 | // security workaround added somewhere in 5.8.x |
466 | // that randomises hash orderings |
475 | // that randomises hash orderings |
467 | if (enc->json.flags & F_CANONICAL) |
476 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
468 | { |
477 | { |
469 | int count = hv_iterinit (hv); |
478 | int count = hv_iterinit (hv); |
470 | |
479 | |
471 | if (SvMAGICAL (hv)) |
480 | if (SvMAGICAL (hv)) |
472 | { |
481 | { |
… | |
… | |
1075 | |
1084 | |
1076 | // special case the rather common 1..5-digit-int case |
1085 | // special case the rather common 1..5-digit-int case |
1077 | if (*start == '-') |
1086 | if (*start == '-') |
1078 | switch (len) |
1087 | switch (len) |
1079 | { |
1088 | { |
1080 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1089 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1081 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1090 | case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
1082 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1091 | case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1083 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1092 | case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1084 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1093 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1085 | } |
1094 | } |
1086 | else |
1095 | else |
1087 | switch (len) |
1096 | switch (len) |
1088 | { |
1097 | { |
1089 | case 1: return newSViv ( start [0] - '0' * 1); |
1098 | case 1: return newSViv ( start [0] - '0' * 1); |
1090 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1099 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1091 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1100 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1092 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1101 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1093 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1102 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1094 | } |
1103 | } |
1095 | |
1104 | |
1096 | { |
1105 | { |
1097 | UV uv; |
1106 | UV uv; |
1098 | int numtype = grok_number (start, len, &uv); |
1107 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1408 | fail: |
1417 | fail: |
1409 | return 0; |
1418 | return 0; |
1410 | } |
1419 | } |
1411 | |
1420 | |
1412 | static SV * |
1421 | static SV * |
1413 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1422 | decode_json (SV *string, JSON *json, char **offset_return) |
1414 | { |
1423 | { |
1415 | dec_t dec; |
1424 | dec_t dec; |
1416 | STRLEN offset; |
|
|
1417 | SV *sv; |
1425 | SV *sv; |
1418 | |
1426 | |
1419 | /* work around bugs in 5.10 where manipulating magic values |
1427 | /* work around bugs in 5.10 where manipulating magic values |
1420 | * will perl ignore the magic in subsequent accesses |
1428 | * will perl ignore the magic in subsequent accesses |
1421 | */ |
1429 | */ |
… | |
… | |
1433 | * and hope for the best. |
1441 | * and hope for the best. |
1434 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
1442 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
1435 | * assertion business is seriously broken, try yet another workaround |
1443 | * assertion business is seriously broken, try yet another workaround |
1436 | * for the broken -DDEBUGGING. |
1444 | * for the broken -DDEBUGGING. |
1437 | */ |
1445 | */ |
|
|
1446 | { |
1438 | #ifdef DEBUGGING |
1447 | #ifdef DEBUGGING |
1439 | offset = SvOK (string) ? sv_len (string) : 0; |
1448 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
1440 | #else |
1449 | #else |
1441 | offset = SvCUR (string); |
1450 | STRLEN offset = SvCUR (string); |
1442 | #endif |
1451 | #endif |
1443 | |
1452 | |
1444 | if (offset > json->max_size && json->max_size) |
1453 | if (offset > json->max_size && json->max_size) |
1445 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1454 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1446 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1455 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1456 | } |
1447 | |
1457 | |
1448 | if (json->flags & F_UTF8) |
1458 | if (json->flags & F_UTF8) |
1449 | sv_utf8_downgrade (string, 0); |
1459 | sv_utf8_downgrade (string, 0); |
1450 | else |
1460 | else |
1451 | sv_utf8_upgrade (string); |
1461 | sv_utf8_upgrade (string); |
… | |
… | |
1463 | |
1473 | |
1464 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1474 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1465 | |
1475 | |
1466 | decode_ws (&dec); |
1476 | decode_ws (&dec); |
1467 | sv = decode_sv (&dec); |
1477 | sv = decode_sv (&dec); |
|
|
1478 | |
|
|
1479 | if (offset_return) |
|
|
1480 | *offset_return = dec.cur; |
1468 | |
1481 | |
1469 | if (!(offset_return || !sv)) |
1482 | if (!(offset_return || !sv)) |
1470 | { |
1483 | { |
1471 | // check for trailing garbage |
1484 | // check for trailing garbage |
1472 | decode_ws (&dec); |
1485 | decode_ws (&dec); |
… | |
… | |
1475 | { |
1488 | { |
1476 | dec.err = "garbage after JSON object"; |
1489 | dec.err = "garbage after JSON object"; |
1477 | SvREFCNT_dec (sv); |
1490 | SvREFCNT_dec (sv); |
1478 | sv = 0; |
1491 | sv = 0; |
1479 | } |
1492 | } |
1480 | } |
|
|
1481 | |
|
|
1482 | if (offset_return || !sv) |
|
|
1483 | { |
|
|
1484 | offset = dec.json.flags & F_UTF8 |
|
|
1485 | ? dec.cur - SvPVX (string) |
|
|
1486 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1487 | |
|
|
1488 | if (offset_return) |
|
|
1489 | *offset_return = offset; |
|
|
1490 | } |
1493 | } |
1491 | |
1494 | |
1492 | if (!sv) |
1495 | if (!sv) |
1493 | { |
1496 | { |
1494 | SV *uni = sv_newmortal (); |
1497 | SV *uni = sv_newmortal (); |
… | |
… | |
1500 | SAVEVPTR (PL_curcop); |
1503 | SAVEVPTR (PL_curcop); |
1501 | PL_curcop = &cop; |
1504 | PL_curcop = &cop; |
1502 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1505 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1503 | LEAVE; |
1506 | LEAVE; |
1504 | |
1507 | |
1505 | croak ("%s, at character offset %d [\"%s\"]", |
1508 | croak ("%s, at character offset %d (before \"%s\")", |
1506 | dec.err, |
1509 | dec.err, |
1507 | (int)offset, |
1510 | ptr_to_index (string, dec.cur), |
1508 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1511 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1509 | } |
1512 | } |
1510 | |
1513 | |
1511 | sv = sv_2mortal (sv); |
1514 | sv = sv_2mortal (sv); |
1512 | |
1515 | |
… | |
… | |
1652 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1655 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1653 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1656 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1654 | |
1657 | |
1655 | json_true = get_bool ("JSON::XS::true"); |
1658 | json_true = get_bool ("JSON::XS::true"); |
1656 | json_false = get_bool ("JSON::XS::false"); |
1659 | json_false = get_bool ("JSON::XS::false"); |
|
|
1660 | |
|
|
1661 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1657 | } |
1662 | } |
1658 | |
1663 | |
1659 | PROTOTYPES: DISABLE |
1664 | PROTOTYPES: DISABLE |
1660 | |
1665 | |
1661 | void CLONE (...) |
1666 | void CLONE (...) |
… | |
… | |
1781 | XPUSHs (decode_json (jsonstr, self, 0)); |
1786 | XPUSHs (decode_json (jsonstr, self, 0)); |
1782 | |
1787 | |
1783 | void decode_prefix (JSON *self, SV *jsonstr) |
1788 | void decode_prefix (JSON *self, SV *jsonstr) |
1784 | PPCODE: |
1789 | PPCODE: |
1785 | { |
1790 | { |
1786 | STRLEN offset; |
1791 | char *offset; |
1787 | EXTEND (SP, 2); |
1792 | EXTEND (SP, 2); |
1788 | PUSHs (decode_json (jsonstr, self, &offset)); |
1793 | PUSHs (decode_json (jsonstr, self, &offset)); |
1789 | PUSHs (sv_2mortal (newSVuv (offset))); |
1794 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1790 | } |
1795 | } |
1791 | |
1796 | |
1792 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1797 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1793 | PPCODE: |
1798 | PPCODE: |
1794 | { |
1799 | { |
… | |
… | |
1796 | self->incr_text = newSVpvn ("", 0); |
1801 | self->incr_text = newSVpvn ("", 0); |
1797 | |
1802 | |
1798 | // append data, if any |
1803 | // append data, if any |
1799 | if (jsonstr) |
1804 | if (jsonstr) |
1800 | { |
1805 | { |
1801 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1806 | if (SvUTF8 (jsonstr)) |
1802 | { |
1807 | { |
|
|
1808 | if (!SvUTF8 (self->incr_text)) |
|
|
1809 | { |
1803 | /* utf-8-ness differs, need to upgrade */ |
1810 | /* utf-8-ness differs, need to upgrade */ |
1804 | sv_utf8_upgrade (self->incr_text); |
1811 | sv_utf8_upgrade (self->incr_text); |
1805 | |
1812 | |
1806 | if (self->incr_pos) |
1813 | if (self->incr_pos) |
1807 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1814 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1808 | - (U8 *)SvPVX (self->incr_text); |
1815 | - (U8 *)SvPVX (self->incr_text); |
|
|
1816 | } |
1809 | } |
1817 | } |
|
|
1818 | else if (SvUTF8 (self->incr_text)) |
|
|
1819 | sv_utf8_upgrade (jsonstr); |
1810 | |
1820 | |
1811 | { |
1821 | { |
1812 | STRLEN len; |
1822 | STRLEN len; |
1813 | const char *str = SvPV (jsonstr, len); |
1823 | const char *str = SvPV (jsonstr, len); |
1814 | STRLEN cur = SvCUR (self->incr_text); |
1824 | STRLEN cur = SvCUR (self->incr_text); |
… | |
… | |
1823 | } |
1833 | } |
1824 | |
1834 | |
1825 | if (GIMME_V != G_VOID) |
1835 | if (GIMME_V != G_VOID) |
1826 | do |
1836 | do |
1827 | { |
1837 | { |
1828 | STRLEN offset; |
1838 | char *offset; |
1829 | |
1839 | |
1830 | if (!INCR_DONE (self)) |
1840 | if (!INCR_DONE (self)) |
1831 | { |
1841 | { |
1832 | incr_parse (self); |
1842 | incr_parse (self); |
1833 | |
1843 | |
… | |
… | |
1839 | break; |
1849 | break; |
1840 | } |
1850 | } |
1841 | |
1851 | |
1842 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1852 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1843 | |
1853 | |
1844 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1845 | self->incr_pos -= offset; |
1854 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1846 | self->incr_nest = 0; |
1855 | self->incr_nest = 0; |
1847 | self->incr_mode = 0; |
1856 | self->incr_mode = 0; |
|
|
1857 | |
|
|
1858 | sv_chop (self->incr_text, offset); |
1848 | } |
1859 | } |
1849 | while (GIMME_V == G_ARRAY); |
1860 | while (GIMME_V == G_ARRAY); |
1850 | } |
1861 | } |
1851 | |
1862 | |
1852 | SV *incr_text (JSON *self) |
1863 | SV *incr_text (JSON *self) |