… | |
… | |
6 | #include <string.h> |
6 | #include <string.h> |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
|
|
11 | #include <inttypes.h> |
11 | |
12 | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
15 | #endif |
15 | |
16 | |
16 | // some old perls do not have this, try to make it work, no |
17 | // some old perls do not have this, try to make it work, no |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
19 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
20 | # define UTF8_MAXBYTES 13 |
|
|
21 | #endif |
|
|
22 | |
|
|
23 | // compatibility with perl <5.18 |
|
|
24 | #ifndef HvNAMELEN_get |
|
|
25 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
26 | #endif |
|
|
27 | #ifndef HvNAMELEN |
|
|
28 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
29 | #endif |
|
|
30 | #ifndef HvNAMEUTF8 |
|
|
31 | # define HvNAMEUTF8(hv) 0 |
20 | #endif |
32 | #endif |
21 | |
33 | |
22 | // three extra for rounding, sign, and end of string |
34 | // three extra for rounding, sign, and end of string |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
35 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
24 | |
36 | |
… | |
… | |
68 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
80 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
69 | |
81 | |
70 | #ifdef USE_ITHREADS |
82 | #ifdef USE_ITHREADS |
71 | # define JSON_SLOW 1 |
83 | # define JSON_SLOW 1 |
72 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
84 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
85 | # define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1)) |
73 | #else |
86 | #else |
74 | # define JSON_SLOW 0 |
87 | # define JSON_SLOW 0 |
75 | # define JSON_STASH json_stash |
88 | # define JSON_STASH json_stash |
|
|
89 | # define BOOL_STASH bool_stash |
76 | #endif |
90 | #endif |
77 | |
91 | |
78 | // the amount of HEs to allocate on the stack, when sorting keys |
92 | // the amount of HEs to allocate on the stack, when sorting keys |
79 | #define STACK_HES 64 |
93 | #define STACK_HES 64 |
80 | |
94 | |
81 | static HV *json_stash, *types_boolean_stash; // JSON::XS:: |
95 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
82 | static SV *types_true, *types_false, *sv_json; |
96 | static SV *bool_true, *bool_false, *sv_json; |
83 | |
97 | |
84 | enum { |
98 | enum { |
85 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
99 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
86 | INCR_M_STR, // inside string |
100 | INCR_M_STR, // inside string |
87 | INCR_M_BS, // inside backslash |
101 | INCR_M_BS, // inside backslash |
… | |
… | |
141 | SvPV_renew (sv, SvCUR (sv) + 1); |
155 | SvPV_renew (sv, SvCUR (sv) + 1); |
142 | #endif |
156 | #endif |
143 | } |
157 | } |
144 | } |
158 | } |
145 | |
159 | |
|
|
160 | /* adds two STRLENs together, slow, and with paranoia */ |
|
|
161 | STRLEN |
|
|
162 | strlen_sum (STRLEN l1, STRLEN l2) |
|
|
163 | { |
|
|
164 | size_t sum = l1 + l2; |
|
|
165 | |
|
|
166 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
|
|
167 | croak ("JSON::XS: string size overflow"); |
|
|
168 | |
|
|
169 | return sum; |
|
|
170 | } |
|
|
171 | |
|
|
172 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
|
|
173 | static char * |
|
|
174 | json_sv_grow (SV *sv, size_t len1, size_t len2) |
|
|
175 | { |
|
|
176 | len1 = strlen_sum (len1, len2); |
|
|
177 | len1 = strlen_sum (len1, len1 >> 1); |
|
|
178 | |
|
|
179 | if (len1 > 4096 - 24) |
|
|
180 | len1 = (len1 | 4095) - 24; |
|
|
181 | |
|
|
182 | return SvGROW (sv, len1); |
|
|
183 | } |
|
|
184 | |
146 | // decode an utf-8 character and return it, or (UV)-1 in |
185 | // decode a utf-8 character and return it, or (UV)-1 in |
147 | // case of an error. |
186 | // case of an error. |
148 | // we special-case "safe" characters from U+80 .. U+7FF, |
187 | // we special-case "safe" characters from U+80 .. U+7FF, |
149 | // but use the very good perl function to parse anything else. |
188 | // but use the very good perl function to parse anything else. |
150 | // note that we never call this function for a ascii codepoints |
189 | // note that we never call this function for a ascii codepoints |
151 | INLINE UV |
190 | INLINE UV |
… | |
… | |
288 | // a recursion depth of ten gives us >>500 bits |
327 | // a recursion depth of ten gives us >>500 bits |
289 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
328 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
290 | |
329 | |
291 | return neg ? -accum : accum; |
330 | return neg ? -accum : accum; |
292 | } |
331 | } |
|
|
332 | |
|
|
333 | // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true |
|
|
334 | static int |
|
|
335 | ref_bool_type (SV *sv) |
|
|
336 | { |
|
|
337 | svtype svt = SvTYPE (sv); |
|
|
338 | |
|
|
339 | if (svt < SVt_PVAV) |
|
|
340 | { |
|
|
341 | STRLEN len = 0; |
|
|
342 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
343 | |
|
|
344 | if (len == 1) |
|
|
345 | if (*pv == '1') |
|
|
346 | return 1; |
|
|
347 | else if (*pv == '0') |
|
|
348 | return 0; |
|
|
349 | } |
|
|
350 | |
|
|
351 | return -1; |
|
|
352 | } |
|
|
353 | |
|
|
354 | // returns whether scalar is not a reference in the sense of allow_nonref |
|
|
355 | static int |
|
|
356 | json_nonref (SV *scalar) |
|
|
357 | { |
|
|
358 | if (!SvROK (scalar)) |
|
|
359 | return 1; |
|
|
360 | |
|
|
361 | scalar = SvRV (scalar); |
|
|
362 | |
|
|
363 | if (SvTYPE (scalar) >= SVt_PVMG) |
|
|
364 | { |
|
|
365 | if (SvSTASH (scalar) == bool_stash) |
|
|
366 | return 1; |
|
|
367 | |
|
|
368 | if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
|
|
369 | return 1; |
|
|
370 | } |
|
|
371 | |
|
|
372 | return 0; |
|
|
373 | } |
|
|
374 | |
293 | ///////////////////////////////////////////////////////////////////////////// |
375 | ///////////////////////////////////////////////////////////////////////////// |
294 | // encoder |
376 | // encoder |
295 | |
377 | |
296 | // structure used for encoding JSON |
378 | // structure used for encoding JSON |
297 | typedef struct |
379 | typedef struct |
… | |
… | |
305 | } enc_t; |
387 | } enc_t; |
306 | |
388 | |
307 | INLINE void |
389 | INLINE void |
308 | need (enc_t *enc, STRLEN len) |
390 | need (enc_t *enc, STRLEN len) |
309 | { |
391 | { |
310 | if (expect_false (enc->cur + len >= enc->end)) |
392 | if (expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
311 | { |
393 | { |
312 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
394 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
313 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
395 | char *buf = json_sv_grow (enc->sv, cur, len); |
314 | enc->cur = SvPVX (enc->sv) + cur; |
396 | enc->cur = buf + cur; |
315 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
397 | enc->end = buf + SvLEN (enc->sv) - 1; |
316 | } |
398 | } |
317 | } |
399 | } |
318 | |
400 | |
319 | INLINE void |
401 | INLINE void |
320 | encode_ch (enc_t *enc, char ch) |
402 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
336 | |
418 | |
337 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
419 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
338 | { |
420 | { |
339 | if (expect_false (ch == '"')) // but with slow exceptions |
421 | if (expect_false (ch == '"')) // but with slow exceptions |
340 | { |
422 | { |
341 | need (enc, len += 1); |
423 | need (enc, len + 1); |
342 | *enc->cur++ = '\\'; |
424 | *enc->cur++ = '\\'; |
343 | *enc->cur++ = '"'; |
425 | *enc->cur++ = '"'; |
344 | } |
426 | } |
345 | else if (expect_false (ch == '\\')) |
427 | else if (expect_false (ch == '\\')) |
346 | { |
428 | { |
347 | need (enc, len += 1); |
429 | need (enc, len + 1); |
348 | *enc->cur++ = '\\'; |
430 | *enc->cur++ = '\\'; |
349 | *enc->cur++ = '\\'; |
431 | *enc->cur++ = '\\'; |
350 | } |
432 | } |
351 | else |
433 | else |
352 | *enc->cur++ = ch; |
434 | *enc->cur++ = ch; |
… | |
… | |
355 | } |
437 | } |
356 | else |
438 | else |
357 | { |
439 | { |
358 | switch (ch) |
440 | switch (ch) |
359 | { |
441 | { |
360 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
442 | case '\010': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
361 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
443 | case '\011': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
362 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
444 | case '\012': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
363 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
445 | case '\014': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
364 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
446 | case '\015': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
365 | |
447 | |
366 | default: |
448 | default: |
367 | { |
449 | { |
368 | STRLEN clen; |
450 | STRLEN clen; |
369 | UV uch; |
451 | UV uch; |
… | |
… | |
385 | if (uch >= 0x10000UL) |
467 | if (uch >= 0x10000UL) |
386 | { |
468 | { |
387 | if (uch >= 0x110000UL) |
469 | if (uch >= 0x110000UL) |
388 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
470 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
389 | |
471 | |
390 | need (enc, len += 11); |
472 | need (enc, len + 11); |
391 | sprintf (enc->cur, "\\u%04x\\u%04x", |
473 | sprintf (enc->cur, "\\u%04x\\u%04x", |
392 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
474 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
393 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
475 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
394 | enc->cur += 12; |
476 | enc->cur += 12; |
395 | } |
477 | } |
396 | else |
478 | else |
397 | { |
479 | { |
398 | need (enc, len += 5); |
480 | need (enc, len + 5); |
399 | *enc->cur++ = '\\'; |
481 | *enc->cur++ = '\\'; |
400 | *enc->cur++ = 'u'; |
482 | *enc->cur++ = 'u'; |
401 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
483 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
402 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
484 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
403 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
485 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
… | |
… | |
411 | *enc->cur++ = uch; |
493 | *enc->cur++ = uch; |
412 | str += clen; |
494 | str += clen; |
413 | } |
495 | } |
414 | else if (is_utf8) |
496 | else if (is_utf8) |
415 | { |
497 | { |
416 | need (enc, len += clen); |
498 | need (enc, len + clen); |
417 | do |
499 | do |
418 | { |
500 | { |
419 | *enc->cur++ = *str++; |
501 | *enc->cur++ = *str++; |
420 | } |
502 | } |
421 | while (--clen); |
503 | while (--clen); |
422 | } |
504 | } |
423 | else |
505 | else |
424 | { |
506 | { |
425 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
507 | need (enc, len + UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
426 | enc->cur = encode_utf8 (enc->cur, uch); |
508 | enc->cur = encode_utf8 (enc->cur, uch); |
427 | ++str; |
509 | ++str; |
428 | } |
510 | } |
429 | } |
511 | } |
430 | } |
512 | } |
… | |
… | |
690 | SvGETMAGIC (sv); |
772 | SvGETMAGIC (sv); |
691 | svt = SvTYPE (sv); |
773 | svt = SvTYPE (sv); |
692 | |
774 | |
693 | if (expect_false (SvOBJECT (sv))) |
775 | if (expect_false (SvOBJECT (sv))) |
694 | { |
776 | { |
695 | HV *boolean_stash = !JSON_SLOW || types_boolean_stash |
|
|
696 | ? types_boolean_stash |
|
|
697 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
698 | HV *stash = SvSTASH (sv); |
777 | HV *stash = SvSTASH (sv); |
699 | |
778 | |
700 | if (stash == boolean_stash) |
779 | if (stash == bool_stash) |
701 | { |
780 | { |
702 | if (SvIV (sv)) |
781 | if (SvIV (sv)) |
703 | encode_str (enc, "true", 4, 0); |
782 | encode_str (enc, "true", 4, 0); |
704 | else |
783 | else |
705 | encode_str (enc, "false", 5, 0); |
784 | encode_str (enc, "false", 5, 0); |
… | |
… | |
707 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
786 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
708 | { |
787 | { |
709 | int count; |
788 | int count; |
710 | dSP; |
789 | dSP; |
711 | |
790 | |
712 | ENTER; SAVETMPS; PUSHMARK (SP); |
791 | ENTER; SAVETMPS; |
|
|
792 | SAVESTACK_POS (); |
|
|
793 | PUSHMARK (SP); |
713 | EXTEND (SP, 2); |
794 | EXTEND (SP, 2); |
714 | // we re-bless the reference to get overload and other niceties right |
795 | // we re-bless the reference to get overload and other niceties right |
715 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
796 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
716 | PUSHs (sv_json); |
797 | PUSHs (sv_json); |
717 | |
798 | |
… | |
… | |
719 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
800 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
720 | SPAGAIN; |
801 | SPAGAIN; |
721 | |
802 | |
722 | // catch this surprisingly common error |
803 | // catch this surprisingly common error |
723 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
804 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
724 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
805 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
725 | |
806 | |
726 | encode_ch (enc, '('); |
807 | encode_ch (enc, '('); |
727 | encode_ch (enc, '"'); |
808 | encode_ch (enc, '"'); |
728 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
809 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
729 | encode_ch (enc, '"'); |
810 | encode_ch (enc, '"'); |
… | |
… | |
738 | encode_ch (enc, ','); |
819 | encode_ch (enc, ','); |
739 | } |
820 | } |
740 | |
821 | |
741 | encode_ch (enc, ']'); |
822 | encode_ch (enc, ']'); |
742 | |
823 | |
743 | PUTBACK; |
|
|
744 | |
|
|
745 | FREETMPS; LEAVE; |
824 | FREETMPS; LEAVE; |
746 | } |
825 | } |
747 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
826 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
748 | { |
827 | { |
749 | dSP; |
828 | dSP; |
750 | |
829 | |
751 | ENTER; SAVETMPS; PUSHMARK (SP); |
830 | ENTER; SAVETMPS; |
|
|
831 | PUSHMARK (SP); |
752 | // we re-bless the reference to get overload and other niceties right |
832 | // we re-bless the reference to get overload and other niceties right |
753 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
833 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
754 | |
834 | |
755 | // calling with G_SCALAR ensures that we always get a 1 return value |
835 | // calling with G_SCALAR ensures that we always get a 1 return value |
756 | PUTBACK; |
836 | PUTBACK; |
… | |
… | |
778 | encode_hv (enc, (HV *)sv); |
858 | encode_hv (enc, (HV *)sv); |
779 | else if (svt == SVt_PVAV) |
859 | else if (svt == SVt_PVAV) |
780 | encode_av (enc, (AV *)sv); |
860 | encode_av (enc, (AV *)sv); |
781 | else if (svt < SVt_PVAV) |
861 | else if (svt < SVt_PVAV) |
782 | { |
862 | { |
783 | STRLEN len = 0; |
863 | int bool_type = ref_bool_type (sv); |
784 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
785 | |
864 | |
786 | if (len == 1 && *pv == '1') |
865 | if (bool_type == 1) |
787 | encode_str (enc, "true", 4, 0); |
866 | encode_str (enc, "true", 4, 0); |
788 | else if (len == 1 && *pv == '0') |
867 | else if (bool_type == 0) |
789 | encode_str (enc, "false", 5, 0); |
868 | encode_str (enc, "false", 5, 0); |
790 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
869 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
791 | encode_str (enc, "null", 4, 0); |
870 | encode_str (enc, "null", 4, 0); |
792 | else |
871 | else |
793 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
872 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
… | |
… | |
876 | static SV * |
955 | static SV * |
877 | encode_json (SV *scalar, JSON *json) |
956 | encode_json (SV *scalar, JSON *json) |
878 | { |
957 | { |
879 | enc_t enc; |
958 | enc_t enc; |
880 | |
959 | |
881 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
960 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
882 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
961 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
883 | |
962 | |
884 | enc.json = *json; |
963 | enc.json = *json; |
885 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
964 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
886 | enc.cur = SvPVX (enc.sv); |
965 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1093 | *cur++ = *dec_cur++; |
1172 | *cur++ = *dec_cur++; |
1094 | while (--clen); |
1173 | while (--clen); |
1095 | |
1174 | |
1096 | utf8 = 1; |
1175 | utf8 = 1; |
1097 | } |
1176 | } |
|
|
1177 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1178 | *cur++ = ch; |
1098 | else |
1179 | else |
1099 | { |
1180 | { |
1100 | --dec_cur; |
1181 | --dec_cur; |
1101 | |
1182 | |
1102 | if (!ch) |
1183 | if (!ch) |
… | |
… | |
1112 | |
1193 | |
1113 | if (sv) |
1194 | if (sv) |
1114 | { |
1195 | { |
1115 | STRLEN cur = SvCUR (sv); |
1196 | STRLEN cur = SvCUR (sv); |
1116 | |
1197 | |
1117 | if (SvLEN (sv) <= cur + len) |
1198 | if (SvLEN (sv) - cur <= len) |
1118 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
1199 | json_sv_grow (sv, cur, len); |
1119 | |
1200 | |
1120 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1201 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1121 | SvCUR_set (sv, SvCUR (sv) + len); |
1202 | SvCUR_set (sv, SvCUR (sv) + len); |
1122 | } |
1203 | } |
1123 | else |
1204 | else |
… | |
… | |
1424 | |
1505 | |
1425 | hv_iterinit (hv); |
1506 | hv_iterinit (hv); |
1426 | he = hv_iternext (hv); |
1507 | he = hv_iternext (hv); |
1427 | hv_iterinit (hv); |
1508 | hv_iterinit (hv); |
1428 | |
1509 | |
1429 | // the next line creates a mortal sv each time its called. |
1510 | // the next line creates a mortal sv each time it's called. |
1430 | // might want to optimise this for common cases. |
1511 | // might want to optimise this for common cases. |
1431 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1512 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1432 | |
1513 | |
1433 | if (cb) |
1514 | if (cb) |
1434 | { |
1515 | { |
1435 | dSP; |
1516 | dSP; |
1436 | int count; |
1517 | int count; |
1437 | |
1518 | |
1438 | ENTER; SAVETMPS; PUSHMARK (SP); |
1519 | ENTER; SAVETMPS; |
|
|
1520 | SAVESTACK_POS (); |
|
|
1521 | PUSHMARK (SP); |
1439 | XPUSHs (HeVAL (he)); |
1522 | XPUSHs (HeVAL (he)); |
1440 | sv_2mortal (sv); |
1523 | sv_2mortal (sv); |
1441 | |
1524 | |
1442 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1525 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1443 | |
1526 | |
… | |
… | |
1456 | if (dec->json.cb_object) |
1539 | if (dec->json.cb_object) |
1457 | { |
1540 | { |
1458 | dSP; |
1541 | dSP; |
1459 | int count; |
1542 | int count; |
1460 | |
1543 | |
1461 | ENTER; SAVETMPS; PUSHMARK (SP); |
1544 | ENTER; SAVETMPS; |
|
|
1545 | SAVESTACK_POS (); |
|
|
1546 | PUSHMARK (SP); |
1462 | XPUSHs (sv_2mortal (sv)); |
1547 | XPUSHs (sv_2mortal (sv)); |
1463 | |
1548 | |
1464 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1549 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1465 | |
1550 | |
1466 | if (count == 1) |
1551 | if (count == 1) |
… | |
… | |
1533 | if (!method) |
1618 | if (!method) |
1534 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1619 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1535 | |
1620 | |
1536 | dSP; |
1621 | dSP; |
1537 | |
1622 | |
1538 | ENTER; SAVETMPS; PUSHMARK (SP); |
1623 | ENTER; SAVETMPS; |
|
|
1624 | PUSHMARK (SP); |
1539 | EXTEND (SP, len + 2); |
1625 | EXTEND (SP, len + 2); |
1540 | // we re-bless the reference to get overload and other niceties right |
1626 | // we re-bless the reference to get overload and other niceties right |
1541 | PUSHs (tag); |
1627 | PUSHs (tag); |
1542 | PUSHs (sv_json); |
1628 | PUSHs (sv_json); |
1543 | |
1629 | |
… | |
… | |
1585 | case 't': |
1671 | case 't': |
1586 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1672 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1587 | { |
1673 | { |
1588 | dec->cur += 4; |
1674 | dec->cur += 4; |
1589 | #if JSON_SLOW |
1675 | #if JSON_SLOW |
1590 | types_true = get_bool ("Types::Serialiser::true"); |
1676 | bool_true = get_bool ("Types::Serialiser::true"); |
1591 | #endif |
1677 | #endif |
1592 | return newSVsv (types_true); |
1678 | return newSVsv (bool_true); |
1593 | } |
1679 | } |
1594 | else |
1680 | else |
1595 | ERR ("'true' expected"); |
1681 | ERR ("'true' expected"); |
1596 | |
1682 | |
1597 | break; |
1683 | break; |
… | |
… | |
1599 | case 'f': |
1685 | case 'f': |
1600 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1686 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1601 | { |
1687 | { |
1602 | dec->cur += 5; |
1688 | dec->cur += 5; |
1603 | #if JSON_SLOW |
1689 | #if JSON_SLOW |
1604 | types_false = get_bool ("Types::Serialiser::false"); |
1690 | bool_false = get_bool ("Types::Serialiser::false"); |
1605 | #endif |
1691 | #endif |
1606 | return newSVsv (types_false); |
1692 | return newSVsv (bool_false); |
1607 | } |
1693 | } |
1608 | else |
1694 | else |
1609 | ERR ("'false' expected"); |
1695 | ERR ("'false' expected"); |
1610 | |
1696 | |
1611 | break; |
1697 | break; |
… | |
… | |
1629 | fail: |
1715 | fail: |
1630 | return 0; |
1716 | return 0; |
1631 | } |
1717 | } |
1632 | |
1718 | |
1633 | static SV * |
1719 | static SV * |
1634 | decode_json (SV *string, JSON *json, char **offset_return) |
1720 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1635 | { |
1721 | { |
1636 | dec_t dec; |
1722 | dec_t dec; |
1637 | SV *sv; |
1723 | SV *sv; |
1638 | |
1724 | |
1639 | /* work around bugs in 5.10 where manipulating magic values |
1725 | /* work around bugs in 5.10 where manipulating magic values |
1640 | * makes perl ignore the magic in subsequent accesses. |
1726 | * makes perl ignore the magic in subsequent accesses. |
1641 | * also make a copy of non-PV values, to get them into a clean |
1727 | * also make a copy of non-PV values, to get them into a clean |
1642 | * state (SvPV should do that, but it's buggy, see below). |
1728 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1729 | * |
|
|
1730 | * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16), |
|
|
1731 | * as reported by Reini Urban. |
1643 | */ |
1732 | */ |
1644 | /*SvGETMAGIC (string);*/ |
1733 | /*SvGETMAGIC (string);*/ |
1645 | if (SvMAGICAL (string) || !SvPOK (string)) |
1734 | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
1646 | string = sv_2mortal (newSVsv (string)); |
1735 | string = sv_2mortal (newSVsv (string)); |
1647 | |
1736 | |
1648 | SvUPGRADE (string, SVt_PV); |
1737 | SvUPGRADE (string, SVt_PV); |
1649 | |
1738 | |
1650 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1739 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1689 | |
1778 | |
1690 | decode_ws (&dec); |
1779 | decode_ws (&dec); |
1691 | sv = decode_sv (&dec); |
1780 | sv = decode_sv (&dec); |
1692 | |
1781 | |
1693 | if (offset_return) |
1782 | if (offset_return) |
1694 | *offset_return = dec.cur; |
1783 | *offset_return = dec.cur - SvPVX (string); |
1695 | |
1784 | else if (sv) |
1696 | if (!(offset_return || !sv)) |
|
|
1697 | { |
1785 | { |
1698 | // check for trailing garbage |
1786 | // check for trailing garbage |
1699 | decode_ws (&dec); |
1787 | decode_ws (&dec); |
1700 | |
1788 | |
1701 | if (*dec.cur) |
1789 | if (*dec.cur) |
… | |
… | |
1725 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1813 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1726 | } |
1814 | } |
1727 | |
1815 | |
1728 | sv = sv_2mortal (sv); |
1816 | sv = sv_2mortal (sv); |
1729 | |
1817 | |
1730 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1818 | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1731 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1819 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1732 | |
1820 | |
1733 | return sv; |
1821 | return sv; |
1734 | } |
1822 | } |
1735 | |
1823 | |
… | |
… | |
1905 | i >= '0' && i <= '9' ? i - '0' |
1993 | i >= '0' && i <= '9' ? i - '0' |
1906 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1994 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1907 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1995 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1908 | : -1; |
1996 | : -1; |
1909 | |
1997 | |
1910 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1998 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1911 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1999 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1912 | |
|
|
1913 | types_true = get_bool ("Types::Serialiser::true"); |
2000 | bool_true = get_bool ("Types::Serialiser::true"); |
1914 | types_false = get_bool ("Types::Serialiser::false"); |
2001 | bool_false = get_bool ("Types::Serialiser::false"); |
1915 | |
2002 | |
1916 | sv_json = newSVpv ("JSON", 0); |
2003 | sv_json = newSVpv ("JSON", 0); |
1917 | SvREADONLY_on (sv_json); |
2004 | SvREADONLY_on (sv_json); |
1918 | |
2005 | |
1919 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
2006 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
… | |
… | |
1921 | |
2008 | |
1922 | PROTOTYPES: DISABLE |
2009 | PROTOTYPES: DISABLE |
1923 | |
2010 | |
1924 | void CLONE (...) |
2011 | void CLONE (...) |
1925 | CODE: |
2012 | CODE: |
1926 | json_stash = 0; |
2013 | json_stash = 0; |
1927 | types_boolean_stash = 0; |
2014 | bool_stash = 0; |
1928 | |
2015 | |
1929 | void new (char *klass) |
2016 | void new (char *klass) |
1930 | PPCODE: |
2017 | PPCODE: |
1931 | { |
2018 | { |
1932 | SV *pv = NEWSV (0, sizeof (JSON)); |
2019 | SV *pv = NEWSV (0, sizeof (JSON)); |
… | |
… | |
2049 | |
2136 | |
2050 | void decode_prefix (JSON *self, SV *jsonstr) |
2137 | void decode_prefix (JSON *self, SV *jsonstr) |
2051 | PPCODE: |
2138 | PPCODE: |
2052 | { |
2139 | { |
2053 | SV *sv; |
2140 | SV *sv; |
2054 | char *offset; |
2141 | STRLEN offset; |
2055 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2142 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2056 | EXTEND (SP, 2); |
2143 | EXTEND (SP, 2); |
2057 | PUSHs (sv); |
2144 | PUSHs (sv); |
2058 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
2145 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset)))); |
2059 | } |
2146 | } |
2060 | |
2147 | |
2061 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2148 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2062 | PPCODE: |
2149 | PPCODE: |
2063 | { |
2150 | { |
… | |
… | |
2097 | { |
2184 | { |
2098 | STRLEN len; |
2185 | STRLEN len; |
2099 | const char *str = SvPV (jsonstr, len); |
2186 | const char *str = SvPV (jsonstr, len); |
2100 | STRLEN cur = SvCUR (self->incr_text); |
2187 | STRLEN cur = SvCUR (self->incr_text); |
2101 | |
2188 | |
2102 | if (SvLEN (self->incr_text) <= cur + len) |
2189 | if (SvLEN (self->incr_text) - cur <= len) |
2103 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
2190 | json_sv_grow (self->incr_text, cur, len); |
2104 | |
2191 | |
2105 | Move (str, SvEND (self->incr_text), len, char); |
2192 | Move (str, SvEND (self->incr_text), len, char); |
2106 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
2193 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
2107 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
2194 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
2108 | } |
2195 | } |
… | |
… | |
2110 | |
2197 | |
2111 | if (GIMME_V != G_VOID) |
2198 | if (GIMME_V != G_VOID) |
2112 | do |
2199 | do |
2113 | { |
2200 | { |
2114 | SV *sv; |
2201 | SV *sv; |
2115 | char *offset; |
2202 | STRLEN offset; |
2116 | |
2203 | |
2117 | if (!INCR_DONE (self)) |
2204 | if (!INCR_DONE (self)) |
2118 | { |
2205 | { |
2119 | incr_parse (self); |
2206 | incr_parse (self); |
2120 | |
2207 | |
… | |
… | |
2136 | } |
2223 | } |
2137 | |
2224 | |
2138 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2225 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2139 | XPUSHs (sv); |
2226 | XPUSHs (sv); |
2140 | |
2227 | |
2141 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2228 | self->incr_pos -= offset; |
2142 | self->incr_nest = 0; |
2229 | self->incr_nest = 0; |
2143 | self->incr_mode = 0; |
2230 | self->incr_mode = 0; |
2144 | |
2231 | |
2145 | sv_chop (self->incr_text, offset); |
2232 | sv_chop (self->incr_text, SvPVX (self->incr_text) + offset); |
2146 | } |
2233 | } |
2147 | while (GIMME_V == G_ARRAY); |
2234 | while (GIMME_V == G_ARRAY); |
2148 | } |
2235 | } |
2149 | |
2236 | |
2150 | SV *incr_text (JSON *self) |
2237 | SV *incr_text (JSON *self) |