ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
Revision: 1.23
Committed: Tue Apr 3 23:34:17 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.22: +13 -8 lines
Log Message:
*** empty log message ***

File Contents

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