1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include <assert.h> |
6 | #include "string.h" |
6 | #include <string.h> |
7 | #include "stdlib.h" |
7 | #include <stdlib.h> |
|
|
8 | #include <stdio.h> |
|
|
9 | #include <limits.h> |
|
|
10 | #include <float.h> |
8 | |
11 | |
|
|
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
14 | #endif |
|
|
15 | |
|
|
16 | // some old perls do not have this, try to make it work, no |
|
|
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
18 | #ifndef UTF8_MAXBYTES |
|
|
19 | # define UTF8_MAXBYTES 13 |
|
|
20 | #endif |
|
|
21 | |
|
|
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
|
|
23 | |
9 | #define F_ASCII 0x00000001 |
24 | #define F_ASCII 0x00000001UL |
|
|
25 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
26 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
27 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
28 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
29 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
30 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
31 | #define F_ALLOW_NONREF 0x00000100UL |
17 | #define F_SHRINK 0x00000100 |
32 | #define F_SHRINK 0x00000200UL |
|
|
33 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
34 | #define F_CONV_BLESSED 0x00000800UL |
|
|
35 | #define F_RELAXED 0x00001000UL |
|
|
36 | |
|
|
37 | #define F_MAXDEPTH 0xf8000000UL |
|
|
38 | #define S_MAXDEPTH 27 |
|
|
39 | #define F_MAXSIZE 0x01f00000UL |
|
|
40 | #define S_MAXSIZE 20 |
|
|
41 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
|
|
42 | |
|
|
43 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
44 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
18 | |
45 | |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
46 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
20 | #define F_DEFAULT 0 |
47 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
21 | |
48 | |
22 | #define INIT_SIZE 32 // initial scalar size to be allocated |
49 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
50 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
51 | |
|
|
52 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
23 | |
53 | |
24 | #define SB do { |
54 | #define SB do { |
25 | #define SE } while (0) |
55 | #define SE } while (0) |
26 | |
56 | |
27 | static HV *json_stash; |
57 | #if __GNUC__ >= 3 |
|
|
58 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
59 | # define INLINE static inline |
|
|
60 | #else |
|
|
61 | # define expect(expr,value) (expr) |
|
|
62 | # define INLINE static |
|
|
63 | #endif |
|
|
64 | |
|
|
65 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
66 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
67 | |
|
|
68 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
69 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
70 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
|
|
71 | |
|
|
72 | #ifdef USE_ITHREADS |
|
|
73 | # define JSON_SLOW 1 |
|
|
74 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
75 | #else |
|
|
76 | # define JSON_SLOW 0 |
|
|
77 | # define JSON_STASH json_stash |
|
|
78 | #endif |
|
|
79 | |
|
|
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
81 | static SV *json_true, *json_false; |
|
|
82 | |
|
|
83 | typedef struct { |
|
|
84 | U32 flags; |
|
|
85 | SV *cb_object; |
|
|
86 | HV *cb_sk_object; |
|
|
87 | } JSON; |
|
|
88 | |
|
|
89 | ///////////////////////////////////////////////////////////////////////////// |
|
|
90 | // utility functions |
|
|
91 | |
|
|
92 | INLINE void |
|
|
93 | shrink (SV *sv) |
|
|
94 | { |
|
|
95 | sv_utf8_downgrade (sv, 1); |
|
|
96 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
97 | { |
|
|
98 | #ifdef SvPV_shrink_to_cur |
|
|
99 | SvPV_shrink_to_cur (sv); |
|
|
100 | #elif defined (SvPV_renew) |
|
|
101 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
102 | #endif |
|
|
103 | } |
|
|
104 | } |
|
|
105 | |
|
|
106 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
107 | // case of an error. |
|
|
108 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
109 | // but use the very good perl function to parse anything else. |
|
|
110 | // note that we never call this function for a ascii codepoints |
|
|
111 | INLINE UV |
|
|
112 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
113 | { |
|
|
114 | if (expect_true (len >= 2 |
|
|
115 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
|
|
116 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
|
|
117 | { |
|
|
118 | *clen = 2; |
|
|
119 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
120 | } |
|
|
121 | else |
|
|
122 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
123 | } |
|
|
124 | |
|
|
125 | // likewise for encoding, also never called for ascii codepoints |
|
|
126 | // this function takes advantage of this fact, although current gccs |
|
|
127 | // seem to optimise the check for >= 0x80 away anyways |
|
|
128 | INLINE unsigned char * |
|
|
129 | encode_utf8 (unsigned char *s, UV ch) |
|
|
130 | { |
|
|
131 | if (expect_false (ch < 0x000080)) |
|
|
132 | *s++ = ch; |
|
|
133 | else if (expect_true (ch < 0x000800)) |
|
|
134 | *s++ = 0xc0 | ( ch >> 6), |
|
|
135 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
136 | else if ( ch < 0x010000) |
|
|
137 | *s++ = 0xe0 | ( ch >> 12), |
|
|
138 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
139 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
140 | else if ( ch < 0x110000) |
|
|
141 | *s++ = 0xf0 | ( ch >> 18), |
|
|
142 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
143 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
144 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
145 | |
|
|
146 | return s; |
|
|
147 | } |
|
|
148 | |
|
|
149 | ///////////////////////////////////////////////////////////////////////////// |
|
|
150 | // encoder |
28 | |
151 | |
29 | // structure used for encoding JSON |
152 | // structure used for encoding JSON |
30 | typedef struct |
153 | typedef struct |
31 | { |
154 | { |
32 | char *cur; |
155 | char *cur; // SvPVX (sv) + current output position |
33 | STRLEN len; // SvLEN (sv) |
|
|
34 | char *end; // SvEND (sv) |
156 | char *end; // SvEND (sv) |
35 | SV *sv; |
157 | SV *sv; // result scalar |
36 | UV flags; |
158 | JSON json; |
37 | int max_recurse; |
159 | U32 indent; // indentation level |
38 | int indent; |
160 | U32 maxdepth; // max. indentation/recursion level |
|
|
161 | UV limit; // escape character values >= this value when encoding |
39 | } enc_t; |
162 | } enc_t; |
40 | |
163 | |
41 | // structure used for decoding JSON |
164 | INLINE void |
42 | typedef struct |
|
|
43 | { |
|
|
44 | char *cur; |
|
|
45 | char *end; |
|
|
46 | const char *err; |
|
|
47 | UV flags; |
|
|
48 | } dec_t; |
|
|
49 | |
|
|
50 | static UV * |
|
|
51 | SvJSON (SV *sv) |
|
|
52 | { |
|
|
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
54 | croak ("object is not of type JSON::XS"); |
|
|
55 | |
|
|
56 | return &SvUVX (SvRV (sv)); |
|
|
57 | } |
|
|
58 | |
|
|
59 | ///////////////////////////////////////////////////////////////////////////// |
|
|
60 | |
|
|
61 | static void |
|
|
62 | need (enc_t *enc, STRLEN len) |
165 | need (enc_t *enc, STRLEN len) |
63 | { |
166 | { |
64 | if (enc->cur + len >= enc->end) |
167 | if (expect_false (enc->cur + len >= enc->end)) |
65 | { |
168 | { |
66 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
169 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
67 | SvGROW (enc->sv, cur + len + 1); |
170 | SvGROW (enc->sv, cur + len + 1); |
68 | enc->cur = SvPVX (enc->sv) + cur; |
171 | enc->cur = SvPVX (enc->sv) + cur; |
69 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
172 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
70 | } |
173 | } |
71 | } |
174 | } |
72 | |
175 | |
73 | static void |
176 | INLINE void |
74 | encode_ch (enc_t *enc, char ch) |
177 | encode_ch (enc_t *enc, char ch) |
75 | { |
178 | { |
76 | need (enc, 1); |
179 | need (enc, 1); |
77 | *enc->cur++ = ch; |
180 | *enc->cur++ = ch; |
78 | } |
181 | } |
… | |
… | |
86 | |
189 | |
87 | while (str < end) |
190 | while (str < end) |
88 | { |
191 | { |
89 | unsigned char ch = *(unsigned char *)str; |
192 | unsigned char ch = *(unsigned char *)str; |
90 | |
193 | |
91 | if (ch >= 0x20 && ch < 0x80) // most common case |
194 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
92 | { |
195 | { |
93 | if (ch == '"') // but with slow exceptions |
196 | if (expect_false (ch == '"')) // but with slow exceptions |
94 | { |
197 | { |
95 | need (enc, len += 1); |
198 | need (enc, len += 1); |
96 | *enc->cur++ = '\\'; |
199 | *enc->cur++ = '\\'; |
97 | *enc->cur++ = '"'; |
200 | *enc->cur++ = '"'; |
98 | } |
201 | } |
99 | else if (ch == '\\') |
202 | else if (expect_false (ch == '\\')) |
100 | { |
203 | { |
101 | need (enc, len += 1); |
204 | need (enc, len += 1); |
102 | *enc->cur++ = '\\'; |
205 | *enc->cur++ = '\\'; |
103 | *enc->cur++ = '\\'; |
206 | *enc->cur++ = '\\'; |
104 | } |
207 | } |
… | |
… | |
122 | STRLEN clen; |
225 | STRLEN clen; |
123 | UV uch; |
226 | UV uch; |
124 | |
227 | |
125 | if (is_utf8) |
228 | if (is_utf8) |
126 | { |
229 | { |
127 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
230 | uch = decode_utf8 (str, end - str, &clen); |
128 | if (clen == (STRLEN)-1) |
231 | if (clen == (STRLEN)-1) |
129 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
232 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
130 | } |
233 | } |
131 | else |
234 | else |
132 | { |
235 | { |
133 | uch = ch; |
236 | uch = ch; |
134 | clen = 1; |
237 | clen = 1; |
135 | } |
238 | } |
136 | |
239 | |
137 | if (uch < 0x80 || enc->flags & F_ASCII) |
240 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
138 | { |
241 | { |
139 | if (uch > 0xFFFFUL) |
242 | if (uch >= 0x10000UL) |
140 | { |
243 | { |
|
|
244 | if (uch >= 0x110000UL) |
|
|
245 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
246 | |
141 | need (enc, len += 11); |
247 | need (enc, len += 11); |
142 | sprintf (enc->cur, "\\u%04x\\u%04x", |
248 | sprintf (enc->cur, "\\u%04x\\u%04x", |
143 | (uch - 0x10000) / 0x400 + 0xD800, |
249 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
144 | (uch - 0x10000) % 0x400 + 0xDC00); |
250 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
145 | enc->cur += 12; |
251 | enc->cur += 12; |
146 | } |
252 | } |
147 | else |
253 | else |
148 | { |
254 | { |
149 | static char hexdigit [16] = "0123456789abcdef"; |
255 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
156 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
262 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
157 | } |
263 | } |
158 | |
264 | |
159 | str += clen; |
265 | str += clen; |
160 | } |
266 | } |
|
|
267 | else if (enc->json.flags & F_LATIN1) |
|
|
268 | { |
|
|
269 | *enc->cur++ = uch; |
|
|
270 | str += clen; |
|
|
271 | } |
161 | else if (is_utf8) |
272 | else if (is_utf8) |
162 | { |
273 | { |
163 | need (enc, len += clen); |
274 | need (enc, len += clen); |
164 | do |
275 | do |
165 | { |
276 | { |
… | |
… | |
167 | } |
278 | } |
168 | while (--clen); |
279 | while (--clen); |
169 | } |
280 | } |
170 | else |
281 | else |
171 | { |
282 | { |
172 | need (enc, 10); // never more than 11 bytes needed |
283 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
284 | enc->cur = encode_utf8 (enc->cur, uch); |
174 | ++str; |
285 | ++str; |
175 | } |
286 | } |
176 | } |
287 | } |
177 | } |
288 | } |
178 | } |
289 | } |
179 | |
290 | |
180 | --len; |
291 | --len; |
181 | } |
292 | } |
182 | } |
293 | } |
183 | |
294 | |
184 | #define INDENT SB \ |
295 | INLINE void |
|
|
296 | encode_indent (enc_t *enc) |
|
|
297 | { |
185 | if (enc->flags & F_INDENT) \ |
298 | if (enc->json.flags & F_INDENT) |
186 | { \ |
299 | { |
187 | int i_; \ |
300 | int spaces = enc->indent * INDENT_STEP; |
188 | need (enc, enc->indent); \ |
301 | |
189 | for (i_ = enc->indent * 3; i_--; )\ |
302 | need (enc, spaces); |
|
|
303 | memset (enc->cur, ' ', spaces); |
|
|
304 | enc->cur += spaces; |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
|
|
308 | INLINE void |
|
|
309 | encode_space (enc_t *enc) |
|
|
310 | { |
|
|
311 | need (enc, 1); |
|
|
312 | encode_ch (enc, ' '); |
|
|
313 | } |
|
|
314 | |
|
|
315 | INLINE void |
|
|
316 | encode_nl (enc_t *enc) |
|
|
317 | { |
|
|
318 | if (enc->json.flags & F_INDENT) |
|
|
319 | { |
|
|
320 | need (enc, 1); |
190 | encode_ch (enc, ' '); \ |
321 | encode_ch (enc, '\n'); |
191 | } \ |
322 | } |
192 | SE |
323 | } |
193 | |
324 | |
194 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
325 | INLINE void |
195 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
326 | encode_comma (enc_t *enc) |
196 | #define COMMA SB \ |
327 | { |
197 | encode_ch (enc, ','); \ |
328 | encode_ch (enc, ','); |
|
|
329 | |
198 | if (enc->flags & F_INDENT) \ |
330 | if (enc->json.flags & F_INDENT) |
199 | NL; \ |
331 | encode_nl (enc); |
200 | else if (enc->flags & F_SPACE_AFTER) \ |
332 | else if (enc->json.flags & F_SPACE_AFTER) |
201 | SPACE; \ |
333 | encode_space (enc); |
202 | SE |
334 | } |
203 | |
335 | |
204 | static void encode_sv (enc_t *enc, SV *sv); |
336 | static void encode_sv (enc_t *enc, SV *sv); |
205 | |
337 | |
206 | static void |
338 | static void |
207 | encode_av (enc_t *enc, AV *av) |
339 | encode_av (enc_t *enc, AV *av) |
208 | { |
340 | { |
209 | int i, len = av_len (av); |
341 | int i, len = av_len (av); |
210 | |
342 | |
|
|
343 | if (enc->indent >= enc->maxdepth) |
|
|
344 | croak ("data structure too deep (hit recursion limit)"); |
|
|
345 | |
211 | encode_ch (enc, '['); NL; |
346 | encode_ch (enc, '['); |
212 | ++enc->indent; |
347 | |
|
|
348 | if (len >= 0) |
|
|
349 | { |
|
|
350 | encode_nl (enc); ++enc->indent; |
213 | |
351 | |
214 | for (i = 0; i <= len; ++i) |
352 | for (i = 0; i <= len; ++i) |
215 | { |
353 | { |
216 | INDENT; |
354 | SV **svp = av_fetch (av, i, 0); |
217 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
218 | |
355 | |
|
|
356 | encode_indent (enc); |
|
|
357 | |
|
|
358 | if (svp) |
|
|
359 | encode_sv (enc, *svp); |
|
|
360 | else |
|
|
361 | encode_str (enc, "null", 4, 0); |
|
|
362 | |
219 | if (i < len) |
363 | if (i < len) |
220 | COMMA; |
364 | encode_comma (enc); |
221 | } |
365 | } |
222 | |
366 | |
223 | NL; |
367 | encode_nl (enc); --enc->indent; encode_indent (enc); |
224 | |
368 | } |
225 | --enc->indent; |
369 | |
226 | INDENT; encode_ch (enc, ']'); |
370 | encode_ch (enc, ']'); |
227 | } |
371 | } |
228 | |
372 | |
229 | static void |
373 | static void |
230 | encode_he (enc_t *enc, HE *he) |
374 | encode_hk (enc_t *enc, HE *he) |
231 | { |
375 | { |
232 | encode_ch (enc, '"'); |
376 | encode_ch (enc, '"'); |
233 | |
377 | |
234 | if (HeKLEN (he) == HEf_SVKEY) |
378 | if (HeKLEN (he) == HEf_SVKEY) |
235 | { |
379 | { |
… | |
… | |
245 | else |
389 | else |
246 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
390 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
247 | |
391 | |
248 | encode_ch (enc, '"'); |
392 | encode_ch (enc, '"'); |
249 | |
393 | |
250 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
394 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
251 | encode_ch (enc, ':'); |
395 | encode_ch (enc, ':'); |
252 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
396 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
253 | encode_sv (enc, HeVAL (he)); |
|
|
254 | } |
397 | } |
255 | |
398 | |
256 | // compare hash entries, used when all keys are bytestrings |
399 | // compare hash entries, used when all keys are bytestrings |
257 | static int |
400 | static int |
258 | he_cmp_fast (const void *a_, const void *b_) |
401 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
263 | HE *b = *(HE **)b_; |
406 | HE *b = *(HE **)b_; |
264 | |
407 | |
265 | STRLEN la = HeKLEN (a); |
408 | STRLEN la = HeKLEN (a); |
266 | STRLEN lb = HeKLEN (b); |
409 | STRLEN lb = HeKLEN (b); |
267 | |
410 | |
268 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
411 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
269 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
412 | cmp = lb - la; |
270 | |
413 | |
271 | return cmp; |
414 | return cmp; |
272 | } |
415 | } |
273 | |
416 | |
274 | // compare hash entries, used when some keys are sv's or utf-x |
417 | // compare hash entries, used when some keys are sv's or utf-x |
275 | static int |
418 | static int |
276 | he_cmp_slow (const void *a, const void *b) |
419 | he_cmp_slow (const void *a, const void *b) |
277 | { |
420 | { |
278 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
421 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
279 | } |
422 | } |
280 | |
423 | |
281 | static void |
424 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
425 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
426 | { |
|
|
427 | HE *he; |
284 | int count, i; |
428 | int count; |
285 | |
429 | |
286 | encode_ch (enc, '{'); NL; ++enc->indent; |
430 | if (enc->indent >= enc->maxdepth) |
|
|
431 | croak ("data structure too deep (hit recursion limit)"); |
287 | |
432 | |
288 | if ((count = hv_iterinit (hv))) |
433 | encode_ch (enc, '{'); |
289 | { |
434 | |
290 | // for canonical output we have to sort by keys first |
435 | // for canonical output we have to sort by keys first |
291 | // actually, this is mostly due to the stupid so-called |
436 | // actually, this is mostly due to the stupid so-called |
292 | // security workaround added somewhere in 5.8.x. |
437 | // security workaround added somewhere in 5.8.x. |
293 | // that randomises hash orderings |
438 | // that randomises hash orderings |
294 | if (enc->flags & F_CANONICAL) |
439 | if (enc->json.flags & F_CANONICAL) |
|
|
440 | { |
|
|
441 | int count = hv_iterinit (hv); |
|
|
442 | |
|
|
443 | if (SvMAGICAL (hv)) |
295 | { |
444 | { |
296 | HE *he, *hes [count]; |
445 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
446 | // but I don't care for the speed of this special case. |
|
|
447 | // note also that we will run into undefined behaviour when the two iterations |
|
|
448 | // do not result in the same count, something I might care for in some later release. |
|
|
449 | |
|
|
450 | count = 0; |
|
|
451 | while (hv_iternext (hv)) |
|
|
452 | ++count; |
|
|
453 | |
|
|
454 | hv_iterinit (hv); |
|
|
455 | } |
|
|
456 | |
|
|
457 | if (count) |
|
|
458 | { |
297 | int fast = 1; |
459 | int i, fast = 1; |
|
|
460 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
461 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
462 | #else |
|
|
463 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
464 | #endif |
298 | |
465 | |
299 | i = 0; |
466 | i = 0; |
300 | while ((he = hv_iternext (hv))) |
467 | while ((he = hv_iternext (hv))) |
301 | { |
468 | { |
302 | hes [i++] = he; |
469 | hes [i++] = he; |
… | |
… | |
308 | |
475 | |
309 | if (fast) |
476 | if (fast) |
310 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
477 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
311 | else |
478 | else |
312 | { |
479 | { |
313 | // hack to disable "use bytes" |
480 | // hack to forcefully disable "use bytes" |
314 | COP *oldcop = PL_curcop, cop; |
481 | COP cop = *PL_curcop; |
315 | cop.op_private = 0; |
482 | cop.op_private = 0; |
|
|
483 | |
|
|
484 | ENTER; |
|
|
485 | SAVETMPS; |
|
|
486 | |
|
|
487 | SAVEVPTR (PL_curcop); |
316 | PL_curcop = &cop; |
488 | PL_curcop = &cop; |
317 | |
489 | |
318 | SAVETMPS; |
|
|
319 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
490 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
491 | |
320 | FREETMPS; |
492 | FREETMPS; |
321 | |
493 | LEAVE; |
322 | PL_curcop = oldcop; |
|
|
323 | } |
494 | } |
324 | |
495 | |
325 | for (i = 0; i < count; ++i) |
496 | encode_nl (enc); ++enc->indent; |
|
|
497 | |
|
|
498 | while (count--) |
326 | { |
499 | { |
327 | INDENT; |
500 | encode_indent (enc); |
|
|
501 | he = hes [count]; |
328 | encode_he (enc, hes [i]); |
502 | encode_hk (enc, he); |
|
|
503 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
329 | |
504 | |
330 | if (i < count - 1) |
505 | if (count) |
331 | COMMA; |
506 | encode_comma (enc); |
332 | } |
507 | } |
333 | |
508 | |
|
|
509 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
510 | } |
|
|
511 | } |
|
|
512 | else |
|
|
513 | { |
|
|
514 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
515 | if ((he = hv_iternext (hv))) |
334 | NL; |
516 | { |
|
|
517 | encode_nl (enc); ++enc->indent; |
|
|
518 | |
|
|
519 | for (;;) |
|
|
520 | { |
|
|
521 | encode_indent (enc); |
|
|
522 | encode_hk (enc, he); |
|
|
523 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
|
|
524 | |
|
|
525 | if (!(he = hv_iternext (hv))) |
|
|
526 | break; |
|
|
527 | |
|
|
528 | encode_comma (enc); |
|
|
529 | } |
|
|
530 | |
|
|
531 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
532 | } |
|
|
533 | } |
|
|
534 | |
|
|
535 | encode_ch (enc, '}'); |
|
|
536 | } |
|
|
537 | |
|
|
538 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
539 | static void |
|
|
540 | encode_rv (enc_t *enc, SV *sv) |
|
|
541 | { |
|
|
542 | svtype svt; |
|
|
543 | |
|
|
544 | SvGETMAGIC (sv); |
|
|
545 | svt = SvTYPE (sv); |
|
|
546 | |
|
|
547 | if (expect_false (SvOBJECT (sv))) |
|
|
548 | { |
|
|
549 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
550 | ? json_boolean_stash |
|
|
551 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
552 | |
|
|
553 | if (SvSTASH (sv) == stash) |
|
|
554 | { |
|
|
555 | if (SvIV (sv)) |
|
|
556 | encode_str (enc, "true", 4, 0); |
|
|
557 | else |
|
|
558 | encode_str (enc, "false", 5, 0); |
335 | } |
559 | } |
336 | else |
560 | else |
337 | { |
561 | { |
338 | SV *sv; |
562 | #if 0 |
339 | HE *he = hv_iternext (hv); |
563 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
340 | |
|
|
341 | for (;;) |
|
|
342 | { |
564 | { |
343 | INDENT; |
565 | // not yet |
344 | encode_he (enc, he); |
|
|
345 | |
|
|
346 | if (!(he = hv_iternext (hv))) |
|
|
347 | break; |
|
|
348 | |
|
|
349 | COMMA; |
|
|
350 | } |
566 | } |
351 | |
567 | #endif |
|
|
568 | if (enc->json.flags & F_CONV_BLESSED) |
352 | NL; |
569 | { |
|
|
570 | // we re-bless the reference to get overload and other niceties right |
|
|
571 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
|
|
572 | |
|
|
573 | if (to_json) |
|
|
574 | { |
|
|
575 | dSP; |
|
|
576 | |
|
|
577 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
578 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
579 | |
|
|
580 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
581 | PUTBACK; |
|
|
582 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
583 | SPAGAIN; |
|
|
584 | |
|
|
585 | // catch this surprisingly common error |
|
|
586 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
587 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
588 | |
|
|
589 | sv = POPs; |
|
|
590 | PUTBACK; |
|
|
591 | |
|
|
592 | encode_sv (enc, sv); |
|
|
593 | |
|
|
594 | FREETMPS; LEAVE; |
|
|
595 | } |
|
|
596 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
597 | encode_str (enc, "null", 4, 0); |
|
|
598 | else |
|
|
599 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
600 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
601 | } |
|
|
602 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
603 | encode_str (enc, "null", 4, 0); |
|
|
604 | else |
|
|
605 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
606 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
353 | } |
607 | } |
354 | } |
608 | } |
|
|
609 | else if (svt == SVt_PVHV) |
|
|
610 | encode_hv (enc, (HV *)sv); |
|
|
611 | else if (svt == SVt_PVAV) |
|
|
612 | encode_av (enc, (AV *)sv); |
|
|
613 | else if (svt < SVt_PVAV) |
|
|
614 | { |
|
|
615 | STRLEN len = 0; |
|
|
616 | char *pv = svt ? SvPV (sv, len) : 0; |
355 | |
617 | |
356 | --enc->indent; INDENT; encode_ch (enc, '}'); |
618 | if (len == 1 && *pv == '1') |
|
|
619 | encode_str (enc, "true", 4, 0); |
|
|
620 | else if (len == 1 && *pv == '0') |
|
|
621 | encode_str (enc, "false", 5, 0); |
|
|
622 | else |
|
|
623 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
624 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
625 | } |
|
|
626 | else |
|
|
627 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
628 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
357 | } |
629 | } |
358 | |
630 | |
359 | static void |
631 | static void |
360 | encode_sv (enc_t *enc, SV *sv) |
632 | encode_sv (enc_t *enc, SV *sv) |
361 | { |
633 | { |
… | |
… | |
369 | encode_str (enc, str, len, SvUTF8 (sv)); |
641 | encode_str (enc, str, len, SvUTF8 (sv)); |
370 | encode_ch (enc, '"'); |
642 | encode_ch (enc, '"'); |
371 | } |
643 | } |
372 | else if (SvNOKp (sv)) |
644 | else if (SvNOKp (sv)) |
373 | { |
645 | { |
|
|
646 | // trust that perl will do the right thing w.r.t. JSON syntax. |
374 | need (enc, NV_DIG + 32); |
647 | need (enc, NV_DIG + 32); |
375 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
648 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
376 | enc->cur += strlen (enc->cur); |
649 | enc->cur += strlen (enc->cur); |
377 | } |
650 | } |
378 | else if (SvIOKp (sv)) |
651 | else if (SvIOKp (sv)) |
379 | { |
652 | { |
|
|
653 | // we assume we can always read an IV as a UV and vice versa |
|
|
654 | // we assume two's complement |
|
|
655 | // we assume no aliasing issues in the union |
|
|
656 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
|
|
657 | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
|
|
658 | { |
|
|
659 | // optimise the "small number case" |
|
|
660 | // code will likely be branchless and use only a single multiplication |
|
|
661 | // works for numbers up to 59074 |
|
|
662 | I32 i = SvIVX (sv); |
|
|
663 | U32 u; |
|
|
664 | char digit, nz = 0; |
|
|
665 | |
380 | need (enc, 64); |
666 | need (enc, 6); |
|
|
667 | |
|
|
668 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
669 | u = i < 0 ? -i : i; |
|
|
670 | |
|
|
671 | // convert to 4.28 fixed-point representation |
|
|
672 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
673 | |
|
|
674 | // now output digit by digit, each time masking out the integer part |
|
|
675 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
676 | // resulting in a net multiplication by 10. |
|
|
677 | // we always write the digit to memory but conditionally increment |
|
|
678 | // the pointer, to enable the use of conditional move instructions. |
|
|
679 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
|
|
680 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
|
|
681 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
|
|
682 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
|
|
683 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
684 | } |
|
|
685 | else |
|
|
686 | { |
|
|
687 | // large integer, use the (rather slow) snprintf way. |
|
|
688 | need (enc, IVUV_MAXCHARS); |
381 | enc->cur += |
689 | enc->cur += |
382 | SvIsUV(sv) |
690 | SvIsUV(sv) |
383 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
691 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
384 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
692 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
|
|
693 | } |
385 | } |
694 | } |
386 | else if (SvROK (sv)) |
695 | else if (SvROK (sv)) |
387 | { |
696 | encode_rv (enc, SvRV (sv)); |
388 | if (!--enc->max_recurse) |
|
|
389 | croak ("data structure too deep (hit recursion limit)"); |
|
|
390 | |
|
|
391 | sv = SvRV (sv); |
|
|
392 | |
|
|
393 | switch (SvTYPE (sv)) |
|
|
394 | { |
|
|
395 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
|
|
396 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
|
|
397 | |
|
|
398 | default: |
|
|
399 | croak ("JSON can only represent references to arrays or hashes"); |
|
|
400 | } |
|
|
401 | } |
|
|
402 | else if (!SvOK (sv)) |
697 | else if (!SvOK (sv)) |
403 | encode_str (enc, "null", 4, 0); |
698 | encode_str (enc, "null", 4, 0); |
404 | else |
699 | else |
405 | croak ("encountered perl type that JSON cannot handle"); |
700 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
701 | SvPV_nolen (sv), SvFLAGS (sv)); |
406 | } |
702 | } |
407 | |
703 | |
408 | static SV * |
704 | static SV * |
409 | encode_json (SV *scalar, UV flags) |
705 | encode_json (SV *scalar, JSON *json) |
410 | { |
706 | { |
411 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
|
|
412 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
|
|
413 | |
|
|
414 | enc_t enc; |
707 | enc_t enc; |
415 | enc.flags = flags; |
708 | |
|
|
709 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
|
|
710 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
|
|
711 | |
|
|
712 | enc.json = *json; |
416 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
713 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
417 | enc.cur = SvPVX (enc.sv); |
714 | enc.cur = SvPVX (enc.sv); |
418 | enc.end = SvEND (enc.sv); |
715 | enc.end = SvEND (enc.sv); |
419 | enc.max_recurse = 0; |
|
|
420 | enc.indent = 0; |
716 | enc.indent = 0; |
|
|
717 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
718 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
719 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
720 | : 0x110000UL; |
421 | |
721 | |
422 | SvPOK_only (enc.sv); |
722 | SvPOK_only (enc.sv); |
423 | encode_sv (&enc, scalar); |
723 | encode_sv (&enc, scalar); |
424 | |
724 | |
|
|
725 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
726 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
727 | |
425 | if (!(flags & (F_ASCII | F_UTF8))) |
728 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
426 | SvUTF8_on (enc.sv); |
729 | SvUTF8_on (enc.sv); |
427 | |
730 | |
428 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
429 | |
|
|
430 | #ifdef SvPV_shrink_to_cur |
|
|
431 | if (enc.flags & F_SHRINK) |
731 | if (enc.json.flags & F_SHRINK) |
432 | SvPV_shrink_to_cur (enc.sv); |
732 | shrink (enc.sv); |
433 | #endif |
733 | |
434 | return enc.sv; |
734 | return enc.sv; |
435 | } |
735 | } |
436 | |
736 | |
437 | ///////////////////////////////////////////////////////////////////////////// |
737 | ///////////////////////////////////////////////////////////////////////////// |
|
|
738 | // decoder |
438 | |
739 | |
439 | #define WS \ |
740 | // structure used for decoding JSON |
|
|
741 | typedef struct |
|
|
742 | { |
|
|
743 | char *cur; // current parser pointer |
|
|
744 | char *end; // end of input string |
|
|
745 | const char *err; // parse error, if != 0 |
|
|
746 | JSON json; |
|
|
747 | U32 depth; // recursion depth |
|
|
748 | U32 maxdepth; // recursion depth limit |
|
|
749 | } dec_t; |
|
|
750 | |
|
|
751 | INLINE void |
|
|
752 | decode_comment (dec_t *dec) |
|
|
753 | { |
|
|
754 | // only '#'-style comments allowed a.t.m. |
|
|
755 | |
|
|
756 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
757 | ++dec->cur; |
|
|
758 | } |
|
|
759 | |
|
|
760 | INLINE void |
|
|
761 | decode_ws (dec_t *dec) |
|
|
762 | { |
440 | for (;;) \ |
763 | for (;;) |
441 | { \ |
764 | { |
442 | char ch = *dec->cur; \ |
765 | char ch = *dec->cur; |
|
|
766 | |
443 | if (ch > 0x20 \ |
767 | if (ch > 0x20) |
|
|
768 | { |
|
|
769 | if (expect_false (ch == '#')) |
|
|
770 | { |
|
|
771 | if (dec->json.flags & F_RELAXED) |
|
|
772 | decode_comment (dec); |
|
|
773 | else |
|
|
774 | break; |
|
|
775 | } |
|
|
776 | else |
|
|
777 | break; |
|
|
778 | } |
444 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
779 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
445 | break; \ |
780 | break; // parse error, but let higher level handle it, gives better error messages |
|
|
781 | |
446 | ++dec->cur; \ |
782 | ++dec->cur; |
447 | } |
783 | } |
|
|
784 | } |
448 | |
785 | |
449 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
786 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
787 | |
450 | #define EXPECT_CH(ch) SB \ |
788 | #define EXPECT_CH(ch) SB \ |
451 | if (*dec->cur != ch) \ |
789 | if (*dec->cur != ch) \ |
452 | ERR (# ch " expected"); \ |
790 | ERR (# ch " expected"); \ |
453 | ++dec->cur; \ |
791 | ++dec->cur; \ |
454 | SE |
792 | SE |
455 | |
793 | |
|
|
794 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
795 | #define DEC_DEC_DEPTH --dec->depth |
|
|
796 | |
456 | static SV *decode_sv (dec_t *dec); |
797 | static SV *decode_sv (dec_t *dec); |
457 | |
798 | |
458 | static signed char decode_hexdigit[256]; |
799 | static signed char decode_hexdigit[256]; |
459 | |
800 | |
460 | static UV |
801 | static UV |
461 | decode_4hex (dec_t *dec) |
802 | decode_4hex (dec_t *dec) |
462 | { |
803 | { |
463 | signed char d1, d2, d3, d4; |
804 | signed char d1, d2, d3, d4; |
|
|
805 | unsigned char *cur = (unsigned char *)dec->cur; |
464 | |
806 | |
465 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
807 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
466 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
808 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
467 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
809 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
468 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
810 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
469 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
470 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
|
|
471 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
472 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
|
|
473 | |
811 | |
474 | dec->cur += 4; |
812 | dec->cur += 4; |
475 | |
813 | |
476 | return ((UV)d1) << 12 |
814 | return ((UV)d1) << 12 |
477 | | ((UV)d2) << 8 |
815 | | ((UV)d2) << 8 |
… | |
… | |
480 | |
818 | |
481 | fail: |
819 | fail: |
482 | return (UV)-1; |
820 | return (UV)-1; |
483 | } |
821 | } |
484 | |
822 | |
485 | #define APPEND_GROW(n) SB \ |
|
|
486 | if (cur + (n) >= end) \ |
|
|
487 | { \ |
|
|
488 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
489 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
490 | cur = SvPVX (sv) + ofs; \ |
|
|
491 | end = SvEND (sv); \ |
|
|
492 | } \ |
|
|
493 | SE |
|
|
494 | |
|
|
495 | #define APPEND_CH(ch) SB \ |
|
|
496 | APPEND_GROW (1); \ |
|
|
497 | *cur++ = (ch); \ |
|
|
498 | SE |
|
|
499 | |
|
|
500 | static SV * |
823 | static SV * |
501 | decode_str (dec_t *dec) |
824 | decode_str (dec_t *dec) |
502 | { |
825 | { |
503 | SV *sv = NEWSV (0,2); |
826 | SV *sv = 0; |
504 | int utf8 = 0; |
827 | int utf8 = 0; |
505 | char *cur = SvPVX (sv); |
828 | char *dec_cur = dec->cur; |
506 | char *end = SvEND (sv); |
|
|
507 | |
829 | |
508 | for (;;) |
830 | do |
509 | { |
831 | { |
510 | unsigned char ch = *(unsigned char *)dec->cur; |
832 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
833 | char *cur = buf; |
511 | |
834 | |
512 | if (ch == '"') |
835 | do |
513 | break; |
|
|
514 | else if (ch == '\\') |
|
|
515 | { |
836 | { |
516 | switch (*++dec->cur) |
837 | unsigned char ch = *(unsigned char *)dec_cur++; |
|
|
838 | |
|
|
839 | if (expect_false (ch == '"')) |
517 | { |
840 | { |
518 | case '\\': |
841 | --dec_cur; |
519 | case '/': |
842 | break; |
520 | case '"': APPEND_CH (*dec->cur++); break; |
843 | } |
521 | |
844 | else if (expect_false (ch == '\\')) |
522 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
845 | { |
523 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
846 | switch (*dec_cur) |
524 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
525 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
526 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
527 | |
|
|
528 | case 'u': |
|
|
529 | { |
847 | { |
530 | UV lo, hi; |
848 | case '\\': |
531 | ++dec->cur; |
849 | case '/': |
|
|
850 | case '"': *cur++ = *dec_cur++; break; |
532 | |
851 | |
533 | hi = decode_4hex (dec); |
852 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
534 | if (hi == (UV)-1) |
853 | case 't': ++dec_cur; *cur++ = '\011'; break; |
535 | goto fail; |
854 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
|
|
855 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
|
|
856 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
536 | |
857 | |
537 | // possibly a surrogate pair |
858 | case 'u': |
538 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
539 | { |
859 | { |
540 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
860 | UV lo, hi; |
541 | ERR ("missing low surrogate character in surrogate pair"); |
861 | ++dec_cur; |
542 | |
862 | |
543 | dec->cur += 2; |
863 | dec->cur = dec_cur; |
544 | |
|
|
545 | lo = decode_4hex (dec); |
864 | hi = decode_4hex (dec); |
|
|
865 | dec_cur = dec->cur; |
546 | if (lo == (UV)-1) |
866 | if (hi == (UV)-1) |
547 | goto fail; |
867 | goto fail; |
548 | |
868 | |
|
|
869 | // possibly a surrogate pair |
|
|
870 | if (hi >= 0xd800) |
|
|
871 | if (hi < 0xdc00) |
|
|
872 | { |
|
|
873 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
|
|
874 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
875 | |
|
|
876 | dec_cur += 2; |
|
|
877 | |
|
|
878 | dec->cur = dec_cur; |
|
|
879 | lo = decode_4hex (dec); |
|
|
880 | dec_cur = dec->cur; |
|
|
881 | if (lo == (UV)-1) |
|
|
882 | goto fail; |
|
|
883 | |
549 | if (lo < 0xdc00 || lo >= 0xe000) |
884 | if (lo < 0xdc00 || lo >= 0xe000) |
550 | ERR ("surrogate pair expected"); |
885 | ERR ("surrogate pair expected"); |
551 | |
886 | |
552 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
887 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
888 | } |
|
|
889 | else if (hi < 0xe000) |
|
|
890 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
891 | |
|
|
892 | if (hi >= 0x80) |
|
|
893 | { |
|
|
894 | utf8 = 1; |
|
|
895 | |
|
|
896 | cur = encode_utf8 (cur, hi); |
|
|
897 | } |
|
|
898 | else |
|
|
899 | *cur++ = hi; |
553 | } |
900 | } |
554 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
555 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
556 | |
|
|
557 | if (hi >= 0x80) |
|
|
558 | { |
901 | break; |
559 | utf8 = 1; |
|
|
560 | |
902 | |
561 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
562 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
563 | } |
|
|
564 | else |
903 | default: |
565 | APPEND_CH (hi); |
904 | --dec_cur; |
|
|
905 | ERR ("illegal backslash escape sequence in string"); |
566 | } |
906 | } |
567 | break; |
907 | } |
|
|
908 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
|
|
909 | *cur++ = ch; |
|
|
910 | else if (ch >= 0x80) |
|
|
911 | { |
|
|
912 | STRLEN clen; |
|
|
913 | UV uch; |
568 | |
914 | |
569 | default: |
|
|
570 | --dec->cur; |
915 | --dec_cur; |
571 | ERR ("illegal backslash escape sequence in string"); |
916 | |
|
|
917 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
|
|
918 | if (clen == (STRLEN)-1) |
|
|
919 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
920 | |
|
|
921 | do |
|
|
922 | *cur++ = *dec_cur++; |
|
|
923 | while (--clen); |
|
|
924 | |
|
|
925 | utf8 = 1; |
|
|
926 | } |
|
|
927 | else |
|
|
928 | { |
|
|
929 | --dec_cur; |
|
|
930 | |
|
|
931 | if (!ch) |
|
|
932 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
933 | else |
|
|
934 | ERR ("invalid character encountered while parsing JSON string"); |
572 | } |
935 | } |
573 | } |
936 | } |
574 | else if (ch >= 0x20 && ch <= 0x7f) |
937 | while (cur < buf + SHORT_STRING_LEN); |
575 | APPEND_CH (*dec->cur++); |
938 | |
576 | else if (ch >= 0x80) |
|
|
577 | { |
939 | { |
578 | STRLEN clen; |
940 | STRLEN len = cur - buf; |
579 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
|
|
580 | if (clen == (STRLEN)-1) |
|
|
581 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
582 | |
941 | |
583 | APPEND_GROW (clen); |
942 | if (sv) |
584 | do |
|
|
585 | { |
943 | { |
586 | *cur++ = *dec->cur++; |
944 | SvGROW (sv, SvCUR (sv) + len + 1); |
|
|
945 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
|
|
946 | SvCUR_set (sv, SvCUR (sv) + len); |
587 | } |
947 | } |
588 | while (--clen); |
|
|
589 | |
|
|
590 | utf8 = 1; |
|
|
591 | } |
|
|
592 | else if (dec->cur == dec->end) |
|
|
593 | ERR ("unexpected end of string while parsing json string"); |
|
|
594 | else |
948 | else |
595 | ERR ("invalid character encountered"); |
949 | sv = newSVpvn (buf, len); |
596 | } |
950 | } |
|
|
951 | } |
|
|
952 | while (*dec_cur != '"'); |
597 | |
953 | |
598 | ++dec->cur; |
954 | ++dec_cur; |
599 | |
955 | |
600 | SvCUR_set (sv, cur - SvPVX (sv)); |
956 | if (sv) |
601 | |
957 | { |
602 | SvPOK_only (sv); |
958 | SvPOK_only (sv); |
603 | *SvEND (sv) = 0; |
959 | *SvEND (sv) = 0; |
604 | |
960 | |
605 | if (utf8) |
961 | if (utf8) |
606 | SvUTF8_on (sv); |
962 | SvUTF8_on (sv); |
|
|
963 | } |
|
|
964 | else |
|
|
965 | sv = newSVpvn ("", 0); |
607 | |
966 | |
608 | #ifdef SvPV_shrink_to_cur |
967 | dec->cur = dec_cur; |
609 | if (dec->flags & F_SHRINK) |
|
|
610 | SvPV_shrink_to_cur (sv); |
|
|
611 | #endif |
|
|
612 | |
|
|
613 | return sv; |
968 | return sv; |
614 | |
969 | |
615 | fail: |
970 | fail: |
616 | SvREFCNT_dec (sv); |
971 | dec->cur = dec_cur; |
617 | return 0; |
972 | return 0; |
618 | } |
973 | } |
619 | |
974 | |
620 | static SV * |
975 | static SV * |
621 | decode_num (dec_t *dec) |
976 | decode_num (dec_t *dec) |
… | |
… | |
679 | is_nv = 1; |
1034 | is_nv = 1; |
680 | } |
1035 | } |
681 | |
1036 | |
682 | if (!is_nv) |
1037 | if (!is_nv) |
683 | { |
1038 | { |
684 | UV uv; |
1039 | int len = dec->cur - start; |
685 | int numtype = grok_number (start, dec->cur - start, &uv); |
1040 | |
686 | if (numtype & IS_NUMBER_IN_UV) |
1041 | // special case the rather common 1..5-digit-int case |
687 | if (numtype & IS_NUMBER_NEG) |
1042 | if (*start == '-') |
|
|
1043 | switch (len) |
688 | { |
1044 | { |
689 | if (uv < (UV)IV_MIN) |
1045 | case 2: return newSViv (-( start [1] - '0' * 1)); |
690 | return newSViv (-(IV)uv); |
1046 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
1047 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
1048 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1049 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
691 | } |
1050 | } |
|
|
1051 | else |
|
|
1052 | switch (len) |
|
|
1053 | { |
|
|
1054 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
1055 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
1056 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
1057 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1058 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
|
|
1059 | } |
|
|
1060 | |
|
|
1061 | { |
|
|
1062 | UV uv; |
|
|
1063 | int numtype = grok_number (start, len, &uv); |
|
|
1064 | if (numtype & IS_NUMBER_IN_UV) |
|
|
1065 | if (numtype & IS_NUMBER_NEG) |
|
|
1066 | { |
|
|
1067 | if (uv < (UV)IV_MIN) |
|
|
1068 | return newSViv (-(IV)uv); |
|
|
1069 | } |
692 | else |
1070 | else |
693 | return newSVuv (uv); |
1071 | return newSVuv (uv); |
694 | } |
1072 | } |
695 | |
1073 | |
|
|
1074 | len -= *start == '-' ? 1 : 0; |
|
|
1075 | |
|
|
1076 | // does not fit into IV or UV, try NV |
|
|
1077 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1078 | #if defined (LDBL_DIG) |
|
|
1079 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1080 | #endif |
|
|
1081 | ) |
|
|
1082 | // fits into NV without loss of precision |
|
|
1083 | return newSVnv (Atof (start)); |
|
|
1084 | |
|
|
1085 | // everything else fails, convert it to a string |
|
|
1086 | return newSVpvn (start, dec->cur - start); |
|
|
1087 | } |
|
|
1088 | |
|
|
1089 | // loss of precision here |
696 | return newSVnv (Atof (start)); |
1090 | return newSVnv (Atof (start)); |
697 | |
1091 | |
698 | fail: |
1092 | fail: |
699 | return 0; |
1093 | return 0; |
700 | } |
1094 | } |
… | |
… | |
702 | static SV * |
1096 | static SV * |
703 | decode_av (dec_t *dec) |
1097 | decode_av (dec_t *dec) |
704 | { |
1098 | { |
705 | AV *av = newAV (); |
1099 | AV *av = newAV (); |
706 | |
1100 | |
707 | WS; |
1101 | DEC_INC_DEPTH; |
|
|
1102 | decode_ws (dec); |
|
|
1103 | |
708 | if (*dec->cur == ']') |
1104 | if (*dec->cur == ']') |
709 | ++dec->cur; |
1105 | ++dec->cur; |
710 | else |
1106 | else |
711 | for (;;) |
1107 | for (;;) |
712 | { |
1108 | { |
… | |
… | |
716 | if (!value) |
1112 | if (!value) |
717 | goto fail; |
1113 | goto fail; |
718 | |
1114 | |
719 | av_push (av, value); |
1115 | av_push (av, value); |
720 | |
1116 | |
721 | WS; |
1117 | decode_ws (dec); |
722 | |
1118 | |
723 | if (*dec->cur == ']') |
1119 | if (*dec->cur == ']') |
724 | { |
1120 | { |
725 | ++dec->cur; |
1121 | ++dec->cur; |
726 | break; |
1122 | break; |
… | |
… | |
728 | |
1124 | |
729 | if (*dec->cur != ',') |
1125 | if (*dec->cur != ',') |
730 | ERR (", or ] expected while parsing array"); |
1126 | ERR (", or ] expected while parsing array"); |
731 | |
1127 | |
732 | ++dec->cur; |
1128 | ++dec->cur; |
|
|
1129 | |
|
|
1130 | decode_ws (dec); |
|
|
1131 | |
|
|
1132 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1133 | { |
|
|
1134 | ++dec->cur; |
|
|
1135 | break; |
|
|
1136 | } |
733 | } |
1137 | } |
734 | |
1138 | |
|
|
1139 | DEC_DEC_DEPTH; |
735 | return newRV_noinc ((SV *)av); |
1140 | return newRV_noinc ((SV *)av); |
736 | |
1141 | |
737 | fail: |
1142 | fail: |
738 | SvREFCNT_dec (av); |
1143 | SvREFCNT_dec (av); |
|
|
1144 | DEC_DEC_DEPTH; |
739 | return 0; |
1145 | return 0; |
740 | } |
1146 | } |
741 | |
1147 | |
742 | static SV * |
1148 | static SV * |
743 | decode_hv (dec_t *dec) |
1149 | decode_hv (dec_t *dec) |
744 | { |
1150 | { |
|
|
1151 | SV *sv; |
745 | HV *hv = newHV (); |
1152 | HV *hv = newHV (); |
746 | |
1153 | |
747 | WS; |
1154 | DEC_INC_DEPTH; |
|
|
1155 | decode_ws (dec); |
|
|
1156 | |
748 | if (*dec->cur == '}') |
1157 | if (*dec->cur == '}') |
749 | ++dec->cur; |
1158 | ++dec->cur; |
750 | else |
1159 | else |
751 | for (;;) |
1160 | for (;;) |
752 | { |
1161 | { |
753 | SV *key, *value; |
|
|
754 | |
|
|
755 | WS; EXPECT_CH ('"'); |
1162 | EXPECT_CH ('"'); |
756 | |
1163 | |
757 | key = decode_str (dec); |
1164 | // heuristic: assume that |
758 | if (!key) |
1165 | // a) decode_str + hv_store_ent are abysmally slow. |
759 | goto fail; |
1166 | // b) most hash keys are short, simple ascii text. |
|
|
1167 | // => try to "fast-match" such strings to avoid |
|
|
1168 | // the overhead of decode_str + hv_store_ent. |
|
|
1169 | { |
|
|
1170 | SV *value; |
|
|
1171 | char *p = dec->cur; |
|
|
1172 | char *e = p + 24; // only try up to 24 bytes |
760 | |
1173 | |
761 | WS; EXPECT_CH (':'); |
1174 | for (;;) |
762 | |
|
|
763 | value = decode_sv (dec); |
|
|
764 | if (!value) |
|
|
765 | { |
1175 | { |
|
|
1176 | // the >= 0x80 is true on most architectures |
|
|
1177 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1178 | { |
|
|
1179 | // slow path, back up and use decode_str |
|
|
1180 | SV *key = decode_str (dec); |
|
|
1181 | if (!key) |
|
|
1182 | goto fail; |
|
|
1183 | |
|
|
1184 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1185 | |
|
|
1186 | decode_ws (dec); |
|
|
1187 | value = decode_sv (dec); |
|
|
1188 | if (!value) |
|
|
1189 | { |
|
|
1190 | SvREFCNT_dec (key); |
|
|
1191 | goto fail; |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | hv_store_ent (hv, key, value, 0); |
766 | SvREFCNT_dec (key); |
1195 | SvREFCNT_dec (key); |
|
|
1196 | |
|
|
1197 | break; |
|
|
1198 | } |
|
|
1199 | else if (*p == '"') |
|
|
1200 | { |
|
|
1201 | // fast path, got a simple key |
|
|
1202 | char *key = dec->cur; |
|
|
1203 | int len = p - key; |
|
|
1204 | dec->cur = p + 1; |
|
|
1205 | |
|
|
1206 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1207 | |
|
|
1208 | decode_ws (dec); |
|
|
1209 | value = decode_sv (dec); |
|
|
1210 | if (!value) |
767 | goto fail; |
1211 | goto fail; |
|
|
1212 | |
|
|
1213 | hv_store (hv, key, len, value, 0); |
|
|
1214 | |
|
|
1215 | break; |
|
|
1216 | } |
|
|
1217 | |
|
|
1218 | ++p; |
768 | } |
1219 | } |
769 | |
|
|
770 | //TODO: optimise |
|
|
771 | hv_store_ent (hv, key, value, 0); |
|
|
772 | |
|
|
773 | WS; |
1220 | } |
|
|
1221 | |
|
|
1222 | decode_ws (dec); |
774 | |
1223 | |
775 | if (*dec->cur == '}') |
1224 | if (*dec->cur == '}') |
776 | { |
1225 | { |
777 | ++dec->cur; |
1226 | ++dec->cur; |
778 | break; |
1227 | break; |
… | |
… | |
780 | |
1229 | |
781 | if (*dec->cur != ',') |
1230 | if (*dec->cur != ',') |
782 | ERR (", or } expected while parsing object/hash"); |
1231 | ERR (", or } expected while parsing object/hash"); |
783 | |
1232 | |
784 | ++dec->cur; |
1233 | ++dec->cur; |
|
|
1234 | |
|
|
1235 | decode_ws (dec); |
|
|
1236 | |
|
|
1237 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1238 | { |
|
|
1239 | ++dec->cur; |
|
|
1240 | break; |
|
|
1241 | } |
785 | } |
1242 | } |
786 | |
1243 | |
|
|
1244 | DEC_DEC_DEPTH; |
787 | return newRV_noinc ((SV *)hv); |
1245 | sv = newRV_noinc ((SV *)hv); |
|
|
1246 | |
|
|
1247 | // check filter callbacks |
|
|
1248 | if (dec->json.flags & F_HOOK) |
|
|
1249 | { |
|
|
1250 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1251 | { |
|
|
1252 | HE *cb, *he; |
|
|
1253 | |
|
|
1254 | hv_iterinit (hv); |
|
|
1255 | he = hv_iternext (hv); |
|
|
1256 | hv_iterinit (hv); |
|
|
1257 | |
|
|
1258 | // the next line creates a mortal sv each time its called. |
|
|
1259 | // might want to optimise this for common cases. |
|
|
1260 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1261 | |
|
|
1262 | if (cb) |
|
|
1263 | { |
|
|
1264 | dSP; |
|
|
1265 | int count; |
|
|
1266 | |
|
|
1267 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1268 | XPUSHs (HeVAL (he)); |
|
|
1269 | |
|
|
1270 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1271 | |
|
|
1272 | if (count == 1) |
|
|
1273 | { |
|
|
1274 | sv = newSVsv (POPs); |
|
|
1275 | FREETMPS; LEAVE; |
|
|
1276 | return sv; |
|
|
1277 | } |
|
|
1278 | |
|
|
1279 | FREETMPS; LEAVE; |
|
|
1280 | } |
|
|
1281 | } |
|
|
1282 | |
|
|
1283 | if (dec->json.cb_object) |
|
|
1284 | { |
|
|
1285 | dSP; |
|
|
1286 | int count; |
|
|
1287 | |
|
|
1288 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1289 | XPUSHs (sv_2mortal (sv)); |
|
|
1290 | |
|
|
1291 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1292 | |
|
|
1293 | if (count == 1) |
|
|
1294 | { |
|
|
1295 | sv = newSVsv (POPs); |
|
|
1296 | FREETMPS; LEAVE; |
|
|
1297 | return sv; |
|
|
1298 | } |
|
|
1299 | |
|
|
1300 | SvREFCNT_inc (sv); |
|
|
1301 | FREETMPS; LEAVE; |
|
|
1302 | } |
|
|
1303 | } |
|
|
1304 | |
|
|
1305 | return sv; |
788 | |
1306 | |
789 | fail: |
1307 | fail: |
790 | SvREFCNT_dec (hv); |
1308 | SvREFCNT_dec (hv); |
|
|
1309 | DEC_DEC_DEPTH; |
791 | return 0; |
1310 | return 0; |
792 | } |
1311 | } |
793 | |
1312 | |
794 | static SV * |
1313 | static SV * |
795 | decode_sv (dec_t *dec) |
1314 | decode_sv (dec_t *dec) |
796 | { |
1315 | { |
797 | WS; |
1316 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1317 | // to parse everything. |
798 | switch (*dec->cur) |
1318 | switch (*dec->cur) |
799 | { |
1319 | { |
800 | case '"': ++dec->cur; return decode_str (dec); |
1320 | case '"': ++dec->cur; return decode_str (dec); |
801 | case '[': ++dec->cur; return decode_av (dec); |
1321 | case '[': ++dec->cur; return decode_av (dec); |
802 | case '{': ++dec->cur; return decode_hv (dec); |
1322 | case '{': ++dec->cur; return decode_hv (dec); |
803 | |
1323 | |
804 | case '-': |
1324 | case '-': |
805 | case '0': case '1': case '2': case '3': case '4': |
1325 | case '0': case '1': case '2': case '3': case '4': |
806 | case '5': case '6': case '7': case '8': case '9': |
1326 | case '5': case '6': case '7': case '8': case '9': |
807 | return decode_num (dec); |
1327 | return decode_num (dec); |
808 | |
1328 | |
809 | case 't': |
1329 | case 't': |
810 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1330 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
811 | { |
1331 | { |
812 | dec->cur += 4; |
1332 | dec->cur += 4; |
813 | return newSViv (1); |
1333 | #if JSON_SLOW |
|
|
1334 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1335 | #endif |
|
|
1336 | return SvREFCNT_inc (json_true); |
814 | } |
1337 | } |
815 | else |
1338 | else |
816 | ERR ("'true' expected"); |
1339 | ERR ("'true' expected"); |
817 | |
1340 | |
818 | break; |
1341 | break; |
819 | |
1342 | |
820 | case 'f': |
1343 | case 'f': |
821 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1344 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
822 | { |
1345 | { |
823 | dec->cur += 5; |
1346 | dec->cur += 5; |
824 | return newSViv (0); |
1347 | #if JSON_SLOW |
|
|
1348 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1349 | #endif |
|
|
1350 | return SvREFCNT_inc (json_false); |
825 | } |
1351 | } |
826 | else |
1352 | else |
827 | ERR ("'false' expected"); |
1353 | ERR ("'false' expected"); |
828 | |
1354 | |
829 | break; |
1355 | break; |
… | |
… | |
838 | ERR ("'null' expected"); |
1364 | ERR ("'null' expected"); |
839 | |
1365 | |
840 | break; |
1366 | break; |
841 | |
1367 | |
842 | default: |
1368 | default: |
843 | ERR ("malformed json string"); |
1369 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
844 | break; |
1370 | break; |
845 | } |
1371 | } |
846 | |
1372 | |
847 | fail: |
1373 | fail: |
848 | return 0; |
1374 | return 0; |
849 | } |
1375 | } |
850 | |
1376 | |
851 | static SV * |
1377 | static SV * |
852 | decode_json (SV *string, UV flags) |
1378 | decode_json (SV *string, JSON *json, UV *offset_return) |
853 | { |
1379 | { |
|
|
1380 | dec_t dec; |
|
|
1381 | UV offset; |
854 | SV *sv; |
1382 | SV *sv; |
855 | |
1383 | |
|
|
1384 | SvGETMAGIC (string); |
|
|
1385 | SvUPGRADE (string, SVt_PV); |
|
|
1386 | |
|
|
1387 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1388 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1389 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1390 | |
856 | if (flags & F_UTF8) |
1391 | if (json->flags & F_UTF8) |
857 | sv_utf8_downgrade (string, 0); |
1392 | sv_utf8_downgrade (string, 0); |
858 | else |
1393 | else |
859 | sv_utf8_upgrade (string); |
1394 | sv_utf8_upgrade (string); |
860 | |
1395 | |
861 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1396 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
862 | |
1397 | |
863 | dec_t dec; |
1398 | dec.json = *json; |
864 | dec.flags = flags; |
|
|
865 | dec.cur = SvPVX (string); |
1399 | dec.cur = SvPVX (string); |
866 | dec.end = SvEND (string); |
1400 | dec.end = SvEND (string); |
867 | dec.err = 0; |
1401 | dec.err = 0; |
|
|
1402 | dec.depth = 0; |
|
|
1403 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
868 | |
1404 | |
|
|
1405 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1406 | dec.json.flags |= F_HOOK; |
|
|
1407 | |
|
|
1408 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1409 | |
|
|
1410 | decode_ws (&dec); |
869 | sv = decode_sv (&dec); |
1411 | sv = decode_sv (&dec); |
870 | |
1412 | |
|
|
1413 | if (!(offset_return || !sv)) |
|
|
1414 | { |
|
|
1415 | // check for trailing garbage |
|
|
1416 | decode_ws (&dec); |
|
|
1417 | |
|
|
1418 | if (*dec.cur) |
|
|
1419 | { |
|
|
1420 | dec.err = "garbage after JSON object"; |
|
|
1421 | SvREFCNT_dec (sv); |
|
|
1422 | sv = 0; |
|
|
1423 | } |
|
|
1424 | } |
|
|
1425 | |
|
|
1426 | if (offset_return || !sv) |
|
|
1427 | { |
|
|
1428 | offset = dec.json.flags & F_UTF8 |
|
|
1429 | ? dec.cur - SvPVX (string) |
|
|
1430 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1431 | |
|
|
1432 | if (offset_return) |
|
|
1433 | *offset_return = offset; |
|
|
1434 | } |
|
|
1435 | |
871 | if (!sv) |
1436 | if (!sv) |
872 | { |
1437 | { |
873 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
|
|
874 | SV *uni = sv_newmortal (); |
1438 | SV *uni = sv_newmortal (); |
|
|
1439 | |
875 | // horrible hack to silence warning inside pv_uni_display |
1440 | // horrible hack to silence warning inside pv_uni_display |
876 | COP cop; |
1441 | COP cop = *PL_curcop; |
877 | memset (&cop, 0, sizeof (cop)); |
|
|
878 | cop.cop_warnings = pWARN_NONE; |
1442 | cop.cop_warnings = pWARN_NONE; |
|
|
1443 | ENTER; |
879 | SAVEVPTR (PL_curcop); |
1444 | SAVEVPTR (PL_curcop); |
880 | PL_curcop = &cop; |
1445 | PL_curcop = &cop; |
881 | |
|
|
882 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1446 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
1447 | LEAVE; |
|
|
1448 | |
883 | croak ("%s, at character offset %d (%s)", |
1449 | croak ("%s, at character offset %d [\"%s\"]", |
884 | dec.err, |
1450 | dec.err, |
885 | (int)offset, |
1451 | (int)offset, |
886 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1452 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
887 | } |
1453 | } |
888 | |
1454 | |
889 | sv = sv_2mortal (sv); |
1455 | sv = sv_2mortal (sv); |
890 | |
1456 | |
891 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1457 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
892 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
1458 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
893 | |
1459 | |
894 | return sv; |
1460 | return sv; |
895 | } |
1461 | } |
896 | |
1462 | |
|
|
1463 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1464 | // XS interface functions |
|
|
1465 | |
897 | MODULE = JSON::XS PACKAGE = JSON::XS |
1466 | MODULE = JSON::XS PACKAGE = JSON::XS |
898 | |
1467 | |
899 | BOOT: |
1468 | BOOT: |
900 | { |
1469 | { |
901 | int i; |
1470 | int i; |
902 | |
1471 | |
903 | memset (decode_hexdigit, 0xff, 256); |
|
|
904 | for (i = 10; i--; ) |
1472 | for (i = 0; i < 256; ++i) |
905 | decode_hexdigit ['0' + i] = i; |
1473 | decode_hexdigit [i] = |
|
|
1474 | i >= '0' && i <= '9' ? i - '0' |
|
|
1475 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
|
|
1476 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
|
|
1477 | : -1; |
906 | |
1478 | |
907 | for (i = 7; i--; ) |
|
|
908 | { |
|
|
909 | decode_hexdigit ['a' + i] = 10 + i; |
|
|
910 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
911 | } |
|
|
912 | |
|
|
913 | json_stash = gv_stashpv ("JSON::XS", 1); |
1479 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1480 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1481 | |
|
|
1482 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1483 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
914 | } |
1484 | } |
915 | |
1485 | |
916 | PROTOTYPES: DISABLE |
1486 | PROTOTYPES: DISABLE |
917 | |
1487 | |
918 | SV *new (char *dummy) |
1488 | void CLONE (...) |
919 | CODE: |
1489 | CODE: |
920 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1490 | json_stash = 0; |
|
|
1491 | json_boolean_stash = 0; |
|
|
1492 | |
|
|
1493 | void new (char *klass) |
|
|
1494 | PPCODE: |
|
|
1495 | { |
|
|
1496 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1497 | SvPOK_only (pv); |
|
|
1498 | Zero (SvPVX (pv), 1, JSON); |
|
|
1499 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1500 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1501 | newRV_noinc (pv), |
|
|
1502 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1503 | ))); |
|
|
1504 | } |
|
|
1505 | |
|
|
1506 | void ascii (JSON *self, int enable = 1) |
|
|
1507 | ALIAS: |
|
|
1508 | ascii = F_ASCII |
|
|
1509 | latin1 = F_LATIN1 |
|
|
1510 | utf8 = F_UTF8 |
|
|
1511 | indent = F_INDENT |
|
|
1512 | canonical = F_CANONICAL |
|
|
1513 | space_before = F_SPACE_BEFORE |
|
|
1514 | space_after = F_SPACE_AFTER |
|
|
1515 | pretty = F_PRETTY |
|
|
1516 | allow_nonref = F_ALLOW_NONREF |
|
|
1517 | shrink = F_SHRINK |
|
|
1518 | allow_blessed = F_ALLOW_BLESSED |
|
|
1519 | convert_blessed = F_CONV_BLESSED |
|
|
1520 | relaxed = F_RELAXED |
|
|
1521 | PPCODE: |
|
|
1522 | { |
|
|
1523 | if (enable) |
|
|
1524 | self->flags |= ix; |
|
|
1525 | else |
|
|
1526 | self->flags &= ~ix; |
|
|
1527 | |
|
|
1528 | XPUSHs (ST (0)); |
|
|
1529 | } |
|
|
1530 | |
|
|
1531 | void get_ascii (JSON *self) |
|
|
1532 | ALIAS: |
|
|
1533 | get_ascii = F_ASCII |
|
|
1534 | get_latin1 = F_LATIN1 |
|
|
1535 | get_utf8 = F_UTF8 |
|
|
1536 | get_indent = F_INDENT |
|
|
1537 | get_canonical = F_CANONICAL |
|
|
1538 | get_space_before = F_SPACE_BEFORE |
|
|
1539 | get_space_after = F_SPACE_AFTER |
|
|
1540 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1541 | get_shrink = F_SHRINK |
|
|
1542 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1543 | get_convert_blessed = F_CONV_BLESSED |
|
|
1544 | get_relaxed = F_RELAXED |
|
|
1545 | PPCODE: |
|
|
1546 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1547 | |
|
|
1548 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
|
|
1549 | PPCODE: |
|
|
1550 | { |
|
|
1551 | UV log2 = 0; |
|
|
1552 | |
|
|
1553 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1554 | |
|
|
1555 | while ((1UL << log2) < max_depth) |
|
|
1556 | ++log2; |
|
|
1557 | |
|
|
1558 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1559 | |
|
|
1560 | XPUSHs (ST (0)); |
|
|
1561 | } |
|
|
1562 | |
|
|
1563 | U32 get_max_depth (JSON *self) |
|
|
1564 | CODE: |
|
|
1565 | RETVAL = DEC_DEPTH (self->flags); |
921 | OUTPUT: |
1566 | OUTPUT: |
922 | RETVAL |
1567 | RETVAL |
923 | |
1568 | |
924 | SV *ascii (SV *self, int enable = 1) |
1569 | void max_size (JSON *self, UV max_size = 0) |
925 | ALIAS: |
1570 | PPCODE: |
926 | ascii = F_ASCII |
1571 | { |
927 | utf8 = F_UTF8 |
1572 | UV log2 = 0; |
928 | indent = F_INDENT |
1573 | |
929 | canonical = F_CANONICAL |
1574 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
930 | space_before = F_SPACE_BEFORE |
1575 | if (max_size == 1) max_size = 2; |
931 | space_after = F_SPACE_AFTER |
1576 | |
932 | json_rpc = F_JSON_RPC |
1577 | while ((1UL << log2) < max_size) |
933 | pretty = F_PRETTY |
1578 | ++log2; |
934 | allow_nonref = F_ALLOW_NONREF |
1579 | |
935 | shrink = F_SHRINK |
1580 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1581 | |
|
|
1582 | XPUSHs (ST (0)); |
|
|
1583 | } |
|
|
1584 | |
|
|
1585 | int get_max_size (JSON *self) |
936 | CODE: |
1586 | CODE: |
937 | { |
1587 | RETVAL = DEC_SIZE (self->flags); |
938 | UV *uv = SvJSON (self); |
|
|
939 | if (enable) |
|
|
940 | *uv |= ix; |
|
|
941 | else |
|
|
942 | *uv &= ~ix; |
|
|
943 | |
|
|
944 | RETVAL = newSVsv (self); |
|
|
945 | } |
|
|
946 | OUTPUT: |
1588 | OUTPUT: |
947 | RETVAL |
1589 | RETVAL |
948 | |
1590 | |
949 | void encode (SV *self, SV *scalar) |
1591 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
950 | PPCODE: |
1592 | PPCODE: |
951 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1593 | { |
|
|
1594 | SvREFCNT_dec (self->cb_object); |
|
|
1595 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
952 | |
1596 | |
953 | void decode (SV *self, SV *jsonstr) |
1597 | XPUSHs (ST (0)); |
|
|
1598 | } |
|
|
1599 | |
|
|
1600 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
954 | PPCODE: |
1601 | PPCODE: |
|
|
1602 | { |
|
|
1603 | if (!self->cb_sk_object) |
|
|
1604 | self->cb_sk_object = newHV (); |
|
|
1605 | |
|
|
1606 | if (SvOK (cb)) |
|
|
1607 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1608 | else |
|
|
1609 | { |
|
|
1610 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1611 | |
|
|
1612 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1613 | { |
|
|
1614 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1615 | self->cb_sk_object = 0; |
|
|
1616 | } |
|
|
1617 | } |
|
|
1618 | |
|
|
1619 | XPUSHs (ST (0)); |
|
|
1620 | } |
|
|
1621 | |
|
|
1622 | void encode (JSON *self, SV *scalar) |
|
|
1623 | PPCODE: |
|
|
1624 | XPUSHs (encode_json (scalar, self)); |
|
|
1625 | |
|
|
1626 | void decode (JSON *self, SV *jsonstr) |
|
|
1627 | PPCODE: |
955 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1628 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1629 | |
|
|
1630 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1631 | PPCODE: |
|
|
1632 | { |
|
|
1633 | UV offset; |
|
|
1634 | EXTEND (SP, 2); |
|
|
1635 | PUSHs (decode_json (jsonstr, self, &offset)); |
|
|
1636 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1637 | } |
|
|
1638 | |
|
|
1639 | void DESTROY (JSON *self) |
|
|
1640 | CODE: |
|
|
1641 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1642 | SvREFCNT_dec (self->cb_object); |
956 | |
1643 | |
957 | PROTOTYPES: ENABLE |
1644 | PROTOTYPES: ENABLE |
958 | |
1645 | |
959 | void to_json (SV *scalar) |
1646 | void encode_json (SV *scalar) |
|
|
1647 | ALIAS: |
|
|
1648 | to_json_ = 0 |
|
|
1649 | encode_json = F_UTF8 |
960 | PPCODE: |
1650 | PPCODE: |
|
|
1651 | { |
|
|
1652 | JSON json = { F_DEFAULT | ix }; |
961 | XPUSHs (encode_json (scalar, F_UTF8)); |
1653 | XPUSHs (encode_json (scalar, &json)); |
|
|
1654 | } |
962 | |
1655 | |
963 | void from_json (SV *jsonstr) |
1656 | void decode_json (SV *jsonstr) |
|
|
1657 | ALIAS: |
|
|
1658 | from_json_ = 0 |
|
|
1659 | decode_json = F_UTF8 |
964 | PPCODE: |
1660 | PPCODE: |
|
|
1661 | { |
|
|
1662 | JSON json = { F_DEFAULT | ix }; |
965 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1663 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1664 | } |
966 | |
1665 | |