… | |
… | |
10 | #define F_UTF8 0x00000002 |
10 | #define F_UTF8 0x00000002 |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004 |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008 |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010 |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020 |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080 |
17 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100 |
18 | |
17 | |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
20 | #define F_DEFAULT 0 |
19 | #define F_DEFAULT 0 |
21 | |
20 | |
22 | #define INIT_SIZE 32 // initial scalar size to be allocated |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
22 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
23 | |
|
|
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 |
23 | |
26 | |
24 | #define SB do { |
27 | #define SB do { |
25 | #define SE } while (0) |
28 | #define SE } while (0) |
26 | |
29 | |
27 | static HV *json_stash; |
30 | static HV *json_stash; // JSON::XS:: |
|
|
31 | |
|
|
32 | ///////////////////////////////////////////////////////////////////////////// |
|
|
33 | // utility functions |
|
|
34 | |
|
|
35 | static UV * |
|
|
36 | SvJSON (SV *sv) |
|
|
37 | { |
|
|
38 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
39 | croak ("object is not of type JSON::XS"); |
|
|
40 | |
|
|
41 | return &SvUVX (SvRV (sv)); |
|
|
42 | } |
|
|
43 | |
|
|
44 | static void |
|
|
45 | shrink (SV *sv) |
|
|
46 | { |
|
|
47 | sv_utf8_downgrade (sv, 1); |
|
|
48 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
49 | { |
|
|
50 | #ifdef SvPV_shrink_to_cur |
|
|
51 | SvPV_shrink_to_cur (sv); |
|
|
52 | #elif defined (SvPV_renew) |
|
|
53 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
54 | #endif |
|
|
55 | } |
|
|
56 | } |
|
|
57 | |
|
|
58 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
59 | // case of an error. |
|
|
60 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
61 | // but use the very good perl function to parse anything else. |
|
|
62 | // note that we never call this function for a ascii codepoints |
|
|
63 | static UV |
|
|
64 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
65 | { |
|
|
66 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
67 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
68 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
69 | { |
|
|
70 | *clen = 2; |
|
|
71 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
72 | } |
|
|
73 | else |
|
|
74 | return (UV)-1; |
|
|
75 | } |
|
|
76 | |
|
|
77 | ///////////////////////////////////////////////////////////////////////////// |
|
|
78 | // encoder |
28 | |
79 | |
29 | // structure used for encoding JSON |
80 | // structure used for encoding JSON |
30 | typedef struct |
81 | typedef struct |
31 | { |
82 | { |
32 | char *cur; |
83 | char *cur; // SvPVX (sv) + current output position |
33 | STRLEN len; // SvLEN (sv) |
|
|
34 | char *end; // SvEND (sv) |
84 | char *end; // SvEND (sv) |
35 | SV *sv; |
85 | SV *sv; // result scalar |
36 | UV flags; |
86 | UV flags; // F_* |
37 | int max_recurse; |
87 | int indent; // indentation level |
38 | int indent; |
88 | int max_depth; // max. recursion level |
39 | } enc_t; |
89 | } enc_t; |
40 | |
|
|
41 | // structure used for decoding JSON |
|
|
42 | typedef struct |
|
|
43 | { |
|
|
44 | char *cur; |
|
|
45 | char *end; |
|
|
46 | const char *err; |
|
|
47 | UV flags; |
|
|
48 | } dec_t; |
|
|
49 | |
|
|
50 | static UV * |
|
|
51 | SvJSON (SV *sv) |
|
|
52 | { |
|
|
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
54 | croak ("object is not of type JSON::XS"); |
|
|
55 | |
|
|
56 | return &SvUVX (SvRV (sv)); |
|
|
57 | } |
|
|
58 | |
|
|
59 | static void |
|
|
60 | shrink (SV *sv) |
|
|
61 | { |
|
|
62 | sv_utf8_downgrade (sv, 1); |
|
|
63 | #ifdef SvPV_shrink_to_cur |
|
|
64 | SvPV_shrink_to_cur (sv); |
|
|
65 | #endif |
|
|
66 | } |
|
|
67 | |
|
|
68 | ///////////////////////////////////////////////////////////////////////////// |
|
|
69 | |
90 | |
70 | static void |
91 | static void |
71 | need (enc_t *enc, STRLEN len) |
92 | need (enc_t *enc, STRLEN len) |
72 | { |
93 | { |
73 | if (enc->cur + len >= enc->end) |
94 | if (enc->cur + len >= enc->end) |
… | |
… | |
131 | STRLEN clen; |
152 | STRLEN clen; |
132 | UV uch; |
153 | UV uch; |
133 | |
154 | |
134 | if (is_utf8) |
155 | if (is_utf8) |
135 | { |
156 | { |
136 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
157 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
158 | uch = decode_utf8 (str, end - str, &clen); |
137 | if (clen == (STRLEN)-1) |
159 | if (clen == (STRLEN)-1) |
138 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
160 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
139 | } |
161 | } |
140 | else |
162 | else |
141 | { |
163 | { |
… | |
… | |
150 | { |
172 | { |
151 | if (uch > 0xFFFFUL) |
173 | if (uch > 0xFFFFUL) |
152 | { |
174 | { |
153 | need (enc, len += 11); |
175 | need (enc, len += 11); |
154 | sprintf (enc->cur, "\\u%04x\\u%04x", |
176 | sprintf (enc->cur, "\\u%04x\\u%04x", |
155 | (uch - 0x10000) / 0x400 + 0xD800, |
177 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
156 | (uch - 0x10000) % 0x400 + 0xDC00); |
178 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
157 | enc->cur += 12; |
179 | enc->cur += 12; |
158 | } |
180 | } |
159 | else |
181 | else |
160 | { |
182 | { |
161 | static char hexdigit [16] = "0123456789abcdef"; |
183 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
179 | } |
201 | } |
180 | while (--clen); |
202 | while (--clen); |
181 | } |
203 | } |
182 | else |
204 | else |
183 | { |
205 | { |
184 | need (enc, len += 10); // never more than 11 bytes needed |
206 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
185 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
207 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
186 | ++str; |
208 | ++str; |
187 | } |
209 | } |
188 | } |
210 | } |
189 | } |
211 | } |
… | |
… | |
191 | |
213 | |
192 | --len; |
214 | --len; |
193 | } |
215 | } |
194 | } |
216 | } |
195 | |
217 | |
196 | #define INDENT SB \ |
218 | static void |
|
|
219 | encode_indent (enc_t *enc) |
|
|
220 | { |
197 | if (enc->flags & F_INDENT) \ |
221 | if (enc->flags & F_INDENT) |
198 | { \ |
222 | { |
199 | int i_; \ |
223 | int spaces = enc->indent * INDENT_STEP; |
200 | need (enc, enc->indent); \ |
|
|
201 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
202 | encode_ch (enc, ' '); \ |
|
|
203 | } \ |
|
|
204 | SE |
|
|
205 | |
224 | |
206 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
225 | need (enc, spaces); |
207 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
226 | memset (enc->cur, ' ', spaces); |
208 | #define COMMA SB \ |
227 | enc->cur += spaces; |
|
|
228 | } |
|
|
229 | } |
|
|
230 | |
|
|
231 | static void |
|
|
232 | encode_space (enc_t *enc) |
|
|
233 | { |
|
|
234 | need (enc, 1); |
209 | encode_ch (enc, ','); \ |
235 | encode_ch (enc, ' '); |
|
|
236 | } |
|
|
237 | |
|
|
238 | static void |
|
|
239 | encode_nl (enc_t *enc) |
|
|
240 | { |
210 | if (enc->flags & F_INDENT) \ |
241 | if (enc->flags & F_INDENT) |
211 | NL; \ |
242 | { |
|
|
243 | need (enc, 1); |
|
|
244 | encode_ch (enc, '\n'); |
|
|
245 | } |
|
|
246 | } |
|
|
247 | |
|
|
248 | static void |
|
|
249 | encode_comma (enc_t *enc) |
|
|
250 | { |
|
|
251 | encode_ch (enc, ','); |
|
|
252 | |
|
|
253 | if (enc->flags & F_INDENT) |
|
|
254 | encode_nl (enc); |
212 | else if (enc->flags & F_SPACE_AFTER) \ |
255 | else if (enc->flags & F_SPACE_AFTER) |
213 | SPACE; \ |
256 | encode_space (enc); |
214 | SE |
257 | } |
215 | |
258 | |
216 | static void encode_sv (enc_t *enc, SV *sv); |
259 | static void encode_sv (enc_t *enc, SV *sv); |
217 | |
260 | |
218 | static void |
261 | static void |
219 | encode_av (enc_t *enc, AV *av) |
262 | encode_av (enc_t *enc, AV *av) |
220 | { |
263 | { |
221 | int i, len = av_len (av); |
264 | int i, len = av_len (av); |
222 | |
265 | |
223 | encode_ch (enc, '['); NL; |
266 | encode_ch (enc, '['); encode_nl (enc); |
224 | ++enc->indent; |
267 | ++enc->indent; |
225 | |
268 | |
226 | for (i = 0; i <= len; ++i) |
269 | for (i = 0; i <= len; ++i) |
227 | { |
270 | { |
228 | INDENT; |
271 | encode_indent (enc); |
229 | encode_sv (enc, *av_fetch (av, i, 0)); |
272 | encode_sv (enc, *av_fetch (av, i, 0)); |
230 | |
273 | |
231 | if (i < len) |
274 | if (i < len) |
232 | COMMA; |
275 | encode_comma (enc); |
233 | } |
276 | } |
234 | |
277 | |
235 | NL; |
278 | encode_nl (enc); |
236 | |
279 | |
237 | --enc->indent; |
280 | --enc->indent; |
238 | INDENT; encode_ch (enc, ']'); |
281 | encode_indent (enc); encode_ch (enc, ']'); |
239 | } |
282 | } |
240 | |
283 | |
241 | static void |
284 | static void |
242 | encode_he (enc_t *enc, HE *he) |
285 | encode_he (enc_t *enc, HE *he) |
243 | { |
286 | { |
… | |
… | |
257 | else |
300 | else |
258 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
301 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
259 | |
302 | |
260 | encode_ch (enc, '"'); |
303 | encode_ch (enc, '"'); |
261 | |
304 | |
262 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
305 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
263 | encode_ch (enc, ':'); |
306 | encode_ch (enc, ':'); |
264 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
307 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
265 | encode_sv (enc, HeVAL (he)); |
308 | encode_sv (enc, HeVAL (he)); |
266 | } |
309 | } |
267 | |
310 | |
268 | // compare hash entries, used when all keys are bytestrings |
311 | // compare hash entries, used when all keys are bytestrings |
269 | static int |
312 | static int |
… | |
… | |
275 | HE *b = *(HE **)b_; |
318 | HE *b = *(HE **)b_; |
276 | |
319 | |
277 | STRLEN la = HeKLEN (a); |
320 | STRLEN la = HeKLEN (a); |
278 | STRLEN lb = HeKLEN (b); |
321 | STRLEN lb = HeKLEN (b); |
279 | |
322 | |
280 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
323 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
281 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
324 | cmp = la - lb; |
282 | |
325 | |
283 | return cmp; |
326 | return cmp; |
284 | } |
327 | } |
285 | |
328 | |
286 | // compare hash entries, used when some keys are sv's or utf-x |
329 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
293 | static void |
336 | static void |
294 | encode_hv (enc_t *enc, HV *hv) |
337 | encode_hv (enc_t *enc, HV *hv) |
295 | { |
338 | { |
296 | int count, i; |
339 | int count, i; |
297 | |
340 | |
298 | encode_ch (enc, '{'); NL; ++enc->indent; |
341 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
299 | |
342 | |
300 | if ((count = hv_iterinit (hv))) |
343 | if ((count = hv_iterinit (hv))) |
301 | { |
344 | { |
302 | // for canonical output we have to sort by keys first |
345 | // for canonical output we have to sort by keys first |
303 | // actually, this is mostly due to the stupid so-called |
346 | // actually, this is mostly due to the stupid so-called |
304 | // security workaround added somewhere in 5.8.x. |
347 | // security workaround added somewhere in 5.8.x. |
305 | // that randomises hash orderings |
348 | // that randomises hash orderings |
306 | if (enc->flags & F_CANONICAL) |
349 | if (enc->flags & F_CANONICAL) |
307 | { |
350 | { |
308 | HE *he, *hes [count]; |
351 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
309 | int fast = 1; |
352 | int fast = 1; |
310 | |
353 | |
311 | i = 0; |
354 | i = 0; |
312 | while ((he = hv_iternext (hv))) |
355 | while ((he = hv_iternext (hv))) |
313 | { |
356 | { |
… | |
… | |
338 | LEAVE; |
381 | LEAVE; |
339 | } |
382 | } |
340 | |
383 | |
341 | for (i = 0; i < count; ++i) |
384 | for (i = 0; i < count; ++i) |
342 | { |
385 | { |
343 | INDENT; |
386 | encode_indent (enc); |
344 | encode_he (enc, hes [i]); |
387 | encode_he (enc, hes [i]); |
345 | |
388 | |
346 | if (i < count - 1) |
389 | if (i < count - 1) |
347 | COMMA; |
390 | encode_comma (enc); |
348 | } |
391 | } |
349 | |
392 | |
350 | NL; |
393 | encode_nl (enc); |
351 | } |
394 | } |
352 | else |
395 | else |
353 | { |
396 | { |
354 | SV *sv; |
397 | SV *sv; |
355 | HE *he = hv_iternext (hv); |
398 | HE *he = hv_iternext (hv); |
356 | |
399 | |
357 | for (;;) |
400 | for (;;) |
358 | { |
401 | { |
359 | INDENT; |
402 | encode_indent (enc); |
360 | encode_he (enc, he); |
403 | encode_he (enc, he); |
361 | |
404 | |
362 | if (!(he = hv_iternext (hv))) |
405 | if (!(he = hv_iternext (hv))) |
363 | break; |
406 | break; |
364 | |
407 | |
365 | COMMA; |
408 | encode_comma (enc); |
366 | } |
409 | } |
367 | |
410 | |
368 | NL; |
411 | encode_nl (enc); |
369 | } |
412 | } |
370 | } |
413 | } |
371 | |
414 | |
372 | --enc->indent; INDENT; encode_ch (enc, '}'); |
415 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
373 | } |
416 | } |
374 | |
417 | |
375 | static void |
418 | static void |
376 | encode_sv (enc_t *enc, SV *sv) |
419 | encode_sv (enc_t *enc, SV *sv) |
377 | { |
420 | { |
… | |
… | |
401 | } |
444 | } |
402 | else if (SvROK (sv)) |
445 | else if (SvROK (sv)) |
403 | { |
446 | { |
404 | SV *rv = SvRV (sv); |
447 | SV *rv = SvRV (sv); |
405 | |
448 | |
406 | if (!--enc->max_recurse) |
449 | if (enc->indent >= enc->max_depth) |
407 | croak ("data structure too deep (hit recursion limit)"); |
450 | croak ("data structure too deep (hit recursion limit)"); |
408 | |
451 | |
409 | switch (SvTYPE (rv)) |
452 | switch (SvTYPE (rv)) |
410 | { |
453 | { |
411 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
454 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
… | |
… | |
428 | { |
471 | { |
429 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
472 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
430 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
473 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
431 | |
474 | |
432 | enc_t enc; |
475 | enc_t enc; |
433 | enc.flags = flags; |
476 | enc.flags = flags; |
434 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
477 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
435 | enc.cur = SvPVX (enc.sv); |
478 | enc.cur = SvPVX (enc.sv); |
436 | enc.end = SvEND (enc.sv); |
479 | enc.end = SvEND (enc.sv); |
437 | enc.max_recurse = 0; |
|
|
438 | enc.indent = 0; |
480 | enc.indent = 0; |
|
|
481 | enc.max_depth = 0x7fffffffUL; |
439 | |
482 | |
440 | SvPOK_only (enc.sv); |
483 | SvPOK_only (enc.sv); |
441 | encode_sv (&enc, scalar); |
484 | encode_sv (&enc, scalar); |
442 | |
485 | |
443 | if (!(flags & (F_ASCII | F_UTF8))) |
486 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
450 | |
493 | |
451 | return enc.sv; |
494 | return enc.sv; |
452 | } |
495 | } |
453 | |
496 | |
454 | ///////////////////////////////////////////////////////////////////////////// |
497 | ///////////////////////////////////////////////////////////////////////////// |
|
|
498 | // decoder |
455 | |
499 | |
456 | #define WS \ |
500 | // structure used for decoding JSON |
|
|
501 | typedef struct |
|
|
502 | { |
|
|
503 | char *cur; // current parser pointer |
|
|
504 | char *end; // end of input string |
|
|
505 | const char *err; // parse error, if != 0 |
|
|
506 | UV flags; // F_* |
|
|
507 | } dec_t; |
|
|
508 | |
|
|
509 | static void |
|
|
510 | decode_ws (dec_t *dec) |
|
|
511 | { |
457 | for (;;) \ |
512 | for (;;) |
458 | { \ |
513 | { |
459 | char ch = *dec->cur; \ |
514 | char ch = *dec->cur; |
|
|
515 | |
460 | if (ch > 0x20 \ |
516 | if (ch > 0x20 |
461 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
517 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
462 | break; \ |
518 | break; |
|
|
519 | |
463 | ++dec->cur; \ |
520 | ++dec->cur; |
464 | } |
521 | } |
|
|
522 | } |
465 | |
523 | |
466 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
524 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
467 | #define EXPECT_CH(ch) SB \ |
525 | #define EXPECT_CH(ch) SB \ |
468 | if (*dec->cur != ch) \ |
526 | if (*dec->cur != ch) \ |
469 | ERR (# ch " expected"); \ |
527 | ERR (# ch " expected"); \ |
… | |
… | |
476 | |
534 | |
477 | static UV |
535 | static UV |
478 | decode_4hex (dec_t *dec) |
536 | decode_4hex (dec_t *dec) |
479 | { |
537 | { |
480 | signed char d1, d2, d3, d4; |
538 | signed char d1, d2, d3, d4; |
|
|
539 | unsigned char *cur = (unsigned char *)dec->cur; |
481 | |
540 | |
482 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
483 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
541 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
484 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
485 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
542 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
486 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
487 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
543 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
488 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
489 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
544 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
490 | |
545 | |
491 | dec->cur += 4; |
546 | dec->cur += 4; |
492 | |
547 | |
493 | return ((UV)d1) << 12 |
548 | return ((UV)d1) << 12 |
494 | | ((UV)d2) << 8 |
549 | | ((UV)d2) << 8 |
… | |
… | |
497 | |
552 | |
498 | fail: |
553 | fail: |
499 | return (UV)-1; |
554 | return (UV)-1; |
500 | } |
555 | } |
501 | |
556 | |
502 | #define APPEND_GROW(n) SB \ |
|
|
503 | if (cur + (n) >= end) \ |
|
|
504 | { \ |
|
|
505 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
506 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
507 | cur = SvPVX (sv) + ofs; \ |
|
|
508 | end = SvEND (sv); \ |
|
|
509 | } \ |
|
|
510 | SE |
|
|
511 | |
|
|
512 | #define APPEND_CH(ch) SB \ |
|
|
513 | APPEND_GROW (1); \ |
|
|
514 | *cur++ = (ch); \ |
|
|
515 | SE |
|
|
516 | |
|
|
517 | static SV * |
557 | static SV * |
518 | decode_str (dec_t *dec) |
558 | decode_str (dec_t *dec) |
519 | { |
559 | { |
520 | SV *sv = NEWSV (0,2); |
560 | SV *sv = 0; |
521 | int utf8 = 0; |
561 | int utf8 = 0; |
522 | char *cur = SvPVX (sv); |
|
|
523 | char *end = SvEND (sv); |
|
|
524 | |
562 | |
525 | for (;;) |
563 | do |
526 | { |
564 | { |
527 | unsigned char ch = *(unsigned char *)dec->cur; |
565 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
566 | char *cur = buf; |
528 | |
567 | |
529 | if (ch == '"') |
568 | do |
530 | break; |
|
|
531 | else if (ch == '\\') |
|
|
532 | { |
569 | { |
533 | switch (*++dec->cur) |
570 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
571 | |
|
|
572 | if (ch == '"') |
534 | { |
573 | { |
535 | case '\\': |
574 | --dec->cur; |
536 | case '/': |
575 | break; |
537 | case '"': APPEND_CH (*dec->cur++); break; |
576 | } |
538 | |
577 | else if (ch == '\\') |
539 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
578 | { |
540 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
579 | switch (*dec->cur) |
541 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
542 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
543 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
544 | |
|
|
545 | case 'u': |
|
|
546 | { |
580 | { |
547 | UV lo, hi; |
581 | case '\\': |
548 | ++dec->cur; |
582 | case '/': |
|
|
583 | case '"': *cur++ = *dec->cur++; break; |
549 | |
584 | |
550 | hi = decode_4hex (dec); |
585 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
551 | if (hi == (UV)-1) |
586 | case 't': ++dec->cur; *cur++ = '\011'; break; |
552 | goto fail; |
587 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
588 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
589 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
553 | |
590 | |
554 | // possibly a surrogate pair |
591 | case 'u': |
555 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
556 | { |
592 | { |
557 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
593 | UV lo, hi; |
558 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
559 | |
|
|
560 | dec->cur += 2; |
594 | ++dec->cur; |
561 | |
595 | |
562 | lo = decode_4hex (dec); |
596 | hi = decode_4hex (dec); |
563 | if (lo == (UV)-1) |
597 | if (hi == (UV)-1) |
564 | goto fail; |
598 | goto fail; |
565 | |
599 | |
|
|
600 | // possibly a surrogate pair |
|
|
601 | if (hi >= 0xd800) |
|
|
602 | if (hi < 0xdc00) |
|
|
603 | { |
|
|
604 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
605 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
606 | |
|
|
607 | dec->cur += 2; |
|
|
608 | |
|
|
609 | lo = decode_4hex (dec); |
|
|
610 | if (lo == (UV)-1) |
|
|
611 | goto fail; |
|
|
612 | |
566 | if (lo < 0xdc00 || lo >= 0xe000) |
613 | if (lo < 0xdc00 || lo >= 0xe000) |
567 | ERR ("surrogate pair expected"); |
614 | ERR ("surrogate pair expected"); |
568 | |
615 | |
569 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
616 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
617 | } |
|
|
618 | else if (hi < 0xe000) |
|
|
619 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
620 | |
|
|
621 | if (hi >= 0x80) |
|
|
622 | { |
|
|
623 | utf8 = 1; |
|
|
624 | |
|
|
625 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
626 | } |
|
|
627 | else |
|
|
628 | *cur++ = hi; |
570 | } |
629 | } |
571 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
572 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
573 | |
|
|
574 | if (hi >= 0x80) |
|
|
575 | { |
630 | break; |
576 | utf8 = 1; |
|
|
577 | |
631 | |
578 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
579 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
580 | } |
|
|
581 | else |
632 | default: |
582 | APPEND_CH (hi); |
633 | --dec->cur; |
|
|
634 | ERR ("illegal backslash escape sequence in string"); |
583 | } |
635 | } |
584 | break; |
|
|
585 | |
|
|
586 | default: |
|
|
587 | --dec->cur; |
|
|
588 | ERR ("illegal backslash escape sequence in string"); |
|
|
589 | } |
636 | } |
|
|
637 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
638 | *cur++ = ch; |
|
|
639 | else if (ch >= 0x80) |
|
|
640 | { |
|
|
641 | --dec->cur; |
|
|
642 | |
|
|
643 | STRLEN clen; |
|
|
644 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
645 | if (clen == (STRLEN)-1) |
|
|
646 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
647 | |
|
|
648 | do |
|
|
649 | { |
|
|
650 | *cur++ = *dec->cur++; |
|
|
651 | } |
|
|
652 | while (--clen); |
|
|
653 | |
|
|
654 | utf8 = 1; |
|
|
655 | } |
|
|
656 | else if (!ch) |
|
|
657 | ERR ("unexpected end of string while parsing json string"); |
|
|
658 | else |
|
|
659 | ERR ("invalid character encountered"); |
|
|
660 | |
590 | } |
661 | } |
591 | else if (ch >= 0x20 && ch <= 0x7f) |
662 | while (cur < buf + SHORT_STRING_LEN); |
592 | APPEND_CH (*dec->cur++); |
663 | |
593 | else if (ch >= 0x80) |
664 | STRLEN len = cur - buf; |
|
|
665 | |
|
|
666 | if (sv) |
594 | { |
667 | { |
595 | STRLEN clen; |
668 | SvGROW (sv, SvCUR (sv) + len + 1); |
596 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
669 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
597 | if (clen == (STRLEN)-1) |
670 | SvCUR_set (sv, SvCUR (sv) + len); |
598 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
599 | |
|
|
600 | APPEND_GROW (clen); |
|
|
601 | do |
|
|
602 | { |
|
|
603 | *cur++ = *dec->cur++; |
|
|
604 | } |
|
|
605 | while (--clen); |
|
|
606 | |
|
|
607 | utf8 = 1; |
|
|
608 | } |
671 | } |
609 | else if (dec->cur == dec->end) |
|
|
610 | ERR ("unexpected end of string while parsing json string"); |
|
|
611 | else |
672 | else |
612 | ERR ("invalid character encountered"); |
673 | sv = newSVpvn (buf, len); |
613 | } |
674 | } |
|
|
675 | while (*dec->cur != '"'); |
614 | |
676 | |
615 | ++dec->cur; |
677 | ++dec->cur; |
616 | |
678 | |
617 | SvCUR_set (sv, cur - SvPVX (sv)); |
679 | if (sv) |
618 | |
680 | { |
619 | SvPOK_only (sv); |
681 | SvPOK_only (sv); |
620 | *SvEND (sv) = 0; |
682 | *SvEND (sv) = 0; |
621 | |
683 | |
622 | if (utf8) |
684 | if (utf8) |
623 | SvUTF8_on (sv); |
685 | SvUTF8_on (sv); |
624 | |
686 | } |
625 | if (dec->flags & F_SHRINK) |
687 | else |
626 | shrink (sv); |
688 | sv = newSVpvn ("", 0); |
627 | |
689 | |
628 | return sv; |
690 | return sv; |
629 | |
691 | |
630 | fail: |
692 | fail: |
631 | SvREFCNT_dec (sv); |
|
|
632 | return 0; |
693 | return 0; |
633 | } |
694 | } |
634 | |
695 | |
635 | static SV * |
696 | static SV * |
636 | decode_num (dec_t *dec) |
697 | decode_num (dec_t *dec) |
… | |
… | |
717 | static SV * |
778 | static SV * |
718 | decode_av (dec_t *dec) |
779 | decode_av (dec_t *dec) |
719 | { |
780 | { |
720 | AV *av = newAV (); |
781 | AV *av = newAV (); |
721 | |
782 | |
722 | WS; |
783 | decode_ws (dec); |
723 | if (*dec->cur == ']') |
784 | if (*dec->cur == ']') |
724 | ++dec->cur; |
785 | ++dec->cur; |
725 | else |
786 | else |
726 | for (;;) |
787 | for (;;) |
727 | { |
788 | { |
… | |
… | |
731 | if (!value) |
792 | if (!value) |
732 | goto fail; |
793 | goto fail; |
733 | |
794 | |
734 | av_push (av, value); |
795 | av_push (av, value); |
735 | |
796 | |
736 | WS; |
797 | decode_ws (dec); |
737 | |
798 | |
738 | if (*dec->cur == ']') |
799 | if (*dec->cur == ']') |
739 | { |
800 | { |
740 | ++dec->cur; |
801 | ++dec->cur; |
741 | break; |
802 | break; |
… | |
… | |
757 | static SV * |
818 | static SV * |
758 | decode_hv (dec_t *dec) |
819 | decode_hv (dec_t *dec) |
759 | { |
820 | { |
760 | HV *hv = newHV (); |
821 | HV *hv = newHV (); |
761 | |
822 | |
762 | WS; |
823 | decode_ws (dec); |
763 | if (*dec->cur == '}') |
824 | if (*dec->cur == '}') |
764 | ++dec->cur; |
825 | ++dec->cur; |
765 | else |
826 | else |
766 | for (;;) |
827 | for (;;) |
767 | { |
828 | { |
768 | SV *key, *value; |
829 | SV *key, *value; |
769 | |
830 | |
770 | WS; EXPECT_CH ('"'); |
831 | decode_ws (dec); EXPECT_CH ('"'); |
771 | |
832 | |
772 | key = decode_str (dec); |
833 | key = decode_str (dec); |
773 | if (!key) |
834 | if (!key) |
774 | goto fail; |
835 | goto fail; |
775 | |
836 | |
776 | WS; EXPECT_CH (':'); |
837 | decode_ws (dec); EXPECT_CH (':'); |
777 | |
838 | |
778 | value = decode_sv (dec); |
839 | value = decode_sv (dec); |
779 | if (!value) |
840 | if (!value) |
780 | { |
841 | { |
781 | SvREFCNT_dec (key); |
842 | SvREFCNT_dec (key); |
… | |
… | |
783 | } |
844 | } |
784 | |
845 | |
785 | //TODO: optimise |
846 | //TODO: optimise |
786 | hv_store_ent (hv, key, value, 0); |
847 | hv_store_ent (hv, key, value, 0); |
787 | |
848 | |
788 | WS; |
849 | decode_ws (dec); |
789 | |
850 | |
790 | if (*dec->cur == '}') |
851 | if (*dec->cur == '}') |
791 | { |
852 | { |
792 | ++dec->cur; |
853 | ++dec->cur; |
793 | break; |
854 | break; |
… | |
… | |
807 | } |
868 | } |
808 | |
869 | |
809 | static SV * |
870 | static SV * |
810 | decode_sv (dec_t *dec) |
871 | decode_sv (dec_t *dec) |
811 | { |
872 | { |
812 | WS; |
873 | decode_ws (dec); |
813 | switch (*dec->cur) |
874 | switch (*dec->cur) |
814 | { |
875 | { |
815 | case '"': ++dec->cur; return decode_str (dec); |
876 | case '"': ++dec->cur; return decode_str (dec); |
816 | case '[': ++dec->cur; return decode_av (dec); |
877 | case '[': ++dec->cur; return decode_av (dec); |
817 | case '{': ++dec->cur; return decode_hv (dec); |
878 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
911 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
972 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
912 | |
973 | |
913 | return sv; |
974 | return sv; |
914 | } |
975 | } |
915 | |
976 | |
|
|
977 | ///////////////////////////////////////////////////////////////////////////// |
|
|
978 | // XS interface functions |
|
|
979 | |
916 | MODULE = JSON::XS PACKAGE = JSON::XS |
980 | MODULE = JSON::XS PACKAGE = JSON::XS |
917 | |
981 | |
918 | BOOT: |
982 | BOOT: |
919 | { |
983 | { |
920 | int i; |
984 | int i; |
… | |
… | |
946 | utf8 = F_UTF8 |
1010 | utf8 = F_UTF8 |
947 | indent = F_INDENT |
1011 | indent = F_INDENT |
948 | canonical = F_CANONICAL |
1012 | canonical = F_CANONICAL |
949 | space_before = F_SPACE_BEFORE |
1013 | space_before = F_SPACE_BEFORE |
950 | space_after = F_SPACE_AFTER |
1014 | space_after = F_SPACE_AFTER |
951 | json_rpc = F_JSON_RPC |
|
|
952 | pretty = F_PRETTY |
1015 | pretty = F_PRETTY |
953 | allow_nonref = F_ALLOW_NONREF |
1016 | allow_nonref = F_ALLOW_NONREF |
954 | shrink = F_SHRINK |
1017 | shrink = F_SHRINK |
955 | CODE: |
1018 | CODE: |
956 | { |
1019 | { |
… | |
… | |
974 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1037 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
975 | |
1038 | |
976 | PROTOTYPES: ENABLE |
1039 | PROTOTYPES: ENABLE |
977 | |
1040 | |
978 | void to_json (SV *scalar) |
1041 | void to_json (SV *scalar) |
|
|
1042 | ALIAS: |
|
|
1043 | objToJson = 0 |
979 | PPCODE: |
1044 | PPCODE: |
980 | XPUSHs (encode_json (scalar, F_UTF8)); |
1045 | XPUSHs (encode_json (scalar, F_UTF8)); |
981 | |
1046 | |
982 | void from_json (SV *jsonstr) |
1047 | void from_json (SV *jsonstr) |
|
|
1048 | ALIAS: |
|
|
1049 | jsonToObj = 0 |
983 | PPCODE: |
1050 | PPCODE: |
984 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1051 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
985 | |
1052 | |