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.90 by root, Sun Jul 20 17:55:19 2008 UTC vs.
Revision 1.96 by root, Sat May 30 06:26:05 2009 UTC

175 *s++ = 0x80 | ( ch & 0x3f); 175 *s++ = 0x80 | ( ch & 0x3f);
176 176
177 return s; 177 return s;
178} 178}
179 179
180// convert offset pointer to character index, sv must be string
181static STRLEN
182ptr_to_index (SV *sv, char *offset)
183{
184 return SvUTF8 (sv)
185 ? utf8_distance (offset, SvPVX (sv))
186 : offset - SvPVX (sv);
187}
188
180///////////////////////////////////////////////////////////////////////////// 189/////////////////////////////////////////////////////////////////////////////
181// encoder 190// encoder
182 191
183// structure used for encoding JSON 192// structure used for encoding JSON
184typedef struct 193typedef struct
194INLINE void 203INLINE void
195need (enc_t *enc, STRLEN len) 204need (enc_t *enc, STRLEN len)
196{ 205{
197 if (expect_false (enc->cur + len >= enc->end)) 206 if (expect_false (enc->cur + len >= enc->end))
198 { 207 {
199 STRLEN cur = enc->cur - SvPVX (enc->sv); 208 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 209 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 210 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 211 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 212 }
204} 213}
205 214
280 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 289 (int)((uch - 0x10000) % 0x400 + 0xDC00));
281 enc->cur += 12; 290 enc->cur += 12;
282 } 291 }
283 else 292 else
284 { 293 {
285 static char hexdigit [16] = "0123456789abcdef";
286 need (enc, len += 5); 294 need (enc, len += 5);
287 *enc->cur++ = '\\'; 295 *enc->cur++ = '\\';
288 *enc->cur++ = 'u'; 296 *enc->cur++ = 'u';
289 *enc->cur++ = hexdigit [ uch >> 12 ]; 297 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
290 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 298 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
291 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 299 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
292 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 300 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
293 } 301 }
294 302
295 str += clen; 303 str += clen;
296 } 304 }
297 else if (enc->json.flags & F_LATIN1) 305 else if (enc->json.flags & F_LATIN1)
461 469
462 encode_ch (enc, '{'); 470 encode_ch (enc, '{');
463 471
464 // for canonical output we have to sort by keys first 472 // for canonical output we have to sort by keys first
465 // actually, this is mostly due to the stupid so-called 473 // actually, this is mostly due to the stupid so-called
466 // security workaround added somewhere in 5.8.x. 474 // security workaround added somewhere in 5.8.x
467 // that randomises hash orderings 475 // that randomises hash orderings
468 if (enc->json.flags & F_CANONICAL) 476 if (enc->json.flags & F_CANONICAL)
469 { 477 {
470 int count = hv_iterinit (hv); 478 int count = hv_iterinit (hv);
471 479
971 { 979 {
972 STRLEN len = cur - buf; 980 STRLEN len = cur - buf;
973 981
974 if (sv) 982 if (sv)
975 { 983 {
976 SvGROW (sv, SvCUR (sv) + len + 1); 984 STRLEN cur = SvCUR (sv);
985
986 if (SvLEN (sv) <= cur + len)
987 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
988
977 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 989 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
978 SvCUR_set (sv, SvCUR (sv) + len); 990 SvCUR_set (sv, SvCUR (sv) + len);
979 } 991 }
980 else 992 else
981 sv = newSVpvn (buf, len); 993 sv = newSVpvn (buf, len);
1405fail: 1417fail:
1406 return 0; 1418 return 0;
1407} 1419}
1408 1420
1409static SV * 1421static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1422decode_json (SV *string, JSON *json, char **offset_return)
1411{ 1423{
1412 dec_t dec; 1424 dec_t dec;
1413 STRLEN offset;
1414 SV *sv; 1425 SV *sv;
1415 1426
1427 /* work around bugs in 5.10 where manipulating magic values
1428 * will perl ignore the magic in subsequent accesses
1429 */
1416 SvGETMAGIC (string); 1430 /*SvGETMAGIC (string);*/
1431 if (SvMAGICAL (string))
1432 string = sv_2mortal (newSVsv (string));
1433
1417 SvUPGRADE (string, SVt_PV); 1434 SvUPGRADE (string, SVt_PV);
1418 1435
1419 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1436 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1420 * assertion with -DDEBUGGING, although SvCUR is documented to 1437 * assertion with -DDEBUGGING, although SvCUR is documented to
1421 * return the xpv_cur field which certainly exists after upgrading. 1438 * return the xpv_cur field which certainly exists after upgrading.
1424 * and hope for the best. 1441 * and hope for the best.
1425 * Damnit, SvPV_nolen still trips over yet another assertion. This 1442 * Damnit, SvPV_nolen still trips over yet another assertion. This
1426 * assertion business is seriously broken, try yet another workaround 1443 * assertion business is seriously broken, try yet another workaround
1427 * for the broken -DDEBUGGING. 1444 * for the broken -DDEBUGGING.
1428 */ 1445 */
1446 {
1429#ifdef DEBUGGING 1447#ifdef DEBUGGING
1430 offset = SvOK (string) ? sv_len (string) : 0; 1448 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1431#else 1449#else
1432 offset = SvCUR (string); 1450 STRLEN offset = SvCUR (string);
1433#endif 1451#endif
1434 1452
1435 if (offset > json->max_size && json->max_size) 1453 if (offset > json->max_size && json->max_size)
1436 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",
1437 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 }
1438 1457
1439 if (json->flags & F_UTF8) 1458 if (json->flags & F_UTF8)
1440 sv_utf8_downgrade (string, 0); 1459 sv_utf8_downgrade (string, 0);
1441 else 1460 else
1442 sv_utf8_upgrade (string); 1461 sv_utf8_upgrade (string);
1454 1473
1455 *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
1456 1475
1457 decode_ws (&dec); 1476 decode_ws (&dec);
1458 sv = decode_sv (&dec); 1477 sv = decode_sv (&dec);
1478
1479 if (offset_return)
1480 *offset_return = dec.cur;
1459 1481
1460 if (!(offset_return || !sv)) 1482 if (!(offset_return || !sv))
1461 { 1483 {
1462 // check for trailing garbage 1484 // check for trailing garbage
1463 decode_ws (&dec); 1485 decode_ws (&dec);
1466 { 1488 {
1467 dec.err = "garbage after JSON object"; 1489 dec.err = "garbage after JSON object";
1468 SvREFCNT_dec (sv); 1490 SvREFCNT_dec (sv);
1469 sv = 0; 1491 sv = 0;
1470 } 1492 }
1471 }
1472
1473 if (offset_return || !sv)
1474 {
1475 offset = dec.json.flags & F_UTF8
1476 ? dec.cur - SvPVX (string)
1477 : utf8_distance (dec.cur, SvPVX (string));
1478
1479 if (offset_return)
1480 *offset_return = offset;
1481 } 1493 }
1482 1494
1483 if (!sv) 1495 if (!sv)
1484 { 1496 {
1485 SV *uni = sv_newmortal (); 1497 SV *uni = sv_newmortal ();
1491 SAVEVPTR (PL_curcop); 1503 SAVEVPTR (PL_curcop);
1492 PL_curcop = &cop; 1504 PL_curcop = &cop;
1493 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);
1494 LEAVE; 1506 LEAVE;
1495 1507
1496 croak ("%s, at character offset %d [\"%s\"]", 1508 croak ("%s, at character offset %d (before \"%s\")",
1497 dec.err, 1509 dec.err,
1498 (int)offset, 1510 ptr_to_index (string, dec.cur),
1499 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1511 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1500 } 1512 }
1501 1513
1502 sv = sv_2mortal (sv); 1514 sv = sv_2mortal (sv);
1503 1515
1772 XPUSHs (decode_json (jsonstr, self, 0)); 1784 XPUSHs (decode_json (jsonstr, self, 0));
1773 1785
1774void decode_prefix (JSON *self, SV *jsonstr) 1786void decode_prefix (JSON *self, SV *jsonstr)
1775 PPCODE: 1787 PPCODE:
1776{ 1788{
1777 STRLEN offset; 1789 char *offset;
1778 EXTEND (SP, 2); 1790 EXTEND (SP, 2);
1779 PUSHs (decode_json (jsonstr, self, &offset)); 1791 PUSHs (decode_json (jsonstr, self, &offset));
1780 PUSHs (sv_2mortal (newSVuv (offset))); 1792 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1781} 1793}
1782 1794
1783void incr_parse (JSON *self, SV *jsonstr = 0) 1795void incr_parse (JSON *self, SV *jsonstr = 0)
1784 PPCODE: 1796 PPCODE:
1785{ 1797{
1787 self->incr_text = newSVpvn ("", 0); 1799 self->incr_text = newSVpvn ("", 0);
1788 1800
1789 // append data, if any 1801 // append data, if any
1790 if (jsonstr) 1802 if (jsonstr)
1791 { 1803 {
1792 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1804 if (SvUTF8 (jsonstr))
1793 { 1805 {
1806 if (!SvUTF8 (self->incr_text))
1807 {
1794 /* utf-8-ness differs, need to upgrade */ 1808 /* utf-8-ness differs, need to upgrade */
1795 sv_utf8_upgrade (self->incr_text); 1809 sv_utf8_upgrade (self->incr_text);
1796 1810
1797 if (self->incr_pos) 1811 if (self->incr_pos)
1798 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1812 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1799 - (U8 *)SvPVX (self->incr_text); 1813 - (U8 *)SvPVX (self->incr_text);
1814 }
1800 } 1815 }
1816 else if (SvUTF8 (self->incr_text))
1817 sv_utf8_upgrade (jsonstr);
1801 1818
1802 { 1819 {
1803 STRLEN len; 1820 STRLEN len;
1804 const char *str = SvPV (jsonstr, len); 1821 const char *str = SvPV (jsonstr, len);
1805 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1822 STRLEN cur = SvCUR (self->incr_text);
1823
1824 if (SvLEN (self->incr_text) <= cur + len)
1825 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1826
1806 Move (str, SvEND (self->incr_text), len, char); 1827 Move (str, SvEND (self->incr_text), len, char);
1807 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1828 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1808 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1829 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1809 } 1830 }
1810 } 1831 }
1811 1832
1812 if (GIMME_V != G_VOID) 1833 if (GIMME_V != G_VOID)
1813 do 1834 do
1814 { 1835 {
1815 STRLEN offset; 1836 char *offset;
1816 1837
1817 if (!INCR_DONE (self)) 1838 if (!INCR_DONE (self))
1818 { 1839 {
1819 incr_parse (self); 1840 incr_parse (self);
1820 1841
1826 break; 1847 break;
1827 } 1848 }
1828 1849
1829 XPUSHs (decode_json (self->incr_text, self, &offset)); 1850 XPUSHs (decode_json (self->incr_text, self, &offset));
1830 1851
1831 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1832 self->incr_pos -= offset; 1852 self->incr_pos -= offset - SvPVX (self->incr_text);
1833 self->incr_nest = 0; 1853 self->incr_nest = 0;
1834 self->incr_mode = 0; 1854 self->incr_mode = 0;
1855
1856 sv_chop (self->incr_text, offset);
1835 } 1857 }
1836 while (GIMME_V == G_ARRAY); 1858 while (GIMME_V == G_ARRAY);
1837} 1859}
1838 1860
1839SV *incr_text (JSON *self) 1861SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines