… | |
… | |
9 | |
9 | |
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 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
14 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
15 | #define F_LATIN1 0x00000002UL |
21 | #define F_LATIN1 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
22 | #define F_UTF8 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
23 | #define F_INDENT 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
24 | #define F_CANONICAL 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
23 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
24 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
25 | |
35 | |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
27 | |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
|
|
30 | |
38 | |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
41 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
35 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
36 | |
44 | |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
38 | |
46 | |
39 | #define SB do { |
47 | #define SB do { |
40 | #define SE } while (0) |
48 | #define SE } while (0) |
41 | |
49 | |
|
|
50 | #if __GNUC__ >= 3 |
|
|
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
52 | # define inline inline |
|
|
53 | #else |
|
|
54 | # define expect(expr,value) (expr) |
|
|
55 | # define inline static |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
60 | |
42 | static HV *json_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
43 | |
63 | |
44 | ///////////////////////////////////////////////////////////////////////////// |
64 | ///////////////////////////////////////////////////////////////////////////// |
45 | // utility functions |
65 | // utility functions |
46 | |
66 | |
47 | static UV * |
67 | static UV * |
… | |
… | |
70 | // decode an utf-8 character and return it, or (UV)-1 in |
90 | // decode an utf-8 character and return it, or (UV)-1 in |
71 | // case of an error. |
91 | // case of an error. |
72 | // we special-case "safe" characters from U+80 .. U+7FF, |
92 | // we special-case "safe" characters from U+80 .. U+7FF, |
73 | // but use the very good perl function to parse anything else. |
93 | // but use the very good perl function to parse anything else. |
74 | // note that we never call this function for a ascii codepoints |
94 | // note that we never call this function for a ascii codepoints |
75 | static UV |
95 | inline UV |
76 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
96 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
77 | { |
97 | { |
78 | if (s[0] > 0xdf || s[0] < 0xc2) |
98 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
79 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
99 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
80 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
100 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
81 | { |
101 | { |
82 | *clen = 2; |
102 | *clen = 2; |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
103 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
… | |
… | |
101 | U32 flags; // F_* |
121 | U32 flags; // F_* |
102 | U32 indent; // indentation level |
122 | U32 indent; // indentation level |
103 | U32 maxdepth; // max. indentation/recursion level |
123 | U32 maxdepth; // max. indentation/recursion level |
104 | } enc_t; |
124 | } enc_t; |
105 | |
125 | |
106 | static void |
126 | inline void |
107 | need (enc_t *enc, STRLEN len) |
127 | need (enc_t *enc, STRLEN len) |
108 | { |
128 | { |
109 | if (enc->cur + len >= enc->end) |
129 | if (expect_false (enc->cur + len >= enc->end)) |
110 | { |
130 | { |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
131 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
112 | SvGROW (enc->sv, cur + len + 1); |
132 | SvGROW (enc->sv, cur + len + 1); |
113 | enc->cur = SvPVX (enc->sv) + cur; |
133 | enc->cur = SvPVX (enc->sv) + cur; |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
134 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
115 | } |
135 | } |
116 | } |
136 | } |
117 | |
137 | |
118 | static void |
138 | inline void |
119 | encode_ch (enc_t *enc, char ch) |
139 | encode_ch (enc_t *enc, char ch) |
120 | { |
140 | { |
121 | need (enc, 1); |
141 | need (enc, 1); |
122 | *enc->cur++ = ch; |
142 | *enc->cur++ = ch; |
123 | } |
143 | } |
… | |
… | |
131 | |
151 | |
132 | while (str < end) |
152 | while (str < end) |
133 | { |
153 | { |
134 | unsigned char ch = *(unsigned char *)str; |
154 | unsigned char ch = *(unsigned char *)str; |
135 | |
155 | |
136 | if (ch >= 0x20 && ch < 0x80) // most common case |
156 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
137 | { |
157 | { |
138 | if (ch == '"') // but with slow exceptions |
158 | if (expect_false (ch == '"')) // but with slow exceptions |
139 | { |
159 | { |
140 | need (enc, len += 1); |
160 | need (enc, len += 1); |
141 | *enc->cur++ = '\\'; |
161 | *enc->cur++ = '\\'; |
142 | *enc->cur++ = '"'; |
162 | *enc->cur++ = '"'; |
143 | } |
163 | } |
144 | else if (ch == '\\') |
164 | else if (expect_false (ch == '\\')) |
145 | { |
165 | { |
146 | need (enc, len += 1); |
166 | need (enc, len += 1); |
147 | *enc->cur++ = '\\'; |
167 | *enc->cur++ = '\\'; |
148 | *enc->cur++ = '\\'; |
168 | *enc->cur++ = '\\'; |
149 | } |
169 | } |
… | |
… | |
167 | STRLEN clen; |
187 | STRLEN clen; |
168 | UV uch; |
188 | UV uch; |
169 | |
189 | |
170 | if (is_utf8) |
190 | if (is_utf8) |
171 | { |
191 | { |
172 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
173 | uch = decode_utf8 (str, end - str, &clen); |
192 | uch = decode_utf8 (str, end - str, &clen); |
174 | if (clen == (STRLEN)-1) |
193 | if (clen == (STRLEN)-1) |
175 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
194 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
176 | } |
195 | } |
177 | else |
196 | else |
… | |
… | |
233 | |
252 | |
234 | --len; |
253 | --len; |
235 | } |
254 | } |
236 | } |
255 | } |
237 | |
256 | |
238 | static void |
257 | inline void |
239 | encode_indent (enc_t *enc) |
258 | encode_indent (enc_t *enc) |
240 | { |
259 | { |
241 | if (enc->flags & F_INDENT) |
260 | if (enc->flags & F_INDENT) |
242 | { |
261 | { |
243 | int spaces = enc->indent * INDENT_STEP; |
262 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
246 | memset (enc->cur, ' ', spaces); |
265 | memset (enc->cur, ' ', spaces); |
247 | enc->cur += spaces; |
266 | enc->cur += spaces; |
248 | } |
267 | } |
249 | } |
268 | } |
250 | |
269 | |
251 | static void |
270 | inline void |
252 | encode_space (enc_t *enc) |
271 | encode_space (enc_t *enc) |
253 | { |
272 | { |
254 | need (enc, 1); |
273 | need (enc, 1); |
255 | encode_ch (enc, ' '); |
274 | encode_ch (enc, ' '); |
256 | } |
275 | } |
257 | |
276 | |
258 | static void |
277 | inline void |
259 | encode_nl (enc_t *enc) |
278 | encode_nl (enc_t *enc) |
260 | { |
279 | { |
261 | if (enc->flags & F_INDENT) |
280 | if (enc->flags & F_INDENT) |
262 | { |
281 | { |
263 | need (enc, 1); |
282 | need (enc, 1); |
264 | encode_ch (enc, '\n'); |
283 | encode_ch (enc, '\n'); |
265 | } |
284 | } |
266 | } |
285 | } |
267 | |
286 | |
268 | static void |
287 | inline void |
269 | encode_comma (enc_t *enc) |
288 | encode_comma (enc_t *enc) |
270 | { |
289 | { |
271 | encode_ch (enc, ','); |
290 | encode_ch (enc, ','); |
272 | |
291 | |
273 | if (enc->flags & F_INDENT) |
292 | if (enc->flags & F_INDENT) |
… | |
… | |
372 | // actually, this is mostly due to the stupid so-called |
391 | // actually, this is mostly due to the stupid so-called |
373 | // security workaround added somewhere in 5.8.x. |
392 | // security workaround added somewhere in 5.8.x. |
374 | // that randomises hash orderings |
393 | // that randomises hash orderings |
375 | if (enc->flags & F_CANONICAL) |
394 | if (enc->flags & F_CANONICAL) |
376 | { |
395 | { |
377 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
378 | int fast = 1; |
396 | int fast = 1; |
|
|
397 | HE *he; |
|
|
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
399 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
400 | #else |
|
|
401 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
402 | #endif |
379 | |
403 | |
380 | i = 0; |
404 | i = 0; |
381 | while ((he = hv_iternext (hv))) |
405 | while ((he = hv_iternext (hv))) |
382 | { |
406 | { |
383 | hes [i++] = he; |
407 | hes [i++] = he; |
… | |
… | |
447 | svtype svt; |
471 | svtype svt; |
448 | |
472 | |
449 | SvGETMAGIC (sv); |
473 | SvGETMAGIC (sv); |
450 | svt = SvTYPE (sv); |
474 | svt = SvTYPE (sv); |
451 | |
475 | |
|
|
476 | if (expect_false (SvOBJECT (sv))) |
|
|
477 | { |
|
|
478 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
479 | { |
|
|
480 | if (SvIV (sv) == 0) |
|
|
481 | encode_str (enc, "false", 5, 0); |
|
|
482 | else |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | } |
|
|
485 | else |
|
|
486 | { |
|
|
487 | #if 0 |
|
|
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
489 | { |
|
|
490 | // not yet |
|
|
491 | } |
|
|
492 | #endif |
|
|
493 | if (enc->flags & F_CONV_BLESSED) |
|
|
494 | { |
|
|
495 | // we re-bless the reference to get overload and other niceties right |
|
|
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
497 | |
|
|
498 | if (to_json) |
|
|
499 | { |
|
|
500 | dSP; |
|
|
501 | ENTER; |
|
|
502 | SAVETMPS; |
|
|
503 | PUSHMARK (SP); |
|
|
504 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
505 | |
|
|
506 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
507 | // check anyways. |
|
|
508 | PUTBACK; |
|
|
509 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
510 | SPAGAIN; |
|
|
511 | |
|
|
512 | encode_sv (enc, POPs); |
|
|
513 | |
|
|
514 | FREETMPS; |
|
|
515 | LEAVE; |
|
|
516 | } |
|
|
517 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
518 | encode_str (enc, "null", 4, 0); |
|
|
519 | else |
|
|
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
522 | } |
|
|
523 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
524 | encode_str (enc, "null", 4, 0); |
|
|
525 | else |
|
|
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
528 | } |
|
|
529 | } |
452 | if (svt == SVt_PVHV) |
530 | else if (svt == SVt_PVHV) |
453 | encode_hv (enc, (HV *)sv); |
531 | encode_hv (enc, (HV *)sv); |
454 | else if (svt == SVt_PVAV) |
532 | else if (svt == SVt_PVAV) |
455 | encode_av (enc, (AV *)sv); |
533 | encode_av (enc, (AV *)sv); |
456 | else if (svt < SVt_PVAV) |
534 | else if (svt < SVt_PVAV) |
457 | { |
535 | { |
… | |
… | |
481 | encode_str (enc, str, len, SvUTF8 (sv)); |
559 | encode_str (enc, str, len, SvUTF8 (sv)); |
482 | encode_ch (enc, '"'); |
560 | encode_ch (enc, '"'); |
483 | } |
561 | } |
484 | else if (SvNOKp (sv)) |
562 | else if (SvNOKp (sv)) |
485 | { |
563 | { |
|
|
564 | // trust that perl will do the right thing w.r.t. JSON syntax. |
486 | need (enc, NV_DIG + 32); |
565 | need (enc, NV_DIG + 32); |
487 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
566 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
488 | enc->cur += strlen (enc->cur); |
567 | enc->cur += strlen (enc->cur); |
489 | } |
568 | } |
490 | else if (SvIOKp (sv)) |
569 | else if (SvIOKp (sv)) |
491 | { |
570 | { |
492 | need (enc, 64); |
571 | // we assume we can always read an IV as a UV |
|
|
572 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
573 | { |
|
|
574 | // large integer, use the (rather slow) snprintf way. |
|
|
575 | need (enc, sizeof (UV) * 3); |
493 | enc->cur += |
576 | enc->cur += |
494 | SvIsUV(sv) |
577 | SvIsUV(sv) |
495 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
578 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
496 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
579 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
580 | } |
|
|
581 | else |
|
|
582 | { |
|
|
583 | // optimise the "small number case" |
|
|
584 | // code will likely be branchless and use only a single multiplication |
|
|
585 | I32 i = SvIV (sv); |
|
|
586 | U32 u; |
|
|
587 | char digit, nz = 0; |
|
|
588 | |
|
|
589 | need (enc, 6); |
|
|
590 | |
|
|
591 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
592 | u = i < 0 ? -i : i; |
|
|
593 | |
|
|
594 | // convert to 4.28 fixed-point representation |
|
|
595 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
596 | |
|
|
597 | // now output digit by digit, each time masking out the integer part |
|
|
598 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
599 | // resulting in a net multiplication by 10. |
|
|
600 | // we always write the digit to memory but conditionally increment |
|
|
601 | // the pointer, to ease the usage of conditional move instructions. |
|
|
602 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
603 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
604 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
605 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
606 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
607 | } |
497 | } |
608 | } |
498 | else if (SvROK (sv)) |
609 | else if (SvROK (sv)) |
499 | encode_rv (enc, SvRV (sv)); |
610 | encode_rv (enc, SvRV (sv)); |
500 | else if (!SvOK (sv)) |
611 | else if (!SvOK (sv)) |
501 | encode_str (enc, "null", 4, 0); |
612 | encode_str (enc, "null", 4, 0); |
… | |
… | |
546 | U32 flags; // F_* |
657 | U32 flags; // F_* |
547 | U32 depth; // recursion depth |
658 | U32 depth; // recursion depth |
548 | U32 maxdepth; // recursion depth limit |
659 | U32 maxdepth; // recursion depth limit |
549 | } dec_t; |
660 | } dec_t; |
550 | |
661 | |
551 | static void |
662 | inline void |
552 | decode_ws (dec_t *dec) |
663 | decode_ws (dec_t *dec) |
553 | { |
664 | { |
554 | for (;;) |
665 | for (;;) |
555 | { |
666 | { |
556 | char ch = *dec->cur; |
667 | char ch = *dec->cur; |
… | |
… | |
582 | decode_4hex (dec_t *dec) |
693 | decode_4hex (dec_t *dec) |
583 | { |
694 | { |
584 | signed char d1, d2, d3, d4; |
695 | signed char d1, d2, d3, d4; |
585 | unsigned char *cur = (unsigned char *)dec->cur; |
696 | unsigned char *cur = (unsigned char *)dec->cur; |
586 | |
697 | |
587 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
698 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
588 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
699 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
589 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
700 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
590 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
701 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
591 | |
702 | |
592 | dec->cur += 4; |
703 | dec->cur += 4; |
593 | |
704 | |
594 | return ((UV)d1) << 12 |
705 | return ((UV)d1) << 12 |
595 | | ((UV)d2) << 8 |
706 | | ((UV)d2) << 8 |
… | |
… | |
603 | static SV * |
714 | static SV * |
604 | decode_str (dec_t *dec) |
715 | decode_str (dec_t *dec) |
605 | { |
716 | { |
606 | SV *sv = 0; |
717 | SV *sv = 0; |
607 | int utf8 = 0; |
718 | int utf8 = 0; |
|
|
719 | char *dec_cur = dec->cur; |
608 | |
720 | |
609 | do |
721 | do |
610 | { |
722 | { |
611 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
723 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
612 | char *cur = buf; |
724 | char *cur = buf; |
613 | |
725 | |
614 | do |
726 | do |
615 | { |
727 | { |
616 | unsigned char ch = *(unsigned char *)dec->cur++; |
728 | unsigned char ch = *(unsigned char *)dec_cur++; |
617 | |
729 | |
618 | if (ch == '"') |
730 | if (expect_false (ch == '"')) |
619 | { |
731 | { |
620 | --dec->cur; |
732 | --dec_cur; |
621 | break; |
733 | break; |
622 | } |
734 | } |
623 | else if (ch == '\\') |
735 | else if (expect_false (ch == '\\')) |
624 | { |
736 | { |
625 | switch (*dec->cur) |
737 | switch (*dec_cur) |
626 | { |
738 | { |
627 | case '\\': |
739 | case '\\': |
628 | case '/': |
740 | case '/': |
629 | case '"': *cur++ = *dec->cur++; break; |
741 | case '"': *cur++ = *dec_cur++; break; |
630 | |
742 | |
631 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
743 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
632 | case 't': ++dec->cur; *cur++ = '\011'; break; |
744 | case 't': ++dec_cur; *cur++ = '\011'; break; |
633 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
745 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
634 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
746 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
635 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
747 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
636 | |
748 | |
637 | case 'u': |
749 | case 'u': |
638 | { |
750 | { |
639 | UV lo, hi; |
751 | UV lo, hi; |
640 | ++dec->cur; |
752 | ++dec_cur; |
641 | |
753 | |
|
|
754 | dec->cur = dec_cur; |
642 | hi = decode_4hex (dec); |
755 | hi = decode_4hex (dec); |
|
|
756 | dec_cur = dec->cur; |
643 | if (hi == (UV)-1) |
757 | if (hi == (UV)-1) |
644 | goto fail; |
758 | goto fail; |
645 | |
759 | |
646 | // possibly a surrogate pair |
760 | // possibly a surrogate pair |
647 | if (hi >= 0xd800) |
761 | if (hi >= 0xd800) |
648 | if (hi < 0xdc00) |
762 | if (hi < 0xdc00) |
649 | { |
763 | { |
650 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
764 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
651 | ERR ("missing low surrogate character in surrogate pair"); |
765 | ERR ("missing low surrogate character in surrogate pair"); |
652 | |
766 | |
653 | dec->cur += 2; |
767 | dec_cur += 2; |
654 | |
768 | |
|
|
769 | dec->cur = dec_cur; |
655 | lo = decode_4hex (dec); |
770 | lo = decode_4hex (dec); |
|
|
771 | dec_cur = dec->cur; |
656 | if (lo == (UV)-1) |
772 | if (lo == (UV)-1) |
657 | goto fail; |
773 | goto fail; |
658 | |
774 | |
659 | if (lo < 0xdc00 || lo >= 0xe000) |
775 | if (lo < 0xdc00 || lo >= 0xe000) |
660 | ERR ("surrogate pair expected"); |
776 | ERR ("surrogate pair expected"); |
… | |
… | |
674 | *cur++ = hi; |
790 | *cur++ = hi; |
675 | } |
791 | } |
676 | break; |
792 | break; |
677 | |
793 | |
678 | default: |
794 | default: |
679 | --dec->cur; |
795 | --dec_cur; |
680 | ERR ("illegal backslash escape sequence in string"); |
796 | ERR ("illegal backslash escape sequence in string"); |
681 | } |
797 | } |
682 | } |
798 | } |
683 | else if (ch >= 0x20 && ch <= 0x7f) |
799 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
684 | *cur++ = ch; |
800 | *cur++ = ch; |
685 | else if (ch >= 0x80) |
801 | else if (ch >= 0x80) |
686 | { |
802 | { |
687 | STRLEN clen; |
803 | STRLEN clen; |
688 | UV uch; |
804 | UV uch; |
689 | |
805 | |
690 | --dec->cur; |
806 | --dec_cur; |
691 | |
807 | |
692 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
808 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
693 | if (clen == (STRLEN)-1) |
809 | if (clen == (STRLEN)-1) |
694 | ERR ("malformed UTF-8 character in JSON string"); |
810 | ERR ("malformed UTF-8 character in JSON string"); |
695 | |
811 | |
696 | do |
812 | do |
697 | *cur++ = *dec->cur++; |
813 | *cur++ = *dec_cur++; |
698 | while (--clen); |
814 | while (--clen); |
699 | |
815 | |
700 | utf8 = 1; |
816 | utf8 = 1; |
701 | } |
817 | } |
702 | else |
818 | else |
703 | { |
819 | { |
704 | --dec->cur; |
820 | --dec_cur; |
705 | |
821 | |
706 | if (!ch) |
822 | if (!ch) |
707 | ERR ("unexpected end of string while parsing JSON string"); |
823 | ERR ("unexpected end of string while parsing JSON string"); |
708 | else |
824 | else |
709 | ERR ("invalid character encountered while parsing JSON string"); |
825 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
722 | } |
838 | } |
723 | else |
839 | else |
724 | sv = newSVpvn (buf, len); |
840 | sv = newSVpvn (buf, len); |
725 | } |
841 | } |
726 | } |
842 | } |
727 | while (*dec->cur != '"'); |
843 | while (*dec_cur != '"'); |
728 | |
844 | |
729 | ++dec->cur; |
845 | ++dec_cur; |
730 | |
846 | |
731 | if (sv) |
847 | if (sv) |
732 | { |
848 | { |
733 | SvPOK_only (sv); |
849 | SvPOK_only (sv); |
734 | *SvEND (sv) = 0; |
850 | *SvEND (sv) = 0; |
… | |
… | |
737 | SvUTF8_on (sv); |
853 | SvUTF8_on (sv); |
738 | } |
854 | } |
739 | else |
855 | else |
740 | sv = newSVpvn ("", 0); |
856 | sv = newSVpvn ("", 0); |
741 | |
857 | |
|
|
858 | dec->cur = dec_cur; |
742 | return sv; |
859 | return sv; |
743 | |
860 | |
744 | fail: |
861 | fail: |
|
|
862 | dec->cur = dec_cur; |
745 | return 0; |
863 | return 0; |
746 | } |
864 | } |
747 | |
865 | |
748 | static SV * |
866 | static SV * |
749 | decode_num (dec_t *dec) |
867 | decode_num (dec_t *dec) |
… | |
… | |
807 | is_nv = 1; |
925 | is_nv = 1; |
808 | } |
926 | } |
809 | |
927 | |
810 | if (!is_nv) |
928 | if (!is_nv) |
811 | { |
929 | { |
812 | UV uv; |
930 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
813 | int numtype = grok_number (start, dec->cur - start, &uv); |
931 | if (*start == '-') |
814 | if (numtype & IS_NUMBER_IN_UV) |
932 | switch (dec->cur - start) |
815 | if (numtype & IS_NUMBER_NEG) |
|
|
816 | { |
933 | { |
817 | if (uv < (UV)IV_MIN) |
934 | case 2: return newSViv (-( start [1] - '0' * 1)); |
818 | return newSViv (-(IV)uv); |
935 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
936 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
937 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
819 | } |
938 | } |
|
|
939 | else |
|
|
940 | switch (dec->cur - start) |
|
|
941 | { |
|
|
942 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
943 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
944 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
945 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
946 | } |
|
|
947 | |
|
|
948 | { |
|
|
949 | UV uv; |
|
|
950 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
951 | if (numtype & IS_NUMBER_IN_UV) |
|
|
952 | if (numtype & IS_NUMBER_NEG) |
|
|
953 | { |
|
|
954 | if (uv < (UV)IV_MIN) |
|
|
955 | return newSViv (-(IV)uv); |
|
|
956 | } |
820 | else |
957 | else |
821 | return newSVuv (uv); |
958 | return newSVuv (uv); |
|
|
959 | |
|
|
960 | // here would likely be the place for bigint support |
822 | } |
961 | } |
|
|
962 | } |
823 | |
963 | |
|
|
964 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
824 | return newSVnv (Atof (start)); |
965 | return newSVnv (Atof (start)); |
825 | |
966 | |
826 | fail: |
967 | fail: |
827 | return 0; |
968 | return 0; |
828 | } |
969 | } |
… | |
… | |
882 | if (*dec->cur == '}') |
1023 | if (*dec->cur == '}') |
883 | ++dec->cur; |
1024 | ++dec->cur; |
884 | else |
1025 | else |
885 | for (;;) |
1026 | for (;;) |
886 | { |
1027 | { |
887 | SV *key, *value; |
|
|
888 | |
|
|
889 | decode_ws (dec); EXPECT_CH ('"'); |
1028 | decode_ws (dec); EXPECT_CH ('"'); |
890 | |
1029 | |
891 | key = decode_str (dec); |
1030 | // heuristic: assume that |
892 | if (!key) |
1031 | // a) decode_str + hv_store_ent are abysmally slow |
893 | goto fail; |
1032 | // b) most hash keys are short, simple ascii text |
|
|
1033 | // so try to "fast-match" such strings to avoid |
|
|
1034 | // the overhead of hv_store_ent. |
|
|
1035 | { |
|
|
1036 | SV *value; |
|
|
1037 | char *p = dec->cur; |
|
|
1038 | char *e = p + 24; // only try up to 24 bytes |
894 | |
1039 | |
895 | decode_ws (dec); EXPECT_CH (':'); |
1040 | for (;;) |
896 | |
|
|
897 | value = decode_sv (dec); |
|
|
898 | if (!value) |
|
|
899 | { |
1041 | { |
|
|
1042 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1043 | { |
|
|
1044 | // slow path, back up and use decode_str |
|
|
1045 | SV *key = decode_str (dec); |
|
|
1046 | if (!key) |
|
|
1047 | goto fail; |
|
|
1048 | |
|
|
1049 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1050 | |
|
|
1051 | value = decode_sv (dec); |
|
|
1052 | if (!value) |
|
|
1053 | { |
|
|
1054 | SvREFCNT_dec (key); |
|
|
1055 | goto fail; |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | hv_store_ent (hv, key, value, 0); |
900 | SvREFCNT_dec (key); |
1059 | SvREFCNT_dec (key); |
|
|
1060 | |
|
|
1061 | break; |
|
|
1062 | } |
|
|
1063 | else if (*p == '"') |
|
|
1064 | { |
|
|
1065 | // fast path, got a simple key |
|
|
1066 | char *key = dec->cur; |
|
|
1067 | int len = p - key; |
|
|
1068 | dec->cur = p + 1; |
|
|
1069 | |
|
|
1070 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1071 | |
|
|
1072 | value = decode_sv (dec); |
|
|
1073 | if (!value) |
901 | goto fail; |
1074 | goto fail; |
|
|
1075 | |
|
|
1076 | hv_store (hv, key, len, value, 0); |
|
|
1077 | |
|
|
1078 | break; |
|
|
1079 | } |
|
|
1080 | |
|
|
1081 | ++p; |
902 | } |
1082 | } |
903 | |
1083 | } |
904 | hv_store_ent (hv, key, value, 0); |
|
|
905 | SvREFCNT_dec (key); |
|
|
906 | |
1084 | |
907 | decode_ws (dec); |
1085 | decode_ws (dec); |
908 | |
1086 | |
909 | if (*dec->cur == '}') |
1087 | if (*dec->cur == '}') |
910 | { |
1088 | { |
… | |
… | |
929 | |
1107 | |
930 | static SV * |
1108 | static SV * |
931 | decode_sv (dec_t *dec) |
1109 | decode_sv (dec_t *dec) |
932 | { |
1110 | { |
933 | decode_ws (dec); |
1111 | decode_ws (dec); |
|
|
1112 | |
|
|
1113 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1114 | // to parse anything. |
934 | switch (*dec->cur) |
1115 | switch (*dec->cur) |
935 | { |
1116 | { |
936 | case '"': ++dec->cur; return decode_str (dec); |
1117 | case '"': ++dec->cur; return decode_str (dec); |
937 | case '[': ++dec->cur; return decode_av (dec); |
1118 | case '[': ++dec->cur; return decode_av (dec); |
938 | case '{': ++dec->cur; return decode_hv (dec); |
1119 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
944 | |
1125 | |
945 | case 't': |
1126 | case 't': |
946 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1127 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
947 | { |
1128 | { |
948 | dec->cur += 4; |
1129 | dec->cur += 4; |
949 | return newSViv (1); |
1130 | return SvREFCNT_inc (json_true); |
950 | } |
1131 | } |
951 | else |
1132 | else |
952 | ERR ("'true' expected"); |
1133 | ERR ("'true' expected"); |
953 | |
1134 | |
954 | break; |
1135 | break; |
955 | |
1136 | |
956 | case 'f': |
1137 | case 'f': |
957 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1138 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
958 | { |
1139 | { |
959 | dec->cur += 5; |
1140 | dec->cur += 5; |
960 | return newSViv (0); |
1141 | return SvREFCNT_inc (json_false); |
961 | } |
1142 | } |
962 | else |
1143 | else |
963 | ERR ("'false' expected"); |
1144 | ERR ("'false' expected"); |
964 | |
1145 | |
965 | break; |
1146 | break; |
… | |
… | |
992 | SV *sv; |
1173 | SV *sv; |
993 | |
1174 | |
994 | SvGETMAGIC (string); |
1175 | SvGETMAGIC (string); |
995 | SvUPGRADE (string, SVt_PV); |
1176 | SvUPGRADE (string, SVt_PV); |
996 | |
1177 | |
|
|
1178 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
|
|
1179 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1180 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
|
|
1181 | |
997 | if (flags & F_UTF8) |
1182 | if (flags & F_UTF8) |
998 | sv_utf8_downgrade (string, 0); |
1183 | sv_utf8_downgrade (string, 0); |
999 | else |
1184 | else |
1000 | sv_utf8_upgrade (string); |
1185 | sv_utf8_upgrade (string); |
1001 | |
1186 | |
… | |
… | |
1009 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1194 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1010 | |
1195 | |
1011 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1196 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1012 | sv = decode_sv (&dec); |
1197 | sv = decode_sv (&dec); |
1013 | |
1198 | |
1014 | if (offset_return || !sv) |
1199 | if (!(offset_return || !sv)) |
1015 | { |
|
|
1016 | offset = dec.flags & F_UTF8 |
|
|
1017 | ? dec.cur - SvPVX (string) |
|
|
1018 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1019 | |
|
|
1020 | if (offset_return) |
|
|
1021 | *offset_return = offset; |
|
|
1022 | } |
|
|
1023 | else |
|
|
1024 | { |
1200 | { |
1025 | // check for trailing garbage |
1201 | // check for trailing garbage |
1026 | decode_ws (&dec); |
1202 | decode_ws (&dec); |
1027 | |
1203 | |
1028 | if (*dec.cur) |
1204 | if (*dec.cur) |
1029 | { |
1205 | { |
1030 | dec.err = "garbage after JSON object"; |
1206 | dec.err = "garbage after JSON object"; |
1031 | SvREFCNT_dec (sv); |
1207 | SvREFCNT_dec (sv); |
1032 | sv = 0; |
1208 | sv = 0; |
1033 | } |
1209 | } |
|
|
1210 | } |
|
|
1211 | |
|
|
1212 | if (offset_return || !sv) |
|
|
1213 | { |
|
|
1214 | offset = dec.flags & F_UTF8 |
|
|
1215 | ? dec.cur - SvPVX (string) |
|
|
1216 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1217 | |
|
|
1218 | if (offset_return) |
|
|
1219 | *offset_return = offset; |
1034 | } |
1220 | } |
1035 | |
1221 | |
1036 | if (!sv) |
1222 | if (!sv) |
1037 | { |
1223 | { |
1038 | SV *uni = sv_newmortal (); |
1224 | SV *uni = sv_newmortal (); |
… | |
… | |
1067 | |
1253 | |
1068 | BOOT: |
1254 | BOOT: |
1069 | { |
1255 | { |
1070 | int i; |
1256 | int i; |
1071 | |
1257 | |
1072 | memset (decode_hexdigit, 0xff, 256); |
|
|
1073 | |
|
|
1074 | for (i = 0; i < 256; ++i) |
1258 | for (i = 0; i < 256; ++i) |
1075 | decode_hexdigit [i] = |
1259 | decode_hexdigit [i] = |
1076 | i >= '0' && i <= '9' ? i - '0' |
1260 | i >= '0' && i <= '9' ? i - '0' |
1077 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1261 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1078 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1262 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1079 | : -1; |
1263 | : -1; |
1080 | |
1264 | |
1081 | json_stash = gv_stashpv ("JSON::XS", 1); |
1265 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1266 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1267 | |
|
|
1268 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1269 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1082 | } |
1270 | } |
1083 | |
1271 | |
1084 | PROTOTYPES: DISABLE |
1272 | PROTOTYPES: DISABLE |
1085 | |
1273 | |
1086 | SV *new (char *dummy) |
1274 | SV *new (char *dummy) |
… | |
… | |
1089 | OUTPUT: |
1277 | OUTPUT: |
1090 | RETVAL |
1278 | RETVAL |
1091 | |
1279 | |
1092 | SV *ascii (SV *self, int enable = 1) |
1280 | SV *ascii (SV *self, int enable = 1) |
1093 | ALIAS: |
1281 | ALIAS: |
1094 | ascii = F_ASCII |
1282 | ascii = F_ASCII |
1095 | latin1 = F_LATIN1 |
1283 | latin1 = F_LATIN1 |
1096 | utf8 = F_UTF8 |
1284 | utf8 = F_UTF8 |
1097 | indent = F_INDENT |
1285 | indent = F_INDENT |
1098 | canonical = F_CANONICAL |
1286 | canonical = F_CANONICAL |
1099 | space_before = F_SPACE_BEFORE |
1287 | space_before = F_SPACE_BEFORE |
1100 | space_after = F_SPACE_AFTER |
1288 | space_after = F_SPACE_AFTER |
1101 | pretty = F_PRETTY |
1289 | pretty = F_PRETTY |
1102 | allow_nonref = F_ALLOW_NONREF |
1290 | allow_nonref = F_ALLOW_NONREF |
1103 | shrink = F_SHRINK |
1291 | shrink = F_SHRINK |
|
|
1292 | allow_blessed = F_ALLOW_BLESSED |
|
|
1293 | convert_blessed = F_CONV_BLESSED |
1104 | CODE: |
1294 | CODE: |
1105 | { |
1295 | { |
1106 | UV *uv = SvJSON (self); |
1296 | UV *uv = SvJSON (self); |
1107 | if (enable) |
1297 | if (enable) |
1108 | *uv |= ix; |
1298 | *uv |= ix; |
… | |
… | |
1130 | RETVAL = newSVsv (self); |
1320 | RETVAL = newSVsv (self); |
1131 | } |
1321 | } |
1132 | OUTPUT: |
1322 | OUTPUT: |
1133 | RETVAL |
1323 | RETVAL |
1134 | |
1324 | |
|
|
1325 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1326 | CODE: |
|
|
1327 | { |
|
|
1328 | UV *uv = SvJSON (self); |
|
|
1329 | UV log2 = 0; |
|
|
1330 | |
|
|
1331 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1332 | if (max_size == 1) max_size = 2; |
|
|
1333 | |
|
|
1334 | while ((1UL << log2) < max_size) |
|
|
1335 | ++log2; |
|
|
1336 | |
|
|
1337 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1338 | |
|
|
1339 | RETVAL = newSVsv (self); |
|
|
1340 | } |
|
|
1341 | OUTPUT: |
|
|
1342 | RETVAL |
|
|
1343 | |
1135 | void encode (SV *self, SV *scalar) |
1344 | void encode (SV *self, SV *scalar) |
1136 | PPCODE: |
1345 | PPCODE: |
1137 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1346 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1138 | |
1347 | |
1139 | void decode (SV *self, SV *jsonstr) |
1348 | void decode (SV *self, SV *jsonstr) |