--- JSON-XS/XS.xs 2007/03/24 22:55:43 1.14 +++ JSON-XS/XS.xs 2007/05/09 16:10:37 1.30 @@ -5,23 +5,35 @@ #include "assert.h" #include "string.h" #include "stdlib.h" +#include "stdio.h" -#define F_ASCII 0x00000001 -#define F_UTF8 0x00000002 -#define F_INDENT 0x00000004 -#define F_CANONICAL 0x00000008 -#define F_SPACE_BEFORE 0x00000010 -#define F_SPACE_AFTER 0x00000020 -#define F_ALLOW_NONREF 0x00000080 -#define F_SHRINK 0x00000100 +#if defined(__BORLANDC__) || defined(_MSC_VER) +# define snprintf _snprintf // C compilers have this in stdio.h +#endif + +#define F_ASCII 0x00000001UL +#define F_LATIN1 0x00000002UL +#define F_UTF8 0x00000004UL +#define F_INDENT 0x00000008UL +#define F_CANONICAL 0x00000010UL +#define F_SPACE_BEFORE 0x00000020UL +#define F_SPACE_AFTER 0x00000040UL +#define F_ALLOW_NONREF 0x00000100UL +#define F_SHRINK 0x00000200UL +#define F_MAXDEPTH 0xf8000000UL +#define S_MAXDEPTH 27 + +#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) + +// F_SELFCONVERT? <=> to_json/toJson +// F_BLESSED? <=> { $__class__$ => } #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER -#define F_DEFAULT 0 +#define F_DEFAULT (9UL << S_MAXDEPTH) #define INIT_SIZE 32 // initial scalar size to be allocated #define INDENT_STEP 3 // spaces per indentation level -#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character #define SHORT_STRING_LEN 512 // special-case strings of up to this size #define SB do { @@ -71,7 +83,10 @@ return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); } else - return (UV)-1; + { + *clen = (STRLEN)-1; + return (UV)-1; + } } ///////////////////////////////////////////////////////////////////////////// @@ -83,9 +98,9 @@ char *cur; // SvPVX (sv) + current output position char *end; // SvEND (sv) SV *sv; // result scalar - UV flags; // F_* - int indent; // indentation level - int max_depth; // max. recursion level + U32 flags; // F_* + U32 indent; // indentation level + U32 maxdepth; // max. indentation/recursion level } enc_t; static void @@ -96,7 +111,7 @@ STRLEN cur = enc->cur - SvPVX (enc->sv); SvGROW (enc->sv, cur + len + 1); enc->cur = SvPVX (enc->sv) + cur; - enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); + enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; } } @@ -168,7 +183,7 @@ if (uch > 0x10FFFFUL) croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); - if (uch < 0x80 || enc->flags & F_ASCII) + if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) { if (uch > 0xFFFFUL) { @@ -192,6 +207,11 @@ str += clen; } + else if (enc->flags & F_LATIN1) + { + *enc->cur++ = uch; + str += clen; + } else if (is_utf8) { need (enc, len += clen); @@ -203,7 +223,7 @@ } else { - need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed + need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); ++str; } @@ -263,6 +283,9 @@ { int i, len = av_len (av); + if (enc->indent >= enc->maxdepth) + croak ("data structure too deep (hit recursion limit)"); + encode_ch (enc, '['); encode_nl (enc); ++enc->indent; @@ -338,6 +361,9 @@ { int count, i; + if (enc->indent >= enc->maxdepth) + croak ("data structure too deep (hit recursion limit)"); + encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; if ((count = hv_iterinit (hv))) @@ -394,7 +420,6 @@ } else { - SV *sv; HE *he = hv_iternext (hv); for (;;) @@ -415,6 +440,34 @@ --enc->indent; encode_indent (enc); encode_ch (enc, '}'); } +// encode objects, arrays and special \0=false and \1=true values. +static void +encode_rv (enc_t *enc, SV *sv) +{ + svtype svt; + + SvGETMAGIC (sv); + svt = SvTYPE (sv); + + if (svt == SVt_PVHV) + encode_hv (enc, (HV *)sv); + else if (svt == SVt_PVAV) + encode_av (enc, (AV *)sv); + else if (svt < SVt_PVAV) + { + if (SvNIOK (sv) && SvIV (sv) == 0) + encode_str (enc, "false", 5, 0); + else if (SvNIOK (sv) && SvIV (sv) == 1) + encode_str (enc, "true", 4, 0); + else + croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", + SvPV_nolen (sv_2mortal (newRV_inc (sv)))); + } + else + croak ("encountered %s, but JSON can only represent references to arrays or hashes", + SvPV_nolen (sv_2mortal (newRV_inc (sv)))); +} + static void encode_sv (enc_t *enc, SV *sv) { @@ -443,22 +496,7 @@ : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); } else if (SvROK (sv)) - { - SV *rv = SvRV (sv); - - if (enc->indent >= enc->max_depth) - croak ("data structure too deep (hit recursion limit)"); - - switch (SvTYPE (rv)) - { - case SVt_PVAV: encode_av (enc, (AV *)rv); break; - case SVt_PVHV: encode_hv (enc, (HV *)rv); break; - - default: - croak ("encountered %s, but JSON can only represent references to arrays or hashes", - SvPV_nolen (sv)); - } - } + encode_rv (enc, SvRV (sv)); else if (!SvOK (sv)) encode_str (enc, "null", 4, 0); else @@ -467,26 +505,28 @@ } static SV * -encode_json (SV *scalar, UV flags) +encode_json (SV *scalar, U32 flags) { + enc_t enc; + if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); - enc_t enc; enc.flags = flags; enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); enc.cur = SvPVX (enc.sv); enc.end = SvEND (enc.sv); enc.indent = 0; - enc.max_depth = 0x7fffffffUL; + enc.maxdepth = DEC_DEPTH (flags); SvPOK_only (enc.sv); encode_sv (&enc, scalar); - if (!(flags & (F_ASCII | F_UTF8))) - SvUTF8_on (enc.sv); - SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); + *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings + + if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) + SvUTF8_on (enc.sv); if (enc.flags & F_SHRINK) shrink (enc.sv); @@ -503,7 +543,9 @@ char *cur; // current parser pointer char *end; // end of input string const char *err; // parse error, if != 0 - UV flags; // F_* + U32 flags; // F_* + U32 depth; // recursion depth + U32 maxdepth; // recursion depth limit } dec_t; static void @@ -522,12 +564,16 @@ } #define ERR(reason) SB dec->err = reason; goto fail; SE + #define EXPECT_CH(ch) SB \ if (*dec->cur != ch) \ ERR (# ch " expected"); \ ++dec->cur; \ SE +#define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") +#define DEC_DEC_DEPTH --dec->depth + static SV *decode_sv (dec_t *dec); static signed char decode_hexdigit[256]; @@ -562,7 +608,7 @@ do { - char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; + char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; char *cur = buf; do @@ -638,39 +684,45 @@ *cur++ = ch; else if (ch >= 0x80) { + STRLEN clen; + UV uch; + --dec->cur; - STRLEN clen; - UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); + uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); if (clen == (STRLEN)-1) ERR ("malformed UTF-8 character in JSON string"); do - { - *cur++ = *dec->cur++; - } + *cur++ = *dec->cur++; while (--clen); utf8 = 1; } - else if (!ch) - ERR ("unexpected end of string while parsing json string"); else - ERR ("invalid character encountered"); + { + --dec->cur; + if (!ch) + ERR ("unexpected end of string while parsing JSON string"); + else + ERR ("invalid character encountered while parsing JSON string"); + } } while (cur < buf + SHORT_STRING_LEN); - STRLEN len = cur - buf; + { + STRLEN len = cur - buf; - if (sv) - { - SvGROW (sv, SvCUR (sv) + len + 1); - memcpy (SvPVX (sv) + SvCUR (sv), buf, len); - SvCUR_set (sv, SvCUR (sv) + len); - } - else - sv = newSVpvn (buf, len); + if (sv) + { + SvGROW (sv, SvCUR (sv) + len + 1); + memcpy (SvPVX (sv) + SvCUR (sv), buf, len); + SvCUR_set (sv, SvCUR (sv) + len); + } + else + sv = newSVpvn (buf, len); + } } while (*dec->cur != '"'); @@ -780,7 +832,9 @@ { AV *av = newAV (); + DEC_INC_DEPTH; decode_ws (dec); + if (*dec->cur == ']') ++dec->cur; else @@ -808,10 +862,12 @@ ++dec->cur; } + DEC_DEC_DEPTH; return newRV_noinc ((SV *)av); fail: SvREFCNT_dec (av); + DEC_DEC_DEPTH; return 0; } @@ -820,7 +876,9 @@ { HV *hv = newHV (); + DEC_INC_DEPTH; decode_ws (dec); + if (*dec->cur == '}') ++dec->cur; else @@ -843,8 +901,8 @@ goto fail; } - //TODO: optimise hv_store_ent (hv, key, value, 0); + SvREFCNT_dec (key); decode_ws (dec); @@ -860,10 +918,12 @@ ++dec->cur; } + DEC_DEC_DEPTH; return newRV_noinc ((SV *)hv); fail: SvREFCNT_dec (hv); + DEC_DEC_DEPTH; return 0; } @@ -916,7 +976,7 @@ break; default: - ERR ("malformed json string, neither array, object, number, string or atom"); + ERR ("malformed JSON string, neither array, object, number, string or atom"); break; } @@ -925,10 +985,14 @@ } static SV * -decode_json (SV *string, UV flags) +decode_json (SV *string, U32 flags) { + dec_t dec; SV *sv; + SvGETMAGIC (string); + SvUPGRADE (string, SVt_PV); + if (flags & F_UTF8) sv_utf8_downgrade (string, 0); else @@ -936,12 +1000,14 @@ SvGROW (string, SvCUR (string) + 1); // should basically be a NOP - dec_t dec; - dec.flags = flags; - dec.cur = SvPVX (string); - dec.end = SvEND (string); - dec.err = 0; + dec.flags = flags; + dec.cur = SvPVX (string); + dec.end = SvEND (string); + dec.err = 0; + dec.depth = 0; + dec.maxdepth = DEC_DEPTH (dec.flags); + *dec.end = 0; // this should basically be a nop, too, but make sure its there sv = decode_sv (&dec); if (!sv) @@ -960,7 +1026,7 @@ pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); LEAVE; - croak ("%s, at character offset %d (%s)", + croak ("%s, at character offset %d [\"%s\"]", dec.err, (int)offset, dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); @@ -984,14 +1050,13 @@ int i; memset (decode_hexdigit, 0xff, 256); - for (i = 10; i--; ) - decode_hexdigit ['0' + i] = i; - for (i = 7; i--; ) - { - decode_hexdigit ['a' + i] = 10 + i; - decode_hexdigit ['A' + i] = 10 + i; - } + for (i = 0; i < 256; ++i) + decode_hexdigit [i] = + i >= '0' && i <= '9' ? i - '0' + : i >= 'a' && i <= 'f' ? i - 'a' + 10 + : i >= 'A' && i <= 'F' ? i - 'A' + 10 + : -1; json_stash = gv_stashpv ("JSON::XS", 1); } @@ -1007,6 +1072,7 @@ SV *ascii (SV *self, int enable = 1) ALIAS: ascii = F_ASCII + latin1 = F_LATIN1 utf8 = F_UTF8 indent = F_INDENT canonical = F_CANONICAL @@ -1028,6 +1094,24 @@ OUTPUT: RETVAL +SV *max_depth (SV *self, UV max_depth = 0x80000000UL) + CODE: +{ + UV *uv = SvJSON (self); + UV log2 = 0; + + if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; + + while ((1UL << log2) < max_depth) + ++log2; + + *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); + + RETVAL = newSVsv (self); +} + OUTPUT: + RETVAL + void encode (SV *self, SV *scalar) PPCODE: XPUSHs (encode_json (scalar, *SvJSON (self))); @@ -1039,10 +1123,14 @@ PROTOTYPES: ENABLE void to_json (SV *scalar) + ALIAS: + objToJson = 0 PPCODE: - XPUSHs (encode_json (scalar, F_UTF8)); + XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); void from_json (SV *jsonstr) + ALIAS: + jsonToObj = 0 PPCODE: - XPUSHs (decode_json (jsonstr, F_UTF8)); + XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8));