ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.86 by root, Tue May 27 05:31:39 2008 UTC vs.
Revision 1.95 by root, Sun Feb 22 06:55:28 2009 UTC

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
86typedef struct { 86typedef 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
101INLINE void 101INLINE void
102json_init (JSON *json) 102json_init (JSON *json)
121 121
122INLINE void 122INLINE void
123shrink (SV *sv) 123shrink (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)
193INLINE void 194INLINE void
194need (enc_t *enc, STRLEN len) 195need (enc_t *enc, STRLEN len)
195{ 196{
196 if (expect_false (enc->cur + len >= enc->end)) 197 if (expect_false (enc->cur + len >= enc->end))
197 { 198 {
198 STRLEN cur = enc->cur - SvPVX (enc->sv); 199 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
199 SvGROW (enc->sv, cur + len + 1); 200 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
200 enc->cur = SvPVX (enc->sv) + cur; 201 enc->cur = SvPVX (enc->sv) + cur;
201 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
202 } 203 }
203} 204}
204 205
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)
460 460
461 encode_ch (enc, '{'); 461 encode_ch (enc, '{');
462 462
463 // for canonical output we have to sort by keys first 463 // for canonical output we have to sort by keys first
464 // actually, this is mostly due to the stupid so-called 464 // actually, this is mostly due to the stupid so-called
465 // security workaround added somewhere in 5.8.x. 465 // security workaround added somewhere in 5.8.x
466 // that randomises hash orderings 466 // that randomises hash orderings
467 if (enc->json.flags & F_CANONICAL) 467 if (enc->json.flags & F_CANONICAL)
468 { 468 {
469 int count = hv_iterinit (hv); 469 int count = hv_iterinit (hv);
470 470
970 { 970 {
971 STRLEN len = cur - buf; 971 STRLEN len = cur - buf;
972 972
973 if (sv) 973 if (sv)
974 { 974 {
975 SvGROW (sv, SvCUR (sv) + len + 1); 975 STRLEN cur = SvCUR (sv);
976
977 if (SvLEN (sv) <= cur + len)
978 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
979
976 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 980 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
977 SvCUR_set (sv, SvCUR (sv) + len); 981 SvCUR_set (sv, SvCUR (sv) + len);
978 } 982 }
979 else 983 else
980 sv = newSVpvn (buf, len); 984 sv = newSVpvn (buf, len);
1410{ 1414{
1411 dec_t dec; 1415 dec_t dec;
1412 STRLEN offset; 1416 STRLEN offset;
1413 SV *sv; 1417 SV *sv;
1414 1418
1419 /* work around bugs in 5.10 where manipulating magic values
1420 * will perl ignore the magic in subsequent accesses
1421 */
1415 SvGETMAGIC (string); 1422 /*SvGETMAGIC (string);*/
1423 if (SvMAGICAL (string))
1424 string = sv_2mortal (newSVsv (string));
1425
1416 SvUPGRADE (string, SVt_PV); 1426 SvUPGRADE (string, SVt_PV);
1417 1427
1428 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1429 * assertion with -DDEBUGGING, although SvCUR is documented to
1430 * return the xpv_cur field which certainly exists after upgrading.
1431 * according to nicholas clark, calling SvPOK fixes this.
1432 * But it doesn't fix it, so try another workaround, call SvPV_nolen
1433 * and hope for the best.
1434 * Damnit, SvPV_nolen still trips over yet another assertion. This
1435 * assertion business is seriously broken, try yet another workaround
1436 * for the broken -DDEBUGGING.
1437 */
1438#ifdef DEBUGGING
1439 offset = SvOK (string) ? sv_len (string) : 0;
1440#else
1441 offset = SvCUR (string);
1442#endif
1443
1418 if (SvCUR (string) > json->max_size && json->max_size) 1444 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", 1445 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); 1446 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1421 1447
1422 if (json->flags & F_UTF8) 1448 if (json->flags & F_UTF8)
1423 sv_utf8_downgrade (string, 0); 1449 sv_utf8_downgrade (string, 0);
1590 croak (ERR_NESTING_EXCEEDED); 1616 croak (ERR_NESTING_EXCEEDED);
1591 break; 1617 break;
1592 1618
1593 case ']': 1619 case ']':
1594 case '}': 1620 case '}':
1595 if (!--self->incr_nest) 1621 if (--self->incr_nest <= 0)
1596 goto interrupt; 1622 goto interrupt;
1597 } 1623 }
1598 } 1624 }
1599 } 1625 }
1600 1626
1783 } 1809 }
1784 1810
1785 { 1811 {
1786 STRLEN len; 1812 STRLEN len;
1787 const char *str = SvPV (jsonstr, len); 1813 const char *str = SvPV (jsonstr, len);
1788 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1814 STRLEN cur = SvCUR (self->incr_text);
1815
1816 if (SvLEN (self->incr_text) <= cur + len)
1817 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1818
1789 Move (str, SvEND (self->incr_text), len, char); 1819 Move (str, SvEND (self->incr_text), len, char);
1790 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1820 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1791 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1821 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1792 } 1822 }
1793 } 1823 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines