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