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