… | |
… | |
17 | // guarantees, 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 |
… | |
… | |
76 | |
77 | |
77 | enum { |
78 | enum { |
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_STR, // inside string |
80 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
81 | INCR_M_BS, // inside backslash |
|
|
82 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
83 | INCR_M_C1, // inside comment in other places |
81 | INCR_M_JSON // outside anything, count nesting |
84 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
85 | }; |
83 | |
86 | |
84 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
87 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
88 | |
… | |
… | |
184 | return SvUTF8 (sv) |
187 | return SvUTF8 (sv) |
185 | ? utf8_distance (offset, SvPVX (sv)) |
188 | ? utf8_distance (offset, SvPVX (sv)) |
186 | : offset - SvPVX (sv); |
189 | : offset - SvPVX (sv); |
187 | } |
190 | } |
188 | |
191 | |
|
|
192 | ///////////////////////////////////////////////////////////////////////////// |
|
|
193 | // fp hell |
|
|
194 | |
|
|
195 | // scan a group of digits, and a trailing exponent |
|
|
196 | static void |
|
|
197 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
|
|
198 | { |
|
|
199 | UV uaccum = 0; |
|
|
200 | int eaccum = 0; |
|
|
201 | |
|
|
202 | // if we recurse too deep, skip all remaining digits |
|
|
203 | // to avoid a stack overflow attack |
|
|
204 | if (expect_false (--maxdepth <= 0)) |
|
|
205 | while (((U8)*s - '0') < 10) |
|
|
206 | ++s; |
|
|
207 | |
|
|
208 | for (;;) |
|
|
209 | { |
|
|
210 | U8 dig = (U8)*s - '0'; |
|
|
211 | |
|
|
212 | if (expect_false (dig >= 10)) |
|
|
213 | { |
|
|
214 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
215 | { |
|
|
216 | ++s; |
|
|
217 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
|
|
218 | } |
|
|
219 | else if ((dig | ' ') == 'e' - '0') |
|
|
220 | { |
|
|
221 | int exp2 = 0; |
|
|
222 | int neg = 0; |
|
|
223 | |
|
|
224 | ++s; |
|
|
225 | |
|
|
226 | if (*s == '-') |
|
|
227 | { |
|
|
228 | ++s; |
|
|
229 | neg = 1; |
|
|
230 | } |
|
|
231 | else if (*s == '+') |
|
|
232 | ++s; |
|
|
233 | |
|
|
234 | while ((dig = (U8)*s - '0') < 10) |
|
|
235 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
236 | |
|
|
237 | *expo += neg ? -exp2 : exp2; |
|
|
238 | } |
|
|
239 | |
|
|
240 | break; |
|
|
241 | } |
|
|
242 | |
|
|
243 | ++s; |
|
|
244 | |
|
|
245 | uaccum = uaccum * 10 + dig; |
|
|
246 | ++eaccum; |
|
|
247 | |
|
|
248 | // if we have too many digits, then recurse for more |
|
|
249 | // we actually do this for rather few digits |
|
|
250 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
251 | { |
|
|
252 | if (postdp) *expo -= eaccum; |
|
|
253 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
|
|
254 | if (postdp) *expo += eaccum; |
|
|
255 | |
|
|
256 | break; |
|
|
257 | } |
|
|
258 | } |
|
|
259 | |
|
|
260 | // this relies greatly on the quality of the pow () |
|
|
261 | // implementation of the platform, but a good |
|
|
262 | // implementation is hard to beat. |
|
|
263 | if (postdp) *expo -= eaccum; |
|
|
264 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
265 | *expo += eaccum; |
|
|
266 | } |
|
|
267 | |
|
|
268 | static NV |
|
|
269 | json_atof (const char *s) |
|
|
270 | { |
|
|
271 | NV accum = 0.; |
|
|
272 | int expo = 0; |
|
|
273 | int neg = 0; |
|
|
274 | |
|
|
275 | if (*s == '-') |
|
|
276 | { |
|
|
277 | ++s; |
|
|
278 | neg = 1; |
|
|
279 | } |
|
|
280 | |
|
|
281 | // a recursion depth of ten gives us >>500 bits |
|
|
282 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
283 | |
|
|
284 | return neg ? -accum : accum; |
|
|
285 | } |
189 | ///////////////////////////////////////////////////////////////////////////// |
286 | ///////////////////////////////////////////////////////////////////////////// |
190 | // encoder |
287 | // encoder |
191 | |
288 | |
192 | // structure used for encoding JSON |
289 | // structure used for encoding JSON |
193 | typedef struct |
290 | typedef struct |
… | |
… | |
1116 | } |
1213 | } |
1117 | |
1214 | |
1118 | len -= *start == '-' ? 1 : 0; |
1215 | len -= *start == '-' ? 1 : 0; |
1119 | |
1216 | |
1120 | // does not fit into IV or UV, try NV |
1217 | // does not fit into IV or UV, try NV |
1121 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1218 | 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 |
1219 | // fits into NV without loss of precision |
1127 | return newSVnv (Atof (start)); |
1220 | return newSVnv (json_atof (start)); |
1128 | |
1221 | |
1129 | // everything else fails, convert it to a string |
1222 | // everything else fails, convert it to a string |
1130 | return newSVpvn (start, dec->cur - start); |
1223 | return newSVpvn (start, dec->cur - start); |
1131 | } |
1224 | } |
1132 | |
1225 | |
1133 | // loss of precision here |
1226 | // loss of precision here |
1134 | return newSVnv (Atof (start)); |
1227 | return newSVnv (json_atof (start)); |
1135 | |
1228 | |
1136 | fail: |
1229 | fail: |
1137 | return 0; |
1230 | return 0; |
1138 | } |
1231 | } |
1139 | |
1232 | |
… | |
… | |
1525 | static void |
1618 | static void |
1526 | incr_parse (JSON *self) |
1619 | incr_parse (JSON *self) |
1527 | { |
1620 | { |
1528 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1621 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1529 | |
1622 | |
|
|
1623 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1624 | // but this would make it slower, so... |
|
|
1625 | |
1530 | for (;;) |
1626 | for (;;) |
1531 | { |
1627 | { |
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 |
1628 | //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) |
1629 | switch (self->incr_mode) |
1534 | { |
1630 | { |
1535 | // only used for intiial whitespace skipping |
1631 | // only used for initial whitespace skipping |
1536 | case INCR_M_WS: |
1632 | case INCR_M_WS: |
1537 | for (;;) |
1633 | for (;;) |
1538 | { |
1634 | { |
1539 | if (*p > 0x20) |
1635 | if (*p > 0x20) |
1540 | { |
1636 | { |
|
|
1637 | if (*p == '#') |
|
|
1638 | { |
|
|
1639 | self->incr_mode = INCR_M_C0; |
|
|
1640 | goto incr_m_c; |
|
|
1641 | } |
|
|
1642 | else |
|
|
1643 | { |
1541 | self->incr_mode = INCR_M_JSON; |
1644 | self->incr_mode = INCR_M_JSON; |
1542 | goto incr_m_json; |
1645 | goto incr_m_json; |
|
|
1646 | } |
1543 | } |
1647 | } |
1544 | else if (!*p) |
1648 | else if (!*p) |
1545 | goto interrupt; |
1649 | goto interrupt; |
1546 | |
1650 | |
1547 | ++p; |
1651 | ++p; |
… | |
… | |
1553 | goto interrupt; |
1657 | goto interrupt; |
1554 | |
1658 | |
1555 | ++p; |
1659 | ++p; |
1556 | self->incr_mode = INCR_M_STR; |
1660 | self->incr_mode = INCR_M_STR; |
1557 | goto incr_m_str; |
1661 | goto incr_m_str; |
|
|
1662 | |
|
|
1663 | // inside #-style comments |
|
|
1664 | case INCR_M_C0: |
|
|
1665 | case INCR_M_C1: |
|
|
1666 | incr_m_c: |
|
|
1667 | for (;;) |
|
|
1668 | { |
|
|
1669 | if (*p == '\n') |
|
|
1670 | { |
|
|
1671 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1672 | break; |
|
|
1673 | } |
|
|
1674 | else if (!*p) |
|
|
1675 | goto interrupt; |
|
|
1676 | |
|
|
1677 | ++p; |
|
|
1678 | } |
|
|
1679 | |
|
|
1680 | break; |
1558 | |
1681 | |
1559 | // inside a string |
1682 | // inside a string |
1560 | case INCR_M_STR: |
1683 | case INCR_M_STR: |
1561 | incr_m_str: |
1684 | incr_m_str: |
1562 | for (;;) |
1685 | for (;;) |
… | |
… | |
1621 | |
1744 | |
1622 | case ']': |
1745 | case ']': |
1623 | case '}': |
1746 | case '}': |
1624 | if (--self->incr_nest <= 0) |
1747 | if (--self->incr_nest <= 0) |
1625 | goto interrupt; |
1748 | goto interrupt; |
|
|
1749 | break; |
|
|
1750 | |
|
|
1751 | case '#': |
|
|
1752 | self->incr_mode = INCR_M_C1; |
|
|
1753 | goto incr_m_c; |
1626 | } |
1754 | } |
1627 | } |
1755 | } |
1628 | } |
1756 | } |
1629 | |
1757 | |
1630 | modechange: |
1758 | modechange: |
1631 | ; |
1759 | ; |
1632 | } |
1760 | } |
1633 | |
1761 | |
1634 | interrupt: |
1762 | interrupt: |
1635 | self->incr_pos = p - SvPVX (self->incr_text); |
1763 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1764 | //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 |
1765 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1637 | } |
1766 | } |
1638 | |
1767 | |
1639 | ///////////////////////////////////////////////////////////////////////////// |
1768 | ///////////////////////////////////////////////////////////////////////////// |
1640 | // XS interface functions |
1769 | // XS interface functions |
… | |
… | |
1924 | json_init (&json); |
2053 | json_init (&json); |
1925 | json.flags |= ix; |
2054 | json.flags |= ix; |
1926 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2055 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1927 | } |
2056 | } |
1928 | |
2057 | |
1929 | |
|
|