--- JSON-XS/XS.xs 2007/04/06 20:37:22 1.25 +++ JSON-XS/XS.xs 2007/05/23 20:26:40 1.33 @@ -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; } @@ -364,8 +374,13 @@ // that randomises hash orderings if (enc->flags & F_CANONICAL) { - HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode int fast = 1; + HE *he; +#if WIN32 + HE **hes = _alloca (count * sizeof (HE)); +#else + HE *hes [count]; // if your compiler dies here, you need to enable C99 mode +#endif i = 0; while ((he = hv_iternext (hv))) @@ -512,10 +527,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); @@ -597,7 +613,7 @@ do { - char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; + char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; char *cur = buf; do @@ -974,11 +990,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) @@ -995,14 +1013,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 @@ -1060,6 +1098,7 @@ SV *ascii (SV *self, int enable = 1) ALIAS: ascii = F_ASCII + latin1 = F_LATIN1 utf8 = F_UTF8 indent = F_INDENT canonical = F_CANONICAL @@ -1105,7 +1144,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 @@ -1119,5 +1167,5 @@ ALIAS: jsonToObj = 0 PPCODE: - XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); + XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));