… | |
… | |
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 256 // 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 | ///////////////////////////////////////////////////////////////////////////// |
|
|
59 | // encoder |
28 | |
60 | |
29 | // structure used for encoding JSON |
61 | // structure used for encoding JSON |
30 | typedef struct |
62 | typedef struct |
31 | { |
63 | { |
32 | char *cur; |
64 | char *cur; // SvPVX (sv) + current output position |
33 | STRLEN len; // SvLEN (sv) |
|
|
34 | char *end; // SvEND (sv) |
65 | char *end; // SvEND (sv) |
35 | SV *sv; |
66 | SV *sv; // result scalar |
36 | UV flags; |
67 | UV flags; // F_* |
37 | int max_recurse; |
68 | int indent; // indentation level |
38 | int indent; |
69 | int max_depth; // max. recursion level |
39 | } enc_t; |
70 | } 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 | ///////////////////////////////////////////////////////////////////////////// |
|
|
60 | |
71 | |
61 | static void |
72 | static void |
62 | need (enc_t *enc, STRLEN len) |
73 | need (enc_t *enc, STRLEN len) |
63 | { |
74 | { |
64 | if (enc->cur + len >= enc->end) |
75 | if (enc->cur + len >= enc->end) |
… | |
… | |
124 | |
135 | |
125 | if (is_utf8) |
136 | if (is_utf8) |
126 | { |
137 | { |
127 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
138 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
128 | if (clen == (STRLEN)-1) |
139 | if (clen == (STRLEN)-1) |
129 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
140 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
130 | } |
141 | } |
131 | else |
142 | else |
132 | { |
143 | { |
133 | uch = ch; |
144 | uch = ch; |
134 | clen = 1; |
145 | clen = 1; |
135 | } |
146 | } |
136 | |
147 | |
|
|
148 | if (uch > 0x10FFFFUL) |
|
|
149 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
150 | |
137 | if (uch < 0x80 || enc->flags & F_ASCII) |
151 | if (uch < 0x80 || enc->flags & F_ASCII) |
138 | { |
152 | { |
139 | if (uch > 0xFFFFUL) |
153 | if (uch > 0xFFFFUL) |
140 | { |
154 | { |
141 | need (enc, len += 11); |
155 | need (enc, len += 11); |
142 | sprintf (enc->cur, "\\u%04x\\u%04x", |
156 | sprintf (enc->cur, "\\u%04x\\u%04x", |
143 | (uch - 0x10000) / 0x400 + 0xD800, |
157 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
144 | (uch - 0x10000) % 0x400 + 0xDC00); |
158 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
145 | enc->cur += 12; |
159 | enc->cur += 12; |
146 | } |
160 | } |
147 | else |
161 | else |
148 | { |
162 | { |
149 | static char hexdigit [16] = "0123456789abcdef"; |
163 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
167 | } |
181 | } |
168 | while (--clen); |
182 | while (--clen); |
169 | } |
183 | } |
170 | else |
184 | else |
171 | { |
185 | { |
172 | need (enc, 10); // never more than 11 bytes needed |
186 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
187 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
174 | ++str; |
188 | ++str; |
175 | } |
189 | } |
176 | } |
190 | } |
177 | } |
191 | } |
… | |
… | |
179 | |
193 | |
180 | --len; |
194 | --len; |
181 | } |
195 | } |
182 | } |
196 | } |
183 | |
197 | |
184 | #define INDENT SB \ |
198 | static void |
|
|
199 | encode_indent (enc_t *enc) |
|
|
200 | { |
185 | if (enc->flags & F_INDENT) \ |
201 | if (enc->flags & F_INDENT) |
186 | { \ |
202 | { |
187 | int i_; \ |
203 | int spaces = enc->indent * INDENT_STEP; |
188 | need (enc, enc->indent); \ |
|
|
189 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
190 | encode_ch (enc, ' '); \ |
|
|
191 | } \ |
|
|
192 | SE |
|
|
193 | |
204 | |
194 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
205 | need (enc, spaces); |
195 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
206 | memset (enc->cur, ' ', spaces); |
196 | #define COMMA SB \ |
207 | enc->cur += spaces; |
|
|
208 | } |
|
|
209 | } |
|
|
210 | |
|
|
211 | static void |
|
|
212 | encode_space (enc_t *enc) |
|
|
213 | { |
|
|
214 | need (enc, 1); |
197 | encode_ch (enc, ','); \ |
215 | encode_ch (enc, ' '); |
|
|
216 | } |
|
|
217 | |
|
|
218 | static void |
|
|
219 | encode_nl (enc_t *enc) |
|
|
220 | { |
198 | if (enc->flags & F_INDENT) \ |
221 | if (enc->flags & F_INDENT) |
199 | NL; \ |
222 | { |
|
|
223 | need (enc, 1); |
|
|
224 | encode_ch (enc, '\n'); |
|
|
225 | } |
|
|
226 | } |
|
|
227 | |
|
|
228 | static void |
|
|
229 | encode_comma (enc_t *enc) |
|
|
230 | { |
|
|
231 | encode_ch (enc, ','); |
|
|
232 | |
|
|
233 | if (enc->flags & F_INDENT) |
|
|
234 | encode_nl (enc); |
200 | else if (enc->flags & F_SPACE_AFTER) \ |
235 | else if (enc->flags & F_SPACE_AFTER) |
201 | SPACE; \ |
236 | encode_space (enc); |
202 | SE |
237 | } |
203 | |
238 | |
204 | static void encode_sv (enc_t *enc, SV *sv); |
239 | static void encode_sv (enc_t *enc, SV *sv); |
205 | |
240 | |
206 | static void |
241 | static void |
207 | encode_av (enc_t *enc, AV *av) |
242 | encode_av (enc_t *enc, AV *av) |
208 | { |
243 | { |
209 | int i, len = av_len (av); |
244 | int i, len = av_len (av); |
210 | |
245 | |
211 | encode_ch (enc, '['); NL; |
246 | encode_ch (enc, '['); encode_nl (enc); |
212 | ++enc->indent; |
247 | ++enc->indent; |
213 | |
248 | |
214 | for (i = 0; i <= len; ++i) |
249 | for (i = 0; i <= len; ++i) |
215 | { |
250 | { |
216 | INDENT; |
251 | encode_indent (enc); |
217 | encode_sv (enc, *av_fetch (av, i, 0)); |
252 | encode_sv (enc, *av_fetch (av, i, 0)); |
218 | |
253 | |
219 | if (i < len) |
254 | if (i < len) |
220 | COMMA; |
255 | encode_comma (enc); |
221 | } |
256 | } |
222 | |
257 | |
223 | NL; |
258 | encode_nl (enc); |
224 | |
259 | |
225 | --enc->indent; |
260 | --enc->indent; |
226 | INDENT; encode_ch (enc, ']'); |
261 | encode_indent (enc); encode_ch (enc, ']'); |
227 | } |
262 | } |
228 | |
263 | |
229 | static void |
264 | static void |
230 | encode_he (enc_t *enc, HE *he) |
265 | encode_he (enc_t *enc, HE *he) |
231 | { |
266 | { |
… | |
… | |
245 | else |
280 | else |
246 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
281 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
247 | |
282 | |
248 | encode_ch (enc, '"'); |
283 | encode_ch (enc, '"'); |
249 | |
284 | |
250 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
285 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
251 | encode_ch (enc, ':'); |
286 | encode_ch (enc, ':'); |
252 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
287 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
253 | encode_sv (enc, HeVAL (he)); |
288 | encode_sv (enc, HeVAL (he)); |
254 | } |
289 | } |
255 | |
290 | |
256 | // compare hash entries, used when all keys are bytestrings |
291 | // compare hash entries, used when all keys are bytestrings |
257 | static int |
292 | static int |
… | |
… | |
263 | HE *b = *(HE **)b_; |
298 | HE *b = *(HE **)b_; |
264 | |
299 | |
265 | STRLEN la = HeKLEN (a); |
300 | STRLEN la = HeKLEN (a); |
266 | STRLEN lb = HeKLEN (b); |
301 | STRLEN lb = HeKLEN (b); |
267 | |
302 | |
268 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
303 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
269 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
304 | cmp = la - lb; |
270 | |
305 | |
271 | return cmp; |
306 | return cmp; |
272 | } |
307 | } |
273 | |
308 | |
274 | // compare hash entries, used when some keys are sv's or utf-x |
309 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
281 | static void |
316 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
317 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
318 | { |
284 | int count, i; |
319 | int count, i; |
285 | |
320 | |
286 | encode_ch (enc, '{'); NL; ++enc->indent; |
321 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
287 | |
322 | |
288 | if ((count = hv_iterinit (hv))) |
323 | if ((count = hv_iterinit (hv))) |
289 | { |
324 | { |
290 | // for canonical output we have to sort by keys first |
325 | // for canonical output we have to sort by keys first |
291 | // actually, this is mostly due to the stupid so-called |
326 | // actually, this is mostly due to the stupid so-called |
292 | // security workaround added somewhere in 5.8.x. |
327 | // security workaround added somewhere in 5.8.x. |
293 | // that randomises hash orderings |
328 | // that randomises hash orderings |
294 | if (enc->flags & F_CANONICAL) |
329 | if (enc->flags & F_CANONICAL) |
295 | { |
330 | { |
296 | HE *he, *hes [count]; |
331 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
297 | int fast = 1; |
332 | int fast = 1; |
298 | |
333 | |
299 | i = 0; |
334 | i = 0; |
300 | while ((he = hv_iternext (hv))) |
335 | while ((he = hv_iternext (hv))) |
301 | { |
336 | { |
… | |
… | |
308 | |
343 | |
309 | if (fast) |
344 | if (fast) |
310 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
345 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
311 | else |
346 | else |
312 | { |
347 | { |
313 | // hack to disable "use bytes" |
348 | // hack to forcefully disable "use bytes" |
314 | COP *oldcop = PL_curcop, cop; |
349 | COP cop = *PL_curcop; |
315 | cop.op_private = 0; |
350 | cop.op_private = 0; |
|
|
351 | |
|
|
352 | ENTER; |
|
|
353 | SAVETMPS; |
|
|
354 | |
|
|
355 | SAVEVPTR (PL_curcop); |
316 | PL_curcop = &cop; |
356 | PL_curcop = &cop; |
317 | |
357 | |
318 | SAVETMPS; |
|
|
319 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
358 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
359 | |
320 | FREETMPS; |
360 | FREETMPS; |
321 | |
361 | LEAVE; |
322 | PL_curcop = oldcop; |
|
|
323 | } |
362 | } |
324 | |
363 | |
325 | for (i = 0; i < count; ++i) |
364 | for (i = 0; i < count; ++i) |
326 | { |
365 | { |
327 | INDENT; |
366 | encode_indent (enc); |
328 | encode_he (enc, hes [i]); |
367 | encode_he (enc, hes [i]); |
329 | |
368 | |
330 | if (i < count - 1) |
369 | if (i < count - 1) |
331 | COMMA; |
370 | encode_comma (enc); |
332 | } |
371 | } |
333 | |
372 | |
334 | NL; |
373 | encode_nl (enc); |
335 | } |
374 | } |
336 | else |
375 | else |
337 | { |
376 | { |
338 | SV *sv; |
377 | SV *sv; |
339 | HE *he = hv_iternext (hv); |
378 | HE *he = hv_iternext (hv); |
340 | |
379 | |
341 | for (;;) |
380 | for (;;) |
342 | { |
381 | { |
343 | INDENT; |
382 | encode_indent (enc); |
344 | encode_he (enc, he); |
383 | encode_he (enc, he); |
345 | |
384 | |
346 | if (!(he = hv_iternext (hv))) |
385 | if (!(he = hv_iternext (hv))) |
347 | break; |
386 | break; |
348 | |
387 | |
349 | COMMA; |
388 | encode_comma (enc); |
350 | } |
389 | } |
351 | |
390 | |
352 | NL; |
391 | encode_nl (enc); |
353 | } |
392 | } |
354 | } |
393 | } |
355 | |
394 | |
356 | --enc->indent; INDENT; encode_ch (enc, '}'); |
395 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
357 | } |
396 | } |
358 | |
397 | |
359 | static void |
398 | static void |
360 | encode_sv (enc_t *enc, SV *sv) |
399 | encode_sv (enc_t *enc, SV *sv) |
361 | { |
400 | { |
… | |
… | |
383 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
422 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
384 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
423 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
385 | } |
424 | } |
386 | else if (SvROK (sv)) |
425 | else if (SvROK (sv)) |
387 | { |
426 | { |
388 | if (!--enc->max_recurse) |
427 | SV *rv = SvRV (sv); |
|
|
428 | |
|
|
429 | if (enc->indent >= enc->max_depth) |
389 | croak ("data structure too deep (hit recursion limit)"); |
430 | croak ("data structure too deep (hit recursion limit)"); |
390 | |
431 | |
391 | sv = SvRV (sv); |
|
|
392 | |
|
|
393 | switch (SvTYPE (sv)) |
432 | switch (SvTYPE (rv)) |
394 | { |
433 | { |
395 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
434 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
396 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
435 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
397 | |
436 | |
398 | default: |
437 | default: |
399 | croak ("JSON can only represent references to arrays or hashes"); |
438 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
439 | SvPV_nolen (sv)); |
400 | } |
440 | } |
401 | } |
441 | } |
402 | else if (!SvOK (sv)) |
442 | else if (!SvOK (sv)) |
403 | encode_str (enc, "null", 4, 0); |
443 | encode_str (enc, "null", 4, 0); |
404 | else |
444 | else |
405 | croak ("encountered perl type that JSON cannot handle"); |
445 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
446 | SvPV_nolen (sv), SvFLAGS (sv)); |
406 | } |
447 | } |
407 | |
448 | |
408 | static SV * |
449 | static SV * |
409 | encode_json (SV *scalar, UV flags) |
450 | encode_json (SV *scalar, UV flags) |
410 | { |
451 | { |
411 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
452 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
412 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
453 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
413 | |
454 | |
414 | enc_t enc; |
455 | enc_t enc; |
415 | enc.flags = flags; |
456 | enc.flags = flags; |
416 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
417 | enc.cur = SvPVX (enc.sv); |
458 | enc.cur = SvPVX (enc.sv); |
418 | enc.end = SvEND (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
419 | enc.max_recurse = 0; |
|
|
420 | enc.indent = 0; |
460 | enc.indent = 0; |
|
|
461 | enc.max_depth = 0x7fffffffUL; |
421 | |
462 | |
422 | SvPOK_only (enc.sv); |
463 | SvPOK_only (enc.sv); |
423 | encode_sv (&enc, scalar); |
464 | encode_sv (&enc, scalar); |
424 | |
465 | |
425 | if (!(flags & (F_ASCII | F_UTF8))) |
466 | if (!(flags & (F_ASCII | F_UTF8))) |
426 | SvUTF8_on (enc.sv); |
467 | SvUTF8_on (enc.sv); |
427 | |
468 | |
428 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
469 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
429 | |
470 | |
430 | #ifdef SvPV_shrink_to_cur |
|
|
431 | if (enc.flags & F_SHRINK) |
471 | if (enc.flags & F_SHRINK) |
432 | SvPV_shrink_to_cur (enc.sv); |
472 | shrink (enc.sv); |
433 | #endif |
473 | |
434 | return enc.sv; |
474 | return enc.sv; |
435 | } |
475 | } |
436 | |
476 | |
437 | ///////////////////////////////////////////////////////////////////////////// |
477 | ///////////////////////////////////////////////////////////////////////////// |
|
|
478 | // decoder |
438 | |
479 | |
439 | #define WS \ |
480 | // structure used for decoding JSON |
|
|
481 | typedef struct |
|
|
482 | { |
|
|
483 | char *cur; // current parser pointer |
|
|
484 | char *end; // end of input string |
|
|
485 | const char *err; // parse error, if != 0 |
|
|
486 | UV flags; // F_* |
|
|
487 | } dec_t; |
|
|
488 | |
|
|
489 | static void |
|
|
490 | decode_ws (dec_t *dec) |
|
|
491 | { |
440 | for (;;) \ |
492 | for (;;) |
441 | { \ |
493 | { |
442 | char ch = *dec->cur; \ |
494 | char ch = *dec->cur; |
|
|
495 | |
443 | if (ch > 0x20 \ |
496 | if (ch > 0x20 |
444 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
497 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
445 | break; \ |
498 | break; |
|
|
499 | |
446 | ++dec->cur; \ |
500 | ++dec->cur; |
447 | } |
501 | } |
|
|
502 | } |
448 | |
503 | |
449 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
504 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
450 | #define EXPECT_CH(ch) SB \ |
505 | #define EXPECT_CH(ch) SB \ |
451 | if (*dec->cur != ch) \ |
506 | if (*dec->cur != ch) \ |
452 | ERR (# ch " expected"); \ |
507 | ERR (# ch " expected"); \ |
… | |
… | |
459 | |
514 | |
460 | static UV |
515 | static UV |
461 | decode_4hex (dec_t *dec) |
516 | decode_4hex (dec_t *dec) |
462 | { |
517 | { |
463 | signed char d1, d2, d3, d4; |
518 | signed char d1, d2, d3, d4; |
|
|
519 | unsigned char *cur = (unsigned char *)dec->cur; |
464 | |
520 | |
465 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
466 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
521 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
467 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
468 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
522 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
469 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
470 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
523 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
471 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
472 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
524 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
473 | |
525 | |
474 | dec->cur += 4; |
526 | dec->cur += 4; |
475 | |
527 | |
476 | return ((UV)d1) << 12 |
528 | return ((UV)d1) << 12 |
477 | | ((UV)d2) << 8 |
529 | | ((UV)d2) << 8 |
… | |
… | |
480 | |
532 | |
481 | fail: |
533 | fail: |
482 | return (UV)-1; |
534 | return (UV)-1; |
483 | } |
535 | } |
484 | |
536 | |
485 | #define APPEND_GROW(n) SB \ |
|
|
486 | if (cur + (n) >= end) \ |
|
|
487 | { \ |
|
|
488 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
489 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
490 | cur = SvPVX (sv) + ofs; \ |
|
|
491 | end = SvEND (sv); \ |
|
|
492 | } \ |
|
|
493 | SE |
|
|
494 | |
|
|
495 | #define APPEND_CH(ch) SB \ |
|
|
496 | APPEND_GROW (1); \ |
|
|
497 | *cur++ = (ch); \ |
|
|
498 | SE |
|
|
499 | |
|
|
500 | static SV * |
537 | static SV * |
501 | decode_str (dec_t *dec) |
538 | decode_str (dec_t *dec) |
502 | { |
539 | { |
503 | SV *sv = NEWSV (0,2); |
540 | SV *sv = 0; |
504 | int utf8 = 0; |
541 | int utf8 = 0; |
505 | char *cur = SvPVX (sv); |
|
|
506 | char *end = SvEND (sv); |
|
|
507 | |
542 | |
508 | for (;;) |
543 | do |
509 | { |
544 | { |
510 | unsigned char ch = *(unsigned char *)dec->cur; |
545 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
546 | char *cur = buf; |
511 | |
547 | |
512 | if (ch == '"') |
548 | do |
513 | break; |
|
|
514 | else if (ch == '\\') |
|
|
515 | { |
549 | { |
516 | switch (*++dec->cur) |
550 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
551 | |
|
|
552 | if (ch == '"') |
517 | { |
553 | { |
518 | case '\\': |
554 | --dec->cur; |
519 | case '/': |
555 | break; |
520 | case '"': APPEND_CH (*dec->cur++); break; |
556 | } |
521 | |
557 | else if (ch == '\\') |
522 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
558 | { |
523 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
559 | switch (*dec->cur) |
524 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
525 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
526 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
527 | |
|
|
528 | case 'u': |
|
|
529 | { |
560 | { |
530 | UV lo, hi; |
561 | case '\\': |
531 | ++dec->cur; |
562 | case '/': |
|
|
563 | case '"': *cur++ = *dec->cur++; break; |
532 | |
564 | |
533 | hi = decode_4hex (dec); |
565 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
534 | if (hi == (UV)-1) |
566 | case 't': ++dec->cur; *cur++ = '\011'; break; |
535 | goto fail; |
567 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
568 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
569 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
536 | |
570 | |
537 | // possibly a surrogate pair |
571 | case 'u': |
538 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
539 | { |
572 | { |
540 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
573 | UV lo, hi; |
541 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
542 | |
|
|
543 | dec->cur += 2; |
574 | ++dec->cur; |
544 | |
575 | |
545 | lo = decode_4hex (dec); |
576 | hi = decode_4hex (dec); |
546 | if (lo == (UV)-1) |
577 | if (hi == (UV)-1) |
547 | goto fail; |
578 | goto fail; |
548 | |
579 | |
|
|
580 | // possibly a surrogate pair |
|
|
581 | if (hi >= 0xd800) |
|
|
582 | if (hi < 0xdc00) |
|
|
583 | { |
|
|
584 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
585 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
586 | |
|
|
587 | dec->cur += 2; |
|
|
588 | |
|
|
589 | lo = decode_4hex (dec); |
|
|
590 | if (lo == (UV)-1) |
|
|
591 | goto fail; |
|
|
592 | |
549 | if (lo < 0xdc00 || lo >= 0xe000) |
593 | if (lo < 0xdc00 || lo >= 0xe000) |
550 | ERR ("surrogate pair expected"); |
594 | ERR ("surrogate pair expected"); |
551 | |
595 | |
552 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
596 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
597 | } |
|
|
598 | else if (hi < 0xe000) |
|
|
599 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
600 | |
|
|
601 | if (hi >= 0x80) |
|
|
602 | { |
|
|
603 | utf8 = 1; |
|
|
604 | |
|
|
605 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
606 | } |
|
|
607 | else |
|
|
608 | *cur++ = hi; |
553 | } |
609 | } |
554 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
555 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
556 | |
|
|
557 | if (hi >= 0x80) |
|
|
558 | { |
610 | break; |
559 | utf8 = 1; |
|
|
560 | |
611 | |
561 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
562 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
563 | } |
|
|
564 | else |
612 | default: |
565 | APPEND_CH (hi); |
613 | --dec->cur; |
|
|
614 | ERR ("illegal backslash escape sequence in string"); |
566 | } |
615 | } |
567 | break; |
|
|
568 | |
|
|
569 | default: |
|
|
570 | --dec->cur; |
|
|
571 | ERR ("illegal backslash escape sequence in string"); |
|
|
572 | } |
616 | } |
|
|
617 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
618 | *cur++ = ch; |
|
|
619 | else if (ch >= 0x80) |
|
|
620 | { |
|
|
621 | --dec->cur; |
|
|
622 | |
|
|
623 | STRLEN clen; |
|
|
624 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
|
|
625 | if (clen == (STRLEN)-1) |
|
|
626 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
627 | |
|
|
628 | do |
|
|
629 | { |
|
|
630 | *cur++ = *dec->cur++; |
|
|
631 | } |
|
|
632 | while (--clen); |
|
|
633 | |
|
|
634 | utf8 = 1; |
|
|
635 | } |
|
|
636 | else if (!ch) |
|
|
637 | ERR ("unexpected end of string while parsing json string"); |
|
|
638 | else |
|
|
639 | ERR ("invalid character encountered"); |
|
|
640 | |
573 | } |
641 | } |
574 | else if (ch >= 0x20 && ch <= 0x7f) |
642 | while (cur < buf + SHORT_STRING_LEN); |
575 | APPEND_CH (*dec->cur++); |
643 | |
576 | else if (ch >= 0x80) |
644 | STRLEN len = cur - buf; |
|
|
645 | |
|
|
646 | if (sv) |
577 | { |
647 | { |
578 | STRLEN clen; |
648 | SvGROW (sv, SvCUR (sv) + len + 1); |
579 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
649 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
580 | if (clen == (STRLEN)-1) |
650 | SvCUR_set (sv, SvCUR (sv) + len); |
581 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
582 | |
|
|
583 | APPEND_GROW (clen); |
|
|
584 | do |
|
|
585 | { |
|
|
586 | *cur++ = *dec->cur++; |
|
|
587 | } |
|
|
588 | while (--clen); |
|
|
589 | |
|
|
590 | utf8 = 1; |
|
|
591 | } |
651 | } |
592 | else if (dec->cur == dec->end) |
|
|
593 | ERR ("unexpected end of string while parsing json string"); |
|
|
594 | else |
652 | else |
595 | ERR ("invalid character encountered"); |
653 | sv = newSVpvn (buf, len); |
596 | } |
654 | } |
|
|
655 | while (*dec->cur != '"'); |
597 | |
656 | |
598 | ++dec->cur; |
657 | ++dec->cur; |
599 | |
658 | |
600 | SvCUR_set (sv, cur - SvPVX (sv)); |
659 | if (sv) |
601 | |
660 | { |
602 | SvPOK_only (sv); |
661 | SvPOK_only (sv); |
603 | *SvEND (sv) = 0; |
662 | *SvEND (sv) = 0; |
604 | |
663 | |
605 | if (utf8) |
664 | if (utf8) |
606 | SvUTF8_on (sv); |
665 | SvUTF8_on (sv); |
607 | |
666 | } |
608 | #ifdef SvPV_shrink_to_cur |
667 | else |
609 | if (dec->flags & F_SHRINK) |
668 | sv = newSVpvn ("", 0); |
610 | SvPV_shrink_to_cur (sv); |
|
|
611 | #endif |
|
|
612 | |
669 | |
613 | return sv; |
670 | return sv; |
614 | |
671 | |
615 | fail: |
672 | fail: |
616 | SvREFCNT_dec (sv); |
|
|
617 | return 0; |
673 | return 0; |
618 | } |
674 | } |
619 | |
675 | |
620 | static SV * |
676 | static SV * |
621 | decode_num (dec_t *dec) |
677 | decode_num (dec_t *dec) |
… | |
… | |
702 | static SV * |
758 | static SV * |
703 | decode_av (dec_t *dec) |
759 | decode_av (dec_t *dec) |
704 | { |
760 | { |
705 | AV *av = newAV (); |
761 | AV *av = newAV (); |
706 | |
762 | |
707 | WS; |
763 | decode_ws (dec); |
708 | if (*dec->cur == ']') |
764 | if (*dec->cur == ']') |
709 | ++dec->cur; |
765 | ++dec->cur; |
710 | else |
766 | else |
711 | for (;;) |
767 | for (;;) |
712 | { |
768 | { |
… | |
… | |
716 | if (!value) |
772 | if (!value) |
717 | goto fail; |
773 | goto fail; |
718 | |
774 | |
719 | av_push (av, value); |
775 | av_push (av, value); |
720 | |
776 | |
721 | WS; |
777 | decode_ws (dec); |
722 | |
778 | |
723 | if (*dec->cur == ']') |
779 | if (*dec->cur == ']') |
724 | { |
780 | { |
725 | ++dec->cur; |
781 | ++dec->cur; |
726 | break; |
782 | break; |
… | |
… | |
742 | static SV * |
798 | static SV * |
743 | decode_hv (dec_t *dec) |
799 | decode_hv (dec_t *dec) |
744 | { |
800 | { |
745 | HV *hv = newHV (); |
801 | HV *hv = newHV (); |
746 | |
802 | |
747 | WS; |
803 | decode_ws (dec); |
748 | if (*dec->cur == '}') |
804 | if (*dec->cur == '}') |
749 | ++dec->cur; |
805 | ++dec->cur; |
750 | else |
806 | else |
751 | for (;;) |
807 | for (;;) |
752 | { |
808 | { |
753 | SV *key, *value; |
809 | SV *key, *value; |
754 | |
810 | |
755 | WS; EXPECT_CH ('"'); |
811 | decode_ws (dec); EXPECT_CH ('"'); |
756 | |
812 | |
757 | key = decode_str (dec); |
813 | key = decode_str (dec); |
758 | if (!key) |
814 | if (!key) |
759 | goto fail; |
815 | goto fail; |
760 | |
816 | |
761 | WS; EXPECT_CH (':'); |
817 | decode_ws (dec); EXPECT_CH (':'); |
762 | |
818 | |
763 | value = decode_sv (dec); |
819 | value = decode_sv (dec); |
764 | if (!value) |
820 | if (!value) |
765 | { |
821 | { |
766 | SvREFCNT_dec (key); |
822 | SvREFCNT_dec (key); |
… | |
… | |
768 | } |
824 | } |
769 | |
825 | |
770 | //TODO: optimise |
826 | //TODO: optimise |
771 | hv_store_ent (hv, key, value, 0); |
827 | hv_store_ent (hv, key, value, 0); |
772 | |
828 | |
773 | WS; |
829 | decode_ws (dec); |
774 | |
830 | |
775 | if (*dec->cur == '}') |
831 | if (*dec->cur == '}') |
776 | { |
832 | { |
777 | ++dec->cur; |
833 | ++dec->cur; |
778 | break; |
834 | break; |
… | |
… | |
792 | } |
848 | } |
793 | |
849 | |
794 | static SV * |
850 | static SV * |
795 | decode_sv (dec_t *dec) |
851 | decode_sv (dec_t *dec) |
796 | { |
852 | { |
797 | WS; |
853 | decode_ws (dec); |
798 | switch (*dec->cur) |
854 | switch (*dec->cur) |
799 | { |
855 | { |
800 | case '"': ++dec->cur; return decode_str (dec); |
856 | case '"': ++dec->cur; return decode_str (dec); |
801 | case '[': ++dec->cur; return decode_av (dec); |
857 | case '[': ++dec->cur; return decode_av (dec); |
802 | case '{': ++dec->cur; return decode_hv (dec); |
858 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
838 | ERR ("'null' expected"); |
894 | ERR ("'null' expected"); |
839 | |
895 | |
840 | break; |
896 | break; |
841 | |
897 | |
842 | default: |
898 | default: |
843 | ERR ("malformed json string"); |
899 | ERR ("malformed json string, neither array, object, number, string or atom"); |
844 | break; |
900 | break; |
845 | } |
901 | } |
846 | |
902 | |
847 | fail: |
903 | fail: |
848 | return 0; |
904 | return 0; |
… | |
… | |
868 | |
924 | |
869 | sv = decode_sv (&dec); |
925 | sv = decode_sv (&dec); |
870 | |
926 | |
871 | if (!sv) |
927 | if (!sv) |
872 | { |
928 | { |
|
|
929 | IV offset = dec.flags & F_UTF8 |
|
|
930 | ? dec.cur - SvPVX (string) |
873 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
931 | : utf8_distance (dec.cur, SvPVX (string)); |
874 | SV *uni = sv_newmortal (); |
932 | SV *uni = sv_newmortal (); |
|
|
933 | |
875 | // horrible hack to silence warning inside pv_uni_display |
934 | // horrible hack to silence warning inside pv_uni_display |
876 | COP cop; |
935 | COP cop = *PL_curcop; |
877 | memset (&cop, 0, sizeof (cop)); |
|
|
878 | cop.cop_warnings = pWARN_NONE; |
936 | cop.cop_warnings = pWARN_NONE; |
|
|
937 | ENTER; |
879 | SAVEVPTR (PL_curcop); |
938 | SAVEVPTR (PL_curcop); |
880 | PL_curcop = &cop; |
939 | PL_curcop = &cop; |
881 | |
|
|
882 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
940 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
941 | LEAVE; |
|
|
942 | |
883 | croak ("%s, at character offset %d (%s)", |
943 | croak ("%s, at character offset %d (%s)", |
884 | dec.err, |
944 | dec.err, |
885 | (int)offset, |
945 | (int)offset, |
886 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
946 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
887 | } |
947 | } |
888 | |
948 | |
889 | sv = sv_2mortal (sv); |
949 | sv = sv_2mortal (sv); |
890 | |
950 | |
891 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
951 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
892 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
952 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
893 | |
953 | |
894 | return sv; |
954 | return sv; |
895 | } |
955 | } |
|
|
956 | |
|
|
957 | ///////////////////////////////////////////////////////////////////////////// |
|
|
958 | // XS interface functions |
896 | |
959 | |
897 | MODULE = JSON::XS PACKAGE = JSON::XS |
960 | MODULE = JSON::XS PACKAGE = JSON::XS |
898 | |
961 | |
899 | BOOT: |
962 | BOOT: |
900 | { |
963 | { |
… | |
… | |
927 | utf8 = F_UTF8 |
990 | utf8 = F_UTF8 |
928 | indent = F_INDENT |
991 | indent = F_INDENT |
929 | canonical = F_CANONICAL |
992 | canonical = F_CANONICAL |
930 | space_before = F_SPACE_BEFORE |
993 | space_before = F_SPACE_BEFORE |
931 | space_after = F_SPACE_AFTER |
994 | space_after = F_SPACE_AFTER |
932 | json_rpc = F_JSON_RPC |
|
|
933 | pretty = F_PRETTY |
995 | pretty = F_PRETTY |
934 | allow_nonref = F_ALLOW_NONREF |
996 | allow_nonref = F_ALLOW_NONREF |
935 | shrink = F_SHRINK |
997 | shrink = F_SHRINK |
936 | CODE: |
998 | CODE: |
937 | { |
999 | { |