… | |
… | |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
8 | #include "stdio.h" |
8 | #include "stdio.h" |
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 |
|
|
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 |
12 | #endif |
18 | #endif |
13 | |
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 |
… | |
… | |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
38 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
39 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
40 | #define INIT_SIZE 32 // initial scalar size to be allocated |
35 | #define INDENT_STEP 3 // spaces per indentation level |
41 | #define INDENT_STEP 3 // spaces per indentation level |
36 | |
42 | |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
43 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
38 | |
44 | |
39 | #define SB do { |
45 | #define SB do { |
40 | #define SE } while (0) |
46 | #define SE } while (0) |
41 | |
47 | |
|
|
48 | #if __GNUC__ >= 3 |
|
|
49 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
50 | # define inline inline |
|
|
51 | #else |
|
|
52 | # define expect(expr,value) (expr) |
|
|
53 | # define inline static |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
57 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
58 | |
42 | static HV *json_stash; // JSON::XS:: |
59 | static HV *json_stash; // JSON::XS:: |
|
|
60 | static SV *json_true, *json_false; |
43 | |
61 | |
44 | ///////////////////////////////////////////////////////////////////////////// |
62 | ///////////////////////////////////////////////////////////////////////////// |
45 | // utility functions |
63 | // utility functions |
46 | |
64 | |
47 | static UV * |
65 | static UV * |
… | |
… | |
70 | // decode an utf-8 character and return it, or (UV)-1 in |
88 | // decode an utf-8 character and return it, or (UV)-1 in |
71 | // case of an error. |
89 | // case of an error. |
72 | // we special-case "safe" characters from U+80 .. U+7FF, |
90 | // we special-case "safe" characters from U+80 .. U+7FF, |
73 | // but use the very good perl function to parse anything else. |
91 | // but use the very good perl function to parse anything else. |
74 | // note that we never call this function for a ascii codepoints |
92 | // note that we never call this function for a ascii codepoints |
75 | static UV |
93 | inline UV |
76 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
94 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
77 | { |
95 | { |
78 | if (s[0] > 0xdf || s[0] < 0xc2) |
96 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
79 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
97 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
80 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
98 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
81 | { |
99 | { |
82 | *clen = 2; |
100 | *clen = 2; |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
101 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
… | |
… | |
101 | U32 flags; // F_* |
119 | U32 flags; // F_* |
102 | U32 indent; // indentation level |
120 | U32 indent; // indentation level |
103 | U32 maxdepth; // max. indentation/recursion level |
121 | U32 maxdepth; // max. indentation/recursion level |
104 | } enc_t; |
122 | } enc_t; |
105 | |
123 | |
106 | static void |
124 | inline void |
107 | need (enc_t *enc, STRLEN len) |
125 | need (enc_t *enc, STRLEN len) |
108 | { |
126 | { |
109 | if (enc->cur + len >= enc->end) |
127 | if (expect_false (enc->cur + len >= enc->end)) |
110 | { |
128 | { |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
129 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
112 | SvGROW (enc->sv, cur + len + 1); |
130 | SvGROW (enc->sv, cur + len + 1); |
113 | enc->cur = SvPVX (enc->sv) + cur; |
131 | enc->cur = SvPVX (enc->sv) + cur; |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
132 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
115 | } |
133 | } |
116 | } |
134 | } |
117 | |
135 | |
118 | static void |
136 | inline void |
119 | encode_ch (enc_t *enc, char ch) |
137 | encode_ch (enc_t *enc, char ch) |
120 | { |
138 | { |
121 | need (enc, 1); |
139 | need (enc, 1); |
122 | *enc->cur++ = ch; |
140 | *enc->cur++ = ch; |
123 | } |
141 | } |
… | |
… | |
131 | |
149 | |
132 | while (str < end) |
150 | while (str < end) |
133 | { |
151 | { |
134 | unsigned char ch = *(unsigned char *)str; |
152 | unsigned char ch = *(unsigned char *)str; |
135 | |
153 | |
136 | if (ch >= 0x20 && ch < 0x80) // most common case |
154 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
137 | { |
155 | { |
138 | if (ch == '"') // but with slow exceptions |
156 | if (expect_false (ch == '"')) // but with slow exceptions |
139 | { |
157 | { |
140 | need (enc, len += 1); |
158 | need (enc, len += 1); |
141 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
142 | *enc->cur++ = '"'; |
160 | *enc->cur++ = '"'; |
143 | } |
161 | } |
144 | else if (ch == '\\') |
162 | else if (expect_false (ch == '\\')) |
145 | { |
163 | { |
146 | need (enc, len += 1); |
164 | need (enc, len += 1); |
147 | *enc->cur++ = '\\'; |
165 | *enc->cur++ = '\\'; |
148 | *enc->cur++ = '\\'; |
166 | *enc->cur++ = '\\'; |
149 | } |
167 | } |
… | |
… | |
167 | STRLEN clen; |
185 | STRLEN clen; |
168 | UV uch; |
186 | UV uch; |
169 | |
187 | |
170 | if (is_utf8) |
188 | if (is_utf8) |
171 | { |
189 | { |
172 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
173 | uch = decode_utf8 (str, end - str, &clen); |
190 | uch = decode_utf8 (str, end - str, &clen); |
174 | if (clen == (STRLEN)-1) |
191 | if (clen == (STRLEN)-1) |
175 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
192 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
176 | } |
193 | } |
177 | else |
194 | else |
… | |
… | |
233 | |
250 | |
234 | --len; |
251 | --len; |
235 | } |
252 | } |
236 | } |
253 | } |
237 | |
254 | |
238 | static void |
255 | inline void |
239 | encode_indent (enc_t *enc) |
256 | encode_indent (enc_t *enc) |
240 | { |
257 | { |
241 | if (enc->flags & F_INDENT) |
258 | if (enc->flags & F_INDENT) |
242 | { |
259 | { |
243 | int spaces = enc->indent * INDENT_STEP; |
260 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
246 | memset (enc->cur, ' ', spaces); |
263 | memset (enc->cur, ' ', spaces); |
247 | enc->cur += spaces; |
264 | enc->cur += spaces; |
248 | } |
265 | } |
249 | } |
266 | } |
250 | |
267 | |
251 | static void |
268 | inline void |
252 | encode_space (enc_t *enc) |
269 | encode_space (enc_t *enc) |
253 | { |
270 | { |
254 | need (enc, 1); |
271 | need (enc, 1); |
255 | encode_ch (enc, ' '); |
272 | encode_ch (enc, ' '); |
256 | } |
273 | } |
257 | |
274 | |
258 | static void |
275 | inline void |
259 | encode_nl (enc_t *enc) |
276 | encode_nl (enc_t *enc) |
260 | { |
277 | { |
261 | if (enc->flags & F_INDENT) |
278 | if (enc->flags & F_INDENT) |
262 | { |
279 | { |
263 | need (enc, 1); |
280 | need (enc, 1); |
264 | encode_ch (enc, '\n'); |
281 | encode_ch (enc, '\n'); |
265 | } |
282 | } |
266 | } |
283 | } |
267 | |
284 | |
268 | static void |
285 | inline void |
269 | encode_comma (enc_t *enc) |
286 | encode_comma (enc_t *enc) |
270 | { |
287 | { |
271 | encode_ch (enc, ','); |
288 | encode_ch (enc, ','); |
272 | |
289 | |
273 | if (enc->flags & F_INDENT) |
290 | if (enc->flags & F_INDENT) |
… | |
… | |
372 | // actually, this is mostly due to the stupid so-called |
389 | // actually, this is mostly due to the stupid so-called |
373 | // security workaround added somewhere in 5.8.x. |
390 | // security workaround added somewhere in 5.8.x. |
374 | // that randomises hash orderings |
391 | // that randomises hash orderings |
375 | if (enc->flags & F_CANONICAL) |
392 | if (enc->flags & F_CANONICAL) |
376 | { |
393 | { |
377 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
378 | int fast = 1; |
394 | int fast = 1; |
|
|
395 | HE *he; |
|
|
396 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
397 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
398 | #else |
|
|
399 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
400 | #endif |
379 | |
401 | |
380 | i = 0; |
402 | i = 0; |
381 | while ((he = hv_iternext (hv))) |
403 | while ((he = hv_iternext (hv))) |
382 | { |
404 | { |
383 | hes [i++] = he; |
405 | hes [i++] = he; |
… | |
… | |
481 | encode_str (enc, str, len, SvUTF8 (sv)); |
503 | encode_str (enc, str, len, SvUTF8 (sv)); |
482 | encode_ch (enc, '"'); |
504 | encode_ch (enc, '"'); |
483 | } |
505 | } |
484 | else if (SvNOKp (sv)) |
506 | else if (SvNOKp (sv)) |
485 | { |
507 | { |
|
|
508 | // trust that perl will do the right thing w.r.t. JSON syntax. |
486 | need (enc, NV_DIG + 32); |
509 | need (enc, NV_DIG + 32); |
487 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
510 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
488 | enc->cur += strlen (enc->cur); |
511 | enc->cur += strlen (enc->cur); |
489 | } |
512 | } |
490 | else if (SvIOKp (sv)) |
513 | else if (SvIOKp (sv)) |
491 | { |
514 | { |
492 | need (enc, 64); |
515 | // we assume we can always read an IV as a UV |
|
|
516 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
517 | { |
|
|
518 | // large integer, use the (rather slow) snprintf way. |
|
|
519 | need (enc, sizeof (UV) * 3); |
493 | enc->cur += |
520 | enc->cur += |
494 | SvIsUV(sv) |
521 | SvIsUV(sv) |
495 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
522 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
496 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
523 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
524 | } |
|
|
525 | else |
|
|
526 | { |
|
|
527 | // optimise the "small number case" |
|
|
528 | // code will likely be branchless and use only a single multiplication |
|
|
529 | I32 i = SvIV (sv); |
|
|
530 | U32 u; |
|
|
531 | char digit, nz = 0; |
|
|
532 | |
|
|
533 | need (enc, 6); |
|
|
534 | |
|
|
535 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
536 | u = i < 0 ? -i : i; |
|
|
537 | |
|
|
538 | // convert to 4.28 fixed-point representation |
|
|
539 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
540 | |
|
|
541 | // now output digit by digit, each time masking out the integer part |
|
|
542 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
543 | // resulting in a net multiplication by 10. |
|
|
544 | // we always write the digit to memory but conditionally increment |
|
|
545 | // the pointer, to ease the usage of conditional move instructions. |
|
|
546 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
547 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
548 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
549 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
550 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
551 | } |
497 | } |
552 | } |
498 | else if (SvROK (sv)) |
553 | else if (SvROK (sv)) |
499 | encode_rv (enc, SvRV (sv)); |
554 | encode_rv (enc, SvRV (sv)); |
500 | else if (!SvOK (sv)) |
555 | else if (!SvOK (sv)) |
501 | encode_str (enc, "null", 4, 0); |
556 | encode_str (enc, "null", 4, 0); |
… | |
… | |
546 | U32 flags; // F_* |
601 | U32 flags; // F_* |
547 | U32 depth; // recursion depth |
602 | U32 depth; // recursion depth |
548 | U32 maxdepth; // recursion depth limit |
603 | U32 maxdepth; // recursion depth limit |
549 | } dec_t; |
604 | } dec_t; |
550 | |
605 | |
551 | static void |
606 | inline void |
552 | decode_ws (dec_t *dec) |
607 | decode_ws (dec_t *dec) |
553 | { |
608 | { |
554 | for (;;) |
609 | for (;;) |
555 | { |
610 | { |
556 | char ch = *dec->cur; |
611 | char ch = *dec->cur; |
… | |
… | |
582 | decode_4hex (dec_t *dec) |
637 | decode_4hex (dec_t *dec) |
583 | { |
638 | { |
584 | signed char d1, d2, d3, d4; |
639 | signed char d1, d2, d3, d4; |
585 | unsigned char *cur = (unsigned char *)dec->cur; |
640 | unsigned char *cur = (unsigned char *)dec->cur; |
586 | |
641 | |
587 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
642 | 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"); |
643 | 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"); |
644 | 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"); |
645 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
591 | |
646 | |
592 | dec->cur += 4; |
647 | dec->cur += 4; |
593 | |
648 | |
594 | return ((UV)d1) << 12 |
649 | return ((UV)d1) << 12 |
595 | | ((UV)d2) << 8 |
650 | | ((UV)d2) << 8 |
… | |
… | |
603 | static SV * |
658 | static SV * |
604 | decode_str (dec_t *dec) |
659 | decode_str (dec_t *dec) |
605 | { |
660 | { |
606 | SV *sv = 0; |
661 | SV *sv = 0; |
607 | int utf8 = 0; |
662 | int utf8 = 0; |
|
|
663 | char *dec_cur = dec->cur; |
608 | |
664 | |
609 | do |
665 | do |
610 | { |
666 | { |
611 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
667 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
612 | char *cur = buf; |
668 | char *cur = buf; |
613 | |
669 | |
614 | do |
670 | do |
615 | { |
671 | { |
616 | unsigned char ch = *(unsigned char *)dec->cur++; |
672 | unsigned char ch = *(unsigned char *)dec_cur++; |
617 | |
673 | |
618 | if (ch == '"') |
674 | if (expect_false (ch == '"')) |
619 | { |
675 | { |
620 | --dec->cur; |
676 | --dec_cur; |
621 | break; |
677 | break; |
622 | } |
678 | } |
623 | else if (ch == '\\') |
679 | else if (expect_false (ch == '\\')) |
624 | { |
680 | { |
625 | switch (*dec->cur) |
681 | switch (*dec_cur) |
626 | { |
682 | { |
627 | case '\\': |
683 | case '\\': |
628 | case '/': |
684 | case '/': |
629 | case '"': *cur++ = *dec->cur++; break; |
685 | case '"': *cur++ = *dec_cur++; break; |
630 | |
686 | |
631 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
687 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
632 | case 't': ++dec->cur; *cur++ = '\011'; break; |
688 | case 't': ++dec_cur; *cur++ = '\011'; break; |
633 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
689 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
634 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
690 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
635 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
691 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
636 | |
692 | |
637 | case 'u': |
693 | case 'u': |
638 | { |
694 | { |
639 | UV lo, hi; |
695 | UV lo, hi; |
640 | ++dec->cur; |
696 | ++dec_cur; |
641 | |
697 | |
|
|
698 | dec->cur = dec_cur; |
642 | hi = decode_4hex (dec); |
699 | hi = decode_4hex (dec); |
|
|
700 | dec_cur = dec->cur; |
643 | if (hi == (UV)-1) |
701 | if (hi == (UV)-1) |
644 | goto fail; |
702 | goto fail; |
645 | |
703 | |
646 | // possibly a surrogate pair |
704 | // possibly a surrogate pair |
647 | if (hi >= 0xd800) |
705 | if (hi >= 0xd800) |
648 | if (hi < 0xdc00) |
706 | if (hi < 0xdc00) |
649 | { |
707 | { |
650 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
708 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
651 | ERR ("missing low surrogate character in surrogate pair"); |
709 | ERR ("missing low surrogate character in surrogate pair"); |
652 | |
710 | |
653 | dec->cur += 2; |
711 | dec_cur += 2; |
654 | |
712 | |
|
|
713 | dec->cur = dec_cur; |
655 | lo = decode_4hex (dec); |
714 | lo = decode_4hex (dec); |
|
|
715 | dec_cur = dec->cur; |
656 | if (lo == (UV)-1) |
716 | if (lo == (UV)-1) |
657 | goto fail; |
717 | goto fail; |
658 | |
718 | |
659 | if (lo < 0xdc00 || lo >= 0xe000) |
719 | if (lo < 0xdc00 || lo >= 0xe000) |
660 | ERR ("surrogate pair expected"); |
720 | ERR ("surrogate pair expected"); |
… | |
… | |
674 | *cur++ = hi; |
734 | *cur++ = hi; |
675 | } |
735 | } |
676 | break; |
736 | break; |
677 | |
737 | |
678 | default: |
738 | default: |
679 | --dec->cur; |
739 | --dec_cur; |
680 | ERR ("illegal backslash escape sequence in string"); |
740 | ERR ("illegal backslash escape sequence in string"); |
681 | } |
741 | } |
682 | } |
742 | } |
683 | else if (ch >= 0x20 && ch <= 0x7f) |
743 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
684 | *cur++ = ch; |
744 | *cur++ = ch; |
685 | else if (ch >= 0x80) |
745 | else if (ch >= 0x80) |
686 | { |
746 | { |
687 | STRLEN clen; |
747 | STRLEN clen; |
688 | UV uch; |
748 | UV uch; |
689 | |
749 | |
690 | --dec->cur; |
750 | --dec_cur; |
691 | |
751 | |
692 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
752 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
693 | if (clen == (STRLEN)-1) |
753 | if (clen == (STRLEN)-1) |
694 | ERR ("malformed UTF-8 character in JSON string"); |
754 | ERR ("malformed UTF-8 character in JSON string"); |
695 | |
755 | |
696 | do |
756 | do |
697 | *cur++ = *dec->cur++; |
757 | *cur++ = *dec_cur++; |
698 | while (--clen); |
758 | while (--clen); |
699 | |
759 | |
700 | utf8 = 1; |
760 | utf8 = 1; |
701 | } |
761 | } |
702 | else |
762 | else |
703 | { |
763 | { |
704 | --dec->cur; |
764 | --dec_cur; |
705 | |
765 | |
706 | if (!ch) |
766 | if (!ch) |
707 | ERR ("unexpected end of string while parsing JSON string"); |
767 | ERR ("unexpected end of string while parsing JSON string"); |
708 | else |
768 | else |
709 | ERR ("invalid character encountered while parsing JSON string"); |
769 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
722 | } |
782 | } |
723 | else |
783 | else |
724 | sv = newSVpvn (buf, len); |
784 | sv = newSVpvn (buf, len); |
725 | } |
785 | } |
726 | } |
786 | } |
727 | while (*dec->cur != '"'); |
787 | while (*dec_cur != '"'); |
728 | |
788 | |
729 | ++dec->cur; |
789 | ++dec_cur; |
730 | |
790 | |
731 | if (sv) |
791 | if (sv) |
732 | { |
792 | { |
733 | SvPOK_only (sv); |
793 | SvPOK_only (sv); |
734 | *SvEND (sv) = 0; |
794 | *SvEND (sv) = 0; |
… | |
… | |
737 | SvUTF8_on (sv); |
797 | SvUTF8_on (sv); |
738 | } |
798 | } |
739 | else |
799 | else |
740 | sv = newSVpvn ("", 0); |
800 | sv = newSVpvn ("", 0); |
741 | |
801 | |
|
|
802 | dec->cur = dec_cur; |
742 | return sv; |
803 | return sv; |
743 | |
804 | |
744 | fail: |
805 | fail: |
|
|
806 | dec->cur = dec_cur; |
745 | return 0; |
807 | return 0; |
746 | } |
808 | } |
747 | |
809 | |
748 | static SV * |
810 | static SV * |
749 | decode_num (dec_t *dec) |
811 | decode_num (dec_t *dec) |
… | |
… | |
807 | is_nv = 1; |
869 | is_nv = 1; |
808 | } |
870 | } |
809 | |
871 | |
810 | if (!is_nv) |
872 | if (!is_nv) |
811 | { |
873 | { |
812 | UV uv; |
874 | // 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); |
875 | if (*start == '-') |
814 | if (numtype & IS_NUMBER_IN_UV) |
876 | switch (dec->cur - start) |
815 | if (numtype & IS_NUMBER_NEG) |
|
|
816 | { |
877 | { |
817 | if (uv < (UV)IV_MIN) |
878 | case 2: return newSViv (-( start [1] - '0' * 1)); |
818 | return newSViv (-(IV)uv); |
879 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
880 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
881 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
819 | } |
882 | } |
|
|
883 | else |
|
|
884 | switch (dec->cur - start) |
|
|
885 | { |
|
|
886 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
887 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
888 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
889 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
890 | } |
|
|
891 | |
|
|
892 | { |
|
|
893 | UV uv; |
|
|
894 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
895 | if (numtype & IS_NUMBER_IN_UV) |
|
|
896 | if (numtype & IS_NUMBER_NEG) |
|
|
897 | { |
|
|
898 | if (uv < (UV)IV_MIN) |
|
|
899 | return newSViv (-(IV)uv); |
|
|
900 | } |
820 | else |
901 | else |
821 | return newSVuv (uv); |
902 | return newSVuv (uv); |
|
|
903 | |
|
|
904 | // here would likely be the place for bigint support |
822 | } |
905 | } |
|
|
906 | } |
823 | |
907 | |
|
|
908 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
824 | return newSVnv (Atof (start)); |
909 | return newSVnv (Atof (start)); |
825 | |
910 | |
826 | fail: |
911 | fail: |
827 | return 0; |
912 | return 0; |
828 | } |
913 | } |
… | |
… | |
929 | |
1014 | |
930 | static SV * |
1015 | static SV * |
931 | decode_sv (dec_t *dec) |
1016 | decode_sv (dec_t *dec) |
932 | { |
1017 | { |
933 | decode_ws (dec); |
1018 | decode_ws (dec); |
|
|
1019 | |
|
|
1020 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1021 | // to parse anything. |
934 | switch (*dec->cur) |
1022 | switch (*dec->cur) |
935 | { |
1023 | { |
936 | case '"': ++dec->cur; return decode_str (dec); |
1024 | case '"': ++dec->cur; return decode_str (dec); |
937 | case '[': ++dec->cur; return decode_av (dec); |
1025 | case '[': ++dec->cur; return decode_av (dec); |
938 | case '{': ++dec->cur; return decode_hv (dec); |
1026 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
944 | |
1032 | |
945 | case 't': |
1033 | case 't': |
946 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1034 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
947 | { |
1035 | { |
948 | dec->cur += 4; |
1036 | dec->cur += 4; |
949 | return newSViv (1); |
1037 | return SvREFCNT_inc (json_true); |
950 | } |
1038 | } |
951 | else |
1039 | else |
952 | ERR ("'true' expected"); |
1040 | ERR ("'true' expected"); |
953 | |
1041 | |
954 | break; |
1042 | break; |
955 | |
1043 | |
956 | case 'f': |
1044 | case 'f': |
957 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1045 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
958 | { |
1046 | { |
959 | dec->cur += 5; |
1047 | dec->cur += 5; |
960 | return newSViv (0); |
1048 | return SvREFCNT_inc (json_false); |
961 | } |
1049 | } |
962 | else |
1050 | else |
963 | ERR ("'false' expected"); |
1051 | ERR ("'false' expected"); |
964 | |
1052 | |
965 | break; |
1053 | break; |
… | |
… | |
1068 | |
1156 | |
1069 | BOOT: |
1157 | BOOT: |
1070 | { |
1158 | { |
1071 | int i; |
1159 | int i; |
1072 | |
1160 | |
1073 | memset (decode_hexdigit, 0xff, 256); |
|
|
1074 | |
|
|
1075 | for (i = 0; i < 256; ++i) |
1161 | for (i = 0; i < 256; ++i) |
1076 | decode_hexdigit [i] = |
1162 | decode_hexdigit [i] = |
1077 | i >= '0' && i <= '9' ? i - '0' |
1163 | i >= '0' && i <= '9' ? i - '0' |
1078 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1164 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1079 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1165 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1080 | : -1; |
1166 | : -1; |
1081 | |
1167 | |
1082 | json_stash = gv_stashpv ("JSON::XS", 1); |
1168 | json_stash = gv_stashpv ("JSON::XS", 1); |
|
|
1169 | |
|
|
1170 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1171 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1083 | } |
1172 | } |
1084 | |
1173 | |
1085 | PROTOTYPES: DISABLE |
1174 | PROTOTYPES: DISABLE |
1086 | |
1175 | |
1087 | SV *new (char *dummy) |
1176 | SV *new (char *dummy) |