… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
|
|
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
8 | |
13 | |
9 | #define F_ASCII 0x00000001UL |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
17 | #define F_MAXDEPTH 0xf8000000UL |
23 | #define F_MAXDEPTH 0xf8000000UL |
18 | #define S_MAXDEPTH 27 |
24 | #define S_MAXDEPTH 27 |
19 | |
25 | |
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
21 | |
27 | |
… | |
… | |
26 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
33 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
36 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
33 | |
38 | |
34 | #define SB do { |
39 | #define SB do { |
35 | #define SE } while (0) |
40 | #define SE } while (0) |
|
|
41 | |
|
|
42 | #if __GNUC__ >= 3 |
|
|
43 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
44 | # define inline inline |
|
|
45 | #else |
|
|
46 | # define expect(expr,value) (expr) |
|
|
47 | # define inline static |
|
|
48 | #endif |
|
|
49 | |
|
|
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
36 | |
52 | |
37 | static HV *json_stash; // JSON::XS:: |
53 | static HV *json_stash; // JSON::XS:: |
38 | |
54 | |
39 | ///////////////////////////////////////////////////////////////////////////// |
55 | ///////////////////////////////////////////////////////////////////////////// |
40 | // utility functions |
56 | // utility functions |
… | |
… | |
65 | // decode an utf-8 character and return it, or (UV)-1 in |
81 | // decode an utf-8 character and return it, or (UV)-1 in |
66 | // case of an error. |
82 | // case of an error. |
67 | // we special-case "safe" characters from U+80 .. U+7FF, |
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
68 | // but use the very good perl function to parse anything else. |
84 | // but use the very good perl function to parse anything else. |
69 | // note that we never call this function for a ascii codepoints |
85 | // note that we never call this function for a ascii codepoints |
70 | static UV |
86 | inline UV |
71 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
72 | { |
88 | { |
73 | if (s[0] > 0xdf || s[0] < 0xc2) |
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
74 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
75 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
76 | { |
92 | { |
77 | *clen = 2; |
93 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
… | |
… | |
96 | U32 flags; // F_* |
112 | U32 flags; // F_* |
97 | U32 indent; // indentation level |
113 | U32 indent; // indentation level |
98 | U32 maxdepth; // max. indentation/recursion level |
114 | U32 maxdepth; // max. indentation/recursion level |
99 | } enc_t; |
115 | } enc_t; |
100 | |
116 | |
101 | static void |
117 | inline void |
102 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
103 | { |
119 | { |
104 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
105 | { |
121 | { |
106 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
107 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
108 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
109 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
110 | } |
126 | } |
111 | } |
127 | } |
112 | |
128 | |
113 | static void |
129 | inline void |
114 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
115 | { |
131 | { |
116 | need (enc, 1); |
132 | need (enc, 1); |
117 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
118 | } |
134 | } |
… | |
… | |
126 | |
142 | |
127 | while (str < end) |
143 | while (str < end) |
128 | { |
144 | { |
129 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
130 | |
146 | |
131 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
132 | { |
148 | { |
133 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
134 | { |
150 | { |
135 | need (enc, len += 1); |
151 | need (enc, len += 1); |
136 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
137 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
138 | } |
154 | } |
139 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
140 | { |
156 | { |
141 | need (enc, len += 1); |
157 | need (enc, len += 1); |
142 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
143 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
144 | } |
160 | } |
… | |
… | |
176 | } |
192 | } |
177 | |
193 | |
178 | if (uch > 0x10FFFFUL) |
194 | if (uch > 0x10FFFFUL) |
179 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
195 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
180 | |
196 | |
181 | if (uch < 0x80 || enc->flags & F_ASCII) |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
182 | { |
198 | { |
183 | if (uch > 0xFFFFUL) |
199 | if (uch > 0xFFFFUL) |
184 | { |
200 | { |
185 | need (enc, len += 11); |
201 | need (enc, len += 11); |
186 | sprintf (enc->cur, "\\u%04x\\u%04x", |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
200 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
201 | } |
217 | } |
202 | |
218 | |
203 | str += clen; |
219 | str += clen; |
204 | } |
220 | } |
|
|
221 | else if (enc->flags & F_LATIN1) |
|
|
222 | { |
|
|
223 | *enc->cur++ = uch; |
|
|
224 | str += clen; |
|
|
225 | } |
205 | else if (is_utf8) |
226 | else if (is_utf8) |
206 | { |
227 | { |
207 | need (enc, len += clen); |
228 | need (enc, len += clen); |
208 | do |
229 | do |
209 | { |
230 | { |
… | |
… | |
211 | } |
232 | } |
212 | while (--clen); |
233 | while (--clen); |
213 | } |
234 | } |
214 | else |
235 | else |
215 | { |
236 | { |
216 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
237 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
217 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
238 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
218 | ++str; |
239 | ++str; |
219 | } |
240 | } |
220 | } |
241 | } |
221 | } |
242 | } |
… | |
… | |
223 | |
244 | |
224 | --len; |
245 | --len; |
225 | } |
246 | } |
226 | } |
247 | } |
227 | |
248 | |
228 | static void |
249 | inline void |
229 | encode_indent (enc_t *enc) |
250 | encode_indent (enc_t *enc) |
230 | { |
251 | { |
231 | if (enc->flags & F_INDENT) |
252 | if (enc->flags & F_INDENT) |
232 | { |
253 | { |
233 | int spaces = enc->indent * INDENT_STEP; |
254 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
236 | memset (enc->cur, ' ', spaces); |
257 | memset (enc->cur, ' ', spaces); |
237 | enc->cur += spaces; |
258 | enc->cur += spaces; |
238 | } |
259 | } |
239 | } |
260 | } |
240 | |
261 | |
241 | static void |
262 | inline void |
242 | encode_space (enc_t *enc) |
263 | encode_space (enc_t *enc) |
243 | { |
264 | { |
244 | need (enc, 1); |
265 | need (enc, 1); |
245 | encode_ch (enc, ' '); |
266 | encode_ch (enc, ' '); |
246 | } |
267 | } |
247 | |
268 | |
248 | static void |
269 | inline void |
249 | encode_nl (enc_t *enc) |
270 | encode_nl (enc_t *enc) |
250 | { |
271 | { |
251 | if (enc->flags & F_INDENT) |
272 | if (enc->flags & F_INDENT) |
252 | { |
273 | { |
253 | need (enc, 1); |
274 | need (enc, 1); |
254 | encode_ch (enc, '\n'); |
275 | encode_ch (enc, '\n'); |
255 | } |
276 | } |
256 | } |
277 | } |
257 | |
278 | |
258 | static void |
279 | inline void |
259 | encode_comma (enc_t *enc) |
280 | encode_comma (enc_t *enc) |
260 | { |
281 | { |
261 | encode_ch (enc, ','); |
282 | encode_ch (enc, ','); |
262 | |
283 | |
263 | if (enc->flags & F_INDENT) |
284 | if (enc->flags & F_INDENT) |
… | |
… | |
362 | // actually, this is mostly due to the stupid so-called |
383 | // actually, this is mostly due to the stupid so-called |
363 | // security workaround added somewhere in 5.8.x. |
384 | // security workaround added somewhere in 5.8.x. |
364 | // that randomises hash orderings |
385 | // that randomises hash orderings |
365 | if (enc->flags & F_CANONICAL) |
386 | if (enc->flags & F_CANONICAL) |
366 | { |
387 | { |
367 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
368 | int fast = 1; |
388 | int fast = 1; |
|
|
389 | HE *he; |
|
|
390 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
391 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
392 | #else |
|
|
393 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
394 | #endif |
369 | |
395 | |
370 | i = 0; |
396 | i = 0; |
371 | while ((he = hv_iternext (hv))) |
397 | while ((he = hv_iternext (hv))) |
372 | { |
398 | { |
373 | hes [i++] = he; |
399 | hes [i++] = he; |
… | |
… | |
471 | encode_str (enc, str, len, SvUTF8 (sv)); |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
472 | encode_ch (enc, '"'); |
498 | encode_ch (enc, '"'); |
473 | } |
499 | } |
474 | else if (SvNOKp (sv)) |
500 | else if (SvNOKp (sv)) |
475 | { |
501 | { |
|
|
502 | // trust that perl will do the right thing w.r.t. JSON syntax. |
476 | need (enc, NV_DIG + 32); |
503 | need (enc, NV_DIG + 32); |
477 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
478 | enc->cur += strlen (enc->cur); |
505 | enc->cur += strlen (enc->cur); |
479 | } |
506 | } |
480 | else if (SvIOKp (sv)) |
507 | else if (SvIOKp (sv)) |
481 | { |
508 | { |
482 | need (enc, 64); |
509 | // we assume we can always read an IV as a UV |
|
|
510 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
511 | { |
|
|
512 | // large integer, use the (rather slow) snprintf way. |
|
|
513 | need (enc, sizeof (UV) * 3); |
483 | enc->cur += |
514 | enc->cur += |
484 | SvIsUV(sv) |
515 | SvIsUV(sv) |
485 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
516 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
486 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
517 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
518 | } |
|
|
519 | else |
|
|
520 | { |
|
|
521 | // optimise the "small number case" |
|
|
522 | // code will likely be branchless and use only a single multiplication |
|
|
523 | I32 i = SvIV (sv); |
|
|
524 | U32 u; |
|
|
525 | char digit, nz = 0; |
|
|
526 | |
|
|
527 | need (enc, 6); |
|
|
528 | |
|
|
529 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
530 | u = i < 0 ? -i : i; |
|
|
531 | |
|
|
532 | // convert to 4.28 fixed-point representation |
|
|
533 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
534 | |
|
|
535 | // now output digit by digit, each time masking out the integer part |
|
|
536 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
537 | // resulting in a net multiplication by 10. |
|
|
538 | // we always write the digit to memory but conditionally increment |
|
|
539 | // the pointer, to ease the usage of conditional move instructions. |
|
|
540 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
541 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
542 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
543 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
544 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
545 | } |
487 | } |
546 | } |
488 | else if (SvROK (sv)) |
547 | else if (SvROK (sv)) |
489 | encode_rv (enc, SvRV (sv)); |
548 | encode_rv (enc, SvRV (sv)); |
490 | else if (!SvOK (sv)) |
549 | else if (!SvOK (sv)) |
491 | encode_str (enc, "null", 4, 0); |
550 | encode_str (enc, "null", 4, 0); |
… | |
… | |
510 | enc.maxdepth = DEC_DEPTH (flags); |
569 | enc.maxdepth = DEC_DEPTH (flags); |
511 | |
570 | |
512 | SvPOK_only (enc.sv); |
571 | SvPOK_only (enc.sv); |
513 | encode_sv (&enc, scalar); |
572 | encode_sv (&enc, scalar); |
514 | |
573 | |
|
|
574 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
575 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
576 | |
515 | if (!(flags & (F_ASCII | F_UTF8))) |
577 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
516 | SvUTF8_on (enc.sv); |
578 | SvUTF8_on (enc.sv); |
517 | |
|
|
518 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
519 | |
579 | |
520 | if (enc.flags & F_SHRINK) |
580 | if (enc.flags & F_SHRINK) |
521 | shrink (enc.sv); |
581 | shrink (enc.sv); |
522 | |
582 | |
523 | return enc.sv; |
583 | return enc.sv; |
… | |
… | |
535 | U32 flags; // F_* |
595 | U32 flags; // F_* |
536 | U32 depth; // recursion depth |
596 | U32 depth; // recursion depth |
537 | U32 maxdepth; // recursion depth limit |
597 | U32 maxdepth; // recursion depth limit |
538 | } dec_t; |
598 | } dec_t; |
539 | |
599 | |
540 | static void |
600 | inline void |
541 | decode_ws (dec_t *dec) |
601 | decode_ws (dec_t *dec) |
542 | { |
602 | { |
543 | for (;;) |
603 | for (;;) |
544 | { |
604 | { |
545 | char ch = *dec->cur; |
605 | char ch = *dec->cur; |
… | |
… | |
571 | decode_4hex (dec_t *dec) |
631 | decode_4hex (dec_t *dec) |
572 | { |
632 | { |
573 | signed char d1, d2, d3, d4; |
633 | signed char d1, d2, d3, d4; |
574 | unsigned char *cur = (unsigned char *)dec->cur; |
634 | unsigned char *cur = (unsigned char *)dec->cur; |
575 | |
635 | |
576 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
636 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
577 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
637 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
578 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
638 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
579 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
639 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
580 | |
640 | |
581 | dec->cur += 4; |
641 | dec->cur += 4; |
582 | |
642 | |
583 | return ((UV)d1) << 12 |
643 | return ((UV)d1) << 12 |
584 | | ((UV)d2) << 8 |
644 | | ((UV)d2) << 8 |
… | |
… | |
592 | static SV * |
652 | static SV * |
593 | decode_str (dec_t *dec) |
653 | decode_str (dec_t *dec) |
594 | { |
654 | { |
595 | SV *sv = 0; |
655 | SV *sv = 0; |
596 | int utf8 = 0; |
656 | int utf8 = 0; |
|
|
657 | char *dec_cur = dec->cur; |
597 | |
658 | |
598 | do |
659 | do |
599 | { |
660 | { |
600 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
661 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
601 | char *cur = buf; |
662 | char *cur = buf; |
602 | |
663 | |
603 | do |
664 | do |
604 | { |
665 | { |
605 | unsigned char ch = *(unsigned char *)dec->cur++; |
666 | unsigned char ch = *(unsigned char *)dec_cur++; |
606 | |
667 | |
607 | if (ch == '"') |
668 | if (expect_false (ch == '"')) |
608 | { |
669 | { |
609 | --dec->cur; |
670 | --dec_cur; |
610 | break; |
671 | break; |
611 | } |
672 | } |
612 | else if (ch == '\\') |
673 | else if (expect_false (ch == '\\')) |
613 | { |
674 | { |
614 | switch (*dec->cur) |
675 | switch (*dec_cur) |
615 | { |
676 | { |
616 | case '\\': |
677 | case '\\': |
617 | case '/': |
678 | case '/': |
618 | case '"': *cur++ = *dec->cur++; break; |
679 | case '"': *cur++ = *dec_cur++; break; |
619 | |
680 | |
620 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
681 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
621 | case 't': ++dec->cur; *cur++ = '\011'; break; |
682 | case 't': ++dec_cur; *cur++ = '\011'; break; |
622 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
683 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
623 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
684 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
624 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
685 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
625 | |
686 | |
626 | case 'u': |
687 | case 'u': |
627 | { |
688 | { |
628 | UV lo, hi; |
689 | UV lo, hi; |
629 | ++dec->cur; |
690 | ++dec_cur; |
630 | |
691 | |
|
|
692 | dec->cur = dec_cur; |
631 | hi = decode_4hex (dec); |
693 | hi = decode_4hex (dec); |
|
|
694 | dec_cur = dec->cur; |
632 | if (hi == (UV)-1) |
695 | if (hi == (UV)-1) |
633 | goto fail; |
696 | goto fail; |
634 | |
697 | |
635 | // possibly a surrogate pair |
698 | // possibly a surrogate pair |
636 | if (hi >= 0xd800) |
699 | if (hi >= 0xd800) |
637 | if (hi < 0xdc00) |
700 | if (hi < 0xdc00) |
638 | { |
701 | { |
639 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
702 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
640 | ERR ("missing low surrogate character in surrogate pair"); |
703 | ERR ("missing low surrogate character in surrogate pair"); |
641 | |
704 | |
642 | dec->cur += 2; |
705 | dec_cur += 2; |
643 | |
706 | |
|
|
707 | dec->cur = dec_cur; |
644 | lo = decode_4hex (dec); |
708 | lo = decode_4hex (dec); |
|
|
709 | dec_cur = dec->cur; |
645 | if (lo == (UV)-1) |
710 | if (lo == (UV)-1) |
646 | goto fail; |
711 | goto fail; |
647 | |
712 | |
648 | if (lo < 0xdc00 || lo >= 0xe000) |
713 | if (lo < 0xdc00 || lo >= 0xe000) |
649 | ERR ("surrogate pair expected"); |
714 | ERR ("surrogate pair expected"); |
… | |
… | |
663 | *cur++ = hi; |
728 | *cur++ = hi; |
664 | } |
729 | } |
665 | break; |
730 | break; |
666 | |
731 | |
667 | default: |
732 | default: |
668 | --dec->cur; |
733 | --dec_cur; |
669 | ERR ("illegal backslash escape sequence in string"); |
734 | ERR ("illegal backslash escape sequence in string"); |
670 | } |
735 | } |
671 | } |
736 | } |
672 | else if (ch >= 0x20 && ch <= 0x7f) |
737 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
673 | *cur++ = ch; |
738 | *cur++ = ch; |
674 | else if (ch >= 0x80) |
739 | else if (ch >= 0x80) |
675 | { |
740 | { |
676 | STRLEN clen; |
741 | STRLEN clen; |
677 | UV uch; |
742 | UV uch; |
678 | |
743 | |
679 | --dec->cur; |
744 | --dec_cur; |
680 | |
745 | |
681 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
746 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
682 | if (clen == (STRLEN)-1) |
747 | if (clen == (STRLEN)-1) |
683 | ERR ("malformed UTF-8 character in JSON string"); |
748 | ERR ("malformed UTF-8 character in JSON string"); |
684 | |
749 | |
685 | do |
750 | do |
686 | *cur++ = *dec->cur++; |
751 | *cur++ = *dec_cur++; |
687 | while (--clen); |
752 | while (--clen); |
688 | |
753 | |
689 | utf8 = 1; |
754 | utf8 = 1; |
690 | } |
755 | } |
691 | else |
756 | else |
692 | { |
757 | { |
693 | --dec->cur; |
758 | --dec_cur; |
694 | |
759 | |
695 | if (!ch) |
760 | if (!ch) |
696 | ERR ("unexpected end of string while parsing JSON string"); |
761 | ERR ("unexpected end of string while parsing JSON string"); |
697 | else |
762 | else |
698 | ERR ("invalid character encountered while parsing JSON string"); |
763 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
711 | } |
776 | } |
712 | else |
777 | else |
713 | sv = newSVpvn (buf, len); |
778 | sv = newSVpvn (buf, len); |
714 | } |
779 | } |
715 | } |
780 | } |
716 | while (*dec->cur != '"'); |
781 | while (*dec_cur != '"'); |
717 | |
782 | |
718 | ++dec->cur; |
783 | ++dec_cur; |
719 | |
784 | |
720 | if (sv) |
785 | if (sv) |
721 | { |
786 | { |
722 | SvPOK_only (sv); |
787 | SvPOK_only (sv); |
723 | *SvEND (sv) = 0; |
788 | *SvEND (sv) = 0; |
… | |
… | |
726 | SvUTF8_on (sv); |
791 | SvUTF8_on (sv); |
727 | } |
792 | } |
728 | else |
793 | else |
729 | sv = newSVpvn ("", 0); |
794 | sv = newSVpvn ("", 0); |
730 | |
795 | |
|
|
796 | dec->cur = dec_cur; |
731 | return sv; |
797 | return sv; |
732 | |
798 | |
733 | fail: |
799 | fail: |
|
|
800 | dec->cur = dec_cur; |
734 | return 0; |
801 | return 0; |
735 | } |
802 | } |
736 | |
803 | |
737 | static SV * |
804 | static SV * |
738 | decode_num (dec_t *dec) |
805 | decode_num (dec_t *dec) |
… | |
… | |
796 | is_nv = 1; |
863 | is_nv = 1; |
797 | } |
864 | } |
798 | |
865 | |
799 | if (!is_nv) |
866 | if (!is_nv) |
800 | { |
867 | { |
801 | UV uv; |
868 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
802 | int numtype = grok_number (start, dec->cur - start, &uv); |
869 | if (*start == '-') |
803 | if (numtype & IS_NUMBER_IN_UV) |
870 | switch (dec->cur - start) |
804 | if (numtype & IS_NUMBER_NEG) |
|
|
805 | { |
871 | { |
806 | if (uv < (UV)IV_MIN) |
872 | case 2: return newSViv (-( start [1] - '0' )); |
807 | return newSViv (-(IV)uv); |
873 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
874 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
875 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
808 | } |
876 | } |
|
|
877 | else |
|
|
878 | switch (dec->cur - start) |
|
|
879 | { |
|
|
880 | case 1: return newSViv ( start [0] - '0' ); |
|
|
881 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
882 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
883 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
884 | } |
|
|
885 | |
|
|
886 | { |
|
|
887 | UV uv; |
|
|
888 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
889 | if (numtype & IS_NUMBER_IN_UV) |
|
|
890 | if (numtype & IS_NUMBER_NEG) |
|
|
891 | { |
|
|
892 | if (uv < (UV)IV_MIN) |
|
|
893 | return newSViv (-(IV)uv); |
|
|
894 | } |
809 | else |
895 | else |
810 | return newSVuv (uv); |
896 | return newSVuv (uv); |
|
|
897 | } |
811 | } |
898 | } |
812 | |
899 | |
813 | return newSVnv (Atof (start)); |
900 | return newSVnv (Atof (start)); |
814 | |
901 | |
815 | fail: |
902 | fail: |
… | |
… | |
972 | fail: |
1059 | fail: |
973 | return 0; |
1060 | return 0; |
974 | } |
1061 | } |
975 | |
1062 | |
976 | static SV * |
1063 | static SV * |
977 | decode_json (SV *string, U32 flags) |
1064 | decode_json (SV *string, U32 flags, UV *offset_return) |
978 | { |
1065 | { |
979 | dec_t dec; |
1066 | dec_t dec; |
|
|
1067 | UV offset; |
980 | SV *sv; |
1068 | SV *sv; |
981 | |
1069 | |
|
|
1070 | SvGETMAGIC (string); |
982 | SvUPGRADE (string, SVt_PV); |
1071 | SvUPGRADE (string, SVt_PV); |
983 | |
1072 | |
984 | if (flags & F_UTF8) |
1073 | if (flags & F_UTF8) |
985 | sv_utf8_downgrade (string, 0); |
1074 | sv_utf8_downgrade (string, 0); |
986 | else |
1075 | else |
… | |
… | |
993 | dec.end = SvEND (string); |
1082 | dec.end = SvEND (string); |
994 | dec.err = 0; |
1083 | dec.err = 0; |
995 | dec.depth = 0; |
1084 | dec.depth = 0; |
996 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1085 | dec.maxdepth = DEC_DEPTH (dec.flags); |
997 | |
1086 | |
998 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1087 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
999 | sv = decode_sv (&dec); |
1088 | sv = decode_sv (&dec); |
1000 | |
1089 | |
|
|
1090 | if (!(offset_return || !sv)) |
|
|
1091 | { |
|
|
1092 | // check for trailing garbage |
|
|
1093 | decode_ws (&dec); |
|
|
1094 | |
|
|
1095 | if (*dec.cur) |
|
|
1096 | { |
|
|
1097 | dec.err = "garbage after JSON object"; |
|
|
1098 | SvREFCNT_dec (sv); |
|
|
1099 | sv = 0; |
|
|
1100 | } |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | if (offset_return || !sv) |
|
|
1104 | { |
|
|
1105 | offset = dec.flags & F_UTF8 |
|
|
1106 | ? dec.cur - SvPVX (string) |
|
|
1107 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1108 | |
|
|
1109 | if (offset_return) |
|
|
1110 | *offset_return = offset; |
|
|
1111 | } |
|
|
1112 | |
1001 | if (!sv) |
1113 | if (!sv) |
1002 | { |
1114 | { |
1003 | IV offset = dec.flags & F_UTF8 |
|
|
1004 | ? dec.cur - SvPVX (string) |
|
|
1005 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1006 | SV *uni = sv_newmortal (); |
1115 | SV *uni = sv_newmortal (); |
1007 | |
1116 | |
1008 | // horrible hack to silence warning inside pv_uni_display |
1117 | // horrible hack to silence warning inside pv_uni_display |
1009 | COP cop = *PL_curcop; |
1118 | COP cop = *PL_curcop; |
1010 | cop.cop_warnings = pWARN_NONE; |
1119 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1058 | RETVAL |
1167 | RETVAL |
1059 | |
1168 | |
1060 | SV *ascii (SV *self, int enable = 1) |
1169 | SV *ascii (SV *self, int enable = 1) |
1061 | ALIAS: |
1170 | ALIAS: |
1062 | ascii = F_ASCII |
1171 | ascii = F_ASCII |
|
|
1172 | latin1 = F_LATIN1 |
1063 | utf8 = F_UTF8 |
1173 | utf8 = F_UTF8 |
1064 | indent = F_INDENT |
1174 | indent = F_INDENT |
1065 | canonical = F_CANONICAL |
1175 | canonical = F_CANONICAL |
1066 | space_before = F_SPACE_BEFORE |
1176 | space_before = F_SPACE_BEFORE |
1067 | space_after = F_SPACE_AFTER |
1177 | space_after = F_SPACE_AFTER |
… | |
… | |
1103 | PPCODE: |
1213 | PPCODE: |
1104 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1214 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1105 | |
1215 | |
1106 | void decode (SV *self, SV *jsonstr) |
1216 | void decode (SV *self, SV *jsonstr) |
1107 | PPCODE: |
1217 | PPCODE: |
1108 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1218 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1219 | |
|
|
1220 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1221 | PPCODE: |
|
|
1222 | { |
|
|
1223 | UV offset; |
|
|
1224 | EXTEND (SP, 2); |
|
|
1225 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1226 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1227 | } |
1109 | |
1228 | |
1110 | PROTOTYPES: ENABLE |
1229 | PROTOTYPES: ENABLE |
1111 | |
1230 | |
1112 | void to_json (SV *scalar) |
1231 | void to_json (SV *scalar) |
1113 | ALIAS: |
1232 | ALIAS: |
… | |
… | |
1117 | |
1236 | |
1118 | void from_json (SV *jsonstr) |
1237 | void from_json (SV *jsonstr) |
1119 | ALIAS: |
1238 | ALIAS: |
1120 | jsonToObj = 0 |
1239 | jsonToObj = 0 |
1121 | PPCODE: |
1240 | PPCODE: |
1122 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1241 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1123 | |
1242 | |