… | |
… | |
79 | INCR_M_STR, // inside string |
79 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
80 | INCR_M_BS, // inside backslash |
81 | INCR_M_JSON // outside anything, count nesting |
81 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
82 | }; |
83 | |
83 | |
84 | #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) |
85 | |
85 | |
86 | typedef struct { |
86 | typedef struct { |
87 | U32 flags; |
87 | U32 flags; |
88 | U32 max_depth; |
88 | U32 max_depth; |
89 | STRLEN max_size; |
89 | STRLEN max_size; |
… | |
… | |
92 | HV *cb_sk_object; |
92 | HV *cb_sk_object; |
93 | |
93 | |
94 | // for the incremental parser |
94 | // for the incremental parser |
95 | SV *incr_text; // the source text so far |
95 | SV *incr_text; // the source text so far |
96 | STRLEN incr_pos; // the current offset into the text |
96 | STRLEN incr_pos; // the current offset into the text |
97 | unsigned char incr_nest; // {[]}-nesting level |
97 | int incr_nest; // {[]}-nesting level |
98 | unsigned char incr_mode; |
98 | unsigned char incr_mode; |
99 | } JSON; |
99 | } JSON; |
100 | |
100 | |
101 | INLINE void |
101 | INLINE void |
102 | json_init (JSON *json) |
102 | json_init (JSON *json) |
… | |
… | |
121 | |
121 | |
122 | INLINE void |
122 | INLINE void |
123 | shrink (SV *sv) |
123 | shrink (SV *sv) |
124 | { |
124 | { |
125 | sv_utf8_downgrade (sv, 1); |
125 | sv_utf8_downgrade (sv, 1); |
|
|
126 | |
126 | if (SvLEN (sv) > SvCUR (sv) + 1) |
127 | if (SvLEN (sv) > SvCUR (sv) + 1) |
127 | { |
128 | { |
128 | #ifdef SvPV_shrink_to_cur |
129 | #ifdef SvPV_shrink_to_cur |
129 | SvPV_shrink_to_cur (sv); |
130 | SvPV_shrink_to_cur (sv); |
130 | #elif defined (SvPV_renew) |
131 | #elif defined (SvPV_renew) |
… | |
… | |
279 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
280 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
280 | enc->cur += 12; |
281 | enc->cur += 12; |
281 | } |
282 | } |
282 | else |
283 | else |
283 | { |
284 | { |
284 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
285 | need (enc, len += 5); |
285 | need (enc, len += 5); |
286 | *enc->cur++ = '\\'; |
286 | *enc->cur++ = '\\'; |
287 | *enc->cur++ = 'u'; |
287 | *enc->cur++ = 'u'; |
288 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
288 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
289 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
289 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
290 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
290 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
291 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
291 | *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
292 | } |
292 | } |
293 | |
293 | |
294 | str += clen; |
294 | str += clen; |
295 | } |
295 | } |
296 | else if (enc->json.flags & F_LATIN1) |
296 | else if (enc->json.flags & F_LATIN1) |
… | |
… | |
1413 | SV *sv; |
1413 | SV *sv; |
1414 | |
1414 | |
1415 | SvGETMAGIC (string); |
1415 | SvGETMAGIC (string); |
1416 | SvUPGRADE (string, SVt_PV); |
1416 | SvUPGRADE (string, SVt_PV); |
1417 | |
1417 | |
|
|
1418 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
|
|
1419 | * assertion with -DDEBUGGING, although SvCUR is documented to |
|
|
1420 | * return the xpv_cur field which certainly exists after upgrading. |
|
|
1421 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1422 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1423 | * and hope for the best. |
|
|
1424 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
1425 | * assertion business is seriously broken, try yet another workaround |
|
|
1426 | * for the broken -DDEBUGGING. |
|
|
1427 | */ |
|
|
1428 | #ifdef DEBUGGING |
|
|
1429 | offset = SvOK (string) ? sv_len (string) : 0; |
|
|
1430 | #else |
|
|
1431 | offset = SvCUR (string); |
|
|
1432 | #endif |
|
|
1433 | |
1418 | if (SvCUR (string) > json->max_size && json->max_size) |
1434 | if (offset > json->max_size && json->max_size) |
1419 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1435 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1420 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1436 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1421 | |
1437 | |
1422 | if (json->flags & F_UTF8) |
1438 | if (json->flags & F_UTF8) |
1423 | sv_utf8_downgrade (string, 0); |
1439 | sv_utf8_downgrade (string, 0); |
… | |
… | |
1590 | croak (ERR_NESTING_EXCEEDED); |
1606 | croak (ERR_NESTING_EXCEEDED); |
1591 | break; |
1607 | break; |
1592 | |
1608 | |
1593 | case ']': |
1609 | case ']': |
1594 | case '}': |
1610 | case '}': |
1595 | if (!--self->incr_nest) |
1611 | if (--self->incr_nest <= 0) |
1596 | goto interrupt; |
1612 | goto interrupt; |
1597 | } |
1613 | } |
1598 | } |
1614 | } |
1599 | } |
1615 | } |
1600 | |
1616 | |
… | |
… | |
1841 | self->incr_nest = 0; |
1857 | self->incr_nest = 0; |
1842 | self->incr_mode = 0; |
1858 | self->incr_mode = 0; |
1843 | } |
1859 | } |
1844 | } |
1860 | } |
1845 | |
1861 | |
|
|
1862 | void incr_reset (JSON *self) |
|
|
1863 | CODE: |
|
|
1864 | { |
|
|
1865 | SvREFCNT_dec (self->incr_text); |
|
|
1866 | self->incr_text = 0; |
|
|
1867 | self->incr_pos = 0; |
|
|
1868 | self->incr_nest = 0; |
|
|
1869 | self->incr_mode = 0; |
|
|
1870 | } |
|
|
1871 | |
1846 | void DESTROY (JSON *self) |
1872 | void DESTROY (JSON *self) |
1847 | CODE: |
1873 | CODE: |
1848 | SvREFCNT_dec (self->cb_sk_object); |
1874 | SvREFCNT_dec (self->cb_sk_object); |
1849 | SvREFCNT_dec (self->cb_object); |
1875 | SvREFCNT_dec (self->cb_object); |
1850 | SvREFCNT_dec (self->incr_text); |
1876 | SvREFCNT_dec (self->incr_text); |