ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.125 by root, Sun Feb 21 15:37:53 2016 UTC vs.
Revision 1.130 by root, Sat Nov 26 06:09:29 2016 UTC

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
79#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?)"
80 81
81#ifdef USE_ITHREADS 82#ifdef USE_ITHREADS
82# define JSON_SLOW 1 83# define JSON_SLOW 1
83# 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))
84#else 86#else
85# define JSON_SLOW 0 87# define JSON_SLOW 0
86# define JSON_STASH json_stash 88# define JSON_STASH json_stash
89# define BOOL_STASH bool_stash
87#endif 90#endif
88 91
89// 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
90#define STACK_HES 64 93#define STACK_HES 64
91 94
92static HV *json_stash, *types_boolean_stash; // JSON::XS:: 95static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean::
93static SV *types_true, *types_false, *sv_json; 96static SV *bool_true, *bool_false, *sv_json;
94 97
95enum { 98enum {
96 INCR_M_WS = 0, // initial whitespace skipping, must be 0 99 INCR_M_WS = 0, // initial whitespace skipping, must be 0
97 INCR_M_STR, // inside string 100 INCR_M_STR, // inside string
98 INCR_M_BS, // inside backslash 101 INCR_M_BS, // inside backslash
299 // a recursion depth of ten gives us >>500 bits 302 // a recursion depth of ten gives us >>500 bits
300 json_atof_scan1 (s, &accum, &expo, 0, 10); 303 json_atof_scan1 (s, &accum, &expo, 0, 10);
301 304
302 return neg ? -accum : accum; 305 return neg ? -accum : accum;
303} 306}
307
308// target of scalar reference is bool? -1 == nope, 0 == false, 1 == true
309static int
310ref_bool_type (SV *sv)
311{
312 svtype svt = SvTYPE (sv);
313
314 if (svt < SVt_PVAV)
315 {
316 STRLEN len = 0;
317 char *pv = svt ? SvPV (sv, len) : 0;
318
319 if (len == 1)
320 if (*pv == '1')
321 return 1;
322 else if (*pv == '0')
323 return 0;
324 }
325
326 return -1;
327}
328
329// returns whether scalar is not a reference in the sense of allow_nonref
330static int
331json_nonref (SV *scalar)
332{
333 if (!SvROK (scalar))
334 return 1;
335
336 scalar = SvRV (scalar);
337
338 if (SvTYPE (scalar) >= SVt_PVMG)
339 {
340 if (SvSTASH (scalar) == bool_stash)
341 return 1;
342
343 if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0)
344 return 1;
345 }
346
347 return 0;
348}
349
304///////////////////////////////////////////////////////////////////////////// 350/////////////////////////////////////////////////////////////////////////////
305// encoder 351// encoder
306 352
307// structure used for encoding JSON 353// structure used for encoding JSON
308typedef struct 354typedef struct
316} enc_t; 362} enc_t;
317 363
318INLINE void 364INLINE void
319need (enc_t *enc, STRLEN len) 365need (enc_t *enc, STRLEN len)
320{ 366{
321 if (expect_false (enc->cur + len >= enc->end)) 367 if (expect_false ((uintptr_t)(enc->end - enc->cur) < len))
322 { 368 {
323 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 369 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
324 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); 370 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
325 enc->cur = SvPVX (enc->sv) + cur; 371 enc->cur = SvPVX (enc->sv) + cur;
326 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 372 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
701 SvGETMAGIC (sv); 747 SvGETMAGIC (sv);
702 svt = SvTYPE (sv); 748 svt = SvTYPE (sv);
703 749
704 if (expect_false (SvOBJECT (sv))) 750 if (expect_false (SvOBJECT (sv)))
705 { 751 {
706 HV *boolean_stash = !JSON_SLOW || types_boolean_stash
707 ? types_boolean_stash
708 : gv_stashpv ("Types::Serialiser::Boolean", 1);
709 HV *stash = SvSTASH (sv); 752 HV *stash = SvSTASH (sv);
710 753
711 if (stash == boolean_stash) 754 if (stash == bool_stash)
712 { 755 {
713 if (SvIV (sv)) 756 if (SvIV (sv))
714 encode_str (enc, "true", 4, 0); 757 encode_str (enc, "true", 4, 0);
715 else 758 else
716 encode_str (enc, "false", 5, 0); 759 encode_str (enc, "false", 5, 0);
790 encode_hv (enc, (HV *)sv); 833 encode_hv (enc, (HV *)sv);
791 else if (svt == SVt_PVAV) 834 else if (svt == SVt_PVAV)
792 encode_av (enc, (AV *)sv); 835 encode_av (enc, (AV *)sv);
793 else if (svt < SVt_PVAV) 836 else if (svt < SVt_PVAV)
794 { 837 {
795 STRLEN len = 0; 838 int bool_type = ref_bool_type (sv);
796 char *pv = svt ? SvPV (sv, len) : 0;
797 839
798 if (len == 1 && *pv == '1') 840 if (bool_type == 1)
799 encode_str (enc, "true", 4, 0); 841 encode_str (enc, "true", 4, 0);
800 else if (len == 1 && *pv == '0') 842 else if (bool_type == 0)
801 encode_str (enc, "false", 5, 0); 843 encode_str (enc, "false", 5, 0);
802 else if (enc->json.flags & F_ALLOW_UNKNOWN) 844 else if (enc->json.flags & F_ALLOW_UNKNOWN)
803 encode_str (enc, "null", 4, 0); 845 encode_str (enc, "null", 4, 0);
804 else 846 else
805 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 847 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
883 else 925 else
884 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", 926 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
885 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); 927 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
886} 928}
887 929
888static int
889json_scalar (SV *scalar)
890{
891 return 0;//D
892 if (!SvROK (scalar))
893 return 1;
894}
895
896static SV * 930static SV *
897encode_json (SV *scalar, JSON *json) 931encode_json (SV *scalar, JSON *json)
898{ 932{
899 enc_t enc; 933 enc_t enc;
900 934
901 if (!(json->flags & F_ALLOW_NONREF) && json_scalar (scalar)) 935 if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar))
902 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 936 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
903 937
904 enc.json = *json; 938 enc.json = *json;
905 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 939 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
906 enc.cur = SvPVX (enc.sv); 940 enc.cur = SvPVX (enc.sv);
1612 case 't': 1646 case 't':
1613 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1647 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1614 { 1648 {
1615 dec->cur += 4; 1649 dec->cur += 4;
1616#if JSON_SLOW 1650#if JSON_SLOW
1617 types_true = get_bool ("Types::Serialiser::true"); 1651 bool_true = get_bool ("Types::Serialiser::true");
1618#endif 1652#endif
1619 return newSVsv (types_true); 1653 return newSVsv (bool_true);
1620 } 1654 }
1621 else 1655 else
1622 ERR ("'true' expected"); 1656 ERR ("'true' expected");
1623 1657
1624 break; 1658 break;
1626 case 'f': 1660 case 'f':
1627 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1661 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1628 { 1662 {
1629 dec->cur += 5; 1663 dec->cur += 5;
1630#if JSON_SLOW 1664#if JSON_SLOW
1631 types_false = get_bool ("Types::Serialiser::false"); 1665 bool_false = get_bool ("Types::Serialiser::false");
1632#endif 1666#endif
1633 return newSVsv (types_false); 1667 return newSVsv (bool_false);
1634 } 1668 }
1635 else 1669 else
1636 ERR ("'false' expected"); 1670 ERR ("'false' expected");
1637 1671
1638 break; 1672 break;
1656fail: 1690fail:
1657 return 0; 1691 return 0;
1658} 1692}
1659 1693
1660static SV * 1694static SV *
1661decode_json (SV *string, JSON *json, char **offset_return) 1695decode_json (SV *string, JSON *json, STRLEN *offset_return)
1662{ 1696{
1663 dec_t dec; 1697 dec_t dec;
1664 SV *sv; 1698 SV *sv;
1665 1699
1666 /* work around bugs in 5.10 where manipulating magic values 1700 /* work around bugs in 5.10 where manipulating magic values
1667 * makes perl ignore the magic in subsequent accesses. 1701 * makes perl ignore the magic in subsequent accesses.
1668 * also make a copy of non-PV values, to get them into a clean 1702 * also make a copy of non-PV values, to get them into a clean
1669 * state (SvPV should do that, but it's buggy, see below). 1703 * state (SvPV should do that, but it's buggy, see below).
1704 *
1705 * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16),
1706 * as reported by Reini Urban.
1670 */ 1707 */
1671 /*SvGETMAGIC (string);*/ 1708 /*SvGETMAGIC (string);*/
1672 if (SvMAGICAL (string) || !SvPOK (string)) 1709 if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string))
1673 string = sv_2mortal (newSVsv (string)); 1710 string = sv_2mortal (newSVsv (string));
1674 1711
1675 SvUPGRADE (string, SVt_PV); 1712 SvUPGRADE (string, SVt_PV);
1676 1713
1677 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1714 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1716 1753
1717 decode_ws (&dec); 1754 decode_ws (&dec);
1718 sv = decode_sv (&dec); 1755 sv = decode_sv (&dec);
1719 1756
1720 if (offset_return) 1757 if (offset_return)
1721 *offset_return = dec.cur; 1758 *offset_return = dec.cur - SvPVX (string);
1722 1759 else if (sv)
1723 if (!(offset_return || !sv))
1724 { 1760 {
1725 // check for trailing garbage 1761 // check for trailing garbage
1726 decode_ws (&dec); 1762 decode_ws (&dec);
1727 1763
1728 if (*dec.cur) 1764 if (*dec.cur)
1752 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1788 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1753 } 1789 }
1754 1790
1755 sv = sv_2mortal (sv); 1791 sv = sv_2mortal (sv);
1756 1792
1757 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1793 if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv))
1758 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1794 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1759 1795
1760 return sv; 1796 return sv;
1761} 1797}
1762 1798
1932 i >= '0' && i <= '9' ? i - '0' 1968 i >= '0' && i <= '9' ? i - '0'
1933 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1969 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1934 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1970 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1935 : -1; 1971 : -1;
1936 1972
1937 json_stash = gv_stashpv ("JSON::XS" , 1); 1973 json_stash = gv_stashpv ("JSON::XS" , 1);
1938 types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); 1974 bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1);
1939
1940 types_true = get_bool ("Types::Serialiser::true"); 1975 bool_true = get_bool ("Types::Serialiser::true");
1941 types_false = get_bool ("Types::Serialiser::false"); 1976 bool_false = get_bool ("Types::Serialiser::false");
1942 1977
1943 sv_json = newSVpv ("JSON", 0); 1978 sv_json = newSVpv ("JSON", 0);
1944 SvREADONLY_on (sv_json); 1979 SvREADONLY_on (sv_json);
1945 1980
1946 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ 1981 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1948 1983
1949PROTOTYPES: DISABLE 1984PROTOTYPES: DISABLE
1950 1985
1951void CLONE (...) 1986void CLONE (...)
1952 CODE: 1987 CODE:
1953 json_stash = 0; 1988 json_stash = 0;
1954 types_boolean_stash = 0; 1989 bool_stash = 0;
1955 1990
1956void new (char *klass) 1991void new (char *klass)
1957 PPCODE: 1992 PPCODE:
1958{ 1993{
1959 SV *pv = NEWSV (0, sizeof (JSON)); 1994 SV *pv = NEWSV (0, sizeof (JSON));
2076 2111
2077void decode_prefix (JSON *self, SV *jsonstr) 2112void decode_prefix (JSON *self, SV *jsonstr)
2078 PPCODE: 2113 PPCODE:
2079{ 2114{
2080 SV *sv; 2115 SV *sv;
2081 char *offset; 2116 STRLEN offset;
2082 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; 2117 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
2083 EXTEND (SP, 2); 2118 EXTEND (SP, 2);
2084 PUSHs (sv); 2119 PUSHs (sv);
2085 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 2120 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset))));
2086} 2121}
2087 2122
2088void incr_parse (JSON *self, SV *jsonstr = 0) 2123void incr_parse (JSON *self, SV *jsonstr = 0)
2089 PPCODE: 2124 PPCODE:
2090{ 2125{
2137 2172
2138 if (GIMME_V != G_VOID) 2173 if (GIMME_V != G_VOID)
2139 do 2174 do
2140 { 2175 {
2141 SV *sv; 2176 SV *sv;
2142 char *offset; 2177 STRLEN offset;
2143 2178
2144 if (!INCR_DONE (self)) 2179 if (!INCR_DONE (self))
2145 { 2180 {
2146 incr_parse (self); 2181 incr_parse (self);
2147 2182
2163 } 2198 }
2164 2199
2165 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; 2200 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2166 XPUSHs (sv); 2201 XPUSHs (sv);
2167 2202
2168 self->incr_pos -= offset - SvPVX (self->incr_text); 2203 self->incr_pos -= offset;
2169 self->incr_nest = 0; 2204 self->incr_nest = 0;
2170 self->incr_mode = 0; 2205 self->incr_mode = 0;
2171 2206
2172 sv_chop (self->incr_text, offset); 2207 sv_chop (self->incr_text, SvPVX (self->incr_text) + offset);
2173 } 2208 }
2174 while (GIMME_V == G_ARRAY); 2209 while (GIMME_V == G_ARRAY);
2175} 2210}
2176 2211
2177SV *incr_text (JSON *self) 2212SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines