… | |
… | |
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 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
28 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
29 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
30 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (12UL << 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); |
79 | } |
95 | } |
80 | else |
96 | else |
|
|
97 | { |
|
|
98 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
99 | return (UV)-1; |
|
|
100 | } |
82 | } |
101 | } |
83 | |
102 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
103 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
104 | // encoder |
86 | |
105 | |
… | |
… | |
93 | U32 flags; // F_* |
112 | U32 flags; // F_* |
94 | U32 indent; // indentation level |
113 | U32 indent; // indentation level |
95 | U32 maxdepth; // max. indentation/recursion level |
114 | U32 maxdepth; // max. indentation/recursion level |
96 | } enc_t; |
115 | } enc_t; |
97 | |
116 | |
98 | static void |
117 | inline void |
99 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
100 | { |
119 | { |
101 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
102 | { |
121 | { |
103 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
105 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | } |
126 | } |
108 | } |
127 | } |
109 | |
128 | |
110 | static void |
129 | inline void |
111 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
112 | { |
131 | { |
113 | need (enc, 1); |
132 | need (enc, 1); |
114 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
115 | } |
134 | } |
… | |
… | |
123 | |
142 | |
124 | while (str < end) |
143 | while (str < end) |
125 | { |
144 | { |
126 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
127 | |
146 | |
128 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
129 | { |
148 | { |
130 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
131 | { |
150 | { |
132 | need (enc, len += 1); |
151 | need (enc, len += 1); |
133 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
134 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
135 | } |
154 | } |
136 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
137 | { |
156 | { |
138 | need (enc, len += 1); |
157 | need (enc, len += 1); |
139 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
140 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
141 | } |
160 | } |
… | |
… | |
173 | } |
192 | } |
174 | |
193 | |
175 | if (uch > 0x10FFFFUL) |
194 | if (uch > 0x10FFFFUL) |
176 | 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); |
177 | |
196 | |
178 | if (uch < 0x80 || enc->flags & F_ASCII) |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
179 | { |
198 | { |
180 | if (uch > 0xFFFFUL) |
199 | if (uch > 0xFFFFUL) |
181 | { |
200 | { |
182 | need (enc, len += 11); |
201 | need (enc, len += 11); |
183 | sprintf (enc->cur, "\\u%04x\\u%04x", |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
197 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
198 | } |
217 | } |
199 | |
218 | |
200 | str += clen; |
219 | str += clen; |
201 | } |
220 | } |
|
|
221 | else if (enc->flags & F_LATIN1) |
|
|
222 | { |
|
|
223 | *enc->cur++ = uch; |
|
|
224 | str += clen; |
|
|
225 | } |
202 | else if (is_utf8) |
226 | else if (is_utf8) |
203 | { |
227 | { |
204 | need (enc, len += clen); |
228 | need (enc, len += clen); |
205 | do |
229 | do |
206 | { |
230 | { |
… | |
… | |
208 | } |
232 | } |
209 | while (--clen); |
233 | while (--clen); |
210 | } |
234 | } |
211 | else |
235 | else |
212 | { |
236 | { |
213 | 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 |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
238 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
215 | ++str; |
239 | ++str; |
216 | } |
240 | } |
217 | } |
241 | } |
218 | } |
242 | } |
… | |
… | |
220 | |
244 | |
221 | --len; |
245 | --len; |
222 | } |
246 | } |
223 | } |
247 | } |
224 | |
248 | |
225 | static void |
249 | inline void |
226 | encode_indent (enc_t *enc) |
250 | encode_indent (enc_t *enc) |
227 | { |
251 | { |
228 | if (enc->flags & F_INDENT) |
252 | if (enc->flags & F_INDENT) |
229 | { |
253 | { |
230 | int spaces = enc->indent * INDENT_STEP; |
254 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
233 | memset (enc->cur, ' ', spaces); |
257 | memset (enc->cur, ' ', spaces); |
234 | enc->cur += spaces; |
258 | enc->cur += spaces; |
235 | } |
259 | } |
236 | } |
260 | } |
237 | |
261 | |
238 | static void |
262 | inline void |
239 | encode_space (enc_t *enc) |
263 | encode_space (enc_t *enc) |
240 | { |
264 | { |
241 | need (enc, 1); |
265 | need (enc, 1); |
242 | encode_ch (enc, ' '); |
266 | encode_ch (enc, ' '); |
243 | } |
267 | } |
244 | |
268 | |
245 | static void |
269 | inline void |
246 | encode_nl (enc_t *enc) |
270 | encode_nl (enc_t *enc) |
247 | { |
271 | { |
248 | if (enc->flags & F_INDENT) |
272 | if (enc->flags & F_INDENT) |
249 | { |
273 | { |
250 | need (enc, 1); |
274 | need (enc, 1); |
251 | encode_ch (enc, '\n'); |
275 | encode_ch (enc, '\n'); |
252 | } |
276 | } |
253 | } |
277 | } |
254 | |
278 | |
255 | static void |
279 | inline void |
256 | encode_comma (enc_t *enc) |
280 | encode_comma (enc_t *enc) |
257 | { |
281 | { |
258 | encode_ch (enc, ','); |
282 | encode_ch (enc, ','); |
259 | |
283 | |
260 | if (enc->flags & F_INDENT) |
284 | if (enc->flags & F_INDENT) |
… | |
… | |
359 | // actually, this is mostly due to the stupid so-called |
383 | // actually, this is mostly due to the stupid so-called |
360 | // security workaround added somewhere in 5.8.x. |
384 | // security workaround added somewhere in 5.8.x. |
361 | // that randomises hash orderings |
385 | // that randomises hash orderings |
362 | if (enc->flags & F_CANONICAL) |
386 | if (enc->flags & F_CANONICAL) |
363 | { |
387 | { |
364 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
365 | 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 |
366 | |
395 | |
367 | i = 0; |
396 | i = 0; |
368 | while ((he = hv_iternext (hv))) |
397 | while ((he = hv_iternext (hv))) |
369 | { |
398 | { |
370 | hes [i++] = he; |
399 | hes [i++] = he; |
… | |
… | |
405 | |
434 | |
406 | encode_nl (enc); |
435 | encode_nl (enc); |
407 | } |
436 | } |
408 | else |
437 | else |
409 | { |
438 | { |
410 | SV *sv; |
|
|
411 | HE *he = hv_iternext (hv); |
439 | HE *he = hv_iternext (hv); |
412 | |
440 | |
413 | for (;;) |
441 | for (;;) |
414 | { |
442 | { |
415 | encode_indent (enc); |
443 | encode_indent (enc); |
… | |
… | |
430 | |
458 | |
431 | // encode objects, arrays and special \0=false and \1=true values. |
459 | // encode objects, arrays and special \0=false and \1=true values. |
432 | static void |
460 | static void |
433 | encode_rv (enc_t *enc, SV *sv) |
461 | encode_rv (enc_t *enc, SV *sv) |
434 | { |
462 | { |
|
|
463 | svtype svt; |
|
|
464 | |
435 | SvGETMAGIC (sv); |
465 | SvGETMAGIC (sv); |
436 | |
|
|
437 | svtype svt = SvTYPE (sv); |
466 | svt = SvTYPE (sv); |
438 | |
467 | |
439 | if (svt == SVt_PVHV) |
468 | if (svt == SVt_PVHV) |
440 | encode_hv (enc, (HV *)sv); |
469 | encode_hv (enc, (HV *)sv); |
441 | else if (svt == SVt_PVAV) |
470 | else if (svt == SVt_PVAV) |
442 | encode_av (enc, (AV *)sv); |
471 | encode_av (enc, (AV *)sv); |
… | |
… | |
474 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
475 | enc->cur += strlen (enc->cur); |
504 | enc->cur += strlen (enc->cur); |
476 | } |
505 | } |
477 | else if (SvIOKp (sv)) |
506 | else if (SvIOKp (sv)) |
478 | { |
507 | { |
479 | need (enc, 64); |
508 | // we assume we can always read an IV as a UV |
|
|
509 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
510 | { |
|
|
511 | need (enc, sizeof (UV) * 3); |
480 | enc->cur += |
512 | enc->cur += |
481 | SvIsUV(sv) |
513 | SvIsUV(sv) |
482 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
514 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
483 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
515 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
516 | } |
|
|
517 | else |
|
|
518 | { |
|
|
519 | // optimise the "small number case" |
|
|
520 | // code will likely be branchless and use only a single multiplication |
|
|
521 | I32 i = SvIV (sv); |
|
|
522 | U32 u; |
|
|
523 | |
|
|
524 | need (enc, 6); |
|
|
525 | |
|
|
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
527 | u = i < 0 ? -i : i; |
|
|
528 | |
|
|
529 | // convert to 4.28 fixed-point representation |
|
|
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
531 | |
|
|
532 | char digit, nz = 0; |
|
|
533 | |
|
|
534 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
535 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
536 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
537 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
538 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; |
|
|
539 | } |
484 | } |
540 | } |
485 | else if (SvROK (sv)) |
541 | else if (SvROK (sv)) |
486 | encode_rv (enc, SvRV (sv)); |
542 | encode_rv (enc, SvRV (sv)); |
487 | else if (!SvOK (sv)) |
543 | else if (!SvOK (sv)) |
488 | encode_str (enc, "null", 4, 0); |
544 | encode_str (enc, "null", 4, 0); |
… | |
… | |
492 | } |
548 | } |
493 | |
549 | |
494 | static SV * |
550 | static SV * |
495 | encode_json (SV *scalar, U32 flags) |
551 | encode_json (SV *scalar, U32 flags) |
496 | { |
552 | { |
|
|
553 | enc_t enc; |
|
|
554 | |
497 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
555 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
498 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
556 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
499 | |
557 | |
500 | enc_t enc; |
|
|
501 | enc.flags = flags; |
558 | enc.flags = flags; |
502 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
559 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
503 | enc.cur = SvPVX (enc.sv); |
560 | enc.cur = SvPVX (enc.sv); |
504 | enc.end = SvEND (enc.sv); |
561 | enc.end = SvEND (enc.sv); |
505 | enc.indent = 0; |
562 | enc.indent = 0; |
506 | enc.maxdepth = DEC_DEPTH (flags); |
563 | enc.maxdepth = DEC_DEPTH (flags); |
507 | |
564 | |
508 | SvPOK_only (enc.sv); |
565 | SvPOK_only (enc.sv); |
509 | encode_sv (&enc, scalar); |
566 | encode_sv (&enc, scalar); |
510 | |
567 | |
|
|
568 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
569 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
570 | |
511 | if (!(flags & (F_ASCII | F_UTF8))) |
571 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
512 | SvUTF8_on (enc.sv); |
572 | SvUTF8_on (enc.sv); |
513 | |
|
|
514 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
515 | |
573 | |
516 | if (enc.flags & F_SHRINK) |
574 | if (enc.flags & F_SHRINK) |
517 | shrink (enc.sv); |
575 | shrink (enc.sv); |
518 | |
576 | |
519 | return enc.sv; |
577 | return enc.sv; |
… | |
… | |
531 | U32 flags; // F_* |
589 | U32 flags; // F_* |
532 | U32 depth; // recursion depth |
590 | U32 depth; // recursion depth |
533 | U32 maxdepth; // recursion depth limit |
591 | U32 maxdepth; // recursion depth limit |
534 | } dec_t; |
592 | } dec_t; |
535 | |
593 | |
536 | static void |
594 | inline void |
537 | decode_ws (dec_t *dec) |
595 | decode_ws (dec_t *dec) |
538 | { |
596 | { |
539 | for (;;) |
597 | for (;;) |
540 | { |
598 | { |
541 | char ch = *dec->cur; |
599 | char ch = *dec->cur; |
… | |
… | |
567 | decode_4hex (dec_t *dec) |
625 | decode_4hex (dec_t *dec) |
568 | { |
626 | { |
569 | signed char d1, d2, d3, d4; |
627 | signed char d1, d2, d3, d4; |
570 | unsigned char *cur = (unsigned char *)dec->cur; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
571 | |
629 | |
572 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
573 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
631 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
574 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
632 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
575 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
633 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
576 | |
634 | |
577 | dec->cur += 4; |
635 | dec->cur += 4; |
578 | |
636 | |
579 | return ((UV)d1) << 12 |
637 | return ((UV)d1) << 12 |
580 | | ((UV)d2) << 8 |
638 | | ((UV)d2) << 8 |
… | |
… | |
588 | static SV * |
646 | static SV * |
589 | decode_str (dec_t *dec) |
647 | decode_str (dec_t *dec) |
590 | { |
648 | { |
591 | SV *sv = 0; |
649 | SV *sv = 0; |
592 | int utf8 = 0; |
650 | int utf8 = 0; |
|
|
651 | char *dec_cur = dec->cur; |
593 | |
652 | |
594 | do |
653 | do |
595 | { |
654 | { |
596 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
655 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
597 | char *cur = buf; |
656 | char *cur = buf; |
598 | |
657 | |
599 | do |
658 | do |
600 | { |
659 | { |
601 | unsigned char ch = *(unsigned char *)dec->cur++; |
660 | unsigned char ch = *(unsigned char *)dec_cur++; |
602 | |
661 | |
603 | if (ch == '"') |
662 | if (expect_false (ch == '"')) |
604 | { |
663 | { |
605 | --dec->cur; |
664 | --dec_cur; |
606 | break; |
665 | break; |
607 | } |
666 | } |
608 | else if (ch == '\\') |
667 | else if (expect_false (ch == '\\')) |
609 | { |
668 | { |
610 | switch (*dec->cur) |
669 | switch (*dec_cur) |
611 | { |
670 | { |
612 | case '\\': |
671 | case '\\': |
613 | case '/': |
672 | case '/': |
614 | case '"': *cur++ = *dec->cur++; break; |
673 | case '"': *cur++ = *dec_cur++; break; |
615 | |
674 | |
616 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
675 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
617 | case 't': ++dec->cur; *cur++ = '\011'; break; |
676 | case 't': ++dec_cur; *cur++ = '\011'; break; |
618 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
677 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
619 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
678 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
620 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
679 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
621 | |
680 | |
622 | case 'u': |
681 | case 'u': |
623 | { |
682 | { |
624 | UV lo, hi; |
683 | UV lo, hi; |
625 | ++dec->cur; |
684 | ++dec_cur; |
626 | |
685 | |
|
|
686 | dec->cur = dec_cur; |
627 | hi = decode_4hex (dec); |
687 | hi = decode_4hex (dec); |
|
|
688 | dec_cur = dec->cur; |
628 | if (hi == (UV)-1) |
689 | if (hi == (UV)-1) |
629 | goto fail; |
690 | goto fail; |
630 | |
691 | |
631 | // possibly a surrogate pair |
692 | // possibly a surrogate pair |
632 | if (hi >= 0xd800) |
693 | if (hi >= 0xd800) |
633 | if (hi < 0xdc00) |
694 | if (hi < 0xdc00) |
634 | { |
695 | { |
635 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
696 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
636 | ERR ("missing low surrogate character in surrogate pair"); |
697 | ERR ("missing low surrogate character in surrogate pair"); |
637 | |
698 | |
638 | dec->cur += 2; |
699 | dec_cur += 2; |
639 | |
700 | |
|
|
701 | dec->cur = dec_cur; |
640 | lo = decode_4hex (dec); |
702 | lo = decode_4hex (dec); |
|
|
703 | dec_cur = dec->cur; |
641 | if (lo == (UV)-1) |
704 | if (lo == (UV)-1) |
642 | goto fail; |
705 | goto fail; |
643 | |
706 | |
644 | if (lo < 0xdc00 || lo >= 0xe000) |
707 | if (lo < 0xdc00 || lo >= 0xe000) |
645 | ERR ("surrogate pair expected"); |
708 | ERR ("surrogate pair expected"); |
… | |
… | |
659 | *cur++ = hi; |
722 | *cur++ = hi; |
660 | } |
723 | } |
661 | break; |
724 | break; |
662 | |
725 | |
663 | default: |
726 | default: |
664 | --dec->cur; |
727 | --dec_cur; |
665 | ERR ("illegal backslash escape sequence in string"); |
728 | ERR ("illegal backslash escape sequence in string"); |
666 | } |
729 | } |
667 | } |
730 | } |
668 | else if (ch >= 0x20 && ch <= 0x7f) |
731 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
669 | *cur++ = ch; |
732 | *cur++ = ch; |
670 | else if (ch >= 0x80) |
733 | else if (ch >= 0x80) |
671 | { |
734 | { |
672 | --dec->cur; |
|
|
673 | |
|
|
674 | STRLEN clen; |
735 | STRLEN clen; |
|
|
736 | UV uch; |
|
|
737 | |
|
|
738 | --dec_cur; |
|
|
739 | |
675 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
740 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
676 | if (clen == (STRLEN)-1) |
741 | if (clen == (STRLEN)-1) |
677 | ERR ("malformed UTF-8 character in JSON string"); |
742 | ERR ("malformed UTF-8 character in JSON string"); |
678 | |
743 | |
679 | do |
744 | do |
680 | { |
|
|
681 | *cur++ = *dec->cur++; |
745 | *cur++ = *dec_cur++; |
682 | } |
|
|
683 | while (--clen); |
746 | while (--clen); |
684 | |
747 | |
685 | utf8 = 1; |
748 | utf8 = 1; |
686 | } |
749 | } |
687 | else if (!ch) |
|
|
688 | ERR ("unexpected end of string while parsing json string"); |
|
|
689 | else |
750 | else |
|
|
751 | { |
|
|
752 | --dec_cur; |
|
|
753 | |
|
|
754 | if (!ch) |
|
|
755 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
756 | else |
690 | ERR ("invalid character encountered"); |
757 | ERR ("invalid character encountered while parsing JSON string"); |
691 | |
758 | } |
692 | } |
759 | } |
693 | while (cur < buf + SHORT_STRING_LEN); |
760 | while (cur < buf + SHORT_STRING_LEN); |
694 | |
761 | |
|
|
762 | { |
695 | STRLEN len = cur - buf; |
763 | STRLEN len = cur - buf; |
696 | |
764 | |
697 | if (sv) |
765 | if (sv) |
698 | { |
766 | { |
699 | SvGROW (sv, SvCUR (sv) + len + 1); |
767 | SvGROW (sv, SvCUR (sv) + len + 1); |
700 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
768 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
701 | SvCUR_set (sv, SvCUR (sv) + len); |
769 | SvCUR_set (sv, SvCUR (sv) + len); |
702 | } |
770 | } |
703 | else |
771 | else |
704 | sv = newSVpvn (buf, len); |
772 | sv = newSVpvn (buf, len); |
705 | } |
773 | } |
|
|
774 | } |
706 | while (*dec->cur != '"'); |
775 | while (*dec_cur != '"'); |
707 | |
776 | |
708 | ++dec->cur; |
777 | ++dec_cur; |
709 | |
778 | |
710 | if (sv) |
779 | if (sv) |
711 | { |
780 | { |
712 | SvPOK_only (sv); |
781 | SvPOK_only (sv); |
713 | *SvEND (sv) = 0; |
782 | *SvEND (sv) = 0; |
… | |
… | |
716 | SvUTF8_on (sv); |
785 | SvUTF8_on (sv); |
717 | } |
786 | } |
718 | else |
787 | else |
719 | sv = newSVpvn ("", 0); |
788 | sv = newSVpvn ("", 0); |
720 | |
789 | |
|
|
790 | dec->cur = dec_cur; |
721 | return sv; |
791 | return sv; |
722 | |
792 | |
723 | fail: |
793 | fail: |
|
|
794 | dec->cur = dec_cur; |
724 | return 0; |
795 | return 0; |
725 | } |
796 | } |
726 | |
797 | |
727 | static SV * |
798 | static SV * |
728 | decode_num (dec_t *dec) |
799 | decode_num (dec_t *dec) |
… | |
… | |
786 | is_nv = 1; |
857 | is_nv = 1; |
787 | } |
858 | } |
788 | |
859 | |
789 | if (!is_nv) |
860 | if (!is_nv) |
790 | { |
861 | { |
791 | UV uv; |
862 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
792 | int numtype = grok_number (start, dec->cur - start, &uv); |
863 | if (*start == '-') |
793 | if (numtype & IS_NUMBER_IN_UV) |
864 | switch (dec->cur - start) |
794 | if (numtype & IS_NUMBER_NEG) |
|
|
795 | { |
865 | { |
796 | if (uv < (UV)IV_MIN) |
866 | case 2: return newSViv (-( start [1] - '0' )); |
797 | return newSViv (-(IV)uv); |
867 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
868 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
869 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
798 | } |
870 | } |
|
|
871 | else |
|
|
872 | switch (dec->cur - start) |
|
|
873 | { |
|
|
874 | case 1: return newSViv ( start [0] - '0' ); |
|
|
875 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
876 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
877 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
878 | } |
|
|
879 | |
|
|
880 | { |
|
|
881 | UV uv; |
|
|
882 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
883 | if (numtype & IS_NUMBER_IN_UV) |
|
|
884 | if (numtype & IS_NUMBER_NEG) |
|
|
885 | { |
|
|
886 | if (uv < (UV)IV_MIN) |
|
|
887 | return newSViv (-(IV)uv); |
|
|
888 | } |
799 | else |
889 | else |
800 | return newSVuv (uv); |
890 | return newSVuv (uv); |
|
|
891 | } |
801 | } |
892 | } |
802 | |
893 | |
803 | return newSVnv (Atof (start)); |
894 | return newSVnv (Atof (start)); |
804 | |
895 | |
805 | fail: |
896 | fail: |
… | |
… | |
953 | ERR ("'null' expected"); |
1044 | ERR ("'null' expected"); |
954 | |
1045 | |
955 | break; |
1046 | break; |
956 | |
1047 | |
957 | default: |
1048 | default: |
958 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1049 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
959 | break; |
1050 | break; |
960 | } |
1051 | } |
961 | |
1052 | |
962 | fail: |
1053 | fail: |
963 | return 0; |
1054 | return 0; |
964 | } |
1055 | } |
965 | |
1056 | |
966 | static SV * |
1057 | static SV * |
967 | decode_json (SV *string, U32 flags) |
1058 | decode_json (SV *string, U32 flags, UV *offset_return) |
968 | { |
1059 | { |
|
|
1060 | dec_t dec; |
|
|
1061 | UV offset; |
969 | SV *sv; |
1062 | SV *sv; |
|
|
1063 | |
|
|
1064 | SvGETMAGIC (string); |
|
|
1065 | SvUPGRADE (string, SVt_PV); |
970 | |
1066 | |
971 | if (flags & F_UTF8) |
1067 | if (flags & F_UTF8) |
972 | sv_utf8_downgrade (string, 0); |
1068 | sv_utf8_downgrade (string, 0); |
973 | else |
1069 | else |
974 | sv_utf8_upgrade (string); |
1070 | sv_utf8_upgrade (string); |
975 | |
1071 | |
976 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1072 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
977 | |
1073 | |
978 | dec_t dec; |
|
|
979 | dec.flags = flags; |
1074 | dec.flags = flags; |
980 | dec.cur = SvPVX (string); |
1075 | dec.cur = SvPVX (string); |
981 | dec.end = SvEND (string); |
1076 | dec.end = SvEND (string); |
982 | dec.err = 0; |
1077 | dec.err = 0; |
983 | dec.depth = 0; |
1078 | dec.depth = 0; |
984 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1079 | dec.maxdepth = DEC_DEPTH (dec.flags); |
985 | |
1080 | |
986 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1081 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
987 | sv = decode_sv (&dec); |
1082 | sv = decode_sv (&dec); |
988 | |
1083 | |
|
|
1084 | if (!(offset_return || !sv)) |
|
|
1085 | { |
|
|
1086 | // check for trailing garbage |
|
|
1087 | decode_ws (&dec); |
|
|
1088 | |
|
|
1089 | if (*dec.cur) |
|
|
1090 | { |
|
|
1091 | dec.err = "garbage after JSON object"; |
|
|
1092 | SvREFCNT_dec (sv); |
|
|
1093 | sv = 0; |
|
|
1094 | } |
|
|
1095 | } |
|
|
1096 | |
|
|
1097 | if (offset_return || !sv) |
|
|
1098 | { |
|
|
1099 | offset = dec.flags & F_UTF8 |
|
|
1100 | ? dec.cur - SvPVX (string) |
|
|
1101 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1102 | |
|
|
1103 | if (offset_return) |
|
|
1104 | *offset_return = offset; |
|
|
1105 | } |
|
|
1106 | |
989 | if (!sv) |
1107 | if (!sv) |
990 | { |
1108 | { |
991 | IV offset = dec.flags & F_UTF8 |
|
|
992 | ? dec.cur - SvPVX (string) |
|
|
993 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
994 | SV *uni = sv_newmortal (); |
1109 | SV *uni = sv_newmortal (); |
995 | |
1110 | |
996 | // horrible hack to silence warning inside pv_uni_display |
1111 | // horrible hack to silence warning inside pv_uni_display |
997 | COP cop = *PL_curcop; |
1112 | COP cop = *PL_curcop; |
998 | cop.cop_warnings = pWARN_NONE; |
1113 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1000 | SAVEVPTR (PL_curcop); |
1115 | SAVEVPTR (PL_curcop); |
1001 | PL_curcop = &cop; |
1116 | PL_curcop = &cop; |
1002 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1117 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1003 | LEAVE; |
1118 | LEAVE; |
1004 | |
1119 | |
1005 | croak ("%s, at character offset %d (%s)", |
1120 | croak ("%s, at character offset %d [\"%s\"]", |
1006 | dec.err, |
1121 | dec.err, |
1007 | (int)offset, |
1122 | (int)offset, |
1008 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1123 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1009 | } |
1124 | } |
1010 | |
1125 | |
… | |
… | |
1046 | RETVAL |
1161 | RETVAL |
1047 | |
1162 | |
1048 | SV *ascii (SV *self, int enable = 1) |
1163 | SV *ascii (SV *self, int enable = 1) |
1049 | ALIAS: |
1164 | ALIAS: |
1050 | ascii = F_ASCII |
1165 | ascii = F_ASCII |
|
|
1166 | latin1 = F_LATIN1 |
1051 | utf8 = F_UTF8 |
1167 | utf8 = F_UTF8 |
1052 | indent = F_INDENT |
1168 | indent = F_INDENT |
1053 | canonical = F_CANONICAL |
1169 | canonical = F_CANONICAL |
1054 | space_before = F_SPACE_BEFORE |
1170 | space_before = F_SPACE_BEFORE |
1055 | space_after = F_SPACE_AFTER |
1171 | space_after = F_SPACE_AFTER |
… | |
… | |
1067 | RETVAL = newSVsv (self); |
1183 | RETVAL = newSVsv (self); |
1068 | } |
1184 | } |
1069 | OUTPUT: |
1185 | OUTPUT: |
1070 | RETVAL |
1186 | RETVAL |
1071 | |
1187 | |
1072 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1188 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1073 | CODE: |
1189 | CODE: |
1074 | { |
1190 | { |
1075 | UV *uv = SvJSON (self); |
1191 | UV *uv = SvJSON (self); |
1076 | UV log2 = 0; |
1192 | UV log2 = 0; |
1077 | |
1193 | |
… | |
… | |
1091 | PPCODE: |
1207 | PPCODE: |
1092 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1208 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1093 | |
1209 | |
1094 | void decode (SV *self, SV *jsonstr) |
1210 | void decode (SV *self, SV *jsonstr) |
1095 | PPCODE: |
1211 | PPCODE: |
1096 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1212 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1213 | |
|
|
1214 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1215 | PPCODE: |
|
|
1216 | { |
|
|
1217 | UV offset; |
|
|
1218 | EXTEND (SP, 2); |
|
|
1219 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1220 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1221 | } |
1097 | |
1222 | |
1098 | PROTOTYPES: ENABLE |
1223 | PROTOTYPES: ENABLE |
1099 | |
1224 | |
1100 | void to_json (SV *scalar) |
1225 | void to_json (SV *scalar) |
1101 | ALIAS: |
1226 | ALIAS: |
… | |
… | |
1105 | |
1230 | |
1106 | void from_json (SV *jsonstr) |
1231 | void from_json (SV *jsonstr) |
1107 | ALIAS: |
1232 | ALIAS: |
1108 | jsonToObj = 0 |
1233 | jsonToObj = 0 |
1109 | PPCODE: |
1234 | PPCODE: |
1110 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1235 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1111 | |
1236 | |