… | |
… | |
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) |
… | |
… | |
1413 | SV *sv; |
1414 | SV *sv; |
1414 | |
1415 | |
1415 | SvGETMAGIC (string); |
1416 | SvGETMAGIC (string); |
1416 | SvUPGRADE (string, SVt_PV); |
1417 | SvUPGRADE (string, SVt_PV); |
1417 | |
1418 | |
|
|
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 | */ |
|
|
1424 | SvPOK (string); |
|
|
1425 | |
1418 | if (SvCUR (string) > json->max_size && json->max_size) |
1426 | if (SvCUR (string) > json->max_size && json->max_size) |
1419 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1427 | 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); |
1428 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1421 | |
1429 | |
1422 | if (json->flags & F_UTF8) |
1430 | if (json->flags & F_UTF8) |
… | |
… | |
1590 | croak (ERR_NESTING_EXCEEDED); |
1598 | croak (ERR_NESTING_EXCEEDED); |
1591 | break; |
1599 | break; |
1592 | |
1600 | |
1593 | case ']': |
1601 | case ']': |
1594 | case '}': |
1602 | case '}': |
1595 | if (!--self->incr_nest) |
1603 | if (--self->incr_nest <= 0) |
1596 | goto interrupt; |
1604 | goto interrupt; |
1597 | } |
1605 | } |
1598 | } |
1606 | } |
1599 | } |
1607 | } |
1600 | |
1608 | |