… | |
… | |
32 | #define F_SHRINK 0x00000200UL |
32 | #define F_SHRINK 0x00000200UL |
33 | #define F_ALLOW_BLESSED 0x00000400UL |
33 | #define F_ALLOW_BLESSED 0x00000400UL |
34 | #define F_CONV_BLESSED 0x00000800UL |
34 | #define F_CONV_BLESSED 0x00000800UL |
35 | #define F_RELAXED 0x00001000UL |
35 | #define F_RELAXED 0x00001000UL |
36 | #define F_ALLOW_UNKNOWN 0x00002000UL |
36 | #define F_ALLOW_UNKNOWN 0x00002000UL |
37 | |
|
|
38 | #define F_MAXDEPTH 0xf8000000UL |
|
|
39 | #define S_MAXDEPTH 27 |
|
|
40 | #define F_MAXSIZE 0x01f00000UL |
|
|
41 | #define S_MAXSIZE 20 |
|
|
42 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
37 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
43 | |
38 | |
44 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
45 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
|
|
46 | |
|
|
47 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
48 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
|
|
49 | |
40 | |
50 | #define INIT_SIZE 32 // initial scalar size to be allocated |
41 | #define INIT_SIZE 32 // initial scalar size to be allocated |
51 | #define INDENT_STEP 3 // spaces per indentation level |
42 | #define INDENT_STEP 3 // spaces per indentation level |
52 | |
43 | |
53 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
44 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
… | |
… | |
68 | |
59 | |
69 | #define IN_RANGE_INC(type,val,beg,end) \ |
60 | #define IN_RANGE_INC(type,val,beg,end) \ |
70 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
61 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
71 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
62 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
72 | |
63 | |
|
|
64 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
|
|
65 | |
73 | #ifdef USE_ITHREADS |
66 | #ifdef USE_ITHREADS |
74 | # define JSON_SLOW 1 |
67 | # define JSON_SLOW 1 |
75 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
68 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
76 | #else |
69 | #else |
77 | # define JSON_SLOW 0 |
70 | # define JSON_SLOW 0 |
… | |
… | |
86 | INCR_M_STR, // inside string |
79 | INCR_M_STR, // inside string |
87 | INCR_M_BS, // inside backslash |
80 | INCR_M_BS, // inside backslash |
88 | INCR_M_JSON // outside anything, count nesting |
81 | INCR_M_JSON // outside anything, count nesting |
89 | }; |
82 | }; |
90 | |
83 | |
91 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
84 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
92 | |
85 | |
93 | typedef struct { |
86 | typedef struct { |
94 | U32 flags; |
87 | U32 flags; |
|
|
88 | U32 max_depth; |
|
|
89 | STRLEN max_size; |
|
|
90 | |
95 | SV *cb_object; |
91 | SV *cb_object; |
96 | HV *cb_sk_object; |
92 | HV *cb_sk_object; |
97 | |
93 | |
98 | // for the incremental parser |
94 | // for the incremental parser |
99 | SV *incr_text; // the source text so far |
95 | SV *incr_text; // the source text so far |
100 | STRLEN incr_pos; // the current offset into the text |
96 | STRLEN incr_pos; // the current offset into the text |
101 | int incr_nest; // {[]}-nesting level |
97 | int incr_nest; // {[]}-nesting level |
102 | int incr_mode; |
98 | unsigned char incr_mode; |
103 | } JSON; |
99 | } JSON; |
|
|
100 | |
|
|
101 | INLINE void |
|
|
102 | json_init (JSON *json) |
|
|
103 | { |
|
|
104 | Zero (json, 1, JSON); |
|
|
105 | json->max_depth = 512; |
|
|
106 | } |
104 | |
107 | |
105 | ///////////////////////////////////////////////////////////////////////////// |
108 | ///////////////////////////////////////////////////////////////////////////// |
106 | // utility functions |
109 | // utility functions |
107 | |
110 | |
108 | INLINE SV * |
111 | INLINE SV * |
… | |
… | |
118 | |
121 | |
119 | INLINE void |
122 | INLINE void |
120 | shrink (SV *sv) |
123 | shrink (SV *sv) |
121 | { |
124 | { |
122 | sv_utf8_downgrade (sv, 1); |
125 | sv_utf8_downgrade (sv, 1); |
|
|
126 | |
123 | if (SvLEN (sv) > SvCUR (sv) + 1) |
127 | if (SvLEN (sv) > SvCUR (sv) + 1) |
124 | { |
128 | { |
125 | #ifdef SvPV_shrink_to_cur |
129 | #ifdef SvPV_shrink_to_cur |
126 | SvPV_shrink_to_cur (sv); |
130 | SvPV_shrink_to_cur (sv); |
127 | #elif defined (SvPV_renew) |
131 | #elif defined (SvPV_renew) |
… | |
… | |
182 | char *cur; // SvPVX (sv) + current output position |
186 | char *cur; // SvPVX (sv) + current output position |
183 | char *end; // SvEND (sv) |
187 | char *end; // SvEND (sv) |
184 | SV *sv; // result scalar |
188 | SV *sv; // result scalar |
185 | JSON json; |
189 | JSON json; |
186 | U32 indent; // indentation level |
190 | U32 indent; // indentation level |
187 | U32 maxdepth; // max. indentation/recursion level |
|
|
188 | UV limit; // escape character values >= this value when encoding |
191 | UV limit; // escape character values >= this value when encoding |
189 | } enc_t; |
192 | } enc_t; |
190 | |
193 | |
191 | INLINE void |
194 | INLINE void |
192 | need (enc_t *enc, STRLEN len) |
195 | need (enc_t *enc, STRLEN len) |
… | |
… | |
365 | static void |
368 | static void |
366 | encode_av (enc_t *enc, AV *av) |
369 | encode_av (enc_t *enc, AV *av) |
367 | { |
370 | { |
368 | int i, len = av_len (av); |
371 | int i, len = av_len (av); |
369 | |
372 | |
370 | if (enc->indent >= enc->maxdepth) |
373 | if (enc->indent >= enc->json.max_depth) |
371 | croak ("data structure too deep (hit recursion limit)"); |
374 | croak (ERR_NESTING_EXCEEDED); |
372 | |
375 | |
373 | encode_ch (enc, '['); |
376 | encode_ch (enc, '['); |
374 | |
377 | |
375 | if (len >= 0) |
378 | if (len >= 0) |
376 | { |
379 | { |
… | |
… | |
451 | static void |
454 | static void |
452 | encode_hv (enc_t *enc, HV *hv) |
455 | encode_hv (enc_t *enc, HV *hv) |
453 | { |
456 | { |
454 | HE *he; |
457 | HE *he; |
455 | |
458 | |
456 | if (enc->indent >= enc->maxdepth) |
459 | if (enc->indent >= enc->json.max_depth) |
457 | croak ("data structure too deep (hit recursion limit)"); |
460 | croak (ERR_NESTING_EXCEEDED); |
458 | |
461 | |
459 | encode_ch (enc, '{'); |
462 | encode_ch (enc, '{'); |
460 | |
463 | |
461 | // for canonical output we have to sort by keys first |
464 | // for canonical output we have to sort by keys first |
462 | // actually, this is mostly due to the stupid so-called |
465 | // actually, this is mostly due to the stupid so-called |
… | |
… | |
742 | enc.json = *json; |
745 | enc.json = *json; |
743 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
746 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
744 | enc.cur = SvPVX (enc.sv); |
747 | enc.cur = SvPVX (enc.sv); |
745 | enc.end = SvEND (enc.sv); |
748 | enc.end = SvEND (enc.sv); |
746 | enc.indent = 0; |
749 | enc.indent = 0; |
747 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
748 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
750 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
749 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
751 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
750 | : 0x110000UL; |
752 | : 0x110000UL; |
751 | |
753 | |
752 | SvPOK_only (enc.sv); |
754 | SvPOK_only (enc.sv); |
… | |
… | |
819 | if (*dec->cur != ch) \ |
821 | if (*dec->cur != ch) \ |
820 | ERR (# ch " expected"); \ |
822 | ERR (# ch " expected"); \ |
821 | ++dec->cur; \ |
823 | ++dec->cur; \ |
822 | SE |
824 | SE |
823 | |
825 | |
824 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
826 | #define DEC_INC_DEPTH if (++dec->depth > dec->json.max_depth) ERR (ERR_NESTING_EXCEEDED) |
825 | #define DEC_DEC_DEPTH --dec->depth |
827 | #define DEC_DEC_DEPTH --dec->depth |
826 | |
828 | |
827 | static SV *decode_sv (dec_t *dec); |
829 | static SV *decode_sv (dec_t *dec); |
828 | |
830 | |
829 | static signed char decode_hexdigit[256]; |
831 | static signed char decode_hexdigit[256]; |
… | |
… | |
1412 | SV *sv; |
1414 | SV *sv; |
1413 | |
1415 | |
1414 | SvGETMAGIC (string); |
1416 | SvGETMAGIC (string); |
1415 | SvUPGRADE (string, SVt_PV); |
1417 | SvUPGRADE (string, SVt_PV); |
1416 | |
1418 | |
1417 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
1419 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
1420 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
1421 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
1422 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1423 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1424 | * and hope for the best. |
|
|
1425 | */ |
|
|
1426 | #ifdef DEBUGGING |
|
|
1427 | SvPV_nolen (string); |
|
|
1428 | #endif |
|
|
1429 | |
|
|
1430 | if (SvCUR (string) > json->max_size && json->max_size) |
1418 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1431 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1419 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
1432 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1420 | |
1433 | |
1421 | if (json->flags & F_UTF8) |
1434 | if (json->flags & F_UTF8) |
1422 | sv_utf8_downgrade (string, 0); |
1435 | sv_utf8_downgrade (string, 0); |
1423 | else |
1436 | else |
1424 | sv_utf8_upgrade (string); |
1437 | sv_utf8_upgrade (string); |
1425 | |
1438 | |
1426 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1439 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1427 | |
1440 | |
1428 | dec.json = *json; |
1441 | dec.json = *json; |
1429 | dec.cur = SvPVX (string); |
1442 | dec.cur = SvPVX (string); |
1430 | dec.end = SvEND (string); |
1443 | dec.end = SvEND (string); |
1431 | dec.err = 0; |
1444 | dec.err = 0; |
1432 | dec.depth = 0; |
1445 | dec.depth = 0; |
1433 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1434 | |
1446 | |
1435 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1447 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1436 | dec.json.flags |= F_HOOK; |
1448 | dec.json.flags |= F_HOOK; |
1437 | |
1449 | |
1438 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1450 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
… | |
… | |
1584 | self->incr_mode = INCR_M_STR; |
1596 | self->incr_mode = INCR_M_STR; |
1585 | goto incr_m_str; |
1597 | goto incr_m_str; |
1586 | |
1598 | |
1587 | case '[': |
1599 | case '[': |
1588 | case '{': |
1600 | case '{': |
1589 | ++self->incr_nest; |
1601 | if (++self->incr_nest > self->max_depth) |
|
|
1602 | croak (ERR_NESTING_EXCEEDED); |
1590 | break; |
1603 | break; |
1591 | |
1604 | |
1592 | case ']': |
1605 | case ']': |
1593 | case '}': |
1606 | case '}': |
1594 | if (!--self->incr_nest) |
1607 | if (--self->incr_nest <= 0) |
1595 | goto interrupt; |
1608 | goto interrupt; |
1596 | } |
1609 | } |
1597 | } |
1610 | } |
1598 | } |
1611 | } |
1599 | |
1612 | |
… | |
… | |
1639 | void new (char *klass) |
1652 | void new (char *klass) |
1640 | PPCODE: |
1653 | PPCODE: |
1641 | { |
1654 | { |
1642 | SV *pv = NEWSV (0, sizeof (JSON)); |
1655 | SV *pv = NEWSV (0, sizeof (JSON)); |
1643 | SvPOK_only (pv); |
1656 | SvPOK_only (pv); |
1644 | Zero (SvPVX (pv), 1, JSON); |
1657 | json_init ((JSON *)SvPVX (pv)); |
1645 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1646 | XPUSHs (sv_2mortal (sv_bless ( |
1658 | XPUSHs (sv_2mortal (sv_bless ( |
1647 | newRV_noinc (pv), |
1659 | newRV_noinc (pv), |
1648 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1660 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1649 | ))); |
1661 | ))); |
1650 | } |
1662 | } |
… | |
… | |
1691 | get_relaxed = F_RELAXED |
1703 | get_relaxed = F_RELAXED |
1692 | get_allow_unknown = F_ALLOW_UNKNOWN |
1704 | get_allow_unknown = F_ALLOW_UNKNOWN |
1693 | PPCODE: |
1705 | PPCODE: |
1694 | XPUSHs (boolSV (self->flags & ix)); |
1706 | XPUSHs (boolSV (self->flags & ix)); |
1695 | |
1707 | |
1696 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1708 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1697 | PPCODE: |
1709 | PPCODE: |
1698 | { |
1710 | self->max_depth = max_depth; |
1699 | UV log2 = 0; |
|
|
1700 | |
|
|
1701 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1702 | |
|
|
1703 | while ((1UL << log2) < max_depth) |
|
|
1704 | ++log2; |
|
|
1705 | |
|
|
1706 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1707 | |
|
|
1708 | XPUSHs (ST (0)); |
1711 | XPUSHs (ST (0)); |
1709 | } |
|
|
1710 | |
1712 | |
1711 | U32 get_max_depth (JSON *self) |
1713 | U32 get_max_depth (JSON *self) |
1712 | CODE: |
1714 | CODE: |
1713 | RETVAL = DEC_DEPTH (self->flags); |
1715 | RETVAL = self->max_depth; |
1714 | OUTPUT: |
1716 | OUTPUT: |
1715 | RETVAL |
1717 | RETVAL |
1716 | |
1718 | |
1717 | void max_size (JSON *self, UV max_size = 0) |
1719 | void max_size (JSON *self, U32 max_size = 0) |
1718 | PPCODE: |
1720 | PPCODE: |
1719 | { |
1721 | self->max_size = max_size; |
1720 | UV log2 = 0; |
|
|
1721 | |
|
|
1722 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1723 | if (max_size == 1) max_size = 2; |
|
|
1724 | |
|
|
1725 | while ((1UL << log2) < max_size) |
|
|
1726 | ++log2; |
|
|
1727 | |
|
|
1728 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1729 | |
|
|
1730 | XPUSHs (ST (0)); |
1722 | XPUSHs (ST (0)); |
1731 | } |
|
|
1732 | |
1723 | |
1733 | int get_max_size (JSON *self) |
1724 | int get_max_size (JSON *self) |
1734 | CODE: |
1725 | CODE: |
1735 | RETVAL = DEC_SIZE (self->flags); |
1726 | RETVAL = self->max_size; |
1736 | OUTPUT: |
1727 | OUTPUT: |
1737 | RETVAL |
1728 | RETVAL |
1738 | |
1729 | |
1739 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1730 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1740 | PPCODE: |
1731 | PPCODE: |
… | |
… | |
1819 | STRLEN offset; |
1810 | STRLEN offset; |
1820 | |
1811 | |
1821 | if (!INCR_DONE (self)) |
1812 | if (!INCR_DONE (self)) |
1822 | { |
1813 | { |
1823 | incr_parse (self); |
1814 | incr_parse (self); |
|
|
1815 | |
|
|
1816 | if (self->incr_pos > self->max_size && self->max_size) |
|
|
1817 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1818 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
|
|
1819 | |
1824 | if (!INCR_DONE (self)) |
1820 | if (!INCR_DONE (self)) |
1825 | break; |
1821 | break; |
1826 | } |
1822 | } |
1827 | |
1823 | |
1828 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1824 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
… | |
… | |
1857 | self->incr_nest = 0; |
1853 | self->incr_nest = 0; |
1858 | self->incr_mode = 0; |
1854 | self->incr_mode = 0; |
1859 | } |
1855 | } |
1860 | } |
1856 | } |
1861 | |
1857 | |
|
|
1858 | void incr_reset (JSON *self) |
|
|
1859 | CODE: |
|
|
1860 | { |
|
|
1861 | SvREFCNT_dec (self->incr_text); |
|
|
1862 | self->incr_text = 0; |
|
|
1863 | self->incr_pos = 0; |
|
|
1864 | self->incr_nest = 0; |
|
|
1865 | self->incr_mode = 0; |
|
|
1866 | } |
|
|
1867 | |
1862 | void DESTROY (JSON *self) |
1868 | void DESTROY (JSON *self) |
1863 | CODE: |
1869 | CODE: |
1864 | SvREFCNT_dec (self->cb_sk_object); |
1870 | SvREFCNT_dec (self->cb_sk_object); |
1865 | SvREFCNT_dec (self->cb_object); |
1871 | SvREFCNT_dec (self->cb_object); |
1866 | SvREFCNT_dec (self->incr_text); |
1872 | SvREFCNT_dec (self->incr_text); |
… | |
… | |
1871 | ALIAS: |
1877 | ALIAS: |
1872 | to_json_ = 0 |
1878 | to_json_ = 0 |
1873 | encode_json = F_UTF8 |
1879 | encode_json = F_UTF8 |
1874 | PPCODE: |
1880 | PPCODE: |
1875 | { |
1881 | { |
1876 | JSON json = { F_DEFAULT | ix }; |
1882 | JSON json; |
|
|
1883 | json_init (&json); |
|
|
1884 | json.flags |= ix; |
1877 | XPUSHs (encode_json (scalar, &json)); |
1885 | XPUSHs (encode_json (scalar, &json)); |
1878 | } |
1886 | } |
1879 | |
1887 | |
1880 | void decode_json (SV *jsonstr) |
1888 | void decode_json (SV *jsonstr) |
1881 | ALIAS: |
1889 | ALIAS: |
1882 | from_json_ = 0 |
1890 | from_json_ = 0 |
1883 | decode_json = F_UTF8 |
1891 | decode_json = F_UTF8 |
1884 | PPCODE: |
1892 | PPCODE: |
1885 | { |
1893 | { |
1886 | JSON json = { F_DEFAULT | ix }; |
1894 | JSON json; |
|
|
1895 | json_init (&json); |
|
|
1896 | json.flags |= ix; |
1887 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1897 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1888 | } |
1898 | } |
1889 | |
1899 | |
1890 | |
1900 | |