1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
#include "assert.h" |
6 |
#include "string.h" |
7 |
#include "stdlib.h" |
8 |
|
9 |
#define F_ASCII 0x00000001UL |
10 |
#define F_UTF8 0x00000002UL |
11 |
#define F_INDENT 0x00000004UL |
12 |
#define F_CANONICAL 0x00000008UL |
13 |
#define F_SPACE_BEFORE 0x00000010UL |
14 |
#define F_SPACE_AFTER 0x00000020UL |
15 |
#define F_ALLOW_NONREF 0x00000080UL |
16 |
#define F_SHRINK 0x00000100UL |
17 |
#define F_MAXDEPTH 0xf8000000UL |
18 |
#define S_MAXDEPTH 27 |
19 |
|
20 |
#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
21 |
|
22 |
// F_SELFCONVERT? <=> to_json/toJson |
23 |
// F_BLESSED? <=> { $__class__$ => } |
24 |
|
25 |
#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 |
#define F_DEFAULT (13UL << S_MAXDEPTH) |
27 |
|
28 |
#define INIT_SIZE 32 // initial scalar size to be allocated |
29 |
#define INDENT_STEP 3 // spaces per indentation level |
30 |
|
31 |
#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
32 |
#define SHORT_STRING_LEN 512 // special-case strings of up to this size |
33 |
|
34 |
#define SB do { |
35 |
#define SE } while (0) |
36 |
|
37 |
static HV *json_stash; // JSON::XS:: |
38 |
|
39 |
///////////////////////////////////////////////////////////////////////////// |
40 |
// utility functions |
41 |
|
42 |
static UV * |
43 |
SvJSON (SV *sv) |
44 |
{ |
45 |
if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
46 |
croak ("object is not of type JSON::XS"); |
47 |
|
48 |
return &SvUVX (SvRV (sv)); |
49 |
} |
50 |
|
51 |
static void |
52 |
shrink (SV *sv) |
53 |
{ |
54 |
sv_utf8_downgrade (sv, 1); |
55 |
if (SvLEN (sv) > SvCUR (sv) + 1) |
56 |
{ |
57 |
#ifdef SvPV_shrink_to_cur |
58 |
SvPV_shrink_to_cur (sv); |
59 |
#elif defined (SvPV_renew) |
60 |
SvPV_renew (sv, SvCUR (sv) + 1); |
61 |
#endif |
62 |
} |
63 |
} |
64 |
|
65 |
// decode an utf-8 character and return it, or (UV)-1 in |
66 |
// case of an error. |
67 |
// we special-case "safe" characters from U+80 .. U+7FF, |
68 |
// but use the very good perl function to parse anything else. |
69 |
// note that we never call this function for a ascii codepoints |
70 |
static UV |
71 |
decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
72 |
{ |
73 |
if (s[0] > 0xdf || s[0] < 0xc2) |
74 |
return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
75 |
else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
76 |
{ |
77 |
*clen = 2; |
78 |
return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 |
} |
80 |
else |
81 |
return (UV)-1; |
82 |
} |
83 |
|
84 |
///////////////////////////////////////////////////////////////////////////// |
85 |
// encoder |
86 |
|
87 |
// structure used for encoding JSON |
88 |
typedef struct |
89 |
{ |
90 |
char *cur; // SvPVX (sv) + current output position |
91 |
char *end; // SvEND (sv) |
92 |
SV *sv; // result scalar |
93 |
U32 flags; // F_* |
94 |
U32 indent; // indentation level |
95 |
U32 maxdepth; // max. indentation/recursion level |
96 |
} enc_t; |
97 |
|
98 |
static void |
99 |
need (enc_t *enc, STRLEN len) |
100 |
{ |
101 |
if (enc->cur + len >= enc->end) |
102 |
{ |
103 |
STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 |
SvGROW (enc->sv, cur + len + 1); |
105 |
enc->cur = SvPVX (enc->sv) + cur; |
106 |
enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
107 |
} |
108 |
} |
109 |
|
110 |
static void |
111 |
encode_ch (enc_t *enc, char ch) |
112 |
{ |
113 |
need (enc, 1); |
114 |
*enc->cur++ = ch; |
115 |
} |
116 |
|
117 |
static void |
118 |
encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
119 |
{ |
120 |
char *end = str + len; |
121 |
|
122 |
need (enc, len); |
123 |
|
124 |
while (str < end) |
125 |
{ |
126 |
unsigned char ch = *(unsigned char *)str; |
127 |
|
128 |
if (ch >= 0x20 && ch < 0x80) // most common case |
129 |
{ |
130 |
if (ch == '"') // but with slow exceptions |
131 |
{ |
132 |
need (enc, len += 1); |
133 |
*enc->cur++ = '\\'; |
134 |
*enc->cur++ = '"'; |
135 |
} |
136 |
else if (ch == '\\') |
137 |
{ |
138 |
need (enc, len += 1); |
139 |
*enc->cur++ = '\\'; |
140 |
*enc->cur++ = '\\'; |
141 |
} |
142 |
else |
143 |
*enc->cur++ = ch; |
144 |
|
145 |
++str; |
146 |
} |
147 |
else |
148 |
{ |
149 |
switch (ch) |
150 |
{ |
151 |
case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
152 |
case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
153 |
case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
154 |
case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
155 |
case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
156 |
|
157 |
default: |
158 |
{ |
159 |
STRLEN clen; |
160 |
UV uch; |
161 |
|
162 |
if (is_utf8) |
163 |
{ |
164 |
//uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
165 |
uch = decode_utf8 (str, end - str, &clen); |
166 |
if (clen == (STRLEN)-1) |
167 |
croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
168 |
} |
169 |
else |
170 |
{ |
171 |
uch = ch; |
172 |
clen = 1; |
173 |
} |
174 |
|
175 |
if (uch > 0x10FFFFUL) |
176 |
croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
177 |
|
178 |
if (uch < 0x80 || enc->flags & F_ASCII) |
179 |
{ |
180 |
if (uch > 0xFFFFUL) |
181 |
{ |
182 |
need (enc, len += 11); |
183 |
sprintf (enc->cur, "\\u%04x\\u%04x", |
184 |
(int)((uch - 0x10000) / 0x400 + 0xD800), |
185 |
(int)((uch - 0x10000) % 0x400 + 0xDC00)); |
186 |
enc->cur += 12; |
187 |
} |
188 |
else |
189 |
{ |
190 |
static char hexdigit [16] = "0123456789abcdef"; |
191 |
need (enc, len += 5); |
192 |
*enc->cur++ = '\\'; |
193 |
*enc->cur++ = 'u'; |
194 |
*enc->cur++ = hexdigit [ uch >> 12 ]; |
195 |
*enc->cur++ = hexdigit [(uch >> 8) & 15]; |
196 |
*enc->cur++ = hexdigit [(uch >> 4) & 15]; |
197 |
*enc->cur++ = hexdigit [(uch >> 0) & 15]; |
198 |
} |
199 |
|
200 |
str += clen; |
201 |
} |
202 |
else if (is_utf8) |
203 |
{ |
204 |
need (enc, len += clen); |
205 |
do |
206 |
{ |
207 |
*enc->cur++ = *str++; |
208 |
} |
209 |
while (--clen); |
210 |
} |
211 |
else |
212 |
{ |
213 |
need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
214 |
enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
215 |
++str; |
216 |
} |
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
--len; |
222 |
} |
223 |
} |
224 |
|
225 |
static void |
226 |
encode_indent (enc_t *enc) |
227 |
{ |
228 |
if (enc->flags & F_INDENT) |
229 |
{ |
230 |
int spaces = enc->indent * INDENT_STEP; |
231 |
|
232 |
need (enc, spaces); |
233 |
memset (enc->cur, ' ', spaces); |
234 |
enc->cur += spaces; |
235 |
} |
236 |
} |
237 |
|
238 |
static void |
239 |
encode_space (enc_t *enc) |
240 |
{ |
241 |
need (enc, 1); |
242 |
encode_ch (enc, ' '); |
243 |
} |
244 |
|
245 |
static void |
246 |
encode_nl (enc_t *enc) |
247 |
{ |
248 |
if (enc->flags & F_INDENT) |
249 |
{ |
250 |
need (enc, 1); |
251 |
encode_ch (enc, '\n'); |
252 |
} |
253 |
} |
254 |
|
255 |
static void |
256 |
encode_comma (enc_t *enc) |
257 |
{ |
258 |
encode_ch (enc, ','); |
259 |
|
260 |
if (enc->flags & F_INDENT) |
261 |
encode_nl (enc); |
262 |
else if (enc->flags & F_SPACE_AFTER) |
263 |
encode_space (enc); |
264 |
} |
265 |
|
266 |
static void encode_sv (enc_t *enc, SV *sv); |
267 |
|
268 |
static void |
269 |
encode_av (enc_t *enc, AV *av) |
270 |
{ |
271 |
int i, len = av_len (av); |
272 |
|
273 |
encode_ch (enc, '['); encode_nl (enc); |
274 |
++enc->indent; |
275 |
|
276 |
for (i = 0; i <= len; ++i) |
277 |
{ |
278 |
encode_indent (enc); |
279 |
encode_sv (enc, *av_fetch (av, i, 0)); |
280 |
|
281 |
if (i < len) |
282 |
encode_comma (enc); |
283 |
} |
284 |
|
285 |
encode_nl (enc); |
286 |
|
287 |
--enc->indent; |
288 |
encode_indent (enc); encode_ch (enc, ']'); |
289 |
} |
290 |
|
291 |
static void |
292 |
encode_he (enc_t *enc, HE *he) |
293 |
{ |
294 |
encode_ch (enc, '"'); |
295 |
|
296 |
if (HeKLEN (he) == HEf_SVKEY) |
297 |
{ |
298 |
SV *sv = HeSVKEY (he); |
299 |
STRLEN len; |
300 |
char *str; |
301 |
|
302 |
SvGETMAGIC (sv); |
303 |
str = SvPV (sv, len); |
304 |
|
305 |
encode_str (enc, str, len, SvUTF8 (sv)); |
306 |
} |
307 |
else |
308 |
encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
309 |
|
310 |
encode_ch (enc, '"'); |
311 |
|
312 |
if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
313 |
encode_ch (enc, ':'); |
314 |
if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
315 |
encode_sv (enc, HeVAL (he)); |
316 |
} |
317 |
|
318 |
// compare hash entries, used when all keys are bytestrings |
319 |
static int |
320 |
he_cmp_fast (const void *a_, const void *b_) |
321 |
{ |
322 |
int cmp; |
323 |
|
324 |
HE *a = *(HE **)a_; |
325 |
HE *b = *(HE **)b_; |
326 |
|
327 |
STRLEN la = HeKLEN (a); |
328 |
STRLEN lb = HeKLEN (b); |
329 |
|
330 |
if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
331 |
cmp = la - lb; |
332 |
|
333 |
return cmp; |
334 |
} |
335 |
|
336 |
// compare hash entries, used when some keys are sv's or utf-x |
337 |
static int |
338 |
he_cmp_slow (const void *a, const void *b) |
339 |
{ |
340 |
return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
341 |
} |
342 |
|
343 |
static void |
344 |
encode_hv (enc_t *enc, HV *hv) |
345 |
{ |
346 |
int count, i; |
347 |
|
348 |
encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
349 |
|
350 |
if ((count = hv_iterinit (hv))) |
351 |
{ |
352 |
// for canonical output we have to sort by keys first |
353 |
// actually, this is mostly due to the stupid so-called |
354 |
// security workaround added somewhere in 5.8.x. |
355 |
// that randomises hash orderings |
356 |
if (enc->flags & F_CANONICAL) |
357 |
{ |
358 |
HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
359 |
int fast = 1; |
360 |
|
361 |
i = 0; |
362 |
while ((he = hv_iternext (hv))) |
363 |
{ |
364 |
hes [i++] = he; |
365 |
if (HeKLEN (he) < 0 || HeKUTF8 (he)) |
366 |
fast = 0; |
367 |
} |
368 |
|
369 |
assert (i == count); |
370 |
|
371 |
if (fast) |
372 |
qsort (hes, count, sizeof (HE *), he_cmp_fast); |
373 |
else |
374 |
{ |
375 |
// hack to forcefully disable "use bytes" |
376 |
COP cop = *PL_curcop; |
377 |
cop.op_private = 0; |
378 |
|
379 |
ENTER; |
380 |
SAVETMPS; |
381 |
|
382 |
SAVEVPTR (PL_curcop); |
383 |
PL_curcop = &cop; |
384 |
|
385 |
qsort (hes, count, sizeof (HE *), he_cmp_slow); |
386 |
|
387 |
FREETMPS; |
388 |
LEAVE; |
389 |
} |
390 |
|
391 |
for (i = 0; i < count; ++i) |
392 |
{ |
393 |
encode_indent (enc); |
394 |
encode_he (enc, hes [i]); |
395 |
|
396 |
if (i < count - 1) |
397 |
encode_comma (enc); |
398 |
} |
399 |
|
400 |
encode_nl (enc); |
401 |
} |
402 |
else |
403 |
{ |
404 |
SV *sv; |
405 |
HE *he = hv_iternext (hv); |
406 |
|
407 |
for (;;) |
408 |
{ |
409 |
encode_indent (enc); |
410 |
encode_he (enc, he); |
411 |
|
412 |
if (!(he = hv_iternext (hv))) |
413 |
break; |
414 |
|
415 |
encode_comma (enc); |
416 |
} |
417 |
|
418 |
encode_nl (enc); |
419 |
} |
420 |
} |
421 |
|
422 |
--enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
423 |
} |
424 |
|
425 |
static void |
426 |
encode_sv (enc_t *enc, SV *sv) |
427 |
{ |
428 |
SvGETMAGIC (sv); |
429 |
|
430 |
if (SvPOKp (sv)) |
431 |
{ |
432 |
STRLEN len; |
433 |
char *str = SvPV (sv, len); |
434 |
encode_ch (enc, '"'); |
435 |
encode_str (enc, str, len, SvUTF8 (sv)); |
436 |
encode_ch (enc, '"'); |
437 |
} |
438 |
else if (SvNOKp (sv)) |
439 |
{ |
440 |
need (enc, NV_DIG + 32); |
441 |
Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
442 |
enc->cur += strlen (enc->cur); |
443 |
} |
444 |
else if (SvIOKp (sv)) |
445 |
{ |
446 |
need (enc, 64); |
447 |
enc->cur += |
448 |
SvIsUV(sv) |
449 |
? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
450 |
: snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
451 |
} |
452 |
else if (SvROK (sv)) |
453 |
{ |
454 |
SV *rv = SvRV (sv); |
455 |
|
456 |
if (enc->indent >= enc->maxdepth) |
457 |
croak ("data structure too deep (hit recursion limit)"); |
458 |
|
459 |
switch (SvTYPE (rv)) |
460 |
{ |
461 |
case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
462 |
case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
463 |
|
464 |
default: |
465 |
croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
466 |
SvPV_nolen (sv)); |
467 |
} |
468 |
} |
469 |
else if (!SvOK (sv)) |
470 |
encode_str (enc, "null", 4, 0); |
471 |
else |
472 |
croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
473 |
SvPV_nolen (sv), SvFLAGS (sv)); |
474 |
} |
475 |
|
476 |
static SV * |
477 |
encode_json (SV *scalar, U32 flags) |
478 |
{ |
479 |
if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
480 |
croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
481 |
|
482 |
enc_t enc; |
483 |
enc.flags = flags; |
484 |
enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
485 |
enc.cur = SvPVX (enc.sv); |
486 |
enc.end = SvEND (enc.sv); |
487 |
enc.indent = 0; |
488 |
enc.maxdepth = DEC_DEPTH (flags); |
489 |
|
490 |
SvPOK_only (enc.sv); |
491 |
encode_sv (&enc, scalar); |
492 |
|
493 |
if (!(flags & (F_ASCII | F_UTF8))) |
494 |
SvUTF8_on (enc.sv); |
495 |
|
496 |
SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
497 |
|
498 |
if (enc.flags & F_SHRINK) |
499 |
shrink (enc.sv); |
500 |
|
501 |
return enc.sv; |
502 |
} |
503 |
|
504 |
///////////////////////////////////////////////////////////////////////////// |
505 |
// decoder |
506 |
|
507 |
// structure used for decoding JSON |
508 |
typedef struct |
509 |
{ |
510 |
char *cur; // current parser pointer |
511 |
char *end; // end of input string |
512 |
const char *err; // parse error, if != 0 |
513 |
U32 flags; // F_* |
514 |
U32 depth; // recursion depth |
515 |
U32 maxdepth; // recursion depth limit |
516 |
} dec_t; |
517 |
|
518 |
static void |
519 |
decode_ws (dec_t *dec) |
520 |
{ |
521 |
for (;;) |
522 |
{ |
523 |
char ch = *dec->cur; |
524 |
|
525 |
if (ch > 0x20 |
526 |
|| (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
527 |
break; |
528 |
|
529 |
++dec->cur; |
530 |
} |
531 |
} |
532 |
|
533 |
#define ERR(reason) SB dec->err = reason; goto fail; SE |
534 |
|
535 |
#define EXPECT_CH(ch) SB \ |
536 |
if (*dec->cur != ch) \ |
537 |
ERR (# ch " expected"); \ |
538 |
++dec->cur; \ |
539 |
SE |
540 |
|
541 |
#define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
542 |
#define DEC_DEC_DEPTH --dec->depth |
543 |
|
544 |
static SV *decode_sv (dec_t *dec); |
545 |
|
546 |
static signed char decode_hexdigit[256]; |
547 |
|
548 |
static UV |
549 |
decode_4hex (dec_t *dec) |
550 |
{ |
551 |
signed char d1, d2, d3, d4; |
552 |
unsigned char *cur = (unsigned char *)dec->cur; |
553 |
|
554 |
d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
555 |
d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
556 |
d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
557 |
d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
558 |
|
559 |
dec->cur += 4; |
560 |
|
561 |
return ((UV)d1) << 12 |
562 |
| ((UV)d2) << 8 |
563 |
| ((UV)d3) << 4 |
564 |
| ((UV)d4); |
565 |
|
566 |
fail: |
567 |
return (UV)-1; |
568 |
} |
569 |
|
570 |
static SV * |
571 |
decode_str (dec_t *dec) |
572 |
{ |
573 |
SV *sv = 0; |
574 |
int utf8 = 0; |
575 |
|
576 |
do |
577 |
{ |
578 |
char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
579 |
char *cur = buf; |
580 |
|
581 |
do |
582 |
{ |
583 |
unsigned char ch = *(unsigned char *)dec->cur++; |
584 |
|
585 |
if (ch == '"') |
586 |
{ |
587 |
--dec->cur; |
588 |
break; |
589 |
} |
590 |
else if (ch == '\\') |
591 |
{ |
592 |
switch (*dec->cur) |
593 |
{ |
594 |
case '\\': |
595 |
case '/': |
596 |
case '"': *cur++ = *dec->cur++; break; |
597 |
|
598 |
case 'b': ++dec->cur; *cur++ = '\010'; break; |
599 |
case 't': ++dec->cur; *cur++ = '\011'; break; |
600 |
case 'n': ++dec->cur; *cur++ = '\012'; break; |
601 |
case 'f': ++dec->cur; *cur++ = '\014'; break; |
602 |
case 'r': ++dec->cur; *cur++ = '\015'; break; |
603 |
|
604 |
case 'u': |
605 |
{ |
606 |
UV lo, hi; |
607 |
++dec->cur; |
608 |
|
609 |
hi = decode_4hex (dec); |
610 |
if (hi == (UV)-1) |
611 |
goto fail; |
612 |
|
613 |
// possibly a surrogate pair |
614 |
if (hi >= 0xd800) |
615 |
if (hi < 0xdc00) |
616 |
{ |
617 |
if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
618 |
ERR ("missing low surrogate character in surrogate pair"); |
619 |
|
620 |
dec->cur += 2; |
621 |
|
622 |
lo = decode_4hex (dec); |
623 |
if (lo == (UV)-1) |
624 |
goto fail; |
625 |
|
626 |
if (lo < 0xdc00 || lo >= 0xe000) |
627 |
ERR ("surrogate pair expected"); |
628 |
|
629 |
hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
630 |
} |
631 |
else if (hi < 0xe000) |
632 |
ERR ("missing high surrogate character in surrogate pair"); |
633 |
|
634 |
if (hi >= 0x80) |
635 |
{ |
636 |
utf8 = 1; |
637 |
|
638 |
cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
639 |
} |
640 |
else |
641 |
*cur++ = hi; |
642 |
} |
643 |
break; |
644 |
|
645 |
default: |
646 |
--dec->cur; |
647 |
ERR ("illegal backslash escape sequence in string"); |
648 |
} |
649 |
} |
650 |
else if (ch >= 0x20 && ch <= 0x7f) |
651 |
*cur++ = ch; |
652 |
else if (ch >= 0x80) |
653 |
{ |
654 |
--dec->cur; |
655 |
|
656 |
STRLEN clen; |
657 |
UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
658 |
if (clen == (STRLEN)-1) |
659 |
ERR ("malformed UTF-8 character in JSON string"); |
660 |
|
661 |
do |
662 |
{ |
663 |
*cur++ = *dec->cur++; |
664 |
} |
665 |
while (--clen); |
666 |
|
667 |
utf8 = 1; |
668 |
} |
669 |
else if (!ch) |
670 |
ERR ("unexpected end of string while parsing json string"); |
671 |
else |
672 |
ERR ("invalid character encountered"); |
673 |
|
674 |
} |
675 |
while (cur < buf + SHORT_STRING_LEN); |
676 |
|
677 |
STRLEN len = cur - buf; |
678 |
|
679 |
if (sv) |
680 |
{ |
681 |
SvGROW (sv, SvCUR (sv) + len + 1); |
682 |
memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
683 |
SvCUR_set (sv, SvCUR (sv) + len); |
684 |
} |
685 |
else |
686 |
sv = newSVpvn (buf, len); |
687 |
} |
688 |
while (*dec->cur != '"'); |
689 |
|
690 |
++dec->cur; |
691 |
|
692 |
if (sv) |
693 |
{ |
694 |
SvPOK_only (sv); |
695 |
*SvEND (sv) = 0; |
696 |
|
697 |
if (utf8) |
698 |
SvUTF8_on (sv); |
699 |
} |
700 |
else |
701 |
sv = newSVpvn ("", 0); |
702 |
|
703 |
return sv; |
704 |
|
705 |
fail: |
706 |
return 0; |
707 |
} |
708 |
|
709 |
static SV * |
710 |
decode_num (dec_t *dec) |
711 |
{ |
712 |
int is_nv = 0; |
713 |
char *start = dec->cur; |
714 |
|
715 |
// [minus] |
716 |
if (*dec->cur == '-') |
717 |
++dec->cur; |
718 |
|
719 |
if (*dec->cur == '0') |
720 |
{ |
721 |
++dec->cur; |
722 |
if (*dec->cur >= '0' && *dec->cur <= '9') |
723 |
ERR ("malformed number (leading zero must not be followed by another digit)"); |
724 |
} |
725 |
else if (*dec->cur < '0' || *dec->cur > '9') |
726 |
ERR ("malformed number (no digits after initial minus)"); |
727 |
else |
728 |
do |
729 |
{ |
730 |
++dec->cur; |
731 |
} |
732 |
while (*dec->cur >= '0' && *dec->cur <= '9'); |
733 |
|
734 |
// [frac] |
735 |
if (*dec->cur == '.') |
736 |
{ |
737 |
++dec->cur; |
738 |
|
739 |
if (*dec->cur < '0' || *dec->cur > '9') |
740 |
ERR ("malformed number (no digits after decimal point)"); |
741 |
|
742 |
do |
743 |
{ |
744 |
++dec->cur; |
745 |
} |
746 |
while (*dec->cur >= '0' && *dec->cur <= '9'); |
747 |
|
748 |
is_nv = 1; |
749 |
} |
750 |
|
751 |
// [exp] |
752 |
if (*dec->cur == 'e' || *dec->cur == 'E') |
753 |
{ |
754 |
++dec->cur; |
755 |
|
756 |
if (*dec->cur == '-' || *dec->cur == '+') |
757 |
++dec->cur; |
758 |
|
759 |
if (*dec->cur < '0' || *dec->cur > '9') |
760 |
ERR ("malformed number (no digits after exp sign)"); |
761 |
|
762 |
do |
763 |
{ |
764 |
++dec->cur; |
765 |
} |
766 |
while (*dec->cur >= '0' && *dec->cur <= '9'); |
767 |
|
768 |
is_nv = 1; |
769 |
} |
770 |
|
771 |
if (!is_nv) |
772 |
{ |
773 |
UV uv; |
774 |
int numtype = grok_number (start, dec->cur - start, &uv); |
775 |
if (numtype & IS_NUMBER_IN_UV) |
776 |
if (numtype & IS_NUMBER_NEG) |
777 |
{ |
778 |
if (uv < (UV)IV_MIN) |
779 |
return newSViv (-(IV)uv); |
780 |
} |
781 |
else |
782 |
return newSVuv (uv); |
783 |
} |
784 |
|
785 |
return newSVnv (Atof (start)); |
786 |
|
787 |
fail: |
788 |
return 0; |
789 |
} |
790 |
|
791 |
static SV * |
792 |
decode_av (dec_t *dec) |
793 |
{ |
794 |
AV *av = newAV (); |
795 |
|
796 |
DEC_INC_DEPTH; |
797 |
decode_ws (dec); |
798 |
|
799 |
if (*dec->cur == ']') |
800 |
++dec->cur; |
801 |
else |
802 |
for (;;) |
803 |
{ |
804 |
SV *value; |
805 |
|
806 |
value = decode_sv (dec); |
807 |
if (!value) |
808 |
goto fail; |
809 |
|
810 |
av_push (av, value); |
811 |
|
812 |
decode_ws (dec); |
813 |
|
814 |
if (*dec->cur == ']') |
815 |
{ |
816 |
++dec->cur; |
817 |
break; |
818 |
} |
819 |
|
820 |
if (*dec->cur != ',') |
821 |
ERR (", or ] expected while parsing array"); |
822 |
|
823 |
++dec->cur; |
824 |
} |
825 |
|
826 |
DEC_DEC_DEPTH; |
827 |
return newRV_noinc ((SV *)av); |
828 |
|
829 |
fail: |
830 |
SvREFCNT_dec (av); |
831 |
DEC_DEC_DEPTH; |
832 |
return 0; |
833 |
} |
834 |
|
835 |
static SV * |
836 |
decode_hv (dec_t *dec) |
837 |
{ |
838 |
HV *hv = newHV (); |
839 |
|
840 |
DEC_INC_DEPTH; |
841 |
decode_ws (dec); |
842 |
|
843 |
if (*dec->cur == '}') |
844 |
++dec->cur; |
845 |
else |
846 |
for (;;) |
847 |
{ |
848 |
SV *key, *value; |
849 |
|
850 |
decode_ws (dec); EXPECT_CH ('"'); |
851 |
|
852 |
key = decode_str (dec); |
853 |
if (!key) |
854 |
goto fail; |
855 |
|
856 |
decode_ws (dec); EXPECT_CH (':'); |
857 |
|
858 |
value = decode_sv (dec); |
859 |
if (!value) |
860 |
{ |
861 |
SvREFCNT_dec (key); |
862 |
goto fail; |
863 |
} |
864 |
|
865 |
hv_store_ent (hv, key, value, 0); |
866 |
SvREFCNT_dec (key); |
867 |
|
868 |
decode_ws (dec); |
869 |
|
870 |
if (*dec->cur == '}') |
871 |
{ |
872 |
++dec->cur; |
873 |
break; |
874 |
} |
875 |
|
876 |
if (*dec->cur != ',') |
877 |
ERR (", or } expected while parsing object/hash"); |
878 |
|
879 |
++dec->cur; |
880 |
} |
881 |
|
882 |
DEC_DEC_DEPTH; |
883 |
return newRV_noinc ((SV *)hv); |
884 |
|
885 |
fail: |
886 |
SvREFCNT_dec (hv); |
887 |
DEC_DEC_DEPTH; |
888 |
return 0; |
889 |
} |
890 |
|
891 |
static SV * |
892 |
decode_sv (dec_t *dec) |
893 |
{ |
894 |
decode_ws (dec); |
895 |
switch (*dec->cur) |
896 |
{ |
897 |
case '"': ++dec->cur; return decode_str (dec); |
898 |
case '[': ++dec->cur; return decode_av (dec); |
899 |
case '{': ++dec->cur; return decode_hv (dec); |
900 |
|
901 |
case '-': |
902 |
case '0': case '1': case '2': case '3': case '4': |
903 |
case '5': case '6': case '7': case '8': case '9': |
904 |
return decode_num (dec); |
905 |
|
906 |
case 't': |
907 |
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
908 |
{ |
909 |
dec->cur += 4; |
910 |
return newSViv (1); |
911 |
} |
912 |
else |
913 |
ERR ("'true' expected"); |
914 |
|
915 |
break; |
916 |
|
917 |
case 'f': |
918 |
if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
919 |
{ |
920 |
dec->cur += 5; |
921 |
return newSViv (0); |
922 |
} |
923 |
else |
924 |
ERR ("'false' expected"); |
925 |
|
926 |
break; |
927 |
|
928 |
case 'n': |
929 |
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
930 |
{ |
931 |
dec->cur += 4; |
932 |
return newSVsv (&PL_sv_undef); |
933 |
} |
934 |
else |
935 |
ERR ("'null' expected"); |
936 |
|
937 |
break; |
938 |
|
939 |
default: |
940 |
ERR ("malformed json string, neither array, object, number, string or atom"); |
941 |
break; |
942 |
} |
943 |
|
944 |
fail: |
945 |
return 0; |
946 |
} |
947 |
|
948 |
static SV * |
949 |
decode_json (SV *string, U32 flags) |
950 |
{ |
951 |
SV *sv; |
952 |
|
953 |
if (flags & F_UTF8) |
954 |
sv_utf8_downgrade (string, 0); |
955 |
else |
956 |
sv_utf8_upgrade (string); |
957 |
|
958 |
SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
959 |
|
960 |
dec_t dec; |
961 |
dec.flags = flags; |
962 |
dec.cur = SvPVX (string); |
963 |
dec.end = SvEND (string); |
964 |
dec.err = 0; |
965 |
dec.depth = 0; |
966 |
dec.maxdepth = DEC_DEPTH (dec.flags); |
967 |
|
968 |
*dec.end = 0; // this should basically be a nop, too, but make sure its there |
969 |
sv = decode_sv (&dec); |
970 |
|
971 |
if (!sv) |
972 |
{ |
973 |
IV offset = dec.flags & F_UTF8 |
974 |
? dec.cur - SvPVX (string) |
975 |
: utf8_distance (dec.cur, SvPVX (string)); |
976 |
SV *uni = sv_newmortal (); |
977 |
|
978 |
// horrible hack to silence warning inside pv_uni_display |
979 |
COP cop = *PL_curcop; |
980 |
cop.cop_warnings = pWARN_NONE; |
981 |
ENTER; |
982 |
SAVEVPTR (PL_curcop); |
983 |
PL_curcop = &cop; |
984 |
pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
985 |
LEAVE; |
986 |
|
987 |
croak ("%s, at character offset %d (%s)", |
988 |
dec.err, |
989 |
(int)offset, |
990 |
dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
991 |
} |
992 |
|
993 |
sv = sv_2mortal (sv); |
994 |
|
995 |
if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
996 |
croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
997 |
|
998 |
return sv; |
999 |
} |
1000 |
|
1001 |
///////////////////////////////////////////////////////////////////////////// |
1002 |
// XS interface functions |
1003 |
|
1004 |
MODULE = JSON::XS PACKAGE = JSON::XS |
1005 |
|
1006 |
BOOT: |
1007 |
{ |
1008 |
int i; |
1009 |
|
1010 |
memset (decode_hexdigit, 0xff, 256); |
1011 |
|
1012 |
for (i = 0; i < 256; ++i) |
1013 |
decode_hexdigit [i] = |
1014 |
i >= '0' && i <= '9' ? i - '0' |
1015 |
: i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1016 |
: i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1017 |
: -1; |
1018 |
|
1019 |
json_stash = gv_stashpv ("JSON::XS", 1); |
1020 |
} |
1021 |
|
1022 |
PROTOTYPES: DISABLE |
1023 |
|
1024 |
SV *new (char *dummy) |
1025 |
CODE: |
1026 |
RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1027 |
OUTPUT: |
1028 |
RETVAL |
1029 |
|
1030 |
SV *ascii (SV *self, int enable = 1) |
1031 |
ALIAS: |
1032 |
ascii = F_ASCII |
1033 |
utf8 = F_UTF8 |
1034 |
indent = F_INDENT |
1035 |
canonical = F_CANONICAL |
1036 |
space_before = F_SPACE_BEFORE |
1037 |
space_after = F_SPACE_AFTER |
1038 |
pretty = F_PRETTY |
1039 |
allow_nonref = F_ALLOW_NONREF |
1040 |
shrink = F_SHRINK |
1041 |
CODE: |
1042 |
{ |
1043 |
UV *uv = SvJSON (self); |
1044 |
if (enable) |
1045 |
*uv |= ix; |
1046 |
else |
1047 |
*uv &= ~ix; |
1048 |
|
1049 |
RETVAL = newSVsv (self); |
1050 |
} |
1051 |
OUTPUT: |
1052 |
RETVAL |
1053 |
|
1054 |
SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1055 |
CODE: |
1056 |
{ |
1057 |
UV *uv = SvJSON (self); |
1058 |
UV log2 = 0; |
1059 |
|
1060 |
if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1061 |
|
1062 |
while ((1UL << log2) < max_depth) |
1063 |
++log2; |
1064 |
|
1065 |
*uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1066 |
|
1067 |
RETVAL = newSVsv (self); |
1068 |
} |
1069 |
OUTPUT: |
1070 |
RETVAL |
1071 |
|
1072 |
void encode (SV *self, SV *scalar) |
1073 |
PPCODE: |
1074 |
XPUSHs (encode_json (scalar, *SvJSON (self))); |
1075 |
|
1076 |
void decode (SV *self, SV *jsonstr) |
1077 |
PPCODE: |
1078 |
XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1079 |
|
1080 |
PROTOTYPES: ENABLE |
1081 |
|
1082 |
void to_json (SV *scalar) |
1083 |
ALIAS: |
1084 |
objToJson = 0 |
1085 |
PPCODE: |
1086 |
XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1087 |
|
1088 |
void from_json (SV *jsonstr) |
1089 |
ALIAS: |
1090 |
jsonToObj = 0 |
1091 |
PPCODE: |
1092 |
XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1093 |
|