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 |
#include <stdio.h> |
9 |
#include <float.h> |
10 |
|
11 |
#if defined(__BORLANDC__) || defined(_MSC_VER) |
12 |
# define snprintf _snprintf // C compilers have this in stdio.h |
13 |
#endif |
14 |
|
15 |
// some old perls do not have this, try to make it work, no |
16 |
// guarentees, though. if it breaks, you get to keep the pieces. |
17 |
#ifndef UTF8_MAXBYTES |
18 |
# define UTF8_MAXBYTES 13 |
19 |
#endif |
20 |
|
21 |
#define F_ASCII 0x00000001UL |
22 |
#define F_LATIN1 0x00000002UL |
23 |
#define F_UTF8 0x00000004UL |
24 |
#define F_INDENT 0x00000008UL |
25 |
#define F_CANONICAL 0x00000010UL |
26 |
#define F_SPACE_BEFORE 0x00000020UL |
27 |
#define F_SPACE_AFTER 0x00000040UL |
28 |
#define F_ALLOW_NONREF 0x00000100UL |
29 |
#define F_SHRINK 0x00000200UL |
30 |
#define F_ALLOW_BLESSED 0x00000400UL |
31 |
#define F_CONV_BLESSED 0x00000800UL |
32 |
#define F_RELAXED 0x00001000UL |
33 |
|
34 |
#define F_MAXDEPTH 0xf8000000UL |
35 |
#define S_MAXDEPTH 27 |
36 |
#define F_MAXSIZE 0x01f00000UL |
37 |
#define S_MAXSIZE 20 |
38 |
#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
39 |
|
40 |
#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
41 |
#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
42 |
|
43 |
#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
44 |
#define F_DEFAULT (9UL << S_MAXDEPTH) |
45 |
|
46 |
#define INIT_SIZE 32 // initial scalar size to be allocated |
47 |
#define INDENT_STEP 3 // spaces per indentation level |
48 |
|
49 |
#define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
50 |
|
51 |
#define SB do { |
52 |
#define SE } while (0) |
53 |
|
54 |
#if __GNUC__ >= 3 |
55 |
# define expect(expr,value) __builtin_expect ((expr), (value)) |
56 |
# define INLINE static inline |
57 |
#else |
58 |
# define expect(expr,value) (expr) |
59 |
# define INLINE static |
60 |
#endif |
61 |
|
62 |
#define expect_false(expr) expect ((expr) != 0, 0) |
63 |
#define expect_true(expr) expect ((expr) != 0, 1) |
64 |
|
65 |
#define IN_RANGE_INC(type,val,beg,end) \ |
66 |
((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
67 |
<= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
68 |
|
69 |
#ifdef USE_ITHREADS |
70 |
# define JSON_SLOW 1 |
71 |
# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
72 |
#else |
73 |
# define JSON_SLOW 0 |
74 |
# define JSON_STASH json_stash |
75 |
#endif |
76 |
|
77 |
static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
78 |
static SV *json_true, *json_false; |
79 |
|
80 |
typedef struct { |
81 |
U32 flags; |
82 |
SV *cb_object; |
83 |
HV *cb_sk_object; |
84 |
} JSON; |
85 |
|
86 |
///////////////////////////////////////////////////////////////////////////// |
87 |
// utility functions |
88 |
|
89 |
INLINE void |
90 |
shrink (SV *sv) |
91 |
{ |
92 |
sv_utf8_downgrade (sv, 1); |
93 |
if (SvLEN (sv) > SvCUR (sv) + 1) |
94 |
{ |
95 |
#ifdef SvPV_shrink_to_cur |
96 |
SvPV_shrink_to_cur (sv); |
97 |
#elif defined (SvPV_renew) |
98 |
SvPV_renew (sv, SvCUR (sv) + 1); |
99 |
#endif |
100 |
} |
101 |
} |
102 |
|
103 |
// decode an utf-8 character and return it, or (UV)-1 in |
104 |
// case of an error. |
105 |
// we special-case "safe" characters from U+80 .. U+7FF, |
106 |
// but use the very good perl function to parse anything else. |
107 |
// note that we never call this function for a ascii codepoints |
108 |
INLINE UV |
109 |
decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
110 |
{ |
111 |
if (expect_true (len >= 2 |
112 |
&& IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
113 |
&& IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
114 |
{ |
115 |
*clen = 2; |
116 |
return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
117 |
} |
118 |
else |
119 |
return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
120 |
} |
121 |
|
122 |
// likewise for encoding, also never called for ascii codepoints |
123 |
// this function takes advantage of this fact, although current gccs |
124 |
// seem to optimise the check for >= 0x80 away anyways |
125 |
INLINE unsigned char * |
126 |
encode_utf8 (unsigned char *s, UV ch) |
127 |
{ |
128 |
if (ch <= 0x7FF) |
129 |
{ |
130 |
*s++ = (ch >> 6) | 0xc0; |
131 |
*s++ = (ch & 0x3f) | 0x80; |
132 |
} |
133 |
else |
134 |
s = uvuni_to_utf8_flags (s, ch, 0); |
135 |
|
136 |
return s; |
137 |
} |
138 |
|
139 |
///////////////////////////////////////////////////////////////////////////// |
140 |
// encoder |
141 |
|
142 |
// structure used for encoding JSON |
143 |
typedef struct |
144 |
{ |
145 |
char *cur; // SvPVX (sv) + current output position |
146 |
char *end; // SvEND (sv) |
147 |
SV *sv; // result scalar |
148 |
JSON json; |
149 |
U32 indent; // indentation level |
150 |
U32 maxdepth; // max. indentation/recursion level |
151 |
UV limit; // escape character values >= this value when encoding |
152 |
} enc_t; |
153 |
|
154 |
INLINE void |
155 |
need (enc_t *enc, STRLEN len) |
156 |
{ |
157 |
if (expect_false (enc->cur + len >= enc->end)) |
158 |
{ |
159 |
STRLEN cur = enc->cur - SvPVX (enc->sv); |
160 |
SvGROW (enc->sv, cur + len + 1); |
161 |
enc->cur = SvPVX (enc->sv) + cur; |
162 |
enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
163 |
} |
164 |
} |
165 |
|
166 |
INLINE void |
167 |
encode_ch (enc_t *enc, char ch) |
168 |
{ |
169 |
need (enc, 1); |
170 |
*enc->cur++ = ch; |
171 |
} |
172 |
|
173 |
static void |
174 |
encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
175 |
{ |
176 |
char *end = str + len; |
177 |
|
178 |
need (enc, len); |
179 |
|
180 |
while (str < end) |
181 |
{ |
182 |
unsigned char ch = *(unsigned char *)str; |
183 |
|
184 |
if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
185 |
{ |
186 |
if (expect_false (ch == '"')) // but with slow exceptions |
187 |
{ |
188 |
need (enc, len += 1); |
189 |
*enc->cur++ = '\\'; |
190 |
*enc->cur++ = '"'; |
191 |
} |
192 |
else if (expect_false (ch == '\\')) |
193 |
{ |
194 |
need (enc, len += 1); |
195 |
*enc->cur++ = '\\'; |
196 |
*enc->cur++ = '\\'; |
197 |
} |
198 |
else |
199 |
*enc->cur++ = ch; |
200 |
|
201 |
++str; |
202 |
} |
203 |
else |
204 |
{ |
205 |
switch (ch) |
206 |
{ |
207 |
case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
208 |
case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
209 |
case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
210 |
case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
211 |
case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
212 |
|
213 |
default: |
214 |
{ |
215 |
STRLEN clen; |
216 |
UV uch; |
217 |
|
218 |
if (is_utf8) |
219 |
{ |
220 |
uch = decode_utf8 (str, end - str, &clen); |
221 |
if (clen == (STRLEN)-1) |
222 |
croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
223 |
} |
224 |
else |
225 |
{ |
226 |
uch = ch; |
227 |
clen = 1; |
228 |
} |
229 |
|
230 |
if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
231 |
{ |
232 |
if (uch > 0xFFFFUL) |
233 |
{ |
234 |
if (uch > 0x10FFFFUL) |
235 |
croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
236 |
|
237 |
need (enc, len += 11); |
238 |
sprintf (enc->cur, "\\u%04x\\u%04x", |
239 |
(int)((uch - 0x10000) / 0x400 + 0xD800), |
240 |
(int)((uch - 0x10000) % 0x400 + 0xDC00)); |
241 |
enc->cur += 12; |
242 |
} |
243 |
else |
244 |
{ |
245 |
static char hexdigit [16] = "0123456789abcdef"; |
246 |
need (enc, len += 5); |
247 |
*enc->cur++ = '\\'; |
248 |
*enc->cur++ = 'u'; |
249 |
*enc->cur++ = hexdigit [ uch >> 12 ]; |
250 |
*enc->cur++ = hexdigit [(uch >> 8) & 15]; |
251 |
*enc->cur++ = hexdigit [(uch >> 4) & 15]; |
252 |
*enc->cur++ = hexdigit [(uch >> 0) & 15]; |
253 |
} |
254 |
|
255 |
str += clen; |
256 |
} |
257 |
else if (enc->json.flags & F_LATIN1) |
258 |
{ |
259 |
*enc->cur++ = uch; |
260 |
str += clen; |
261 |
} |
262 |
else if (is_utf8) |
263 |
{ |
264 |
need (enc, len += clen); |
265 |
do |
266 |
{ |
267 |
*enc->cur++ = *str++; |
268 |
} |
269 |
while (--clen); |
270 |
} |
271 |
else |
272 |
{ |
273 |
need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
274 |
enc->cur = encode_utf8 (enc->cur, uch); |
275 |
++str; |
276 |
} |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
--len; |
282 |
} |
283 |
} |
284 |
|
285 |
INLINE void |
286 |
encode_indent (enc_t *enc) |
287 |
{ |
288 |
if (enc->json.flags & F_INDENT) |
289 |
{ |
290 |
int spaces = enc->indent * INDENT_STEP; |
291 |
|
292 |
need (enc, spaces); |
293 |
memset (enc->cur, ' ', spaces); |
294 |
enc->cur += spaces; |
295 |
} |
296 |
} |
297 |
|
298 |
INLINE void |
299 |
encode_space (enc_t *enc) |
300 |
{ |
301 |
need (enc, 1); |
302 |
encode_ch (enc, ' '); |
303 |
} |
304 |
|
305 |
INLINE void |
306 |
encode_nl (enc_t *enc) |
307 |
{ |
308 |
if (enc->json.flags & F_INDENT) |
309 |
{ |
310 |
need (enc, 1); |
311 |
encode_ch (enc, '\n'); |
312 |
} |
313 |
} |
314 |
|
315 |
INLINE void |
316 |
encode_comma (enc_t *enc) |
317 |
{ |
318 |
encode_ch (enc, ','); |
319 |
|
320 |
if (enc->json.flags & F_INDENT) |
321 |
encode_nl (enc); |
322 |
else if (enc->json.flags & F_SPACE_AFTER) |
323 |
encode_space (enc); |
324 |
} |
325 |
|
326 |
static void encode_sv (enc_t *enc, SV *sv); |
327 |
|
328 |
static void |
329 |
encode_av (enc_t *enc, AV *av) |
330 |
{ |
331 |
int i, len = av_len (av); |
332 |
|
333 |
if (enc->indent >= enc->maxdepth) |
334 |
croak ("data structure too deep (hit recursion limit)"); |
335 |
|
336 |
encode_ch (enc, '['); |
337 |
|
338 |
if (len >= 0) |
339 |
{ |
340 |
encode_nl (enc); ++enc->indent; |
341 |
|
342 |
for (i = 0; i <= len; ++i) |
343 |
{ |
344 |
SV **svp = av_fetch (av, i, 0); |
345 |
|
346 |
encode_indent (enc); |
347 |
|
348 |
if (svp) |
349 |
encode_sv (enc, *svp); |
350 |
else |
351 |
encode_str (enc, "null", 4, 0); |
352 |
|
353 |
if (i < len) |
354 |
encode_comma (enc); |
355 |
} |
356 |
|
357 |
encode_nl (enc); --enc->indent; encode_indent (enc); |
358 |
} |
359 |
|
360 |
encode_ch (enc, ']'); |
361 |
} |
362 |
|
363 |
static void |
364 |
encode_hk (enc_t *enc, HE *he) |
365 |
{ |
366 |
encode_ch (enc, '"'); |
367 |
|
368 |
if (HeKLEN (he) == HEf_SVKEY) |
369 |
{ |
370 |
SV *sv = HeSVKEY (he); |
371 |
STRLEN len; |
372 |
char *str; |
373 |
|
374 |
SvGETMAGIC (sv); |
375 |
str = SvPV (sv, len); |
376 |
|
377 |
encode_str (enc, str, len, SvUTF8 (sv)); |
378 |
} |
379 |
else |
380 |
encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
381 |
|
382 |
encode_ch (enc, '"'); |
383 |
|
384 |
if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
385 |
encode_ch (enc, ':'); |
386 |
if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
387 |
} |
388 |
|
389 |
// compare hash entries, used when all keys are bytestrings |
390 |
static int |
391 |
he_cmp_fast (const void *a_, const void *b_) |
392 |
{ |
393 |
int cmp; |
394 |
|
395 |
HE *a = *(HE **)a_; |
396 |
HE *b = *(HE **)b_; |
397 |
|
398 |
STRLEN la = HeKLEN (a); |
399 |
STRLEN lb = HeKLEN (b); |
400 |
|
401 |
if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
402 |
cmp = lb - la; |
403 |
|
404 |
return cmp; |
405 |
} |
406 |
|
407 |
// compare hash entries, used when some keys are sv's or utf-x |
408 |
static int |
409 |
he_cmp_slow (const void *a, const void *b) |
410 |
{ |
411 |
return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
412 |
} |
413 |
|
414 |
static void |
415 |
encode_hv (enc_t *enc, HV *hv) |
416 |
{ |
417 |
HE *he; |
418 |
int count; |
419 |
|
420 |
if (enc->indent >= enc->maxdepth) |
421 |
croak ("data structure too deep (hit recursion limit)"); |
422 |
|
423 |
encode_ch (enc, '{'); |
424 |
|
425 |
// for canonical output we have to sort by keys first |
426 |
// actually, this is mostly due to the stupid so-called |
427 |
// security workaround added somewhere in 5.8.x. |
428 |
// that randomises hash orderings |
429 |
if (enc->json.flags & F_CANONICAL) |
430 |
{ |
431 |
int count = hv_iterinit (hv); |
432 |
|
433 |
if (SvMAGICAL (hv)) |
434 |
{ |
435 |
// need to count by iterating. could improve by dynamically building the vector below |
436 |
// but I don't care for the speed of this special case. |
437 |
// note also that we will run into undefined behaviour when the two iterations |
438 |
// do not result in the same count, something I might care for in some later release. |
439 |
|
440 |
count = 0; |
441 |
while (hv_iternext (hv)) |
442 |
++count; |
443 |
|
444 |
hv_iterinit (hv); |
445 |
} |
446 |
|
447 |
if (count) |
448 |
{ |
449 |
int i, fast = 1; |
450 |
#if defined(__BORLANDC__) || defined(_MSC_VER) |
451 |
HE **hes = _alloca (count * sizeof (HE)); |
452 |
#else |
453 |
HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
454 |
#endif |
455 |
|
456 |
i = 0; |
457 |
while ((he = hv_iternext (hv))) |
458 |
{ |
459 |
hes [i++] = he; |
460 |
if (HeKLEN (he) < 0 || HeKUTF8 (he)) |
461 |
fast = 0; |
462 |
} |
463 |
|
464 |
assert (i == count); |
465 |
|
466 |
if (fast) |
467 |
qsort (hes, count, sizeof (HE *), he_cmp_fast); |
468 |
else |
469 |
{ |
470 |
// hack to forcefully disable "use bytes" |
471 |
COP cop = *PL_curcop; |
472 |
cop.op_private = 0; |
473 |
|
474 |
ENTER; |
475 |
SAVETMPS; |
476 |
|
477 |
SAVEVPTR (PL_curcop); |
478 |
PL_curcop = &cop; |
479 |
|
480 |
qsort (hes, count, sizeof (HE *), he_cmp_slow); |
481 |
|
482 |
FREETMPS; |
483 |
LEAVE; |
484 |
} |
485 |
|
486 |
encode_nl (enc); ++enc->indent; |
487 |
|
488 |
while (count--) |
489 |
{ |
490 |
encode_indent (enc); |
491 |
he = hes [count]; |
492 |
encode_hk (enc, he); |
493 |
encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
494 |
|
495 |
if (count) |
496 |
encode_comma (enc); |
497 |
} |
498 |
|
499 |
encode_nl (enc); --enc->indent; encode_indent (enc); |
500 |
} |
501 |
} |
502 |
else |
503 |
{ |
504 |
if (hv_iterinit (hv) || SvMAGICAL (hv)) |
505 |
if ((he = hv_iternext (hv))) |
506 |
{ |
507 |
encode_nl (enc); ++enc->indent; |
508 |
|
509 |
for (;;) |
510 |
{ |
511 |
encode_indent (enc); |
512 |
encode_hk (enc, he); |
513 |
encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
514 |
|
515 |
if (!(he = hv_iternext (hv))) |
516 |
break; |
517 |
|
518 |
encode_comma (enc); |
519 |
} |
520 |
|
521 |
encode_nl (enc); --enc->indent; encode_indent (enc); |
522 |
} |
523 |
} |
524 |
|
525 |
encode_ch (enc, '}'); |
526 |
} |
527 |
|
528 |
// encode objects, arrays and special \0=false and \1=true values. |
529 |
static void |
530 |
encode_rv (enc_t *enc, SV *sv) |
531 |
{ |
532 |
svtype svt; |
533 |
|
534 |
SvGETMAGIC (sv); |
535 |
svt = SvTYPE (sv); |
536 |
|
537 |
if (expect_false (SvOBJECT (sv))) |
538 |
{ |
539 |
HV *stash = !JSON_SLOW || json_boolean_stash |
540 |
? json_boolean_stash |
541 |
: gv_stashpv ("JSON::XS::Boolean", 1); |
542 |
|
543 |
if (SvSTASH (sv) == stash) |
544 |
{ |
545 |
if (SvIV (sv)) |
546 |
encode_str (enc, "true", 4, 0); |
547 |
else |
548 |
encode_str (enc, "false", 5, 0); |
549 |
} |
550 |
else |
551 |
{ |
552 |
#if 0 |
553 |
if (0 && sv_derived_from (rv, "JSON::Literal")) |
554 |
{ |
555 |
// not yet |
556 |
} |
557 |
#endif |
558 |
if (enc->json.flags & F_CONV_BLESSED) |
559 |
{ |
560 |
// we re-bless the reference to get overload and other niceties right |
561 |
GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
562 |
|
563 |
if (to_json) |
564 |
{ |
565 |
dSP; |
566 |
|
567 |
ENTER; SAVETMPS; PUSHMARK (SP); |
568 |
XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
569 |
|
570 |
// calling with G_SCALAR ensures that we always get a 1 return value |
571 |
PUTBACK; |
572 |
call_sv ((SV *)GvCV (to_json), G_SCALAR); |
573 |
SPAGAIN; |
574 |
|
575 |
// catch this surprisingly common error |
576 |
if (SvROK (TOPs) && SvRV (TOPs) == sv) |
577 |
croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
578 |
|
579 |
sv = POPs; |
580 |
PUTBACK; |
581 |
|
582 |
encode_sv (enc, sv); |
583 |
|
584 |
FREETMPS; LEAVE; |
585 |
} |
586 |
else if (enc->json.flags & F_ALLOW_BLESSED) |
587 |
encode_str (enc, "null", 4, 0); |
588 |
else |
589 |
croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
590 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
591 |
} |
592 |
else if (enc->json.flags & F_ALLOW_BLESSED) |
593 |
encode_str (enc, "null", 4, 0); |
594 |
else |
595 |
croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
596 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
597 |
} |
598 |
} |
599 |
else if (svt == SVt_PVHV) |
600 |
encode_hv (enc, (HV *)sv); |
601 |
else if (svt == SVt_PVAV) |
602 |
encode_av (enc, (AV *)sv); |
603 |
else if (svt < SVt_PVAV) |
604 |
{ |
605 |
STRLEN len = 0; |
606 |
char *pv = svt ? SvPV (sv, len) : 0; |
607 |
|
608 |
if (len == 1 && *pv == '1') |
609 |
encode_str (enc, "true", 4, 0); |
610 |
else if (len == 1 && *pv == '0') |
611 |
encode_str (enc, "false", 5, 0); |
612 |
else |
613 |
croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
614 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
615 |
} |
616 |
else |
617 |
croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
618 |
SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
619 |
} |
620 |
|
621 |
static void |
622 |
encode_sv (enc_t *enc, SV *sv) |
623 |
{ |
624 |
SvGETMAGIC (sv); |
625 |
|
626 |
if (SvPOKp (sv)) |
627 |
{ |
628 |
STRLEN len; |
629 |
char *str = SvPV (sv, len); |
630 |
encode_ch (enc, '"'); |
631 |
encode_str (enc, str, len, SvUTF8 (sv)); |
632 |
encode_ch (enc, '"'); |
633 |
} |
634 |
else if (SvNOKp (sv)) |
635 |
{ |
636 |
// trust that perl will do the right thing w.r.t. JSON syntax. |
637 |
need (enc, NV_DIG + 32); |
638 |
Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
639 |
enc->cur += strlen (enc->cur); |
640 |
} |
641 |
else if (SvIOKp (sv)) |
642 |
{ |
643 |
// we assume we can always read an IV as a UV |
644 |
if (SvUV (sv) & ~(UV)0x7fff) |
645 |
{ |
646 |
// large integer, use the (rather slow) snprintf way. |
647 |
need (enc, sizeof (UV) * 3); |
648 |
enc->cur += |
649 |
SvIsUV(sv) |
650 |
? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
651 |
: snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
652 |
} |
653 |
else |
654 |
{ |
655 |
// optimise the "small number case" |
656 |
// code will likely be branchless and use only a single multiplication |
657 |
I32 i = SvIV (sv); |
658 |
U32 u; |
659 |
char digit, nz = 0; |
660 |
|
661 |
need (enc, 6); |
662 |
|
663 |
*enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
664 |
u = i < 0 ? -i : i; |
665 |
|
666 |
// convert to 4.28 fixed-point representation |
667 |
u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
668 |
|
669 |
// now output digit by digit, each time masking out the integer part |
670 |
// and multiplying by 5 while moving the decimal point one to the right, |
671 |
// resulting in a net multiplication by 10. |
672 |
// we always write the digit to memory but conditionally increment |
673 |
// the pointer, to ease the usage of conditional move instructions. |
674 |
digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
675 |
digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
676 |
digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
677 |
digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
678 |
digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
679 |
} |
680 |
} |
681 |
else if (SvROK (sv)) |
682 |
encode_rv (enc, SvRV (sv)); |
683 |
else if (!SvOK (sv)) |
684 |
encode_str (enc, "null", 4, 0); |
685 |
else |
686 |
croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
687 |
SvPV_nolen (sv), SvFLAGS (sv)); |
688 |
} |
689 |
|
690 |
static SV * |
691 |
encode_json (SV *scalar, JSON *json) |
692 |
{ |
693 |
enc_t enc; |
694 |
|
695 |
if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
696 |
croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
697 |
|
698 |
enc.json = *json; |
699 |
enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
700 |
enc.cur = SvPVX (enc.sv); |
701 |
enc.end = SvEND (enc.sv); |
702 |
enc.indent = 0; |
703 |
enc.maxdepth = DEC_DEPTH (enc.json.flags); |
704 |
enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
705 |
: enc.json.flags & F_LATIN1 ? 0x000100UL |
706 |
: 0x10FFFFUL; |
707 |
|
708 |
SvPOK_only (enc.sv); |
709 |
encode_sv (&enc, scalar); |
710 |
|
711 |
SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
712 |
*SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
713 |
|
714 |
if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
715 |
SvUTF8_on (enc.sv); |
716 |
|
717 |
if (enc.json.flags & F_SHRINK) |
718 |
shrink (enc.sv); |
719 |
|
720 |
return enc.sv; |
721 |
} |
722 |
|
723 |
///////////////////////////////////////////////////////////////////////////// |
724 |
// decoder |
725 |
|
726 |
// structure used for decoding JSON |
727 |
typedef struct |
728 |
{ |
729 |
char *cur; // current parser pointer |
730 |
char *end; // end of input string |
731 |
const char *err; // parse error, if != 0 |
732 |
JSON json; |
733 |
U32 depth; // recursion depth |
734 |
U32 maxdepth; // recursion depth limit |
735 |
} dec_t; |
736 |
|
737 |
INLINE void |
738 |
decode_comment (dec_t *dec) |
739 |
{ |
740 |
// only '#'-style comments allowed a.t.m. |
741 |
|
742 |
while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
743 |
++dec->cur; |
744 |
} |
745 |
|
746 |
INLINE void |
747 |
decode_ws (dec_t *dec) |
748 |
{ |
749 |
for (;;) |
750 |
{ |
751 |
char ch = *dec->cur; |
752 |
|
753 |
if (ch > 0x20) |
754 |
{ |
755 |
if (expect_false (ch == '#')) |
756 |
{ |
757 |
if (dec->json.flags & F_RELAXED) |
758 |
decode_comment (dec); |
759 |
else |
760 |
break; |
761 |
} |
762 |
else |
763 |
break; |
764 |
} |
765 |
else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
766 |
break; // parse error, but let higher level handle it, gives better error messages |
767 |
|
768 |
++dec->cur; |
769 |
} |
770 |
} |
771 |
|
772 |
#define ERR(reason) SB dec->err = reason; goto fail; SE |
773 |
|
774 |
#define EXPECT_CH(ch) SB \ |
775 |
if (*dec->cur != ch) \ |
776 |
ERR (# ch " expected"); \ |
777 |
++dec->cur; \ |
778 |
SE |
779 |
|
780 |
#define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
781 |
#define DEC_DEC_DEPTH --dec->depth |
782 |
|
783 |
static SV *decode_sv (dec_t *dec); |
784 |
|
785 |
static signed char decode_hexdigit[256]; |
786 |
|
787 |
static UV |
788 |
decode_4hex (dec_t *dec) |
789 |
{ |
790 |
signed char d1, d2, d3, d4; |
791 |
unsigned char *cur = (unsigned char *)dec->cur; |
792 |
|
793 |
d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
794 |
d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
795 |
d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
796 |
d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
797 |
|
798 |
dec->cur += 4; |
799 |
|
800 |
return ((UV)d1) << 12 |
801 |
| ((UV)d2) << 8 |
802 |
| ((UV)d3) << 4 |
803 |
| ((UV)d4); |
804 |
|
805 |
fail: |
806 |
return (UV)-1; |
807 |
} |
808 |
|
809 |
static SV * |
810 |
decode_str (dec_t *dec) |
811 |
{ |
812 |
SV *sv = 0; |
813 |
int utf8 = 0; |
814 |
char *dec_cur = dec->cur; |
815 |
|
816 |
do |
817 |
{ |
818 |
char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
819 |
char *cur = buf; |
820 |
|
821 |
do |
822 |
{ |
823 |
unsigned char ch = *(unsigned char *)dec_cur++; |
824 |
|
825 |
if (expect_false (ch == '"')) |
826 |
{ |
827 |
--dec_cur; |
828 |
break; |
829 |
} |
830 |
else if (expect_false (ch == '\\')) |
831 |
{ |
832 |
switch (*dec_cur) |
833 |
{ |
834 |
case '\\': |
835 |
case '/': |
836 |
case '"': *cur++ = *dec_cur++; break; |
837 |
|
838 |
case 'b': ++dec_cur; *cur++ = '\010'; break; |
839 |
case 't': ++dec_cur; *cur++ = '\011'; break; |
840 |
case 'n': ++dec_cur; *cur++ = '\012'; break; |
841 |
case 'f': ++dec_cur; *cur++ = '\014'; break; |
842 |
case 'r': ++dec_cur; *cur++ = '\015'; break; |
843 |
|
844 |
case 'u': |
845 |
{ |
846 |
UV lo, hi; |
847 |
++dec_cur; |
848 |
|
849 |
dec->cur = dec_cur; |
850 |
hi = decode_4hex (dec); |
851 |
dec_cur = dec->cur; |
852 |
if (hi == (UV)-1) |
853 |
goto fail; |
854 |
|
855 |
// possibly a surrogate pair |
856 |
if (hi >= 0xd800) |
857 |
if (hi < 0xdc00) |
858 |
{ |
859 |
if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
860 |
ERR ("missing low surrogate character in surrogate pair"); |
861 |
|
862 |
dec_cur += 2; |
863 |
|
864 |
dec->cur = dec_cur; |
865 |
lo = decode_4hex (dec); |
866 |
dec_cur = dec->cur; |
867 |
if (lo == (UV)-1) |
868 |
goto fail; |
869 |
|
870 |
if (lo < 0xdc00 || lo >= 0xe000) |
871 |
ERR ("surrogate pair expected"); |
872 |
|
873 |
hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
874 |
} |
875 |
else if (hi < 0xe000) |
876 |
ERR ("missing high surrogate character in surrogate pair"); |
877 |
|
878 |
if (hi >= 0x80) |
879 |
{ |
880 |
utf8 = 1; |
881 |
|
882 |
cur = encode_utf8 (cur, hi); |
883 |
} |
884 |
else |
885 |
*cur++ = hi; |
886 |
} |
887 |
break; |
888 |
|
889 |
default: |
890 |
--dec_cur; |
891 |
ERR ("illegal backslash escape sequence in string"); |
892 |
} |
893 |
} |
894 |
else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
895 |
*cur++ = ch; |
896 |
else if (ch >= 0x80) |
897 |
{ |
898 |
STRLEN clen; |
899 |
UV uch; |
900 |
|
901 |
--dec_cur; |
902 |
|
903 |
uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
904 |
if (clen == (STRLEN)-1) |
905 |
ERR ("malformed UTF-8 character in JSON string"); |
906 |
|
907 |
do |
908 |
*cur++ = *dec_cur++; |
909 |
while (--clen); |
910 |
|
911 |
utf8 = 1; |
912 |
} |
913 |
else |
914 |
{ |
915 |
--dec_cur; |
916 |
|
917 |
if (!ch) |
918 |
ERR ("unexpected end of string while parsing JSON string"); |
919 |
else |
920 |
ERR ("invalid character encountered while parsing JSON string"); |
921 |
} |
922 |
} |
923 |
while (cur < buf + SHORT_STRING_LEN); |
924 |
|
925 |
{ |
926 |
STRLEN len = cur - buf; |
927 |
|
928 |
if (sv) |
929 |
{ |
930 |
SvGROW (sv, SvCUR (sv) + len + 1); |
931 |
memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
932 |
SvCUR_set (sv, SvCUR (sv) + len); |
933 |
} |
934 |
else |
935 |
sv = newSVpvn (buf, len); |
936 |
} |
937 |
} |
938 |
while (*dec_cur != '"'); |
939 |
|
940 |
++dec_cur; |
941 |
|
942 |
if (sv) |
943 |
{ |
944 |
SvPOK_only (sv); |
945 |
*SvEND (sv) = 0; |
946 |
|
947 |
if (utf8) |
948 |
SvUTF8_on (sv); |
949 |
} |
950 |
else |
951 |
sv = newSVpvn ("", 0); |
952 |
|
953 |
dec->cur = dec_cur; |
954 |
return sv; |
955 |
|
956 |
fail: |
957 |
dec->cur = dec_cur; |
958 |
return 0; |
959 |
} |
960 |
|
961 |
static SV * |
962 |
decode_num (dec_t *dec) |
963 |
{ |
964 |
int is_nv = 0; |
965 |
char *start = dec->cur; |
966 |
|
967 |
// [minus] |
968 |
if (*dec->cur == '-') |
969 |
++dec->cur; |
970 |
|
971 |
if (*dec->cur == '0') |
972 |
{ |
973 |
++dec->cur; |
974 |
if (*dec->cur >= '0' && *dec->cur <= '9') |
975 |
ERR ("malformed number (leading zero must not be followed by another digit)"); |
976 |
} |
977 |
else if (*dec->cur < '0' || *dec->cur > '9') |
978 |
ERR ("malformed number (no digits after initial minus)"); |
979 |
else |
980 |
do |
981 |
{ |
982 |
++dec->cur; |
983 |
} |
984 |
while (*dec->cur >= '0' && *dec->cur <= '9'); |
985 |
|
986 |
// [frac] |
987 |
if (*dec->cur == '.') |
988 |
{ |
989 |
++dec->cur; |
990 |
|
991 |
if (*dec->cur < '0' || *dec->cur > '9') |
992 |
ERR ("malformed number (no digits after decimal point)"); |
993 |
|
994 |
do |
995 |
{ |
996 |
++dec->cur; |
997 |
} |
998 |
while (*dec->cur >= '0' && *dec->cur <= '9'); |
999 |
|
1000 |
is_nv = 1; |
1001 |
} |
1002 |
|
1003 |
// [exp] |
1004 |
if (*dec->cur == 'e' || *dec->cur == 'E') |
1005 |
{ |
1006 |
++dec->cur; |
1007 |
|
1008 |
if (*dec->cur == '-' || *dec->cur == '+') |
1009 |
++dec->cur; |
1010 |
|
1011 |
if (*dec->cur < '0' || *dec->cur > '9') |
1012 |
ERR ("malformed number (no digits after exp sign)"); |
1013 |
|
1014 |
do |
1015 |
{ |
1016 |
++dec->cur; |
1017 |
} |
1018 |
while (*dec->cur >= '0' && *dec->cur <= '9'); |
1019 |
|
1020 |
is_nv = 1; |
1021 |
} |
1022 |
|
1023 |
if (!is_nv) |
1024 |
{ |
1025 |
int len = dec->cur - start; |
1026 |
|
1027 |
// special case the rather common 1..5-digit-int case |
1028 |
if (*start == '-') |
1029 |
switch (len) |
1030 |
{ |
1031 |
case 2: return newSViv (-( start [1] - '0' * 1)); |
1032 |
case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1033 |
case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1034 |
case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1035 |
case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1036 |
} |
1037 |
else |
1038 |
switch (len) |
1039 |
{ |
1040 |
case 1: return newSViv ( start [0] - '0' * 1); |
1041 |
case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1042 |
case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1043 |
case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1044 |
case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1045 |
} |
1046 |
|
1047 |
{ |
1048 |
UV uv; |
1049 |
int numtype = grok_number (start, len, &uv); |
1050 |
if (numtype & IS_NUMBER_IN_UV) |
1051 |
if (numtype & IS_NUMBER_NEG) |
1052 |
{ |
1053 |
if (uv < (UV)IV_MIN) |
1054 |
return newSViv (-(IV)uv); |
1055 |
} |
1056 |
else |
1057 |
return newSVuv (uv); |
1058 |
} |
1059 |
|
1060 |
len -= *start == '-' ? 1 : 0; |
1061 |
|
1062 |
// does not fit into IV or UV, try NV |
1063 |
if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1064 |
#if defined (LDBL_DIG) |
1065 |
|| (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
1066 |
#endif |
1067 |
) |
1068 |
// fits into NV without loss of precision |
1069 |
return newSVnv (Atof (start)); |
1070 |
|
1071 |
// everything else fails, convert it to a string |
1072 |
return newSVpvn (start, dec->cur - start); |
1073 |
} |
1074 |
|
1075 |
// loss of precision here |
1076 |
return newSVnv (Atof (start)); |
1077 |
|
1078 |
fail: |
1079 |
return 0; |
1080 |
} |
1081 |
|
1082 |
static SV * |
1083 |
decode_av (dec_t *dec) |
1084 |
{ |
1085 |
AV *av = newAV (); |
1086 |
|
1087 |
DEC_INC_DEPTH; |
1088 |
decode_ws (dec); |
1089 |
|
1090 |
if (*dec->cur == ']') |
1091 |
++dec->cur; |
1092 |
else |
1093 |
for (;;) |
1094 |
{ |
1095 |
SV *value; |
1096 |
|
1097 |
value = decode_sv (dec); |
1098 |
if (!value) |
1099 |
goto fail; |
1100 |
|
1101 |
av_push (av, value); |
1102 |
|
1103 |
decode_ws (dec); |
1104 |
|
1105 |
if (*dec->cur == ']') |
1106 |
{ |
1107 |
++dec->cur; |
1108 |
break; |
1109 |
} |
1110 |
|
1111 |
if (*dec->cur != ',') |
1112 |
ERR (", or ] expected while parsing array"); |
1113 |
|
1114 |
++dec->cur; |
1115 |
|
1116 |
decode_ws (dec); |
1117 |
|
1118 |
if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
1119 |
{ |
1120 |
++dec->cur; |
1121 |
break; |
1122 |
} |
1123 |
} |
1124 |
|
1125 |
DEC_DEC_DEPTH; |
1126 |
return newRV_noinc ((SV *)av); |
1127 |
|
1128 |
fail: |
1129 |
SvREFCNT_dec (av); |
1130 |
DEC_DEC_DEPTH; |
1131 |
return 0; |
1132 |
} |
1133 |
|
1134 |
static SV * |
1135 |
decode_hv (dec_t *dec) |
1136 |
{ |
1137 |
SV *sv; |
1138 |
HV *hv = newHV (); |
1139 |
|
1140 |
DEC_INC_DEPTH; |
1141 |
decode_ws (dec); |
1142 |
|
1143 |
if (*dec->cur == '}') |
1144 |
++dec->cur; |
1145 |
else |
1146 |
for (;;) |
1147 |
{ |
1148 |
EXPECT_CH ('"'); |
1149 |
|
1150 |
// heuristic: assume that |
1151 |
// a) decode_str + hv_store_ent are abysmally slow. |
1152 |
// b) most hash keys are short, simple ascii text. |
1153 |
// => try to "fast-match" such strings to avoid |
1154 |
// the overhead of decode_str + hv_store_ent. |
1155 |
{ |
1156 |
SV *value; |
1157 |
char *p = dec->cur; |
1158 |
char *e = p + 24; // only try up to 24 bytes |
1159 |
|
1160 |
for (;;) |
1161 |
{ |
1162 |
// the >= 0x80 is true on most architectures |
1163 |
if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1164 |
{ |
1165 |
// slow path, back up and use decode_str |
1166 |
SV *key = decode_str (dec); |
1167 |
if (!key) |
1168 |
goto fail; |
1169 |
|
1170 |
decode_ws (dec); EXPECT_CH (':'); |
1171 |
|
1172 |
decode_ws (dec); |
1173 |
value = decode_sv (dec); |
1174 |
if (!value) |
1175 |
{ |
1176 |
SvREFCNT_dec (key); |
1177 |
goto fail; |
1178 |
} |
1179 |
|
1180 |
hv_store_ent (hv, key, value, 0); |
1181 |
SvREFCNT_dec (key); |
1182 |
|
1183 |
break; |
1184 |
} |
1185 |
else if (*p == '"') |
1186 |
{ |
1187 |
// fast path, got a simple key |
1188 |
char *key = dec->cur; |
1189 |
int len = p - key; |
1190 |
dec->cur = p + 1; |
1191 |
|
1192 |
decode_ws (dec); EXPECT_CH (':'); |
1193 |
|
1194 |
decode_ws (dec); |
1195 |
value = decode_sv (dec); |
1196 |
if (!value) |
1197 |
goto fail; |
1198 |
|
1199 |
hv_store (hv, key, len, value, 0); |
1200 |
|
1201 |
break; |
1202 |
} |
1203 |
|
1204 |
++p; |
1205 |
} |
1206 |
} |
1207 |
|
1208 |
decode_ws (dec); |
1209 |
|
1210 |
if (*dec->cur == '}') |
1211 |
{ |
1212 |
++dec->cur; |
1213 |
break; |
1214 |
} |
1215 |
|
1216 |
if (*dec->cur != ',') |
1217 |
ERR (", or } expected while parsing object/hash"); |
1218 |
|
1219 |
++dec->cur; |
1220 |
|
1221 |
decode_ws (dec); |
1222 |
|
1223 |
if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
1224 |
{ |
1225 |
++dec->cur; |
1226 |
break; |
1227 |
} |
1228 |
} |
1229 |
|
1230 |
DEC_DEC_DEPTH; |
1231 |
sv = newRV_noinc ((SV *)hv); |
1232 |
|
1233 |
// check filter callbacks |
1234 |
if (dec->json.flags & F_HOOK) |
1235 |
{ |
1236 |
if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1237 |
{ |
1238 |
HE *cb, *he; |
1239 |
|
1240 |
hv_iterinit (hv); |
1241 |
he = hv_iternext (hv); |
1242 |
hv_iterinit (hv); |
1243 |
|
1244 |
// the next line creates a mortal sv each time its called. |
1245 |
// might want to optimise this for common cases. |
1246 |
cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1247 |
|
1248 |
if (cb) |
1249 |
{ |
1250 |
dSP; |
1251 |
int count; |
1252 |
|
1253 |
ENTER; SAVETMPS; PUSHMARK (SP); |
1254 |
XPUSHs (HeVAL (he)); |
1255 |
|
1256 |
PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1257 |
|
1258 |
if (count == 1) |
1259 |
{ |
1260 |
sv = newSVsv (POPs); |
1261 |
FREETMPS; LEAVE; |
1262 |
return sv; |
1263 |
} |
1264 |
|
1265 |
FREETMPS; LEAVE; |
1266 |
} |
1267 |
} |
1268 |
|
1269 |
if (dec->json.cb_object) |
1270 |
{ |
1271 |
dSP; |
1272 |
int count; |
1273 |
|
1274 |
ENTER; SAVETMPS; PUSHMARK (SP); |
1275 |
XPUSHs (sv_2mortal (sv)); |
1276 |
|
1277 |
PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1278 |
|
1279 |
if (count == 1) |
1280 |
{ |
1281 |
sv = newSVsv (POPs); |
1282 |
FREETMPS; LEAVE; |
1283 |
return sv; |
1284 |
} |
1285 |
|
1286 |
SvREFCNT_inc (sv); |
1287 |
FREETMPS; LEAVE; |
1288 |
} |
1289 |
} |
1290 |
|
1291 |
return sv; |
1292 |
|
1293 |
fail: |
1294 |
SvREFCNT_dec (hv); |
1295 |
DEC_DEC_DEPTH; |
1296 |
return 0; |
1297 |
} |
1298 |
|
1299 |
static SV * |
1300 |
decode_sv (dec_t *dec) |
1301 |
{ |
1302 |
// the beauty of JSON: you need exactly one character lookahead |
1303 |
// to parse anything. |
1304 |
switch (*dec->cur) |
1305 |
{ |
1306 |
case '"': ++dec->cur; return decode_str (dec); |
1307 |
case '[': ++dec->cur; return decode_av (dec); |
1308 |
case '{': ++dec->cur; return decode_hv (dec); |
1309 |
|
1310 |
case '-': |
1311 |
case '0': case '1': case '2': case '3': case '4': |
1312 |
case '5': case '6': case '7': case '8': case '9': |
1313 |
return decode_num (dec); |
1314 |
|
1315 |
case 't': |
1316 |
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1317 |
{ |
1318 |
dec->cur += 4; |
1319 |
#if JSON_SLOW |
1320 |
json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
1321 |
#endif |
1322 |
return SvREFCNT_inc (json_true); |
1323 |
} |
1324 |
else |
1325 |
ERR ("'true' expected"); |
1326 |
|
1327 |
break; |
1328 |
|
1329 |
case 'f': |
1330 |
if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1331 |
{ |
1332 |
dec->cur += 5; |
1333 |
#if JSON_SLOW |
1334 |
json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1335 |
#endif |
1336 |
return SvREFCNT_inc (json_false); |
1337 |
} |
1338 |
else |
1339 |
ERR ("'false' expected"); |
1340 |
|
1341 |
break; |
1342 |
|
1343 |
case 'n': |
1344 |
if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
1345 |
{ |
1346 |
dec->cur += 4; |
1347 |
return newSVsv (&PL_sv_undef); |
1348 |
} |
1349 |
else |
1350 |
ERR ("'null' expected"); |
1351 |
|
1352 |
break; |
1353 |
|
1354 |
default: |
1355 |
ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1356 |
break; |
1357 |
} |
1358 |
|
1359 |
fail: |
1360 |
return 0; |
1361 |
} |
1362 |
|
1363 |
static SV * |
1364 |
decode_json (SV *string, JSON *json, UV *offset_return) |
1365 |
{ |
1366 |
dec_t dec; |
1367 |
UV offset; |
1368 |
SV *sv; |
1369 |
|
1370 |
SvGETMAGIC (string); |
1371 |
SvUPGRADE (string, SVt_PV); |
1372 |
|
1373 |
if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
1374 |
croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1375 |
(unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
1376 |
|
1377 |
if (json->flags & F_UTF8) |
1378 |
sv_utf8_downgrade (string, 0); |
1379 |
else |
1380 |
sv_utf8_upgrade (string); |
1381 |
|
1382 |
SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1383 |
|
1384 |
dec.json = *json; |
1385 |
dec.cur = SvPVX (string); |
1386 |
dec.end = SvEND (string); |
1387 |
dec.err = 0; |
1388 |
dec.depth = 0; |
1389 |
dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1390 |
|
1391 |
if (dec.json.cb_object || dec.json.cb_sk_object) |
1392 |
dec.json.flags |= F_HOOK; |
1393 |
|
1394 |
*dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1395 |
|
1396 |
decode_ws (&dec); |
1397 |
sv = decode_sv (&dec); |
1398 |
|
1399 |
if (!(offset_return || !sv)) |
1400 |
{ |
1401 |
// check for trailing garbage |
1402 |
decode_ws (&dec); |
1403 |
|
1404 |
if (*dec.cur) |
1405 |
{ |
1406 |
dec.err = "garbage after JSON object"; |
1407 |
SvREFCNT_dec (sv); |
1408 |
sv = 0; |
1409 |
} |
1410 |
} |
1411 |
|
1412 |
if (offset_return || !sv) |
1413 |
{ |
1414 |
offset = dec.json.flags & F_UTF8 |
1415 |
? dec.cur - SvPVX (string) |
1416 |
: utf8_distance (dec.cur, SvPVX (string)); |
1417 |
|
1418 |
if (offset_return) |
1419 |
*offset_return = offset; |
1420 |
} |
1421 |
|
1422 |
if (!sv) |
1423 |
{ |
1424 |
SV *uni = sv_newmortal (); |
1425 |
|
1426 |
// horrible hack to silence warning inside pv_uni_display |
1427 |
COP cop = *PL_curcop; |
1428 |
cop.cop_warnings = pWARN_NONE; |
1429 |
ENTER; |
1430 |
SAVEVPTR (PL_curcop); |
1431 |
PL_curcop = &cop; |
1432 |
pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1433 |
LEAVE; |
1434 |
|
1435 |
croak ("%s, at character offset %d [\"%s\"]", |
1436 |
dec.err, |
1437 |
(int)offset, |
1438 |
dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1439 |
} |
1440 |
|
1441 |
sv = sv_2mortal (sv); |
1442 |
|
1443 |
if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1444 |
croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1445 |
|
1446 |
return sv; |
1447 |
} |
1448 |
|
1449 |
///////////////////////////////////////////////////////////////////////////// |
1450 |
// XS interface functions |
1451 |
|
1452 |
MODULE = JSON::XS PACKAGE = JSON::XS |
1453 |
|
1454 |
BOOT: |
1455 |
{ |
1456 |
int i; |
1457 |
|
1458 |
for (i = 0; i < 256; ++i) |
1459 |
decode_hexdigit [i] = |
1460 |
i >= '0' && i <= '9' ? i - '0' |
1461 |
: i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1462 |
: i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1463 |
: -1; |
1464 |
|
1465 |
json_stash = gv_stashpv ("JSON::XS" , 1); |
1466 |
json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1467 |
|
1468 |
json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
1469 |
json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1470 |
} |
1471 |
|
1472 |
PROTOTYPES: DISABLE |
1473 |
|
1474 |
void CLONE (...) |
1475 |
CODE: |
1476 |
json_stash = 0; |
1477 |
json_boolean_stash = 0; |
1478 |
|
1479 |
void new (char *klass) |
1480 |
PPCODE: |
1481 |
{ |
1482 |
SV *pv = NEWSV (0, sizeof (JSON)); |
1483 |
SvPOK_only (pv); |
1484 |
Zero (SvPVX (pv), 1, JSON); |
1485 |
((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1486 |
XPUSHs (sv_2mortal (sv_bless ( |
1487 |
newRV_noinc (pv), |
1488 |
strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1489 |
))); |
1490 |
} |
1491 |
|
1492 |
void ascii (JSON *self, int enable = 1) |
1493 |
ALIAS: |
1494 |
ascii = F_ASCII |
1495 |
latin1 = F_LATIN1 |
1496 |
utf8 = F_UTF8 |
1497 |
indent = F_INDENT |
1498 |
canonical = F_CANONICAL |
1499 |
space_before = F_SPACE_BEFORE |
1500 |
space_after = F_SPACE_AFTER |
1501 |
pretty = F_PRETTY |
1502 |
allow_nonref = F_ALLOW_NONREF |
1503 |
shrink = F_SHRINK |
1504 |
allow_blessed = F_ALLOW_BLESSED |
1505 |
convert_blessed = F_CONV_BLESSED |
1506 |
relaxed = F_RELAXED |
1507 |
PPCODE: |
1508 |
{ |
1509 |
if (enable) |
1510 |
self->flags |= ix; |
1511 |
else |
1512 |
self->flags &= ~ix; |
1513 |
|
1514 |
XPUSHs (ST (0)); |
1515 |
} |
1516 |
|
1517 |
void get_ascii (JSON *self) |
1518 |
ALIAS: |
1519 |
get_ascii = F_ASCII |
1520 |
get_latin1 = F_LATIN1 |
1521 |
get_utf8 = F_UTF8 |
1522 |
get_indent = F_INDENT |
1523 |
get_canonical = F_CANONICAL |
1524 |
get_space_before = F_SPACE_BEFORE |
1525 |
get_space_after = F_SPACE_AFTER |
1526 |
get_allow_nonref = F_ALLOW_NONREF |
1527 |
get_shrink = F_SHRINK |
1528 |
get_allow_blessed = F_ALLOW_BLESSED |
1529 |
get_convert_blessed = F_CONV_BLESSED |
1530 |
get_relaxed = F_RELAXED |
1531 |
PPCODE: |
1532 |
XPUSHs (boolSV (self->flags & ix)); |
1533 |
|
1534 |
void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1535 |
PPCODE: |
1536 |
{ |
1537 |
UV log2 = 0; |
1538 |
|
1539 |
if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1540 |
|
1541 |
while ((1UL << log2) < max_depth) |
1542 |
++log2; |
1543 |
|
1544 |
self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1545 |
|
1546 |
XPUSHs (ST (0)); |
1547 |
} |
1548 |
|
1549 |
U32 get_max_depth (JSON *self) |
1550 |
CODE: |
1551 |
RETVAL = DEC_DEPTH (self->flags); |
1552 |
OUTPUT: |
1553 |
RETVAL |
1554 |
|
1555 |
void max_size (JSON *self, UV max_size = 0) |
1556 |
PPCODE: |
1557 |
{ |
1558 |
UV log2 = 0; |
1559 |
|
1560 |
if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1561 |
if (max_size == 1) max_size = 2; |
1562 |
|
1563 |
while ((1UL << log2) < max_size) |
1564 |
++log2; |
1565 |
|
1566 |
self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1567 |
|
1568 |
XPUSHs (ST (0)); |
1569 |
} |
1570 |
|
1571 |
int get_max_size (JSON *self) |
1572 |
CODE: |
1573 |
RETVAL = DEC_SIZE (self->flags); |
1574 |
OUTPUT: |
1575 |
RETVAL |
1576 |
|
1577 |
void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1578 |
PPCODE: |
1579 |
{ |
1580 |
SvREFCNT_dec (self->cb_object); |
1581 |
self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1582 |
|
1583 |
XPUSHs (ST (0)); |
1584 |
} |
1585 |
|
1586 |
void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1587 |
PPCODE: |
1588 |
{ |
1589 |
if (!self->cb_sk_object) |
1590 |
self->cb_sk_object = newHV (); |
1591 |
|
1592 |
if (SvOK (cb)) |
1593 |
hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1594 |
else |
1595 |
{ |
1596 |
hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
1597 |
|
1598 |
if (!HvKEYS (self->cb_sk_object)) |
1599 |
{ |
1600 |
SvREFCNT_dec (self->cb_sk_object); |
1601 |
self->cb_sk_object = 0; |
1602 |
} |
1603 |
} |
1604 |
|
1605 |
XPUSHs (ST (0)); |
1606 |
} |
1607 |
|
1608 |
void encode (JSON *self, SV *scalar) |
1609 |
PPCODE: |
1610 |
XPUSHs (encode_json (scalar, self)); |
1611 |
|
1612 |
void decode (JSON *self, SV *jsonstr) |
1613 |
PPCODE: |
1614 |
XPUSHs (decode_json (jsonstr, self, 0)); |
1615 |
|
1616 |
void decode_prefix (JSON *self, SV *jsonstr) |
1617 |
PPCODE: |
1618 |
{ |
1619 |
UV offset; |
1620 |
EXTEND (SP, 2); |
1621 |
PUSHs (decode_json (jsonstr, self, &offset)); |
1622 |
PUSHs (sv_2mortal (newSVuv (offset))); |
1623 |
} |
1624 |
|
1625 |
void DESTROY (JSON *self) |
1626 |
CODE: |
1627 |
SvREFCNT_dec (self->cb_sk_object); |
1628 |
SvREFCNT_dec (self->cb_object); |
1629 |
|
1630 |
PROTOTYPES: ENABLE |
1631 |
|
1632 |
void encode_json (SV *scalar) |
1633 |
PPCODE: |
1634 |
{ |
1635 |
JSON json = { F_DEFAULT | F_UTF8 }; |
1636 |
XPUSHs (encode_json (scalar, &json)); |
1637 |
} |
1638 |
|
1639 |
void decode_json (SV *jsonstr) |
1640 |
PPCODE: |
1641 |
{ |
1642 |
JSON json = { F_DEFAULT | F_UTF8 }; |
1643 |
XPUSHs (decode_json (jsonstr, &json, 0)); |
1644 |
} |
1645 |
|