--- JSON-XS/XS.xs 2007/04/03 23:59:04 1.24 +++ JSON-XS/XS.xs 2007/05/09 16:41:12 1.32 @@ -5,15 +5,21 @@ #include "assert.h" #include "string.h" #include "stdlib.h" +#include "stdio.h" + +#if defined(__BORLANDC__) || defined(_MSC_VER) +# define snprintf _snprintf // C compilers have this in stdio.h +#endif #define F_ASCII 0x00000001UL -#define F_UTF8 0x00000002UL -#define F_INDENT 0x00000004UL -#define F_CANONICAL 0x00000008UL -#define F_SPACE_BEFORE 0x00000010UL -#define F_SPACE_AFTER 0x00000020UL -#define F_ALLOW_NONREF 0x00000080UL -#define F_SHRINK 0x00000100UL +#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 @@ -28,7 +34,6 @@ #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 { @@ -106,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; } } @@ -178,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) { @@ -202,6 +207,11 @@ str += clen; } + else if (enc->flags & F_LATIN1) + { + *enc->cur++ = uch; + str += clen; + } else if (is_utf8) { need (enc, len += clen); @@ -213,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; } @@ -410,7 +420,6 @@ } else { - SV *sv; HE *he = hv_iternext (hv); for (;;) @@ -435,9 +444,10 @@ static void encode_rv (enc_t *enc, SV *sv) { - SvGETMAGIC (sv); + svtype svt; - svtype svt = SvTYPE (sv); + SvGETMAGIC (sv); + svt = SvTYPE (sv); if (svt == SVt_PVHV) encode_hv (enc, (HV *)sv); @@ -497,10 +507,11 @@ static SV * 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); @@ -511,10 +522,11 @@ 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); @@ -596,7 +608,7 @@ do { - char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; + char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; char *cur = buf; do @@ -672,10 +684,12 @@ *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"); @@ -697,16 +711,18 @@ } 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 != '"'); @@ -969,10 +985,13 @@ } static SV * -decode_json (SV *string, U32 flags) +decode_json (SV *string, U32 flags, UV *offset_return) { + dec_t dec; + UV offset; SV *sv; + SvGETMAGIC (string); SvUPGRADE (string, SVt_PV); if (flags & F_UTF8) @@ -982,7 +1001,6 @@ SvGROW (string, SvCUR (string) + 1); // should basically be a NOP - dec_t dec; dec.flags = flags; dec.cur = SvPVX (string); dec.end = SvEND (string); @@ -990,14 +1008,34 @@ dec.depth = 0; dec.maxdepth = DEC_DEPTH (dec.flags); - *dec.end = 0; // this should basically be a nop, too, but make sure its there + *dec.end = 0; // this should basically be a nop, too, but make sure it's there sv = decode_sv (&dec); + if (!(offset_return || !sv)) + { + // check for trailing garbage + decode_ws (&dec); + + if (*dec.cur) + { + dec.err = "garbage after JSON object"; + SvREFCNT_dec (sv); + sv = 0; + } + } + + if (offset_return || !sv) + { + offset = dec.flags & F_UTF8 + ? dec.cur - SvPVX (string) + : utf8_distance (dec.cur, SvPVX (string)); + + if (offset_return) + *offset_return = offset; + } + if (!sv) { - IV offset = dec.flags & F_UTF8 - ? dec.cur - SvPVX (string) - : utf8_distance (dec.cur, SvPVX (string)); SV *uni = sv_newmortal (); // horrible hack to silence warning inside pv_uni_display @@ -1055,6 +1093,7 @@ SV *ascii (SV *self, int enable = 1) ALIAS: ascii = F_ASCII + latin1 = F_LATIN1 utf8 = F_UTF8 indent = F_INDENT canonical = F_CANONICAL @@ -1076,7 +1115,7 @@ OUTPUT: RETVAL -SV *max_depth (SV *self, int max_depth = 0x80000000UL) +SV *max_depth (SV *self, UV max_depth = 0x80000000UL) CODE: { UV *uv = SvJSON (self); @@ -1100,7 +1139,16 @@ void decode (SV *self, SV *jsonstr) PPCODE: - XPUSHs (decode_json (jsonstr, *SvJSON (self))); + XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); + +void decode_prefix (SV *self, SV *jsonstr) + PPCODE: +{ + UV offset; + EXTEND (SP, 2); + PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); + PUSHs (sv_2mortal (newSVuv (offset))); +} PROTOTYPES: ENABLE @@ -1114,5 +1162,5 @@ ALIAS: jsonToObj = 0 PPCODE: - XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); + XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));