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