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