… | |
… | |
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" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
9 | #define F_ASCII 0x00000001 |
20 | #define F_ASCII 0x00000001UL |
|
|
21 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
22 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
23 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
24 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
25 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
26 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
27 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
|
|
31 | #define F_MAXDEPTH 0xf8000000UL |
|
|
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
|
|
35 | |
|
|
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
17 | |
38 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
41 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
44 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
25 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
26 | |
46 | |
27 | #define SB do { |
47 | #define SB do { |
28 | #define SE } while (0) |
48 | #define SE } while (0) |
29 | |
49 | |
|
|
50 | #if __GNUC__ >= 3 |
|
|
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
52 | # define inline inline |
|
|
53 | #else |
|
|
54 | # define expect(expr,value) (expr) |
|
|
55 | # define inline static |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
60 | |
30 | static HV *json_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
|
|
63 | |
|
|
64 | typedef struct json { |
|
|
65 | U32 flags; |
|
|
66 | } JSON__XS; |
31 | |
67 | |
32 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
33 | // utility functions |
69 | // utility functions |
34 | |
70 | |
35 | static UV * |
71 | static UV * |
… | |
… | |
58 | // decode an utf-8 character and return it, or (UV)-1 in |
94 | // decode an utf-8 character and return it, or (UV)-1 in |
59 | // case of an error. |
95 | // case of an error. |
60 | // we special-case "safe" characters from U+80 .. U+7FF, |
96 | // we special-case "safe" characters from U+80 .. U+7FF, |
61 | // but use the very good perl function to parse anything else. |
97 | // but use the very good perl function to parse anything else. |
62 | // note that we never call this function for a ascii codepoints |
98 | // note that we never call this function for a ascii codepoints |
63 | static UV |
99 | inline UV |
64 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
100 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
65 | { |
101 | { |
66 | if (s[0] > 0xdf || s[0] < 0xc2) |
102 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
67 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
103 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
68 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
104 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
69 | { |
105 | { |
70 | *clen = 2; |
106 | *clen = 2; |
71 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
107 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
72 | } |
108 | } |
73 | else |
109 | else |
|
|
110 | { |
|
|
111 | *clen = (STRLEN)-1; |
74 | return (UV)-1; |
112 | return (UV)-1; |
|
|
113 | } |
75 | } |
114 | } |
76 | |
115 | |
77 | ///////////////////////////////////////////////////////////////////////////// |
116 | ///////////////////////////////////////////////////////////////////////////// |
78 | // encoder |
117 | // encoder |
79 | |
118 | |
… | |
… | |
81 | typedef struct |
120 | typedef struct |
82 | { |
121 | { |
83 | char *cur; // SvPVX (sv) + current output position |
122 | char *cur; // SvPVX (sv) + current output position |
84 | char *end; // SvEND (sv) |
123 | char *end; // SvEND (sv) |
85 | SV *sv; // result scalar |
124 | SV *sv; // result scalar |
86 | UV flags; // F_* |
125 | struct json json; |
87 | int indent; // indentation level |
126 | U32 indent; // indentation level |
88 | int max_depth; // max. recursion level |
127 | U32 maxdepth; // max. indentation/recursion level |
89 | } enc_t; |
128 | } enc_t; |
90 | |
129 | |
91 | static void |
130 | inline void |
92 | need (enc_t *enc, STRLEN len) |
131 | need (enc_t *enc, STRLEN len) |
93 | { |
132 | { |
94 | if (enc->cur + len >= enc->end) |
133 | if (expect_false (enc->cur + len >= enc->end)) |
95 | { |
134 | { |
96 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
135 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
97 | SvGROW (enc->sv, cur + len + 1); |
136 | SvGROW (enc->sv, cur + len + 1); |
98 | enc->cur = SvPVX (enc->sv) + cur; |
137 | enc->cur = SvPVX (enc->sv) + cur; |
99 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
138 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
100 | } |
139 | } |
101 | } |
140 | } |
102 | |
141 | |
103 | static void |
142 | inline void |
104 | encode_ch (enc_t *enc, char ch) |
143 | encode_ch (enc_t *enc, char ch) |
105 | { |
144 | { |
106 | need (enc, 1); |
145 | need (enc, 1); |
107 | *enc->cur++ = ch; |
146 | *enc->cur++ = ch; |
108 | } |
147 | } |
… | |
… | |
116 | |
155 | |
117 | while (str < end) |
156 | while (str < end) |
118 | { |
157 | { |
119 | unsigned char ch = *(unsigned char *)str; |
158 | unsigned char ch = *(unsigned char *)str; |
120 | |
159 | |
121 | if (ch >= 0x20 && ch < 0x80) // most common case |
160 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
122 | { |
161 | { |
123 | if (ch == '"') // but with slow exceptions |
162 | if (expect_false (ch == '"')) // but with slow exceptions |
124 | { |
163 | { |
125 | need (enc, len += 1); |
164 | need (enc, len += 1); |
126 | *enc->cur++ = '\\'; |
165 | *enc->cur++ = '\\'; |
127 | *enc->cur++ = '"'; |
166 | *enc->cur++ = '"'; |
128 | } |
167 | } |
129 | else if (ch == '\\') |
168 | else if (expect_false (ch == '\\')) |
130 | { |
169 | { |
131 | need (enc, len += 1); |
170 | need (enc, len += 1); |
132 | *enc->cur++ = '\\'; |
171 | *enc->cur++ = '\\'; |
133 | *enc->cur++ = '\\'; |
172 | *enc->cur++ = '\\'; |
134 | } |
173 | } |
… | |
… | |
152 | STRLEN clen; |
191 | STRLEN clen; |
153 | UV uch; |
192 | UV uch; |
154 | |
193 | |
155 | if (is_utf8) |
194 | if (is_utf8) |
156 | { |
195 | { |
157 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
158 | uch = decode_utf8 (str, end - str, &clen); |
196 | uch = decode_utf8 (str, end - str, &clen); |
159 | if (clen == (STRLEN)-1) |
197 | if (clen == (STRLEN)-1) |
160 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
198 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
161 | } |
199 | } |
162 | else |
200 | else |
… | |
… | |
166 | } |
204 | } |
167 | |
205 | |
168 | if (uch > 0x10FFFFUL) |
206 | if (uch > 0x10FFFFUL) |
169 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
207 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
170 | |
208 | |
171 | if (uch < 0x80 || enc->flags & F_ASCII) |
209 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
172 | { |
210 | { |
173 | if (uch > 0xFFFFUL) |
211 | if (uch > 0xFFFFUL) |
174 | { |
212 | { |
175 | need (enc, len += 11); |
213 | need (enc, len += 11); |
176 | sprintf (enc->cur, "\\u%04x\\u%04x", |
214 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
190 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
228 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
191 | } |
229 | } |
192 | |
230 | |
193 | str += clen; |
231 | str += clen; |
194 | } |
232 | } |
|
|
233 | else if (enc->json.flags & F_LATIN1) |
|
|
234 | { |
|
|
235 | *enc->cur++ = uch; |
|
|
236 | str += clen; |
|
|
237 | } |
195 | else if (is_utf8) |
238 | else if (is_utf8) |
196 | { |
239 | { |
197 | need (enc, len += clen); |
240 | need (enc, len += clen); |
198 | do |
241 | do |
199 | { |
242 | { |
… | |
… | |
201 | } |
244 | } |
202 | while (--clen); |
245 | while (--clen); |
203 | } |
246 | } |
204 | else |
247 | else |
205 | { |
248 | { |
206 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
249 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
207 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
250 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
208 | ++str; |
251 | ++str; |
209 | } |
252 | } |
210 | } |
253 | } |
211 | } |
254 | } |
… | |
… | |
213 | |
256 | |
214 | --len; |
257 | --len; |
215 | } |
258 | } |
216 | } |
259 | } |
217 | |
260 | |
218 | static void |
261 | inline void |
219 | encode_indent (enc_t *enc) |
262 | encode_indent (enc_t *enc) |
220 | { |
263 | { |
221 | if (enc->flags & F_INDENT) |
264 | if (enc->json.flags & F_INDENT) |
222 | { |
265 | { |
223 | int spaces = enc->indent * INDENT_STEP; |
266 | int spaces = enc->indent * INDENT_STEP; |
224 | |
267 | |
225 | need (enc, spaces); |
268 | need (enc, spaces); |
226 | memset (enc->cur, ' ', spaces); |
269 | memset (enc->cur, ' ', spaces); |
227 | enc->cur += spaces; |
270 | enc->cur += spaces; |
228 | } |
271 | } |
229 | } |
272 | } |
230 | |
273 | |
231 | static void |
274 | inline void |
232 | encode_space (enc_t *enc) |
275 | encode_space (enc_t *enc) |
233 | { |
276 | { |
234 | need (enc, 1); |
277 | need (enc, 1); |
235 | encode_ch (enc, ' '); |
278 | encode_ch (enc, ' '); |
236 | } |
279 | } |
237 | |
280 | |
238 | static void |
281 | inline void |
239 | encode_nl (enc_t *enc) |
282 | encode_nl (enc_t *enc) |
240 | { |
283 | { |
241 | if (enc->flags & F_INDENT) |
284 | if (enc->json.flags & F_INDENT) |
242 | { |
285 | { |
243 | need (enc, 1); |
286 | need (enc, 1); |
244 | encode_ch (enc, '\n'); |
287 | encode_ch (enc, '\n'); |
245 | } |
288 | } |
246 | } |
289 | } |
247 | |
290 | |
248 | static void |
291 | inline void |
249 | encode_comma (enc_t *enc) |
292 | encode_comma (enc_t *enc) |
250 | { |
293 | { |
251 | encode_ch (enc, ','); |
294 | encode_ch (enc, ','); |
252 | |
295 | |
253 | if (enc->flags & F_INDENT) |
296 | if (enc->json.flags & F_INDENT) |
254 | encode_nl (enc); |
297 | encode_nl (enc); |
255 | else if (enc->flags & F_SPACE_AFTER) |
298 | else if (enc->json.flags & F_SPACE_AFTER) |
256 | encode_space (enc); |
299 | encode_space (enc); |
257 | } |
300 | } |
258 | |
301 | |
259 | static void encode_sv (enc_t *enc, SV *sv); |
302 | static void encode_sv (enc_t *enc, SV *sv); |
260 | |
303 | |
261 | static void |
304 | static void |
262 | encode_av (enc_t *enc, AV *av) |
305 | encode_av (enc_t *enc, AV *av) |
263 | { |
306 | { |
264 | int i, len = av_len (av); |
307 | int i, len = av_len (av); |
|
|
308 | |
|
|
309 | if (enc->indent >= enc->maxdepth) |
|
|
310 | croak ("data structure too deep (hit recursion limit)"); |
265 | |
311 | |
266 | encode_ch (enc, '['); encode_nl (enc); |
312 | encode_ch (enc, '['); encode_nl (enc); |
267 | ++enc->indent; |
313 | ++enc->indent; |
268 | |
314 | |
269 | for (i = 0; i <= len; ++i) |
315 | for (i = 0; i <= len; ++i) |
… | |
… | |
300 | else |
346 | else |
301 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
347 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
302 | |
348 | |
303 | encode_ch (enc, '"'); |
349 | encode_ch (enc, '"'); |
304 | |
350 | |
305 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
306 | encode_ch (enc, ':'); |
352 | encode_ch (enc, ':'); |
307 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
308 | encode_sv (enc, HeVAL (he)); |
354 | encode_sv (enc, HeVAL (he)); |
309 | } |
355 | } |
310 | |
356 | |
311 | // compare hash entries, used when all keys are bytestrings |
357 | // compare hash entries, used when all keys are bytestrings |
312 | static int |
358 | static int |
… | |
… | |
335 | |
381 | |
336 | static void |
382 | static void |
337 | encode_hv (enc_t *enc, HV *hv) |
383 | encode_hv (enc_t *enc, HV *hv) |
338 | { |
384 | { |
339 | int count, i; |
385 | int count, i; |
|
|
386 | |
|
|
387 | if (enc->indent >= enc->maxdepth) |
|
|
388 | croak ("data structure too deep (hit recursion limit)"); |
340 | |
389 | |
341 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
390 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
342 | |
391 | |
343 | if ((count = hv_iterinit (hv))) |
392 | if ((count = hv_iterinit (hv))) |
344 | { |
393 | { |
345 | // for canonical output we have to sort by keys first |
394 | // for canonical output we have to sort by keys first |
346 | // actually, this is mostly due to the stupid so-called |
395 | // actually, this is mostly due to the stupid so-called |
347 | // security workaround added somewhere in 5.8.x. |
396 | // security workaround added somewhere in 5.8.x. |
348 | // that randomises hash orderings |
397 | // that randomises hash orderings |
349 | if (enc->flags & F_CANONICAL) |
398 | if (enc->json.flags & F_CANONICAL) |
350 | { |
399 | { |
351 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
352 | int fast = 1; |
400 | int fast = 1; |
|
|
401 | HE *he; |
|
|
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
403 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
404 | #else |
|
|
405 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
406 | #endif |
353 | |
407 | |
354 | i = 0; |
408 | i = 0; |
355 | while ((he = hv_iternext (hv))) |
409 | while ((he = hv_iternext (hv))) |
356 | { |
410 | { |
357 | hes [i++] = he; |
411 | hes [i++] = he; |
… | |
… | |
392 | |
446 | |
393 | encode_nl (enc); |
447 | encode_nl (enc); |
394 | } |
448 | } |
395 | else |
449 | else |
396 | { |
450 | { |
397 | SV *sv; |
|
|
398 | HE *he = hv_iternext (hv); |
451 | HE *he = hv_iternext (hv); |
399 | |
452 | |
400 | for (;;) |
453 | for (;;) |
401 | { |
454 | { |
402 | encode_indent (enc); |
455 | encode_indent (enc); |
… | |
… | |
411 | encode_nl (enc); |
464 | encode_nl (enc); |
412 | } |
465 | } |
413 | } |
466 | } |
414 | |
467 | |
415 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
468 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
469 | } |
|
|
470 | |
|
|
471 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
472 | static void |
|
|
473 | encode_rv (enc_t *enc, SV *sv) |
|
|
474 | { |
|
|
475 | svtype svt; |
|
|
476 | |
|
|
477 | SvGETMAGIC (sv); |
|
|
478 | svt = SvTYPE (sv); |
|
|
479 | |
|
|
480 | if (expect_false (SvOBJECT (sv))) |
|
|
481 | { |
|
|
482 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
483 | { |
|
|
484 | if (SvIV (sv) == 0) |
|
|
485 | encode_str (enc, "false", 5, 0); |
|
|
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
|
|
489 | else |
|
|
490 | { |
|
|
491 | #if 0 |
|
|
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
493 | { |
|
|
494 | // not yet |
|
|
495 | } |
|
|
496 | #endif |
|
|
497 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
498 | { |
|
|
499 | // we re-bless the reference to get overload and other niceties right |
|
|
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
501 | |
|
|
502 | if (to_json) |
|
|
503 | { |
|
|
504 | dSP; |
|
|
505 | ENTER; |
|
|
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
|
|
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
509 | |
|
|
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
511 | // check anyways. |
|
|
512 | PUTBACK; |
|
|
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
514 | SPAGAIN; |
|
|
515 | |
|
|
516 | encode_sv (enc, POPs); |
|
|
517 | |
|
|
518 | FREETMPS; |
|
|
519 | LEAVE; |
|
|
520 | } |
|
|
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
522 | encode_str (enc, "null", 4, 0); |
|
|
523 | else |
|
|
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
525 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
526 | } |
|
|
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
528 | encode_str (enc, "null", 4, 0); |
|
|
529 | else |
|
|
530 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
532 | } |
|
|
533 | } |
|
|
534 | else if (svt == SVt_PVHV) |
|
|
535 | encode_hv (enc, (HV *)sv); |
|
|
536 | else if (svt == SVt_PVAV) |
|
|
537 | encode_av (enc, (AV *)sv); |
|
|
538 | else if (svt < SVt_PVAV) |
|
|
539 | { |
|
|
540 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
541 | encode_str (enc, "false", 5, 0); |
|
|
542 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
543 | encode_str (enc, "true", 4, 0); |
|
|
544 | else |
|
|
545 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
547 | } |
|
|
548 | else |
|
|
549 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
550 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
416 | } |
551 | } |
417 | |
552 | |
418 | static void |
553 | static void |
419 | encode_sv (enc_t *enc, SV *sv) |
554 | encode_sv (enc_t *enc, SV *sv) |
420 | { |
555 | { |
… | |
… | |
428 | encode_str (enc, str, len, SvUTF8 (sv)); |
563 | encode_str (enc, str, len, SvUTF8 (sv)); |
429 | encode_ch (enc, '"'); |
564 | encode_ch (enc, '"'); |
430 | } |
565 | } |
431 | else if (SvNOKp (sv)) |
566 | else if (SvNOKp (sv)) |
432 | { |
567 | { |
|
|
568 | // trust that perl will do the right thing w.r.t. JSON syntax. |
433 | need (enc, NV_DIG + 32); |
569 | need (enc, NV_DIG + 32); |
434 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
570 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
435 | enc->cur += strlen (enc->cur); |
571 | enc->cur += strlen (enc->cur); |
436 | } |
572 | } |
437 | else if (SvIOKp (sv)) |
573 | else if (SvIOKp (sv)) |
438 | { |
574 | { |
439 | need (enc, 64); |
575 | // we assume we can always read an IV as a UV |
|
|
576 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
577 | { |
|
|
578 | // large integer, use the (rather slow) snprintf way. |
|
|
579 | need (enc, sizeof (UV) * 3); |
440 | enc->cur += |
580 | enc->cur += |
441 | SvIsUV(sv) |
581 | SvIsUV(sv) |
442 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
582 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
443 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
583 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
584 | } |
|
|
585 | else |
|
|
586 | { |
|
|
587 | // optimise the "small number case" |
|
|
588 | // code will likely be branchless and use only a single multiplication |
|
|
589 | I32 i = SvIV (sv); |
|
|
590 | U32 u; |
|
|
591 | char digit, nz = 0; |
|
|
592 | |
|
|
593 | need (enc, 6); |
|
|
594 | |
|
|
595 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
596 | u = i < 0 ? -i : i; |
|
|
597 | |
|
|
598 | // convert to 4.28 fixed-point representation |
|
|
599 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
600 | |
|
|
601 | // now output digit by digit, each time masking out the integer part |
|
|
602 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
603 | // resulting in a net multiplication by 10. |
|
|
604 | // we always write the digit to memory but conditionally increment |
|
|
605 | // the pointer, to ease the usage of conditional move instructions. |
|
|
606 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
607 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
608 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
609 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
610 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
611 | } |
444 | } |
612 | } |
445 | else if (SvROK (sv)) |
613 | else if (SvROK (sv)) |
446 | { |
614 | encode_rv (enc, SvRV (sv)); |
447 | SV *rv = SvRV (sv); |
|
|
448 | |
|
|
449 | if (enc->indent >= enc->max_depth) |
|
|
450 | croak ("data structure too deep (hit recursion limit)"); |
|
|
451 | |
|
|
452 | switch (SvTYPE (rv)) |
|
|
453 | { |
|
|
454 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
455 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
456 | |
|
|
457 | default: |
|
|
458 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
459 | SvPV_nolen (sv)); |
|
|
460 | } |
|
|
461 | } |
|
|
462 | else if (!SvOK (sv)) |
615 | else if (!SvOK (sv)) |
463 | encode_str (enc, "null", 4, 0); |
616 | encode_str (enc, "null", 4, 0); |
464 | else |
617 | else |
465 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
466 | SvPV_nolen (sv), SvFLAGS (sv)); |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
467 | } |
620 | } |
468 | |
621 | |
469 | static SV * |
622 | static SV * |
470 | encode_json (SV *scalar, UV flags) |
623 | encode_json (SV *scalar, struct json *json) |
471 | { |
624 | { |
|
|
625 | enc_t enc; |
|
|
626 | |
472 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
473 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
474 | |
629 | |
475 | enc_t enc; |
630 | enc.json = *json; |
476 | enc.flags = flags; |
|
|
477 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
478 | enc.cur = SvPVX (enc.sv); |
632 | enc.cur = SvPVX (enc.sv); |
479 | enc.end = SvEND (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
480 | enc.indent = 0; |
634 | enc.indent = 0; |
481 | enc.max_depth = 0x7fffffffUL; |
635 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
482 | |
636 | |
483 | SvPOK_only (enc.sv); |
637 | SvPOK_only (enc.sv); |
484 | encode_sv (&enc, scalar); |
638 | encode_sv (&enc, scalar); |
485 | |
639 | |
|
|
640 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
641 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
642 | |
486 | if (!(flags & (F_ASCII | F_UTF8))) |
643 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
487 | SvUTF8_on (enc.sv); |
644 | SvUTF8_on (enc.sv); |
488 | |
645 | |
489 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
490 | |
|
|
491 | if (enc.flags & F_SHRINK) |
646 | if (enc.json.flags & F_SHRINK) |
492 | shrink (enc.sv); |
647 | shrink (enc.sv); |
493 | |
648 | |
494 | return enc.sv; |
649 | return enc.sv; |
495 | } |
650 | } |
496 | |
651 | |
… | |
… | |
501 | typedef struct |
656 | typedef struct |
502 | { |
657 | { |
503 | char *cur; // current parser pointer |
658 | char *cur; // current parser pointer |
504 | char *end; // end of input string |
659 | char *end; // end of input string |
505 | const char *err; // parse error, if != 0 |
660 | const char *err; // parse error, if != 0 |
506 | UV flags; // F_* |
661 | struct json json; |
|
|
662 | U32 depth; // recursion depth |
|
|
663 | U32 maxdepth; // recursion depth limit |
507 | } dec_t; |
664 | } dec_t; |
508 | |
665 | |
509 | static void |
666 | inline void |
510 | decode_ws (dec_t *dec) |
667 | decode_ws (dec_t *dec) |
511 | { |
668 | { |
512 | for (;;) |
669 | for (;;) |
513 | { |
670 | { |
514 | char ch = *dec->cur; |
671 | char ch = *dec->cur; |
… | |
… | |
520 | ++dec->cur; |
677 | ++dec->cur; |
521 | } |
678 | } |
522 | } |
679 | } |
523 | |
680 | |
524 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
681 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
682 | |
525 | #define EXPECT_CH(ch) SB \ |
683 | #define EXPECT_CH(ch) SB \ |
526 | if (*dec->cur != ch) \ |
684 | if (*dec->cur != ch) \ |
527 | ERR (# ch " expected"); \ |
685 | ERR (# ch " expected"); \ |
528 | ++dec->cur; \ |
686 | ++dec->cur; \ |
529 | SE |
687 | SE |
530 | |
688 | |
|
|
689 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
690 | #define DEC_DEC_DEPTH --dec->depth |
|
|
691 | |
531 | static SV *decode_sv (dec_t *dec); |
692 | static SV *decode_sv (dec_t *dec); |
532 | |
693 | |
533 | static signed char decode_hexdigit[256]; |
694 | static signed char decode_hexdigit[256]; |
534 | |
695 | |
535 | static UV |
696 | static UV |
536 | decode_4hex (dec_t *dec) |
697 | decode_4hex (dec_t *dec) |
537 | { |
698 | { |
538 | signed char d1, d2, d3, d4; |
699 | signed char d1, d2, d3, d4; |
539 | unsigned char *cur = (unsigned char *)dec->cur; |
700 | unsigned char *cur = (unsigned char *)dec->cur; |
540 | |
701 | |
541 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
702 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
542 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
703 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
543 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
704 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
544 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
705 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
545 | |
706 | |
546 | dec->cur += 4; |
707 | dec->cur += 4; |
547 | |
708 | |
548 | return ((UV)d1) << 12 |
709 | return ((UV)d1) << 12 |
549 | | ((UV)d2) << 8 |
710 | | ((UV)d2) << 8 |
… | |
… | |
557 | static SV * |
718 | static SV * |
558 | decode_str (dec_t *dec) |
719 | decode_str (dec_t *dec) |
559 | { |
720 | { |
560 | SV *sv = 0; |
721 | SV *sv = 0; |
561 | int utf8 = 0; |
722 | int utf8 = 0; |
|
|
723 | char *dec_cur = dec->cur; |
562 | |
724 | |
563 | do |
725 | do |
564 | { |
726 | { |
565 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
727 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
566 | char *cur = buf; |
728 | char *cur = buf; |
567 | |
729 | |
568 | do |
730 | do |
569 | { |
731 | { |
570 | unsigned char ch = *(unsigned char *)dec->cur++; |
732 | unsigned char ch = *(unsigned char *)dec_cur++; |
571 | |
733 | |
572 | if (ch == '"') |
734 | if (expect_false (ch == '"')) |
573 | { |
735 | { |
574 | --dec->cur; |
736 | --dec_cur; |
575 | break; |
737 | break; |
576 | } |
738 | } |
577 | else if (ch == '\\') |
739 | else if (expect_false (ch == '\\')) |
578 | { |
740 | { |
579 | switch (*dec->cur) |
741 | switch (*dec_cur) |
580 | { |
742 | { |
581 | case '\\': |
743 | case '\\': |
582 | case '/': |
744 | case '/': |
583 | case '"': *cur++ = *dec->cur++; break; |
745 | case '"': *cur++ = *dec_cur++; break; |
584 | |
746 | |
585 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
747 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
586 | case 't': ++dec->cur; *cur++ = '\011'; break; |
748 | case 't': ++dec_cur; *cur++ = '\011'; break; |
587 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
749 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
588 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
750 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
589 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
751 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
590 | |
752 | |
591 | case 'u': |
753 | case 'u': |
592 | { |
754 | { |
593 | UV lo, hi; |
755 | UV lo, hi; |
594 | ++dec->cur; |
756 | ++dec_cur; |
595 | |
757 | |
|
|
758 | dec->cur = dec_cur; |
596 | hi = decode_4hex (dec); |
759 | hi = decode_4hex (dec); |
|
|
760 | dec_cur = dec->cur; |
597 | if (hi == (UV)-1) |
761 | if (hi == (UV)-1) |
598 | goto fail; |
762 | goto fail; |
599 | |
763 | |
600 | // possibly a surrogate pair |
764 | // possibly a surrogate pair |
601 | if (hi >= 0xd800) |
765 | if (hi >= 0xd800) |
602 | if (hi < 0xdc00) |
766 | if (hi < 0xdc00) |
603 | { |
767 | { |
604 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
768 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
605 | ERR ("missing low surrogate character in surrogate pair"); |
769 | ERR ("missing low surrogate character in surrogate pair"); |
606 | |
770 | |
607 | dec->cur += 2; |
771 | dec_cur += 2; |
608 | |
772 | |
|
|
773 | dec->cur = dec_cur; |
609 | lo = decode_4hex (dec); |
774 | lo = decode_4hex (dec); |
|
|
775 | dec_cur = dec->cur; |
610 | if (lo == (UV)-1) |
776 | if (lo == (UV)-1) |
611 | goto fail; |
777 | goto fail; |
612 | |
778 | |
613 | if (lo < 0xdc00 || lo >= 0xe000) |
779 | if (lo < 0xdc00 || lo >= 0xe000) |
614 | ERR ("surrogate pair expected"); |
780 | ERR ("surrogate pair expected"); |
… | |
… | |
628 | *cur++ = hi; |
794 | *cur++ = hi; |
629 | } |
795 | } |
630 | break; |
796 | break; |
631 | |
797 | |
632 | default: |
798 | default: |
633 | --dec->cur; |
799 | --dec_cur; |
634 | ERR ("illegal backslash escape sequence in string"); |
800 | ERR ("illegal backslash escape sequence in string"); |
635 | } |
801 | } |
636 | } |
802 | } |
637 | else if (ch >= 0x20 && ch <= 0x7f) |
803 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
638 | *cur++ = ch; |
804 | *cur++ = ch; |
639 | else if (ch >= 0x80) |
805 | else if (ch >= 0x80) |
640 | { |
806 | { |
641 | --dec->cur; |
|
|
642 | |
|
|
643 | STRLEN clen; |
807 | STRLEN clen; |
|
|
808 | UV uch; |
|
|
809 | |
|
|
810 | --dec_cur; |
|
|
811 | |
644 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
812 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
645 | if (clen == (STRLEN)-1) |
813 | if (clen == (STRLEN)-1) |
646 | ERR ("malformed UTF-8 character in JSON string"); |
814 | ERR ("malformed UTF-8 character in JSON string"); |
647 | |
815 | |
648 | do |
816 | do |
649 | { |
|
|
650 | *cur++ = *dec->cur++; |
817 | *cur++ = *dec_cur++; |
651 | } |
|
|
652 | while (--clen); |
818 | while (--clen); |
653 | |
819 | |
654 | utf8 = 1; |
820 | utf8 = 1; |
655 | } |
821 | } |
656 | else if (!ch) |
|
|
657 | ERR ("unexpected end of string while parsing json string"); |
|
|
658 | else |
822 | else |
|
|
823 | { |
|
|
824 | --dec_cur; |
|
|
825 | |
|
|
826 | if (!ch) |
|
|
827 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
828 | else |
659 | ERR ("invalid character encountered"); |
829 | ERR ("invalid character encountered while parsing JSON string"); |
660 | |
830 | } |
661 | } |
831 | } |
662 | while (cur < buf + SHORT_STRING_LEN); |
832 | while (cur < buf + SHORT_STRING_LEN); |
663 | |
833 | |
|
|
834 | { |
664 | STRLEN len = cur - buf; |
835 | STRLEN len = cur - buf; |
665 | |
836 | |
666 | if (sv) |
837 | if (sv) |
667 | { |
838 | { |
668 | SvGROW (sv, SvCUR (sv) + len + 1); |
839 | SvGROW (sv, SvCUR (sv) + len + 1); |
669 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
840 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
670 | SvCUR_set (sv, SvCUR (sv) + len); |
841 | SvCUR_set (sv, SvCUR (sv) + len); |
671 | } |
842 | } |
672 | else |
843 | else |
673 | sv = newSVpvn (buf, len); |
844 | sv = newSVpvn (buf, len); |
674 | } |
845 | } |
|
|
846 | } |
675 | while (*dec->cur != '"'); |
847 | while (*dec_cur != '"'); |
676 | |
848 | |
677 | ++dec->cur; |
849 | ++dec_cur; |
678 | |
850 | |
679 | if (sv) |
851 | if (sv) |
680 | { |
852 | { |
681 | SvPOK_only (sv); |
853 | SvPOK_only (sv); |
682 | *SvEND (sv) = 0; |
854 | *SvEND (sv) = 0; |
… | |
… | |
685 | SvUTF8_on (sv); |
857 | SvUTF8_on (sv); |
686 | } |
858 | } |
687 | else |
859 | else |
688 | sv = newSVpvn ("", 0); |
860 | sv = newSVpvn ("", 0); |
689 | |
861 | |
|
|
862 | dec->cur = dec_cur; |
690 | return sv; |
863 | return sv; |
691 | |
864 | |
692 | fail: |
865 | fail: |
|
|
866 | dec->cur = dec_cur; |
693 | return 0; |
867 | return 0; |
694 | } |
868 | } |
695 | |
869 | |
696 | static SV * |
870 | static SV * |
697 | decode_num (dec_t *dec) |
871 | decode_num (dec_t *dec) |
… | |
… | |
755 | is_nv = 1; |
929 | is_nv = 1; |
756 | } |
930 | } |
757 | |
931 | |
758 | if (!is_nv) |
932 | if (!is_nv) |
759 | { |
933 | { |
760 | UV uv; |
934 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
761 | int numtype = grok_number (start, dec->cur - start, &uv); |
935 | if (*start == '-') |
762 | if (numtype & IS_NUMBER_IN_UV) |
936 | switch (dec->cur - start) |
763 | if (numtype & IS_NUMBER_NEG) |
|
|
764 | { |
937 | { |
765 | if (uv < (UV)IV_MIN) |
938 | case 2: return newSViv (-( start [1] - '0' * 1)); |
766 | return newSViv (-(IV)uv); |
939 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
940 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
941 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
767 | } |
942 | } |
|
|
943 | else |
|
|
944 | switch (dec->cur - start) |
|
|
945 | { |
|
|
946 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
947 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
948 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
949 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
950 | } |
|
|
951 | |
|
|
952 | { |
|
|
953 | UV uv; |
|
|
954 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
955 | if (numtype & IS_NUMBER_IN_UV) |
|
|
956 | if (numtype & IS_NUMBER_NEG) |
|
|
957 | { |
|
|
958 | if (uv < (UV)IV_MIN) |
|
|
959 | return newSViv (-(IV)uv); |
|
|
960 | } |
768 | else |
961 | else |
769 | return newSVuv (uv); |
962 | return newSVuv (uv); |
|
|
963 | |
|
|
964 | // here would likely be the place for bigint support |
770 | } |
965 | } |
|
|
966 | } |
771 | |
967 | |
|
|
968 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
772 | return newSVnv (Atof (start)); |
969 | return newSVnv (Atof (start)); |
773 | |
970 | |
774 | fail: |
971 | fail: |
775 | return 0; |
972 | return 0; |
776 | } |
973 | } |
… | |
… | |
778 | static SV * |
975 | static SV * |
779 | decode_av (dec_t *dec) |
976 | decode_av (dec_t *dec) |
780 | { |
977 | { |
781 | AV *av = newAV (); |
978 | AV *av = newAV (); |
782 | |
979 | |
|
|
980 | DEC_INC_DEPTH; |
783 | decode_ws (dec); |
981 | decode_ws (dec); |
|
|
982 | |
784 | if (*dec->cur == ']') |
983 | if (*dec->cur == ']') |
785 | ++dec->cur; |
984 | ++dec->cur; |
786 | else |
985 | else |
787 | for (;;) |
986 | for (;;) |
788 | { |
987 | { |
… | |
… | |
806 | ERR (", or ] expected while parsing array"); |
1005 | ERR (", or ] expected while parsing array"); |
807 | |
1006 | |
808 | ++dec->cur; |
1007 | ++dec->cur; |
809 | } |
1008 | } |
810 | |
1009 | |
|
|
1010 | DEC_DEC_DEPTH; |
811 | return newRV_noinc ((SV *)av); |
1011 | return newRV_noinc ((SV *)av); |
812 | |
1012 | |
813 | fail: |
1013 | fail: |
814 | SvREFCNT_dec (av); |
1014 | SvREFCNT_dec (av); |
|
|
1015 | DEC_DEC_DEPTH; |
815 | return 0; |
1016 | return 0; |
816 | } |
1017 | } |
817 | |
1018 | |
818 | static SV * |
1019 | static SV * |
819 | decode_hv (dec_t *dec) |
1020 | decode_hv (dec_t *dec) |
820 | { |
1021 | { |
821 | HV *hv = newHV (); |
1022 | HV *hv = newHV (); |
822 | |
1023 | |
|
|
1024 | DEC_INC_DEPTH; |
823 | decode_ws (dec); |
1025 | decode_ws (dec); |
|
|
1026 | |
824 | if (*dec->cur == '}') |
1027 | if (*dec->cur == '}') |
825 | ++dec->cur; |
1028 | ++dec->cur; |
826 | else |
1029 | else |
827 | for (;;) |
1030 | for (;;) |
828 | { |
1031 | { |
829 | SV *key, *value; |
|
|
830 | |
|
|
831 | decode_ws (dec); EXPECT_CH ('"'); |
1032 | decode_ws (dec); EXPECT_CH ('"'); |
832 | |
1033 | |
833 | key = decode_str (dec); |
1034 | // heuristic: assume that |
834 | if (!key) |
1035 | // a) decode_str + hv_store_ent are abysmally slow. |
835 | goto fail; |
1036 | // b) most hash keys are short, simple ascii text. |
|
|
1037 | // => try to "fast-match" such strings to avoid |
|
|
1038 | // the overhead of decode_str + hv_store_ent. |
|
|
1039 | { |
|
|
1040 | SV *value; |
|
|
1041 | char *p = dec->cur; |
|
|
1042 | char *e = p + 24; // only try up to 24 bytes |
836 | |
1043 | |
837 | decode_ws (dec); EXPECT_CH (':'); |
1044 | for (;;) |
838 | |
|
|
839 | value = decode_sv (dec); |
|
|
840 | if (!value) |
|
|
841 | { |
1045 | { |
|
|
1046 | // the >= 0x80 is true on most architectures |
|
|
1047 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1048 | { |
|
|
1049 | // slow path, back up and use decode_str |
|
|
1050 | SV *key = decode_str (dec); |
|
|
1051 | if (!key) |
|
|
1052 | goto fail; |
|
|
1053 | |
|
|
1054 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1055 | |
|
|
1056 | value = decode_sv (dec); |
|
|
1057 | if (!value) |
|
|
1058 | { |
|
|
1059 | SvREFCNT_dec (key); |
|
|
1060 | goto fail; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | hv_store_ent (hv, key, value, 0); |
842 | SvREFCNT_dec (key); |
1064 | SvREFCNT_dec (key); |
|
|
1065 | |
|
|
1066 | break; |
|
|
1067 | } |
|
|
1068 | else if (*p == '"') |
|
|
1069 | { |
|
|
1070 | // fast path, got a simple key |
|
|
1071 | char *key = dec->cur; |
|
|
1072 | int len = p - key; |
|
|
1073 | dec->cur = p + 1; |
|
|
1074 | |
|
|
1075 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1076 | |
|
|
1077 | value = decode_sv (dec); |
|
|
1078 | if (!value) |
843 | goto fail; |
1079 | goto fail; |
|
|
1080 | |
|
|
1081 | hv_store (hv, key, len, value, 0); |
|
|
1082 | |
|
|
1083 | break; |
|
|
1084 | } |
|
|
1085 | |
|
|
1086 | ++p; |
844 | } |
1087 | } |
845 | |
1088 | } |
846 | //TODO: optimise |
|
|
847 | hv_store_ent (hv, key, value, 0); |
|
|
848 | |
1089 | |
849 | decode_ws (dec); |
1090 | decode_ws (dec); |
850 | |
1091 | |
851 | if (*dec->cur == '}') |
1092 | if (*dec->cur == '}') |
852 | { |
1093 | { |
… | |
… | |
858 | ERR (", or } expected while parsing object/hash"); |
1099 | ERR (", or } expected while parsing object/hash"); |
859 | |
1100 | |
860 | ++dec->cur; |
1101 | ++dec->cur; |
861 | } |
1102 | } |
862 | |
1103 | |
|
|
1104 | DEC_DEC_DEPTH; |
863 | return newRV_noinc ((SV *)hv); |
1105 | return newRV_noinc ((SV *)hv); |
864 | |
1106 | |
865 | fail: |
1107 | fail: |
866 | SvREFCNT_dec (hv); |
1108 | SvREFCNT_dec (hv); |
|
|
1109 | DEC_DEC_DEPTH; |
867 | return 0; |
1110 | return 0; |
868 | } |
1111 | } |
869 | |
1112 | |
870 | static SV * |
1113 | static SV * |
871 | decode_sv (dec_t *dec) |
1114 | decode_sv (dec_t *dec) |
872 | { |
1115 | { |
873 | decode_ws (dec); |
1116 | decode_ws (dec); |
|
|
1117 | |
|
|
1118 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1119 | // to parse anything. |
874 | switch (*dec->cur) |
1120 | switch (*dec->cur) |
875 | { |
1121 | { |
876 | case '"': ++dec->cur; return decode_str (dec); |
1122 | case '"': ++dec->cur; return decode_str (dec); |
877 | case '[': ++dec->cur; return decode_av (dec); |
1123 | case '[': ++dec->cur; return decode_av (dec); |
878 | case '{': ++dec->cur; return decode_hv (dec); |
1124 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
884 | |
1130 | |
885 | case 't': |
1131 | case 't': |
886 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1132 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
887 | { |
1133 | { |
888 | dec->cur += 4; |
1134 | dec->cur += 4; |
889 | return newSViv (1); |
1135 | return SvREFCNT_inc (json_true); |
890 | } |
1136 | } |
891 | else |
1137 | else |
892 | ERR ("'true' expected"); |
1138 | ERR ("'true' expected"); |
893 | |
1139 | |
894 | break; |
1140 | break; |
895 | |
1141 | |
896 | case 'f': |
1142 | case 'f': |
897 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1143 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
898 | { |
1144 | { |
899 | dec->cur += 5; |
1145 | dec->cur += 5; |
900 | return newSViv (0); |
1146 | return SvREFCNT_inc (json_false); |
901 | } |
1147 | } |
902 | else |
1148 | else |
903 | ERR ("'false' expected"); |
1149 | ERR ("'false' expected"); |
904 | |
1150 | |
905 | break; |
1151 | break; |
… | |
… | |
914 | ERR ("'null' expected"); |
1160 | ERR ("'null' expected"); |
915 | |
1161 | |
916 | break; |
1162 | break; |
917 | |
1163 | |
918 | default: |
1164 | default: |
919 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1165 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
920 | break; |
1166 | break; |
921 | } |
1167 | } |
922 | |
1168 | |
923 | fail: |
1169 | fail: |
924 | return 0; |
1170 | return 0; |
925 | } |
1171 | } |
926 | |
1172 | |
927 | static SV * |
1173 | static SV * |
928 | decode_json (SV *string, UV flags) |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
929 | { |
1175 | { |
|
|
1176 | dec_t dec; |
|
|
1177 | UV offset; |
930 | SV *sv; |
1178 | SV *sv; |
931 | |
1179 | |
|
|
1180 | SvGETMAGIC (string); |
|
|
1181 | SvUPGRADE (string, SVt_PV); |
|
|
1182 | |
|
|
1183 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1184 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1185 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1186 | |
932 | if (flags & F_UTF8) |
1187 | if (json->flags & F_UTF8) |
933 | sv_utf8_downgrade (string, 0); |
1188 | sv_utf8_downgrade (string, 0); |
934 | else |
1189 | else |
935 | sv_utf8_upgrade (string); |
1190 | sv_utf8_upgrade (string); |
936 | |
1191 | |
937 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1192 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
938 | |
1193 | |
939 | dec_t dec; |
1194 | dec.json = *json; |
940 | dec.flags = flags; |
|
|
941 | dec.cur = SvPVX (string); |
1195 | dec.cur = SvPVX (string); |
942 | dec.end = SvEND (string); |
1196 | dec.end = SvEND (string); |
943 | dec.err = 0; |
1197 | dec.err = 0; |
|
|
1198 | dec.depth = 0; |
|
|
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
944 | |
1200 | |
|
|
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
945 | sv = decode_sv (&dec); |
1202 | sv = decode_sv (&dec); |
946 | |
1203 | |
|
|
1204 | if (!(offset_return || !sv)) |
|
|
1205 | { |
|
|
1206 | // check for trailing garbage |
|
|
1207 | decode_ws (&dec); |
|
|
1208 | |
|
|
1209 | if (*dec.cur) |
|
|
1210 | { |
|
|
1211 | dec.err = "garbage after JSON object"; |
|
|
1212 | SvREFCNT_dec (sv); |
|
|
1213 | sv = 0; |
|
|
1214 | } |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | if (offset_return || !sv) |
|
|
1218 | { |
|
|
1219 | offset = dec.json.flags & F_UTF8 |
|
|
1220 | ? dec.cur - SvPVX (string) |
|
|
1221 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1222 | |
|
|
1223 | if (offset_return) |
|
|
1224 | *offset_return = offset; |
|
|
1225 | } |
|
|
1226 | |
947 | if (!sv) |
1227 | if (!sv) |
948 | { |
1228 | { |
949 | IV offset = dec.flags & F_UTF8 |
|
|
950 | ? dec.cur - SvPVX (string) |
|
|
951 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
952 | SV *uni = sv_newmortal (); |
1229 | SV *uni = sv_newmortal (); |
953 | |
1230 | |
954 | // horrible hack to silence warning inside pv_uni_display |
1231 | // horrible hack to silence warning inside pv_uni_display |
955 | COP cop = *PL_curcop; |
1232 | COP cop = *PL_curcop; |
956 | cop.cop_warnings = pWARN_NONE; |
1233 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
958 | SAVEVPTR (PL_curcop); |
1235 | SAVEVPTR (PL_curcop); |
959 | PL_curcop = &cop; |
1236 | PL_curcop = &cop; |
960 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1237 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
961 | LEAVE; |
1238 | LEAVE; |
962 | |
1239 | |
963 | croak ("%s, at character offset %d (%s)", |
1240 | croak ("%s, at character offset %d [\"%s\"]", |
964 | dec.err, |
1241 | dec.err, |
965 | (int)offset, |
1242 | (int)offset, |
966 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1243 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
967 | } |
1244 | } |
968 | |
1245 | |
969 | sv = sv_2mortal (sv); |
1246 | sv = sv_2mortal (sv); |
970 | |
1247 | |
971 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1248 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
972 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1249 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
973 | |
1250 | |
974 | return sv; |
1251 | return sv; |
975 | } |
1252 | } |
976 | |
1253 | |
… | |
… | |
981 | |
1258 | |
982 | BOOT: |
1259 | BOOT: |
983 | { |
1260 | { |
984 | int i; |
1261 | int i; |
985 | |
1262 | |
986 | memset (decode_hexdigit, 0xff, 256); |
|
|
987 | for (i = 10; i--; ) |
1263 | for (i = 0; i < 256; ++i) |
988 | decode_hexdigit ['0' + i] = i; |
1264 | decode_hexdigit [i] = |
|
|
1265 | i >= '0' && i <= '9' ? i - '0' |
|
|
1266 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
|
|
1267 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
|
|
1268 | : -1; |
989 | |
1269 | |
990 | for (i = 7; i--; ) |
|
|
991 | { |
|
|
992 | decode_hexdigit ['a' + i] = 10 + i; |
|
|
993 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
994 | } |
|
|
995 | |
|
|
996 | json_stash = gv_stashpv ("JSON::XS", 1); |
1270 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1271 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1272 | |
|
|
1273 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
997 | } |
1275 | } |
998 | |
1276 | |
999 | PROTOTYPES: DISABLE |
1277 | PROTOTYPES: DISABLE |
1000 | |
1278 | |
1001 | SV *new (char *dummy) |
1279 | SV *new (char *dummy) |
… | |
… | |
1004 | OUTPUT: |
1282 | OUTPUT: |
1005 | RETVAL |
1283 | RETVAL |
1006 | |
1284 | |
1007 | SV *ascii (SV *self, int enable = 1) |
1285 | SV *ascii (SV *self, int enable = 1) |
1008 | ALIAS: |
1286 | ALIAS: |
1009 | ascii = F_ASCII |
1287 | ascii = F_ASCII |
|
|
1288 | latin1 = F_LATIN1 |
1010 | utf8 = F_UTF8 |
1289 | utf8 = F_UTF8 |
1011 | indent = F_INDENT |
1290 | indent = F_INDENT |
1012 | canonical = F_CANONICAL |
1291 | canonical = F_CANONICAL |
1013 | space_before = F_SPACE_BEFORE |
1292 | space_before = F_SPACE_BEFORE |
1014 | space_after = F_SPACE_AFTER |
1293 | space_after = F_SPACE_AFTER |
1015 | pretty = F_PRETTY |
1294 | pretty = F_PRETTY |
1016 | allow_nonref = F_ALLOW_NONREF |
1295 | allow_nonref = F_ALLOW_NONREF |
1017 | shrink = F_SHRINK |
1296 | shrink = F_SHRINK |
|
|
1297 | allow_blessed = F_ALLOW_BLESSED |
|
|
1298 | convert_blessed = F_CONV_BLESSED |
1018 | CODE: |
1299 | CODE: |
1019 | { |
1300 | { |
1020 | UV *uv = SvJSON (self); |
1301 | UV *uv = SvJSON (self); |
1021 | if (enable) |
1302 | if (enable) |
1022 | *uv |= ix; |
1303 | *uv |= ix; |
… | |
… | |
1026 | RETVAL = newSVsv (self); |
1307 | RETVAL = newSVsv (self); |
1027 | } |
1308 | } |
1028 | OUTPUT: |
1309 | OUTPUT: |
1029 | RETVAL |
1310 | RETVAL |
1030 | |
1311 | |
|
|
1312 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1313 | CODE: |
|
|
1314 | { |
|
|
1315 | UV *uv = SvJSON (self); |
|
|
1316 | UV log2 = 0; |
|
|
1317 | |
|
|
1318 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1319 | |
|
|
1320 | while ((1UL << log2) < max_depth) |
|
|
1321 | ++log2; |
|
|
1322 | |
|
|
1323 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1324 | |
|
|
1325 | RETVAL = newSVsv (self); |
|
|
1326 | } |
|
|
1327 | OUTPUT: |
|
|
1328 | RETVAL |
|
|
1329 | |
|
|
1330 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1331 | CODE: |
|
|
1332 | { |
|
|
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
|
|
1335 | |
|
|
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1337 | if (max_size == 1) max_size = 2; |
|
|
1338 | |
|
|
1339 | while ((1UL << log2) < max_size) |
|
|
1340 | ++log2; |
|
|
1341 | |
|
|
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1343 | |
|
|
1344 | RETVAL = newSVsv (self); |
|
|
1345 | } |
|
|
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1031 | void encode (SV *self, SV *scalar) |
1349 | void encode (SV *self, SV *scalar) |
1032 | PPCODE: |
1350 | PPCODE: |
|
|
1351 | { |
|
|
1352 | struct json json = { *SvJSON (self) }; |
1033 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1353 | XPUSHs (encode_json (scalar, &json)); |
|
|
1354 | } |
1034 | |
1355 | |
1035 | void decode (SV *self, SV *jsonstr) |
1356 | void decode (SV *self, SV *jsonstr) |
1036 | PPCODE: |
1357 | PPCODE: |
|
|
1358 | { |
|
|
1359 | struct json json = { *SvJSON (self) }; |
1037 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1361 | } |
|
|
1362 | |
|
|
1363 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1364 | PPCODE: |
|
|
1365 | { |
|
|
1366 | UV offset; |
|
|
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
|
|
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
|
|
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1371 | } |
1038 | |
1372 | |
1039 | PROTOTYPES: ENABLE |
1373 | PROTOTYPES: ENABLE |
1040 | |
1374 | |
1041 | void to_json (SV *scalar) |
1375 | void to_json (SV *scalar) |
1042 | PPCODE: |
1376 | PPCODE: |
|
|
1377 | { |
|
|
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1043 | XPUSHs (encode_json (scalar, F_UTF8)); |
1379 | XPUSHs (encode_json (scalar, &json)); |
|
|
1380 | } |
1044 | |
1381 | |
1045 | void from_json (SV *jsonstr) |
1382 | void from_json (SV *jsonstr) |
1046 | PPCODE: |
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1047 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1387 | } |
1048 | |
1388 | |