… | |
… | |
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> |
8 | #include <stdio.h> |
|
|
9 | #include <limits.h> |
9 | #include <float.h> |
10 | #include <float.h> |
10 | |
11 | |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | #endif |
14 | #endif |
… | |
… | |
15 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
16 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
18 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
19 | #endif |
20 | #endif |
|
|
21 | |
|
|
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
20 | |
23 | |
21 | #define F_ASCII 0x00000001UL |
24 | #define F_ASCII 0x00000001UL |
22 | #define F_LATIN1 0x00000002UL |
25 | #define F_LATIN1 0x00000002UL |
23 | #define F_UTF8 0x00000004UL |
26 | #define F_UTF8 0x00000004UL |
24 | #define F_INDENT 0x00000008UL |
27 | #define F_INDENT 0x00000008UL |
… | |
… | |
27 | #define F_SPACE_AFTER 0x00000040UL |
30 | #define F_SPACE_AFTER 0x00000040UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
31 | #define F_ALLOW_NONREF 0x00000100UL |
29 | #define F_SHRINK 0x00000200UL |
32 | #define F_SHRINK 0x00000200UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
33 | #define F_ALLOW_BLESSED 0x00000400UL |
31 | #define F_CONV_BLESSED 0x00000800UL |
34 | #define F_CONV_BLESSED 0x00000800UL |
|
|
35 | #define F_RELAXED 0x00001000UL |
|
|
36 | |
32 | #define F_MAXDEPTH 0xf8000000UL |
37 | #define F_MAXDEPTH 0xf8000000UL |
33 | #define S_MAXDEPTH 27 |
38 | #define S_MAXDEPTH 27 |
34 | #define F_MAXSIZE 0x01f00000UL |
39 | #define F_MAXSIZE 0x01f00000UL |
35 | #define S_MAXSIZE 20 |
40 | #define S_MAXSIZE 20 |
36 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
41 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
… | |
… | |
48 | |
53 | |
49 | #define SB do { |
54 | #define SB do { |
50 | #define SE } while (0) |
55 | #define SE } while (0) |
51 | |
56 | |
52 | #if __GNUC__ >= 3 |
57 | #if __GNUC__ >= 3 |
53 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
58 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
54 | # define inline inline |
59 | # define INLINE static inline |
55 | #else |
60 | #else |
56 | # define expect(expr,value) (expr) |
61 | # define expect(expr,value) (expr) |
57 | # define inline static |
62 | # define INLINE static |
58 | #endif |
63 | #endif |
59 | |
64 | |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
65 | #define expect_false(expr) expect ((expr) != 0, 0) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
66 | #define expect_true(expr) expect ((expr) != 0, 1) |
62 | |
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 | |
63 | #ifdef USE_ITHREADS |
72 | #ifdef USE_ITHREADS |
64 | # define JSON_SLOW 1 |
73 | # define JSON_SLOW 1 |
|
|
74 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
65 | #else |
75 | #else |
66 | # define JSON_SLOW 1 |
76 | # define JSON_SLOW 0 |
|
|
77 | # define JSON_STASH json_stash |
67 | #endif |
78 | #endif |
68 | |
79 | |
69 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
70 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
|
|
82 | |
|
|
83 | enum { |
|
|
84 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
|
|
85 | INCR_M_STR, // inside string |
|
|
86 | INCR_M_BS, // inside backslash |
|
|
87 | INCR_M_JSON // outside anything, count nesting |
|
|
88 | }; |
|
|
89 | |
|
|
90 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
71 | |
91 | |
72 | typedef struct { |
92 | typedef struct { |
73 | U32 flags; |
93 | U32 flags; |
74 | SV *cb_object; |
94 | SV *cb_object; |
75 | HV *cb_sk_object; |
95 | HV *cb_sk_object; |
|
|
96 | |
|
|
97 | // for the incremental parser |
|
|
98 | SV *incr_text; // the source text so far |
|
|
99 | STRLEN incr_pos; // the current offset into the text |
|
|
100 | int incr_nest; // {[]}-nesting level |
|
|
101 | int incr_mode; |
76 | } JSON; |
102 | } JSON; |
77 | |
103 | |
78 | ///////////////////////////////////////////////////////////////////////////// |
104 | ///////////////////////////////////////////////////////////////////////////// |
79 | // utility functions |
105 | // utility functions |
80 | |
106 | |
81 | inline void |
107 | INLINE void |
82 | shrink (SV *sv) |
108 | shrink (SV *sv) |
83 | { |
109 | { |
84 | sv_utf8_downgrade (sv, 1); |
110 | sv_utf8_downgrade (sv, 1); |
85 | if (SvLEN (sv) > SvCUR (sv) + 1) |
111 | if (SvLEN (sv) > SvCUR (sv) + 1) |
86 | { |
112 | { |
… | |
… | |
95 | // decode an utf-8 character and return it, or (UV)-1 in |
121 | // decode an utf-8 character and return it, or (UV)-1 in |
96 | // case of an error. |
122 | // case of an error. |
97 | // we special-case "safe" characters from U+80 .. U+7FF, |
123 | // we special-case "safe" characters from U+80 .. U+7FF, |
98 | // but use the very good perl function to parse anything else. |
124 | // but use the very good perl function to parse anything else. |
99 | // note that we never call this function for a ascii codepoints |
125 | // note that we never call this function for a ascii codepoints |
100 | inline UV |
126 | INLINE UV |
101 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
127 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
102 | { |
128 | { |
103 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
129 | if (expect_true (len >= 2 |
104 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
130 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
105 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
131 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
106 | { |
132 | { |
107 | *clen = 2; |
133 | *clen = 2; |
108 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
134 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
109 | } |
135 | } |
110 | else |
136 | else |
111 | { |
137 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
112 | *clen = (STRLEN)-1; |
138 | } |
113 | return (UV)-1; |
139 | |
114 | } |
140 | // likewise for encoding, also never called for ascii codepoints |
|
|
141 | // this function takes advantage of this fact, although current gccs |
|
|
142 | // seem to optimise the check for >= 0x80 away anyways |
|
|
143 | INLINE unsigned char * |
|
|
144 | encode_utf8 (unsigned char *s, UV ch) |
|
|
145 | { |
|
|
146 | if (expect_false (ch < 0x000080)) |
|
|
147 | *s++ = ch; |
|
|
148 | else if (expect_true (ch < 0x000800)) |
|
|
149 | *s++ = 0xc0 | ( ch >> 6), |
|
|
150 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
151 | else if ( ch < 0x010000) |
|
|
152 | *s++ = 0xe0 | ( ch >> 12), |
|
|
153 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
154 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
155 | else if ( ch < 0x110000) |
|
|
156 | *s++ = 0xf0 | ( ch >> 18), |
|
|
157 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
158 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
159 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
160 | |
|
|
161 | return s; |
115 | } |
162 | } |
116 | |
163 | |
117 | ///////////////////////////////////////////////////////////////////////////// |
164 | ///////////////////////////////////////////////////////////////////////////// |
118 | // encoder |
165 | // encoder |
119 | |
166 | |
… | |
… | |
124 | char *end; // SvEND (sv) |
171 | char *end; // SvEND (sv) |
125 | SV *sv; // result scalar |
172 | SV *sv; // result scalar |
126 | JSON json; |
173 | JSON json; |
127 | U32 indent; // indentation level |
174 | U32 indent; // indentation level |
128 | U32 maxdepth; // max. indentation/recursion level |
175 | U32 maxdepth; // max. indentation/recursion level |
|
|
176 | UV limit; // escape character values >= this value when encoding |
129 | } enc_t; |
177 | } enc_t; |
130 | |
178 | |
131 | inline void |
179 | INLINE void |
132 | need (enc_t *enc, STRLEN len) |
180 | need (enc_t *enc, STRLEN len) |
133 | { |
181 | { |
134 | if (expect_false (enc->cur + len >= enc->end)) |
182 | if (expect_false (enc->cur + len >= enc->end)) |
135 | { |
183 | { |
136 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
184 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
138 | enc->cur = SvPVX (enc->sv) + cur; |
186 | enc->cur = SvPVX (enc->sv) + cur; |
139 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
187 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
140 | } |
188 | } |
141 | } |
189 | } |
142 | |
190 | |
143 | inline void |
191 | INLINE void |
144 | encode_ch (enc_t *enc, char ch) |
192 | encode_ch (enc_t *enc, char ch) |
145 | { |
193 | { |
146 | need (enc, 1); |
194 | need (enc, 1); |
147 | *enc->cur++ = ch; |
195 | *enc->cur++ = ch; |
148 | } |
196 | } |
… | |
… | |
202 | { |
250 | { |
203 | uch = ch; |
251 | uch = ch; |
204 | clen = 1; |
252 | clen = 1; |
205 | } |
253 | } |
206 | |
254 | |
207 | if (uch > 0x10FFFFUL) |
255 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
208 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
209 | |
|
|
210 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
|
|
211 | { |
256 | { |
212 | if (uch > 0xFFFFUL) |
257 | if (uch >= 0x10000UL) |
213 | { |
258 | { |
|
|
259 | if (uch >= 0x110000UL) |
|
|
260 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
261 | |
214 | need (enc, len += 11); |
262 | need (enc, len += 11); |
215 | sprintf (enc->cur, "\\u%04x\\u%04x", |
263 | sprintf (enc->cur, "\\u%04x\\u%04x", |
216 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
264 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
217 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
265 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
218 | enc->cur += 12; |
266 | enc->cur += 12; |
… | |
… | |
246 | while (--clen); |
294 | while (--clen); |
247 | } |
295 | } |
248 | else |
296 | else |
249 | { |
297 | { |
250 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
298 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
251 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
299 | enc->cur = encode_utf8 (enc->cur, uch); |
252 | ++str; |
300 | ++str; |
253 | } |
301 | } |
254 | } |
302 | } |
255 | } |
303 | } |
256 | } |
304 | } |
257 | |
305 | |
258 | --len; |
306 | --len; |
259 | } |
307 | } |
260 | } |
308 | } |
261 | |
309 | |
262 | inline void |
310 | INLINE void |
263 | encode_indent (enc_t *enc) |
311 | encode_indent (enc_t *enc) |
264 | { |
312 | { |
265 | if (enc->json.flags & F_INDENT) |
313 | if (enc->json.flags & F_INDENT) |
266 | { |
314 | { |
267 | int spaces = enc->indent * INDENT_STEP; |
315 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
270 | memset (enc->cur, ' ', spaces); |
318 | memset (enc->cur, ' ', spaces); |
271 | enc->cur += spaces; |
319 | enc->cur += spaces; |
272 | } |
320 | } |
273 | } |
321 | } |
274 | |
322 | |
275 | inline void |
323 | INLINE void |
276 | encode_space (enc_t *enc) |
324 | encode_space (enc_t *enc) |
277 | { |
325 | { |
278 | need (enc, 1); |
326 | need (enc, 1); |
279 | encode_ch (enc, ' '); |
327 | encode_ch (enc, ' '); |
280 | } |
328 | } |
281 | |
329 | |
282 | inline void |
330 | INLINE void |
283 | encode_nl (enc_t *enc) |
331 | encode_nl (enc_t *enc) |
284 | { |
332 | { |
285 | if (enc->json.flags & F_INDENT) |
333 | if (enc->json.flags & F_INDENT) |
286 | { |
334 | { |
287 | need (enc, 1); |
335 | need (enc, 1); |
288 | encode_ch (enc, '\n'); |
336 | encode_ch (enc, '\n'); |
289 | } |
337 | } |
290 | } |
338 | } |
291 | |
339 | |
292 | inline void |
340 | INLINE void |
293 | encode_comma (enc_t *enc) |
341 | encode_comma (enc_t *enc) |
294 | { |
342 | { |
295 | encode_ch (enc, ','); |
343 | encode_ch (enc, ','); |
296 | |
344 | |
297 | if (enc->json.flags & F_INDENT) |
345 | if (enc->json.flags & F_INDENT) |
… | |
… | |
308 | int i, len = av_len (av); |
356 | int i, len = av_len (av); |
309 | |
357 | |
310 | if (enc->indent >= enc->maxdepth) |
358 | if (enc->indent >= enc->maxdepth) |
311 | croak ("data structure too deep (hit recursion limit)"); |
359 | croak ("data structure too deep (hit recursion limit)"); |
312 | |
360 | |
313 | encode_ch (enc, '['); encode_nl (enc); |
361 | encode_ch (enc, '['); |
314 | ++enc->indent; |
362 | |
|
|
363 | if (len >= 0) |
|
|
364 | { |
|
|
365 | encode_nl (enc); ++enc->indent; |
315 | |
366 | |
316 | for (i = 0; i <= len; ++i) |
367 | for (i = 0; i <= len; ++i) |
317 | { |
368 | { |
318 | SV **svp = av_fetch (av, i, 0); |
369 | SV **svp = av_fetch (av, i, 0); |
319 | |
370 | |
320 | encode_indent (enc); |
371 | encode_indent (enc); |
321 | |
372 | |
322 | if (svp) |
373 | if (svp) |
323 | encode_sv (enc, *svp); |
374 | encode_sv (enc, *svp); |
324 | else |
375 | else |
325 | encode_str (enc, "null", 4, 0); |
376 | encode_str (enc, "null", 4, 0); |
326 | |
377 | |
327 | if (i < len) |
378 | if (i < len) |
328 | encode_comma (enc); |
379 | encode_comma (enc); |
329 | } |
380 | } |
330 | |
381 | |
|
|
382 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
383 | } |
|
|
384 | |
331 | encode_nl (enc); |
385 | encode_ch (enc, ']'); |
332 | |
|
|
333 | --enc->indent; |
|
|
334 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
335 | } |
386 | } |
336 | |
387 | |
337 | static void |
388 | static void |
338 | encode_he (enc_t *enc, HE *he) |
389 | encode_hk (enc_t *enc, HE *he) |
339 | { |
390 | { |
340 | encode_ch (enc, '"'); |
391 | encode_ch (enc, '"'); |
341 | |
392 | |
342 | if (HeKLEN (he) == HEf_SVKEY) |
393 | if (HeKLEN (he) == HEf_SVKEY) |
343 | { |
394 | { |
… | |
… | |
356 | encode_ch (enc, '"'); |
407 | encode_ch (enc, '"'); |
357 | |
408 | |
358 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
409 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
359 | encode_ch (enc, ':'); |
410 | encode_ch (enc, ':'); |
360 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
411 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
361 | encode_sv (enc, HeVAL (he)); |
|
|
362 | } |
412 | } |
363 | |
413 | |
364 | // compare hash entries, used when all keys are bytestrings |
414 | // compare hash entries, used when all keys are bytestrings |
365 | static int |
415 | static int |
366 | he_cmp_fast (const void *a_, const void *b_) |
416 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
371 | HE *b = *(HE **)b_; |
421 | HE *b = *(HE **)b_; |
372 | |
422 | |
373 | STRLEN la = HeKLEN (a); |
423 | STRLEN la = HeKLEN (a); |
374 | STRLEN lb = HeKLEN (b); |
424 | STRLEN lb = HeKLEN (b); |
375 | |
425 | |
376 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
426 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
377 | cmp = la - lb; |
427 | cmp = lb - la; |
378 | |
428 | |
379 | return cmp; |
429 | return cmp; |
380 | } |
430 | } |
381 | |
431 | |
382 | // compare hash entries, used when some keys are sv's or utf-x |
432 | // compare hash entries, used when some keys are sv's or utf-x |
383 | static int |
433 | static int |
384 | he_cmp_slow (const void *a, const void *b) |
434 | he_cmp_slow (const void *a, const void *b) |
385 | { |
435 | { |
386 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
436 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
387 | } |
437 | } |
388 | |
438 | |
389 | static void |
439 | static void |
390 | encode_hv (enc_t *enc, HV *hv) |
440 | encode_hv (enc_t *enc, HV *hv) |
391 | { |
441 | { |
392 | int count, i; |
442 | HE *he; |
393 | |
443 | |
394 | if (enc->indent >= enc->maxdepth) |
444 | if (enc->indent >= enc->maxdepth) |
395 | croak ("data structure too deep (hit recursion limit)"); |
445 | croak ("data structure too deep (hit recursion limit)"); |
396 | |
446 | |
397 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
447 | encode_ch (enc, '{'); |
398 | |
448 | |
399 | if ((count = hv_iterinit (hv))) |
|
|
400 | { |
|
|
401 | // for canonical output we have to sort by keys first |
449 | // for canonical output we have to sort by keys first |
402 | // actually, this is mostly due to the stupid so-called |
450 | // actually, this is mostly due to the stupid so-called |
403 | // security workaround added somewhere in 5.8.x. |
451 | // security workaround added somewhere in 5.8.x. |
404 | // that randomises hash orderings |
452 | // that randomises hash orderings |
405 | if (enc->json.flags & F_CANONICAL) |
453 | if (enc->json.flags & F_CANONICAL) |
|
|
454 | { |
|
|
455 | int count = hv_iterinit (hv); |
|
|
456 | |
|
|
457 | if (SvMAGICAL (hv)) |
406 | { |
458 | { |
|
|
459 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
460 | // but I don't care for the speed of this special case. |
|
|
461 | // note also that we will run into undefined behaviour when the two iterations |
|
|
462 | // do not result in the same count, something I might care for in some later release. |
|
|
463 | |
|
|
464 | count = 0; |
|
|
465 | while (hv_iternext (hv)) |
|
|
466 | ++count; |
|
|
467 | |
|
|
468 | hv_iterinit (hv); |
|
|
469 | } |
|
|
470 | |
|
|
471 | if (count) |
|
|
472 | { |
407 | int fast = 1; |
473 | int i, fast = 1; |
408 | HE *he; |
|
|
409 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
474 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
410 | HE **hes = _alloca (count * sizeof (HE)); |
475 | HE **hes = _alloca (count * sizeof (HE)); |
411 | #else |
476 | #else |
412 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
477 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
413 | #endif |
478 | #endif |
… | |
… | |
440 | |
505 | |
441 | FREETMPS; |
506 | FREETMPS; |
442 | LEAVE; |
507 | LEAVE; |
443 | } |
508 | } |
444 | |
509 | |
445 | for (i = 0; i < count; ++i) |
510 | encode_nl (enc); ++enc->indent; |
|
|
511 | |
|
|
512 | while (count--) |
446 | { |
513 | { |
447 | encode_indent (enc); |
514 | encode_indent (enc); |
|
|
515 | he = hes [count]; |
448 | encode_he (enc, hes [i]); |
516 | encode_hk (enc, he); |
|
|
517 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
449 | |
518 | |
450 | if (i < count - 1) |
519 | if (count) |
451 | encode_comma (enc); |
520 | encode_comma (enc); |
452 | } |
521 | } |
453 | |
522 | |
454 | encode_nl (enc); |
523 | encode_nl (enc); --enc->indent; encode_indent (enc); |
455 | } |
524 | } |
|
|
525 | } |
456 | else |
526 | else |
|
|
527 | { |
|
|
528 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
529 | if ((he = hv_iternext (hv))) |
457 | { |
530 | { |
458 | HE *he = hv_iternext (hv); |
531 | encode_nl (enc); ++enc->indent; |
459 | |
532 | |
460 | for (;;) |
533 | for (;;) |
461 | { |
534 | { |
462 | encode_indent (enc); |
535 | encode_indent (enc); |
463 | encode_he (enc, he); |
536 | encode_hk (enc, he); |
|
|
537 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
464 | |
538 | |
465 | if (!(he = hv_iternext (hv))) |
539 | if (!(he = hv_iternext (hv))) |
466 | break; |
540 | break; |
467 | |
541 | |
468 | encode_comma (enc); |
542 | encode_comma (enc); |
469 | } |
543 | } |
470 | |
544 | |
471 | encode_nl (enc); |
545 | encode_nl (enc); --enc->indent; encode_indent (enc); |
472 | } |
546 | } |
473 | } |
547 | } |
474 | |
548 | |
475 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
549 | encode_ch (enc, '}'); |
476 | } |
550 | } |
477 | |
551 | |
478 | // encode objects, arrays and special \0=false and \1=true values. |
552 | // encode objects, arrays and special \0=false and \1=true values. |
479 | static void |
553 | static void |
480 | encode_rv (enc_t *enc, SV *sv) |
554 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
510 | // we re-bless the reference to get overload and other niceties right |
584 | // we re-bless the reference to get overload and other niceties right |
511 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
585 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
512 | |
586 | |
513 | if (to_json) |
587 | if (to_json) |
514 | { |
588 | { |
515 | int count; |
|
|
516 | dSP; |
589 | dSP; |
517 | |
590 | |
518 | ENTER; SAVETMPS; PUSHMARK (SP); |
591 | ENTER; SAVETMPS; PUSHMARK (SP); |
519 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
592 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
520 | |
593 | |
… | |
… | |
589 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
662 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
590 | enc->cur += strlen (enc->cur); |
663 | enc->cur += strlen (enc->cur); |
591 | } |
664 | } |
592 | else if (SvIOKp (sv)) |
665 | else if (SvIOKp (sv)) |
593 | { |
666 | { |
594 | // we assume we can always read an IV as a UV |
667 | // we assume we can always read an IV as a UV and vice versa |
595 | if (SvUV (sv) & ~(UV)0x7fff) |
668 | // we assume two's complement |
596 | { |
669 | // we assume no aliasing issues in the union |
597 | // large integer, use the (rather slow) snprintf way. |
670 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
598 | need (enc, sizeof (UV) * 3); |
671 | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
599 | enc->cur += |
|
|
600 | SvIsUV(sv) |
|
|
601 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
|
|
602 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
603 | } |
|
|
604 | else |
|
|
605 | { |
672 | { |
606 | // optimise the "small number case" |
673 | // optimise the "small number case" |
607 | // code will likely be branchless and use only a single multiplication |
674 | // code will likely be branchless and use only a single multiplication |
|
|
675 | // works for numbers up to 59074 |
608 | I32 i = SvIV (sv); |
676 | I32 i = SvIVX (sv); |
609 | U32 u; |
677 | U32 u; |
610 | char digit, nz = 0; |
678 | char digit, nz = 0; |
611 | |
679 | |
612 | need (enc, 6); |
680 | need (enc, 6); |
613 | |
681 | |
… | |
… | |
619 | |
687 | |
620 | // now output digit by digit, each time masking out the integer part |
688 | // now output digit by digit, each time masking out the integer part |
621 | // and multiplying by 5 while moving the decimal point one to the right, |
689 | // and multiplying by 5 while moving the decimal point one to the right, |
622 | // resulting in a net multiplication by 10. |
690 | // resulting in a net multiplication by 10. |
623 | // we always write the digit to memory but conditionally increment |
691 | // we always write the digit to memory but conditionally increment |
624 | // the pointer, to ease the usage of conditional move instructions. |
692 | // the pointer, to enable the use of conditional move instructions. |
625 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
693 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
626 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
694 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
627 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
695 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
628 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
696 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
629 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
697 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
698 | } |
|
|
699 | else |
|
|
700 | { |
|
|
701 | // large integer, use the (rather slow) snprintf way. |
|
|
702 | need (enc, IVUV_MAXCHARS); |
|
|
703 | enc->cur += |
|
|
704 | SvIsUV(sv) |
|
|
705 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
|
|
706 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
630 | } |
707 | } |
631 | } |
708 | } |
632 | else if (SvROK (sv)) |
709 | else if (SvROK (sv)) |
633 | encode_rv (enc, SvRV (sv)); |
710 | encode_rv (enc, SvRV (sv)); |
634 | else if (!SvOK (sv)) |
711 | else if (!SvOK (sv)) |
… | |
… | |
650 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
727 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
651 | enc.cur = SvPVX (enc.sv); |
728 | enc.cur = SvPVX (enc.sv); |
652 | enc.end = SvEND (enc.sv); |
729 | enc.end = SvEND (enc.sv); |
653 | enc.indent = 0; |
730 | enc.indent = 0; |
654 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
731 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
732 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
733 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
734 | : 0x110000UL; |
655 | |
735 | |
656 | SvPOK_only (enc.sv); |
736 | SvPOK_only (enc.sv); |
657 | encode_sv (&enc, scalar); |
737 | encode_sv (&enc, scalar); |
658 | |
738 | |
659 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
739 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
680 | JSON json; |
760 | JSON json; |
681 | U32 depth; // recursion depth |
761 | U32 depth; // recursion depth |
682 | U32 maxdepth; // recursion depth limit |
762 | U32 maxdepth; // recursion depth limit |
683 | } dec_t; |
763 | } dec_t; |
684 | |
764 | |
685 | inline void |
765 | INLINE void |
|
|
766 | decode_comment (dec_t *dec) |
|
|
767 | { |
|
|
768 | // only '#'-style comments allowed a.t.m. |
|
|
769 | |
|
|
770 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
771 | ++dec->cur; |
|
|
772 | } |
|
|
773 | |
|
|
774 | INLINE void |
686 | decode_ws (dec_t *dec) |
775 | decode_ws (dec_t *dec) |
687 | { |
776 | { |
688 | for (;;) |
777 | for (;;) |
689 | { |
778 | { |
690 | char ch = *dec->cur; |
779 | char ch = *dec->cur; |
691 | |
780 | |
692 | if (ch > 0x20 |
781 | if (ch > 0x20) |
|
|
782 | { |
|
|
783 | if (expect_false (ch == '#')) |
|
|
784 | { |
|
|
785 | if (dec->json.flags & F_RELAXED) |
|
|
786 | decode_comment (dec); |
|
|
787 | else |
|
|
788 | break; |
|
|
789 | } |
|
|
790 | else |
|
|
791 | break; |
|
|
792 | } |
693 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
793 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
694 | break; |
794 | break; // parse error, but let higher level handle it, gives better error messages |
695 | |
795 | |
696 | ++dec->cur; |
796 | ++dec->cur; |
697 | } |
797 | } |
698 | } |
798 | } |
699 | |
799 | |
… | |
… | |
805 | |
905 | |
806 | if (hi >= 0x80) |
906 | if (hi >= 0x80) |
807 | { |
907 | { |
808 | utf8 = 1; |
908 | utf8 = 1; |
809 | |
909 | |
810 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
910 | cur = encode_utf8 (cur, hi); |
811 | } |
911 | } |
812 | else |
912 | else |
813 | *cur++ = hi; |
913 | *cur++ = hi; |
814 | } |
914 | } |
815 | break; |
915 | break; |
… | |
… | |
817 | default: |
917 | default: |
818 | --dec_cur; |
918 | --dec_cur; |
819 | ERR ("illegal backslash escape sequence in string"); |
919 | ERR ("illegal backslash escape sequence in string"); |
820 | } |
920 | } |
821 | } |
921 | } |
822 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
922 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
823 | *cur++ = ch; |
923 | *cur++ = ch; |
824 | else if (ch >= 0x80) |
924 | else if (ch >= 0x80) |
825 | { |
925 | { |
826 | STRLEN clen; |
926 | STRLEN clen; |
827 | UV uch; |
927 | UV uch; |
… | |
… | |
950 | |
1050 | |
951 | if (!is_nv) |
1051 | if (!is_nv) |
952 | { |
1052 | { |
953 | int len = dec->cur - start; |
1053 | int len = dec->cur - start; |
954 | |
1054 | |
955 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1055 | // special case the rather common 1..5-digit-int case |
956 | if (*start == '-') |
1056 | if (*start == '-') |
957 | switch (len) |
1057 | switch (len) |
958 | { |
1058 | { |
959 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1059 | case 2: return newSViv (-( start [1] - '0' * 1)); |
960 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1060 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
961 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1061 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
962 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1062 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1063 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
963 | } |
1064 | } |
964 | else |
1065 | else |
965 | switch (len) |
1066 | switch (len) |
966 | { |
1067 | { |
967 | case 1: return newSViv ( start [0] - '0' * 1); |
1068 | case 1: return newSViv ( start [0] - '0' * 1); |
968 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1069 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
969 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1070 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
970 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1071 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1072 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
971 | } |
1073 | } |
972 | |
1074 | |
973 | { |
1075 | { |
974 | UV uv; |
1076 | UV uv; |
975 | int numtype = grok_number (start, len, &uv); |
1077 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1036 | |
1138 | |
1037 | if (*dec->cur != ',') |
1139 | if (*dec->cur != ',') |
1038 | ERR (", or ] expected while parsing array"); |
1140 | ERR (", or ] expected while parsing array"); |
1039 | |
1141 | |
1040 | ++dec->cur; |
1142 | ++dec->cur; |
|
|
1143 | |
|
|
1144 | decode_ws (dec); |
|
|
1145 | |
|
|
1146 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1147 | { |
|
|
1148 | ++dec->cur; |
|
|
1149 | break; |
|
|
1150 | } |
1041 | } |
1151 | } |
1042 | |
1152 | |
1043 | DEC_DEC_DEPTH; |
1153 | DEC_DEC_DEPTH; |
1044 | return newRV_noinc ((SV *)av); |
1154 | return newRV_noinc ((SV *)av); |
1045 | |
1155 | |
… | |
… | |
1061 | if (*dec->cur == '}') |
1171 | if (*dec->cur == '}') |
1062 | ++dec->cur; |
1172 | ++dec->cur; |
1063 | else |
1173 | else |
1064 | for (;;) |
1174 | for (;;) |
1065 | { |
1175 | { |
1066 | decode_ws (dec); EXPECT_CH ('"'); |
1176 | EXPECT_CH ('"'); |
1067 | |
1177 | |
1068 | // heuristic: assume that |
1178 | // heuristic: assume that |
1069 | // a) decode_str + hv_store_ent are abysmally slow. |
1179 | // a) decode_str + hv_store_ent are abysmally slow. |
1070 | // b) most hash keys are short, simple ascii text. |
1180 | // b) most hash keys are short, simple ascii text. |
1071 | // => try to "fast-match" such strings to avoid |
1181 | // => try to "fast-match" such strings to avoid |
… | |
… | |
1075 | char *p = dec->cur; |
1185 | char *p = dec->cur; |
1076 | char *e = p + 24; // only try up to 24 bytes |
1186 | char *e = p + 24; // only try up to 24 bytes |
1077 | |
1187 | |
1078 | for (;;) |
1188 | for (;;) |
1079 | { |
1189 | { |
1080 | // the >= 0x80 is true on most architectures |
1190 | // the >= 0x80 is false on most architectures |
1081 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1191 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1082 | { |
1192 | { |
1083 | // slow path, back up and use decode_str |
1193 | // slow path, back up and use decode_str |
1084 | SV *key = decode_str (dec); |
1194 | SV *key = decode_str (dec); |
1085 | if (!key) |
1195 | if (!key) |
1086 | goto fail; |
1196 | goto fail; |
1087 | |
1197 | |
1088 | decode_ws (dec); EXPECT_CH (':'); |
1198 | decode_ws (dec); EXPECT_CH (':'); |
1089 | |
1199 | |
|
|
1200 | decode_ws (dec); |
1090 | value = decode_sv (dec); |
1201 | value = decode_sv (dec); |
1091 | if (!value) |
1202 | if (!value) |
1092 | { |
1203 | { |
1093 | SvREFCNT_dec (key); |
1204 | SvREFCNT_dec (key); |
1094 | goto fail; |
1205 | goto fail; |
… | |
… | |
1106 | int len = p - key; |
1217 | int len = p - key; |
1107 | dec->cur = p + 1; |
1218 | dec->cur = p + 1; |
1108 | |
1219 | |
1109 | decode_ws (dec); EXPECT_CH (':'); |
1220 | decode_ws (dec); EXPECT_CH (':'); |
1110 | |
1221 | |
|
|
1222 | decode_ws (dec); |
1111 | value = decode_sv (dec); |
1223 | value = decode_sv (dec); |
1112 | if (!value) |
1224 | if (!value) |
1113 | goto fail; |
1225 | goto fail; |
1114 | |
1226 | |
1115 | hv_store (hv, key, len, value, 0); |
1227 | hv_store (hv, key, len, value, 0); |
… | |
… | |
1131 | |
1243 | |
1132 | if (*dec->cur != ',') |
1244 | if (*dec->cur != ',') |
1133 | ERR (", or } expected while parsing object/hash"); |
1245 | ERR (", or } expected while parsing object/hash"); |
1134 | |
1246 | |
1135 | ++dec->cur; |
1247 | ++dec->cur; |
|
|
1248 | |
|
|
1249 | decode_ws (dec); |
|
|
1250 | |
|
|
1251 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1252 | { |
|
|
1253 | ++dec->cur; |
|
|
1254 | break; |
|
|
1255 | } |
1136 | } |
1256 | } |
1137 | |
1257 | |
1138 | DEC_DEC_DEPTH; |
1258 | DEC_DEC_DEPTH; |
1139 | sv = newRV_noinc ((SV *)hv); |
1259 | sv = newRV_noinc ((SV *)hv); |
1140 | |
1260 | |
… | |
… | |
1205 | } |
1325 | } |
1206 | |
1326 | |
1207 | static SV * |
1327 | static SV * |
1208 | decode_sv (dec_t *dec) |
1328 | decode_sv (dec_t *dec) |
1209 | { |
1329 | { |
1210 | decode_ws (dec); |
|
|
1211 | |
|
|
1212 | // the beauty of JSON: you need exactly one character lookahead |
1330 | // the beauty of JSON: you need exactly one character lookahead |
1213 | // to parse anything. |
1331 | // to parse everything. |
1214 | switch (*dec->cur) |
1332 | switch (*dec->cur) |
1215 | { |
1333 | { |
1216 | case '"': ++dec->cur; return decode_str (dec); |
1334 | case '"': ++dec->cur; return decode_str (dec); |
1217 | case '[': ++dec->cur; return decode_av (dec); |
1335 | case '[': ++dec->cur; return decode_av (dec); |
1218 | case '{': ++dec->cur; return decode_hv (dec); |
1336 | case '{': ++dec->cur; return decode_hv (dec); |
1219 | |
1337 | |
1220 | case '-': |
1338 | case '-': |
1221 | case '0': case '1': case '2': case '3': case '4': |
1339 | case '0': case '1': case '2': case '3': case '4': |
1222 | case '5': case '6': case '7': case '8': case '9': |
1340 | case '5': case '6': case '7': case '8': case '9': |
1223 | return decode_num (dec); |
1341 | return decode_num (dec); |
1224 | |
1342 | |
1225 | case 't': |
1343 | case 't': |
1226 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1344 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1227 | { |
1345 | { |
1228 | dec->cur += 4; |
1346 | dec->cur += 4; |
|
|
1347 | #if JSON_SLOW |
|
|
1348 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1349 | #endif |
1229 | return SvREFCNT_inc (json_true); |
1350 | return SvREFCNT_inc (json_true); |
1230 | } |
1351 | } |
1231 | else |
1352 | else |
1232 | ERR ("'true' expected"); |
1353 | ERR ("'true' expected"); |
1233 | |
1354 | |
… | |
… | |
1235 | |
1356 | |
1236 | case 'f': |
1357 | case 'f': |
1237 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1358 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1238 | { |
1359 | { |
1239 | dec->cur += 5; |
1360 | dec->cur += 5; |
|
|
1361 | #if JSON_SLOW |
|
|
1362 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1363 | #endif |
1240 | return SvREFCNT_inc (json_false); |
1364 | return SvREFCNT_inc (json_false); |
1241 | } |
1365 | } |
1242 | else |
1366 | else |
1243 | ERR ("'false' expected"); |
1367 | ERR ("'false' expected"); |
1244 | |
1368 | |
… | |
… | |
1263 | fail: |
1387 | fail: |
1264 | return 0; |
1388 | return 0; |
1265 | } |
1389 | } |
1266 | |
1390 | |
1267 | static SV * |
1391 | static SV * |
1268 | decode_json (SV *string, JSON *json, UV *offset_return) |
1392 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1269 | { |
1393 | { |
1270 | dec_t dec; |
1394 | dec_t dec; |
1271 | UV offset; |
1395 | STRLEN offset; |
1272 | SV *sv; |
1396 | SV *sv; |
1273 | |
1397 | |
1274 | SvGETMAGIC (string); |
1398 | SvGETMAGIC (string); |
1275 | SvUPGRADE (string, SVt_PV); |
1399 | SvUPGRADE (string, SVt_PV); |
1276 | |
1400 | |
… | |
… | |
1294 | |
1418 | |
1295 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1419 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1296 | dec.json.flags |= F_HOOK; |
1420 | dec.json.flags |= F_HOOK; |
1297 | |
1421 | |
1298 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1422 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1423 | |
|
|
1424 | decode_ws (&dec); |
1299 | sv = decode_sv (&dec); |
1425 | sv = decode_sv (&dec); |
1300 | |
1426 | |
1301 | if (!(offset_return || !sv)) |
1427 | if (!(offset_return || !sv)) |
1302 | { |
1428 | { |
1303 | // check for trailing garbage |
1429 | // check for trailing garbage |
… | |
… | |
1347 | |
1473 | |
1348 | return sv; |
1474 | return sv; |
1349 | } |
1475 | } |
1350 | |
1476 | |
1351 | ///////////////////////////////////////////////////////////////////////////// |
1477 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1478 | // incremental parser |
|
|
1479 | |
|
|
1480 | static void |
|
|
1481 | incr_parse (JSON *self) |
|
|
1482 | { |
|
|
1483 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1484 | |
|
|
1485 | for (;;) |
|
|
1486 | { |
|
|
1487 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
|
|
1488 | switch (self->incr_mode) |
|
|
1489 | { |
|
|
1490 | // only used for intiial whitespace skipping |
|
|
1491 | case INCR_M_WS: |
|
|
1492 | for (;;) |
|
|
1493 | { |
|
|
1494 | if (*p > 0x20) |
|
|
1495 | { |
|
|
1496 | self->incr_mode = INCR_M_JSON; |
|
|
1497 | goto incr_m_json; |
|
|
1498 | } |
|
|
1499 | else if (!*p) |
|
|
1500 | goto interrupt; |
|
|
1501 | |
|
|
1502 | ++p; |
|
|
1503 | } |
|
|
1504 | |
|
|
1505 | // skip a single char inside a string (for \\-processing) |
|
|
1506 | case INCR_M_BS: |
|
|
1507 | if (!*p) |
|
|
1508 | goto interrupt; |
|
|
1509 | |
|
|
1510 | ++p; |
|
|
1511 | self->incr_mode = INCR_M_STR; |
|
|
1512 | goto incr_m_str; |
|
|
1513 | |
|
|
1514 | // inside a string |
|
|
1515 | case INCR_M_STR: |
|
|
1516 | incr_m_str: |
|
|
1517 | for (;;) |
|
|
1518 | { |
|
|
1519 | if (*p == '"') |
|
|
1520 | { |
|
|
1521 | ++p; |
|
|
1522 | self->incr_mode = INCR_M_JSON; |
|
|
1523 | |
|
|
1524 | if (!self->incr_nest) |
|
|
1525 | goto interrupt; |
|
|
1526 | |
|
|
1527 | goto incr_m_json; |
|
|
1528 | } |
|
|
1529 | else if (*p == '\\') |
|
|
1530 | { |
|
|
1531 | ++p; // "virtually" consumes character after \ |
|
|
1532 | |
|
|
1533 | if (!*p) // if at end of string we have to switch modes |
|
|
1534 | { |
|
|
1535 | self->incr_mode = INCR_M_BS; |
|
|
1536 | goto interrupt; |
|
|
1537 | } |
|
|
1538 | } |
|
|
1539 | else if (!*p) |
|
|
1540 | goto interrupt; |
|
|
1541 | |
|
|
1542 | ++p; |
|
|
1543 | } |
|
|
1544 | |
|
|
1545 | // after initial ws, outside string |
|
|
1546 | case INCR_M_JSON: |
|
|
1547 | incr_m_json: |
|
|
1548 | for (;;) |
|
|
1549 | { |
|
|
1550 | switch (*p++) |
|
|
1551 | { |
|
|
1552 | case 0: |
|
|
1553 | --p; |
|
|
1554 | goto interrupt; |
|
|
1555 | |
|
|
1556 | case 0x09: |
|
|
1557 | case 0x0a: |
|
|
1558 | case 0x0d: |
|
|
1559 | case 0x20: |
|
|
1560 | if (!self->incr_nest) |
|
|
1561 | { |
|
|
1562 | --p; // do not eat the whitespace, let the next round do it |
|
|
1563 | goto interrupt; |
|
|
1564 | } |
|
|
1565 | break; |
|
|
1566 | |
|
|
1567 | case '"': |
|
|
1568 | self->incr_mode = INCR_M_STR; |
|
|
1569 | goto incr_m_str; |
|
|
1570 | |
|
|
1571 | case '[': |
|
|
1572 | case '{': |
|
|
1573 | ++self->incr_nest; |
|
|
1574 | break; |
|
|
1575 | |
|
|
1576 | case ']': |
|
|
1577 | case '}': |
|
|
1578 | if (!--self->incr_nest) |
|
|
1579 | goto interrupt; |
|
|
1580 | } |
|
|
1581 | } |
|
|
1582 | } |
|
|
1583 | |
|
|
1584 | modechange: |
|
|
1585 | ; |
|
|
1586 | } |
|
|
1587 | |
|
|
1588 | interrupt: |
|
|
1589 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1590 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
|
|
1591 | } |
|
|
1592 | |
|
|
1593 | ///////////////////////////////////////////////////////////////////////////// |
1352 | // XS interface functions |
1594 | // XS interface functions |
1353 | |
1595 | |
1354 | MODULE = JSON::XS PACKAGE = JSON::XS |
1596 | MODULE = JSON::XS PACKAGE = JSON::XS |
1355 | |
1597 | |
1356 | BOOT: |
1598 | BOOT: |
… | |
… | |
1379 | json_boolean_stash = 0; |
1621 | json_boolean_stash = 0; |
1380 | |
1622 | |
1381 | void new (char *klass) |
1623 | void new (char *klass) |
1382 | PPCODE: |
1624 | PPCODE: |
1383 | { |
1625 | { |
1384 | HV *stash = !JSON_SLOW || json_stash |
|
|
1385 | ? json_stash |
|
|
1386 | : gv_stashpv ("JSON::XS", 1); |
|
|
1387 | SV *pv = NEWSV (0, sizeof (JSON)); |
1626 | SV *pv = NEWSV (0, sizeof (JSON)); |
1388 | SvPOK_only (pv); |
1627 | SvPOK_only (pv); |
1389 | Zero (SvPVX (pv), 1, JSON); |
1628 | Zero (SvPVX (pv), 1, JSON); |
1390 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1629 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1391 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); |
1630 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1631 | newRV_noinc (pv), |
|
|
1632 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1633 | ))); |
1392 | } |
1634 | } |
1393 | |
1635 | |
1394 | void ascii (JSON *self, int enable = 1) |
1636 | void ascii (JSON *self, int enable = 1) |
1395 | ALIAS: |
1637 | ALIAS: |
1396 | ascii = F_ASCII |
1638 | ascii = F_ASCII |
… | |
… | |
1403 | pretty = F_PRETTY |
1645 | pretty = F_PRETTY |
1404 | allow_nonref = F_ALLOW_NONREF |
1646 | allow_nonref = F_ALLOW_NONREF |
1405 | shrink = F_SHRINK |
1647 | shrink = F_SHRINK |
1406 | allow_blessed = F_ALLOW_BLESSED |
1648 | allow_blessed = F_ALLOW_BLESSED |
1407 | convert_blessed = F_CONV_BLESSED |
1649 | convert_blessed = F_CONV_BLESSED |
|
|
1650 | relaxed = F_RELAXED |
1408 | PPCODE: |
1651 | PPCODE: |
1409 | { |
1652 | { |
1410 | if (enable) |
1653 | if (enable) |
1411 | self->flags |= ix; |
1654 | self->flags |= ix; |
1412 | else |
1655 | else |
1413 | self->flags &= ~ix; |
1656 | self->flags &= ~ix; |
1414 | |
1657 | |
1415 | XPUSHs (ST (0)); |
1658 | XPUSHs (ST (0)); |
1416 | } |
1659 | } |
1417 | |
1660 | |
|
|
1661 | void get_ascii (JSON *self) |
|
|
1662 | ALIAS: |
|
|
1663 | get_ascii = F_ASCII |
|
|
1664 | get_latin1 = F_LATIN1 |
|
|
1665 | get_utf8 = F_UTF8 |
|
|
1666 | get_indent = F_INDENT |
|
|
1667 | get_canonical = F_CANONICAL |
|
|
1668 | get_space_before = F_SPACE_BEFORE |
|
|
1669 | get_space_after = F_SPACE_AFTER |
|
|
1670 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1671 | get_shrink = F_SHRINK |
|
|
1672 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1673 | get_convert_blessed = F_CONV_BLESSED |
|
|
1674 | get_relaxed = F_RELAXED |
|
|
1675 | PPCODE: |
|
|
1676 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1677 | |
1418 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1678 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1419 | PPCODE: |
1679 | PPCODE: |
1420 | { |
1680 | { |
1421 | UV log2 = 0; |
1681 | UV log2 = 0; |
1422 | |
1682 | |
… | |
… | |
1428 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1688 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1429 | |
1689 | |
1430 | XPUSHs (ST (0)); |
1690 | XPUSHs (ST (0)); |
1431 | } |
1691 | } |
1432 | |
1692 | |
|
|
1693 | U32 get_max_depth (JSON *self) |
|
|
1694 | CODE: |
|
|
1695 | RETVAL = DEC_DEPTH (self->flags); |
|
|
1696 | OUTPUT: |
|
|
1697 | RETVAL |
|
|
1698 | |
1433 | void max_size (JSON *self, UV max_size = 0) |
1699 | void max_size (JSON *self, UV max_size = 0) |
1434 | PPCODE: |
1700 | PPCODE: |
1435 | { |
1701 | { |
1436 | UV log2 = 0; |
1702 | UV log2 = 0; |
1437 | |
1703 | |
… | |
… | |
1443 | |
1709 | |
1444 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1710 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1445 | |
1711 | |
1446 | XPUSHs (ST (0)); |
1712 | XPUSHs (ST (0)); |
1447 | } |
1713 | } |
|
|
1714 | |
|
|
1715 | int get_max_size (JSON *self) |
|
|
1716 | CODE: |
|
|
1717 | RETVAL = DEC_SIZE (self->flags); |
|
|
1718 | OUTPUT: |
|
|
1719 | RETVAL |
1448 | |
1720 | |
1449 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1721 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1450 | PPCODE: |
1722 | PPCODE: |
1451 | { |
1723 | { |
1452 | SvREFCNT_dec (self->cb_object); |
1724 | SvREFCNT_dec (self->cb_object); |
… | |
… | |
1486 | XPUSHs (decode_json (jsonstr, self, 0)); |
1758 | XPUSHs (decode_json (jsonstr, self, 0)); |
1487 | |
1759 | |
1488 | void decode_prefix (JSON *self, SV *jsonstr) |
1760 | void decode_prefix (JSON *self, SV *jsonstr) |
1489 | PPCODE: |
1761 | PPCODE: |
1490 | { |
1762 | { |
1491 | UV offset; |
1763 | STRLEN offset; |
1492 | EXTEND (SP, 2); |
1764 | EXTEND (SP, 2); |
1493 | PUSHs (decode_json (jsonstr, self, &offset)); |
1765 | PUSHs (decode_json (jsonstr, self, &offset)); |
1494 | PUSHs (sv_2mortal (newSVuv (offset))); |
1766 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1767 | } |
|
|
1768 | |
|
|
1769 | void incr_parse (JSON *self, SV *jsonstr = 0) |
|
|
1770 | PPCODE: |
|
|
1771 | { |
|
|
1772 | if (!self->incr_text) |
|
|
1773 | self->incr_text = newSVpvn ("", 0); |
|
|
1774 | |
|
|
1775 | // append data, if any |
|
|
1776 | if (jsonstr) |
|
|
1777 | { |
|
|
1778 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
|
|
1779 | { |
|
|
1780 | /* utf-8-ness differs, need to upgrade */ |
|
|
1781 | sv_utf8_upgrade (self->incr_text); |
|
|
1782 | |
|
|
1783 | if (self->incr_pos) |
|
|
1784 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1785 | - (U8 *)SvPVX (self->incr_text); |
|
|
1786 | } |
|
|
1787 | |
|
|
1788 | { |
|
|
1789 | STRLEN len; |
|
|
1790 | const char *str = SvPV (jsonstr, len); |
|
|
1791 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
|
|
1792 | Move (str, SvEND (self->incr_text), len, char); |
|
|
1793 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
|
|
1794 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1795 | } |
|
|
1796 | } |
|
|
1797 | |
|
|
1798 | if (GIMME_V != G_VOID) |
|
|
1799 | do |
|
|
1800 | { |
|
|
1801 | STRLEN offset; |
|
|
1802 | |
|
|
1803 | incr_parse (self); |
|
|
1804 | |
|
|
1805 | if (!INCR_DONE (self)) |
|
|
1806 | break; |
|
|
1807 | |
|
|
1808 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
|
|
1809 | |
|
|
1810 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1811 | self->incr_pos -= offset; |
|
|
1812 | self->incr_nest = 0; |
|
|
1813 | self->incr_mode = 0; |
|
|
1814 | } |
|
|
1815 | while (GIMME_V == G_ARRAY); |
|
|
1816 | } |
|
|
1817 | |
|
|
1818 | SV *incr_text (JSON *self) |
|
|
1819 | ATTRS: lvalue |
|
|
1820 | CODE: |
|
|
1821 | { |
|
|
1822 | if (self->incr_pos) |
|
|
1823 | croak ("incr_text can only be called after a successful incr_parse call in scalar context"); |
|
|
1824 | |
|
|
1825 | RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; |
|
|
1826 | } |
|
|
1827 | OUTPUT: |
|
|
1828 | RETVAL |
|
|
1829 | |
|
|
1830 | void incr_skip (JSON *self) |
|
|
1831 | CODE: |
|
|
1832 | { |
|
|
1833 | if (self->incr_pos) |
|
|
1834 | { |
|
|
1835 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos); |
|
|
1836 | self->incr_pos = 0; |
|
|
1837 | self->incr_nest = 0; |
|
|
1838 | self->incr_mode = 0; |
|
|
1839 | } |
1495 | } |
1840 | } |
1496 | |
1841 | |
1497 | void DESTROY (JSON *self) |
1842 | void DESTROY (JSON *self) |
1498 | CODE: |
1843 | CODE: |
1499 | SvREFCNT_dec (self->cb_sk_object); |
1844 | SvREFCNT_dec (self->cb_sk_object); |
1500 | SvREFCNT_dec (self->cb_object); |
1845 | SvREFCNT_dec (self->cb_object); |
|
|
1846 | SvREFCNT_dec (self->incr_text); |
1501 | |
1847 | |
1502 | PROTOTYPES: ENABLE |
1848 | PROTOTYPES: ENABLE |
1503 | |
1849 | |
1504 | void to_json (SV *scalar) |
1850 | void encode_json (SV *scalar) |
|
|
1851 | ALIAS: |
|
|
1852 | to_json_ = 0 |
|
|
1853 | encode_json = F_UTF8 |
1505 | PPCODE: |
1854 | PPCODE: |
1506 | { |
1855 | { |
1507 | JSON json = { F_DEFAULT | F_UTF8 }; |
1856 | JSON json = { F_DEFAULT | ix }; |
1508 | XPUSHs (encode_json (scalar, &json)); |
1857 | XPUSHs (encode_json (scalar, &json)); |
1509 | } |
1858 | } |
1510 | |
1859 | |
1511 | void from_json (SV *jsonstr) |
1860 | void decode_json (SV *jsonstr) |
|
|
1861 | ALIAS: |
|
|
1862 | from_json_ = 0 |
|
|
1863 | decode_json = F_UTF8 |
1512 | PPCODE: |
1864 | PPCODE: |
1513 | { |
1865 | { |
1514 | JSON json = { F_DEFAULT | F_UTF8 }; |
1866 | JSON json = { F_DEFAULT | ix }; |
1515 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1867 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1516 | } |
1868 | } |
1517 | |
1869 | |
|
|
1870 | |