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.96 by root, Sat May 30 06:26:05 2009 UTC vs.
Revision 1.109 by root, Tue Mar 8 10:18:50 2011 UTC

12#if defined(__BORLANDC__) || defined(_MSC_VER) 12#if defined(__BORLANDC__) || defined(_MSC_VER)
13# define snprintf _snprintf // C compilers have this in stdio.h 13# define snprintf _snprintf // C compilers have this in stdio.h
14#endif 14#endif
15 15
16// some old perls do not have this, try to make it work, no 16// some old perls do not have this, try to make it work, no
17// guarentees, though. if it breaks, you get to keep the pieces. 17// guarantees, though. if it breaks, you get to keep the pieces.
18#ifndef UTF8_MAXBYTES 18#ifndef UTF8_MAXBYTES
19# define UTF8_MAXBYTES 13 19# define UTF8_MAXBYTES 13
20#endif 20#endif
21 21
22// three extra for rounding, sign, and end of string
22#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) 23#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
23 24
24#define F_ASCII 0x00000001UL 25#define F_ASCII 0x00000001UL
25#define F_LATIN1 0x00000002UL 26#define F_LATIN1 0x00000002UL
26#define F_UTF8 0x00000004UL 27#define F_UTF8 0x00000004UL
27#define F_INDENT 0x00000008UL 28#define F_INDENT 0x00000008UL
41#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
42#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
43 44
44#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
45 46
47#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
48
46#define SB do { 49#define SB do {
47#define SE } while (0) 50#define SE } while (0)
48 51
49#if __GNUC__ >= 3 52#if __GNUC__ >= 3
50# define expect(expr,value) __builtin_expect ((expr), (value)) 53# define expect(expr,value) __builtin_expect ((expr), (value))
76 79
77enum { 80enum {
78 INCR_M_WS = 0, // initial whitespace skipping, must be 0 81 INCR_M_WS = 0, // initial whitespace skipping, must be 0
79 INCR_M_STR, // inside string 82 INCR_M_STR, // inside string
80 INCR_M_BS, // inside backslash 83 INCR_M_BS, // inside backslash
84 INCR_M_C0, // inside comment in initial whitespace sequence
85 INCR_M_C1, // inside comment in other places
81 INCR_M_JSON // outside anything, count nesting 86 INCR_M_JSON // outside anything, count nesting
82}; 87};
83 88
84#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) 89#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
85 90
184 return SvUTF8 (sv) 189 return SvUTF8 (sv)
185 ? utf8_distance (offset, SvPVX (sv)) 190 ? utf8_distance (offset, SvPVX (sv))
186 : offset - SvPVX (sv); 191 : offset - SvPVX (sv);
187} 192}
188 193
194/////////////////////////////////////////////////////////////////////////////
195// fp hell
196
197// scan a group of digits, and a trailing exponent
198static void
199json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
200{
201 UV uaccum = 0;
202 int eaccum = 0;
203
204 // if we recurse too deep, skip all remaining digits
205 // to avoid a stack overflow attack
206 if (expect_false (--maxdepth <= 0))
207 while (((U8)*s - '0') < 10)
208 ++s;
209
210 for (;;)
211 {
212 U8 dig = (U8)*s - '0';
213
214 if (expect_false (dig >= 10))
215 {
216 if (dig == (U8)((U8)'.' - (U8)'0'))
217 {
218 ++s;
219 json_atof_scan1 (s, accum, expo, 1, maxdepth);
220 }
221 else if ((dig | ' ') == 'e' - '0')
222 {
223 int exp2 = 0;
224 int neg = 0;
225
226 ++s;
227
228 if (*s == '-')
229 {
230 ++s;
231 neg = 1;
232 }
233 else if (*s == '+')
234 ++s;
235
236 while ((dig = (U8)*s - '0') < 10)
237 exp2 = exp2 * 10 + *s++ - '0';
238
239 *expo += neg ? -exp2 : exp2;
240 }
241
242 break;
243 }
244
245 ++s;
246
247 uaccum = uaccum * 10 + dig;
248 ++eaccum;
249
250 // if we have too many digits, then recurse for more
251 // we actually do this for rather few digits
252 if (uaccum >= (UV_MAX - 9) / 10)
253 {
254 if (postdp) *expo -= eaccum;
255 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
256 if (postdp) *expo += eaccum;
257
258 break;
259 }
260 }
261
262 // this relies greatly on the quality of the pow ()
263 // implementation of the platform, but a good
264 // implementation is hard to beat.
265 if (postdp) *expo -= eaccum;
266 *accum += uaccum * Perl_pow (10., *expo);
267 *expo += eaccum;
268}
269
270static NV
271json_atof (const char *s)
272{
273 NV accum = 0.;
274 int expo = 0;
275 int neg = 0;
276
277 if (*s == '-')
278 {
279 ++s;
280 neg = 1;
281 }
282
283 // a recursion depth of ten gives us >>500 bits
284 json_atof_scan1 (s, &accum, &expo, 0, 10);
285
286 return neg ? -accum : accum;
287}
189///////////////////////////////////////////////////////////////////////////// 288/////////////////////////////////////////////////////////////////////////////
190// encoder 289// encoder
191 290
192// structure used for encoding JSON 291// structure used for encoding JSON
193typedef struct 292typedef struct
471 570
472 // for canonical output we have to sort by keys first 571 // for canonical output we have to sort by keys first
473 // actually, this is mostly due to the stupid so-called 572 // actually, this is mostly due to the stupid so-called
474 // security workaround added somewhere in 5.8.x 573 // security workaround added somewhere in 5.8.x
475 // that randomises hash orderings 574 // that randomises hash orderings
476 if (enc->json.flags & F_CANONICAL) 575 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
477 { 576 {
478 int count = hv_iterinit (hv); 577 int count = hv_iterinit (hv);
479 578
480 if (SvMAGICAL (hv)) 579 if (SvMAGICAL (hv))
481 { 580 {
759 : enc.json.flags & F_LATIN1 ? 0x000100UL 858 : enc.json.flags & F_LATIN1 ? 0x000100UL
760 : 0x110000UL; 859 : 0x110000UL;
761 860
762 SvPOK_only (enc.sv); 861 SvPOK_only (enc.sv);
763 encode_sv (&enc, scalar); 862 encode_sv (&enc, scalar);
863 encode_nl (&enc);
764 864
765 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 865 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
766 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 866 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
767 867
768 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 868 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
948 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1048 else if (expect_true (ch >= 0x20 && ch < 0x80))
949 *cur++ = ch; 1049 *cur++ = ch;
950 else if (ch >= 0x80) 1050 else if (ch >= 0x80)
951 { 1051 {
952 STRLEN clen; 1052 STRLEN clen;
953 UV uch;
954 1053
955 --dec_cur; 1054 --dec_cur;
956 1055
957 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1056 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
958 if (clen == (STRLEN)-1) 1057 if (clen == (STRLEN)-1)
959 ERR ("malformed UTF-8 character in JSON string"); 1058 ERR ("malformed UTF-8 character in JSON string");
960 1059
961 do 1060 do
962 *cur++ = *dec_cur++; 1061 *cur++ = *dec_cur++;
1084 1183
1085 // special case the rather common 1..5-digit-int case 1184 // special case the rather common 1..5-digit-int case
1086 if (*start == '-') 1185 if (*start == '-')
1087 switch (len) 1186 switch (len)
1088 { 1187 {
1089 case 2: return newSViv (-( start [1] - '0' * 1)); 1188 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1090 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1189 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
1091 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1190 case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
1092 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1191 case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1093 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); 1192 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1094 } 1193 }
1095 else 1194 else
1096 switch (len) 1195 switch (len)
1097 { 1196 {
1098 case 1: return newSViv ( start [0] - '0' * 1); 1197 case 1: return newSViv ( start [0] - '0' * 1);
1099 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1198 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1100 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1199 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
1101 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1200 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1102 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); 1201 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1103 } 1202 }
1104 1203
1105 { 1204 {
1106 UV uv; 1205 UV uv;
1107 int numtype = grok_number (start, len, &uv); 1206 int numtype = grok_number (start, len, &uv);
1116 } 1215 }
1117 1216
1118 len -= *start == '-' ? 1 : 0; 1217 len -= *start == '-' ? 1 : 0;
1119 1218
1120 // does not fit into IV or UV, try NV 1219 // does not fit into IV or UV, try NV
1121 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1220 if (len <= NV_DIG)
1122 #if defined (LDBL_DIG)
1123 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1124 #endif
1125 )
1126 // fits into NV without loss of precision 1221 // fits into NV without loss of precision
1127 return newSVnv (Atof (start)); 1222 return newSVnv (json_atof (start));
1128 1223
1129 // everything else fails, convert it to a string 1224 // everything else fails, convert it to a string
1130 return newSVpvn (start, dec->cur - start); 1225 return newSVpvn (start, dec->cur - start);
1131 } 1226 }
1132 1227
1133 // loss of precision here 1228 // loss of precision here
1134 return newSVnv (Atof (start)); 1229 return newSVnv (json_atof (start));
1135 1230
1136fail: 1231fail:
1137 return 0; 1232 return 0;
1138} 1233}
1139 1234
1308 dSP; 1403 dSP;
1309 int count; 1404 int count;
1310 1405
1311 ENTER; SAVETMPS; PUSHMARK (SP); 1406 ENTER; SAVETMPS; PUSHMARK (SP);
1312 XPUSHs (HeVAL (he)); 1407 XPUSHs (HeVAL (he));
1408 sv_2mortal (sv);
1313 1409
1314 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1410 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1315 1411
1316 if (count == 1) 1412 if (count == 1)
1317 { 1413 {
1318 sv = newSVsv (POPs); 1414 sv = newSVsv (POPs);
1319 FREETMPS; LEAVE; 1415 FREETMPS; LEAVE;
1320 return sv; 1416 return sv;
1321 } 1417 }
1322 1418
1419 SvREFCNT_inc (sv);
1323 FREETMPS; LEAVE; 1420 FREETMPS; LEAVE;
1324 } 1421 }
1325 } 1422 }
1326 1423
1327 if (dec->json.cb_object) 1424 if (dec->json.cb_object)
1423{ 1520{
1424 dec_t dec; 1521 dec_t dec;
1425 SV *sv; 1522 SV *sv;
1426 1523
1427 /* work around bugs in 5.10 where manipulating magic values 1524 /* work around bugs in 5.10 where manipulating magic values
1428 * will perl ignore the magic in subsequent accesses 1525 * will perl ignore the magic in subsequent accesses.
1526 * also make a copy of non-PV values, to get them into a clean
1527 * state (SvPV should do that, but it's buggy, see below).
1429 */ 1528 */
1430 /*SvGETMAGIC (string);*/ 1529 /*SvGETMAGIC (string);*/
1431 if (SvMAGICAL (string)) 1530 if (SvMAGICAL (string) || !SvPOK (string))
1432 string = sv_2mortal (newSVsv (string)); 1531 string = sv_2mortal (newSVsv (string));
1433 1532
1434 SvUPGRADE (string, SVt_PV); 1533 SvUPGRADE (string, SVt_PV);
1435 1534
1436 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1535 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1453 if (offset > json->max_size && json->max_size) 1552 if (offset > json->max_size && json->max_size)
1454 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1553 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1554 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 } 1555 }
1457 1556
1458 if (json->flags & F_UTF8) 1557 if (DECODE_WANTS_OCTETS (json))
1459 sv_utf8_downgrade (string, 0); 1558 sv_utf8_downgrade (string, 0);
1460 else 1559 else
1461 sv_utf8_upgrade (string); 1560 sv_utf8_upgrade (string);
1462 1561
1463 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1562 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1525static void 1624static void
1526incr_parse (JSON *self) 1625incr_parse (JSON *self)
1527{ 1626{
1528 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1627 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1529 1628
1629 // the state machine here is a bit convoluted and could be simplified a lot
1630 // but this would make it slower, so...
1631
1530 for (;;) 1632 for (;;)
1531 { 1633 {
1532 //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D 1634 //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D
1533 switch (self->incr_mode) 1635 switch (self->incr_mode)
1534 { 1636 {
1535 // only used for intiial whitespace skipping 1637 // only used for initial whitespace skipping
1536 case INCR_M_WS: 1638 case INCR_M_WS:
1537 for (;;) 1639 for (;;)
1538 { 1640 {
1539 if (*p > 0x20) 1641 if (*p > 0x20)
1540 { 1642 {
1643 if (*p == '#')
1644 {
1645 self->incr_mode = INCR_M_C0;
1646 goto incr_m_c;
1647 }
1648 else
1649 {
1541 self->incr_mode = INCR_M_JSON; 1650 self->incr_mode = INCR_M_JSON;
1542 goto incr_m_json; 1651 goto incr_m_json;
1652 }
1543 } 1653 }
1544 else if (!*p) 1654 else if (!*p)
1545 goto interrupt; 1655 goto interrupt;
1546 1656
1547 ++p; 1657 ++p;
1553 goto interrupt; 1663 goto interrupt;
1554 1664
1555 ++p; 1665 ++p;
1556 self->incr_mode = INCR_M_STR; 1666 self->incr_mode = INCR_M_STR;
1557 goto incr_m_str; 1667 goto incr_m_str;
1668
1669 // inside #-style comments
1670 case INCR_M_C0:
1671 case INCR_M_C1:
1672 incr_m_c:
1673 for (;;)
1674 {
1675 if (*p == '\n')
1676 {
1677 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1678 break;
1679 }
1680 else if (!*p)
1681 goto interrupt;
1682
1683 ++p;
1684 }
1685
1686 break;
1558 1687
1559 // inside a string 1688 // inside a string
1560 case INCR_M_STR: 1689 case INCR_M_STR:
1561 incr_m_str: 1690 incr_m_str:
1562 for (;;) 1691 for (;;)
1621 1750
1622 case ']': 1751 case ']':
1623 case '}': 1752 case '}':
1624 if (--self->incr_nest <= 0) 1753 if (--self->incr_nest <= 0)
1625 goto interrupt; 1754 goto interrupt;
1755 break;
1756
1757 case '#':
1758 self->incr_mode = INCR_M_C1;
1759 goto incr_m_c;
1626 } 1760 }
1627 } 1761 }
1628 } 1762 }
1629 1763
1630 modechange: 1764 modechange:
1631 ; 1765 ;
1632 } 1766 }
1633 1767
1634interrupt: 1768interrupt:
1635 self->incr_pos = p - SvPVX (self->incr_text); 1769 self->incr_pos = p - SvPVX (self->incr_text);
1770 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
1636 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D 1771 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1637} 1772}
1638 1773
1639///////////////////////////////////////////////////////////////////////////// 1774/////////////////////////////////////////////////////////////////////////////
1640// XS interface functions 1775// XS interface functions
1655 json_stash = gv_stashpv ("JSON::XS" , 1); 1790 json_stash = gv_stashpv ("JSON::XS" , 1);
1656 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1791 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1657 1792
1658 json_true = get_bool ("JSON::XS::true"); 1793 json_true = get_bool ("JSON::XS::true");
1659 json_false = get_bool ("JSON::XS::false"); 1794 json_false = get_bool ("JSON::XS::false");
1795
1796 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1660} 1797}
1661 1798
1662PROTOTYPES: DISABLE 1799PROTOTYPES: DISABLE
1663 1800
1664void CLONE (...) 1801void CLONE (...)
1796 PPCODE: 1933 PPCODE:
1797{ 1934{
1798 if (!self->incr_text) 1935 if (!self->incr_text)
1799 self->incr_text = newSVpvn ("", 0); 1936 self->incr_text = newSVpvn ("", 0);
1800 1937
1938 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
1939 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
1940 if (DECODE_WANTS_OCTETS (self))
1941 {
1942 if (self->incr_pos)
1943 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
1944 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
1945
1946 sv_utf8_downgrade (self->incr_text, 0);
1947 }
1948 else
1949 {
1950 sv_utf8_upgrade (self->incr_text);
1951
1952 if (self->incr_pos)
1953 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1954 - (U8 *)SvPVX (self->incr_text);
1955 }
1956
1801 // append data, if any 1957 // append data, if any
1802 if (jsonstr) 1958 if (jsonstr)
1803 { 1959 {
1960 /* make sure both strings have same encoding */
1961 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1804 if (SvUTF8 (jsonstr)) 1962 if (SvUTF8 (jsonstr))
1963 sv_utf8_downgrade (jsonstr, 0);
1805 { 1964 else
1806 if (!SvUTF8 (self->incr_text))
1807 {
1808 /* utf-8-ness differs, need to upgrade */
1809 sv_utf8_upgrade (self->incr_text);
1810
1811 if (self->incr_pos)
1812 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1813 - (U8 *)SvPVX (self->incr_text);
1814 }
1815 }
1816 else if (SvUTF8 (self->incr_text))
1817 sv_utf8_upgrade (jsonstr); 1965 sv_utf8_upgrade (jsonstr);
1818 1966
1967 /* and then just blindly append */
1819 { 1968 {
1820 STRLEN len; 1969 STRLEN len;
1821 const char *str = SvPV (jsonstr, len); 1970 const char *str = SvPV (jsonstr, len);
1822 STRLEN cur = SvCUR (self->incr_text); 1971 STRLEN cur = SvCUR (self->incr_text);
1823 1972
1922 json_init (&json); 2071 json_init (&json);
1923 json.flags |= ix; 2072 json.flags |= ix;
1924 XPUSHs (decode_json (jsonstr, &json, 0)); 2073 XPUSHs (decode_json (jsonstr, &json, 0));
1925} 2074}
1926 2075
1927

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines