… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
22 | #define F_SHRINK 0x00000200UL |
|
|
23 | #define F_MAXDEPTH 0xf8000000UL |
|
|
24 | #define S_MAXDEPTH 27 |
|
|
25 | |
|
|
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
27 | |
|
|
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
30 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
33 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
35 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
36 | |
|
|
37 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
22 | |
38 | |
23 | #define SB do { |
39 | #define SB do { |
24 | #define SE } while (0) |
40 | #define SE } while (0) |
25 | |
41 | |
|
|
42 | #if __GNUC__ >= 3 |
|
|
43 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
44 | # define inline inline |
|
|
45 | #else |
|
|
46 | # define expect(expr,value) (expr) |
|
|
47 | # define inline static |
|
|
48 | #endif |
|
|
49 | |
|
|
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
52 | |
26 | static HV *json_stash; |
53 | static HV *json_stash; // JSON::XS:: |
|
|
54 | |
|
|
55 | ///////////////////////////////////////////////////////////////////////////// |
|
|
56 | // utility functions |
|
|
57 | |
|
|
58 | static UV * |
|
|
59 | SvJSON (SV *sv) |
|
|
60 | { |
|
|
61 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
62 | croak ("object is not of type JSON::XS"); |
|
|
63 | |
|
|
64 | return &SvUVX (SvRV (sv)); |
|
|
65 | } |
|
|
66 | |
|
|
67 | static void |
|
|
68 | shrink (SV *sv) |
|
|
69 | { |
|
|
70 | sv_utf8_downgrade (sv, 1); |
|
|
71 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
72 | { |
|
|
73 | #ifdef SvPV_shrink_to_cur |
|
|
74 | SvPV_shrink_to_cur (sv); |
|
|
75 | #elif defined (SvPV_renew) |
|
|
76 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
77 | #endif |
|
|
78 | } |
|
|
79 | } |
|
|
80 | |
|
|
81 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
82 | // case of an error. |
|
|
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
84 | // but use the very good perl function to parse anything else. |
|
|
85 | // note that we never call this function for a ascii codepoints |
|
|
86 | inline UV |
|
|
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
88 | { |
|
|
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
|
|
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
92 | { |
|
|
93 | *clen = 2; |
|
|
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
95 | } |
|
|
96 | else |
|
|
97 | { |
|
|
98 | *clen = (STRLEN)-1; |
|
|
99 | return (UV)-1; |
|
|
100 | } |
|
|
101 | } |
|
|
102 | |
|
|
103 | ///////////////////////////////////////////////////////////////////////////// |
|
|
104 | // encoder |
27 | |
105 | |
28 | // structure used for encoding JSON |
106 | // structure used for encoding JSON |
29 | typedef struct |
107 | typedef struct |
30 | { |
108 | { |
31 | char *cur; |
109 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
110 | char *end; // SvEND (sv) |
34 | SV *sv; |
111 | SV *sv; // result scalar |
35 | UV flags; |
112 | U32 flags; // F_* |
36 | int max_recurse; |
113 | U32 indent; // indentation level |
37 | int indent; |
114 | U32 maxdepth; // max. indentation/recursion level |
38 | } enc_t; |
115 | } enc_t; |
39 | |
116 | |
40 | // structure used for decoding JSON |
117 | inline void |
41 | typedef struct |
|
|
42 | { |
|
|
43 | char *cur; |
|
|
44 | char *end; |
|
|
45 | const char *err; |
|
|
46 | UV flags; |
|
|
47 | } dec_t; |
|
|
48 | |
|
|
49 | static UV * |
|
|
50 | SvJSON (SV *sv) |
|
|
51 | { |
|
|
52 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
53 | croak ("object is not of type JSON::XS"); |
|
|
54 | |
|
|
55 | return &SvUVX (SvRV (sv)); |
|
|
56 | } |
|
|
57 | |
|
|
58 | static void |
|
|
59 | shrink (SV *sv) |
|
|
60 | { |
|
|
61 | sv_utf8_downgrade (sv, 1); |
|
|
62 | #ifdef SvPV_shrink_to_cur |
|
|
63 | SvPV_shrink_to_cur (sv); |
|
|
64 | #endif |
|
|
65 | } |
|
|
66 | |
|
|
67 | ///////////////////////////////////////////////////////////////////////////// |
|
|
68 | |
|
|
69 | static void |
|
|
70 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
71 | { |
119 | { |
72 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
73 | { |
121 | { |
74 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
75 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
76 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
77 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
78 | } |
126 | } |
79 | } |
127 | } |
80 | |
128 | |
81 | static void |
129 | inline void |
82 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
83 | { |
131 | { |
84 | need (enc, 1); |
132 | need (enc, 1); |
85 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
86 | } |
134 | } |
… | |
… | |
94 | |
142 | |
95 | while (str < end) |
143 | while (str < end) |
96 | { |
144 | { |
97 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
98 | |
146 | |
99 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
100 | { |
148 | { |
101 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
102 | { |
150 | { |
103 | need (enc, len += 1); |
151 | need (enc, len += 1); |
104 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
105 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
106 | } |
154 | } |
107 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
108 | { |
156 | { |
109 | need (enc, len += 1); |
157 | need (enc, len += 1); |
110 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
111 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
112 | } |
160 | } |
… | |
… | |
130 | STRLEN clen; |
178 | STRLEN clen; |
131 | UV uch; |
179 | UV uch; |
132 | |
180 | |
133 | if (is_utf8) |
181 | if (is_utf8) |
134 | { |
182 | { |
135 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
136 | if (clen == (STRLEN)-1) |
185 | if (clen == (STRLEN)-1) |
137 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
138 | } |
187 | } |
139 | else |
188 | else |
140 | { |
189 | { |
… | |
… | |
143 | } |
192 | } |
144 | |
193 | |
145 | if (uch > 0x10FFFFUL) |
194 | if (uch > 0x10FFFFUL) |
146 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
195 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
147 | |
196 | |
148 | if (uch < 0x80 || enc->flags & F_ASCII) |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
149 | { |
198 | { |
150 | if (uch > 0xFFFFUL) |
199 | if (uch > 0xFFFFUL) |
151 | { |
200 | { |
152 | need (enc, len += 11); |
201 | need (enc, len += 11); |
153 | sprintf (enc->cur, "\\u%04x\\u%04x", |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
167 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
168 | } |
217 | } |
169 | |
218 | |
170 | str += clen; |
219 | str += clen; |
171 | } |
220 | } |
|
|
221 | else if (enc->flags & F_LATIN1) |
|
|
222 | { |
|
|
223 | *enc->cur++ = uch; |
|
|
224 | str += clen; |
|
|
225 | } |
172 | else if (is_utf8) |
226 | else if (is_utf8) |
173 | { |
227 | { |
174 | need (enc, len += clen); |
228 | need (enc, len += clen); |
175 | do |
229 | do |
176 | { |
230 | { |
… | |
… | |
178 | } |
232 | } |
179 | while (--clen); |
233 | while (--clen); |
180 | } |
234 | } |
181 | else |
235 | else |
182 | { |
236 | { |
183 | need (enc, len += 10); // never more than 11 bytes needed |
237 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
184 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
238 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
185 | ++str; |
239 | ++str; |
186 | } |
240 | } |
187 | } |
241 | } |
188 | } |
242 | } |
… | |
… | |
190 | |
244 | |
191 | --len; |
245 | --len; |
192 | } |
246 | } |
193 | } |
247 | } |
194 | |
248 | |
195 | #define INDENT SB \ |
249 | inline void |
|
|
250 | encode_indent (enc_t *enc) |
|
|
251 | { |
196 | if (enc->flags & F_INDENT) \ |
252 | if (enc->flags & F_INDENT) |
197 | { \ |
253 | { |
198 | int i_; \ |
254 | int spaces = enc->indent * INDENT_STEP; |
199 | need (enc, enc->indent); \ |
|
|
200 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
201 | encode_ch (enc, ' '); \ |
|
|
202 | } \ |
|
|
203 | SE |
|
|
204 | |
255 | |
205 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
256 | need (enc, spaces); |
206 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
257 | memset (enc->cur, ' ', spaces); |
207 | #define COMMA SB \ |
258 | enc->cur += spaces; |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
|
|
262 | inline void |
|
|
263 | encode_space (enc_t *enc) |
|
|
264 | { |
|
|
265 | need (enc, 1); |
208 | encode_ch (enc, ','); \ |
266 | encode_ch (enc, ' '); |
|
|
267 | } |
|
|
268 | |
|
|
269 | inline void |
|
|
270 | encode_nl (enc_t *enc) |
|
|
271 | { |
209 | if (enc->flags & F_INDENT) \ |
272 | if (enc->flags & F_INDENT) |
210 | NL; \ |
273 | { |
|
|
274 | need (enc, 1); |
|
|
275 | encode_ch (enc, '\n'); |
|
|
276 | } |
|
|
277 | } |
|
|
278 | |
|
|
279 | inline void |
|
|
280 | encode_comma (enc_t *enc) |
|
|
281 | { |
|
|
282 | encode_ch (enc, ','); |
|
|
283 | |
|
|
284 | if (enc->flags & F_INDENT) |
|
|
285 | encode_nl (enc); |
211 | else if (enc->flags & F_SPACE_AFTER) \ |
286 | else if (enc->flags & F_SPACE_AFTER) |
212 | SPACE; \ |
287 | encode_space (enc); |
213 | SE |
288 | } |
214 | |
289 | |
215 | static void encode_sv (enc_t *enc, SV *sv); |
290 | static void encode_sv (enc_t *enc, SV *sv); |
216 | |
291 | |
217 | static void |
292 | static void |
218 | encode_av (enc_t *enc, AV *av) |
293 | encode_av (enc_t *enc, AV *av) |
219 | { |
294 | { |
220 | int i, len = av_len (av); |
295 | int i, len = av_len (av); |
221 | |
296 | |
222 | encode_ch (enc, '['); NL; |
297 | if (enc->indent >= enc->maxdepth) |
|
|
298 | croak ("data structure too deep (hit recursion limit)"); |
|
|
299 | |
|
|
300 | encode_ch (enc, '['); encode_nl (enc); |
223 | ++enc->indent; |
301 | ++enc->indent; |
224 | |
302 | |
225 | for (i = 0; i <= len; ++i) |
303 | for (i = 0; i <= len; ++i) |
226 | { |
304 | { |
227 | INDENT; |
305 | encode_indent (enc); |
228 | encode_sv (enc, *av_fetch (av, i, 0)); |
306 | encode_sv (enc, *av_fetch (av, i, 0)); |
229 | |
307 | |
230 | if (i < len) |
308 | if (i < len) |
231 | COMMA; |
309 | encode_comma (enc); |
232 | } |
310 | } |
233 | |
311 | |
234 | NL; |
312 | encode_nl (enc); |
235 | |
313 | |
236 | --enc->indent; |
314 | --enc->indent; |
237 | INDENT; encode_ch (enc, ']'); |
315 | encode_indent (enc); encode_ch (enc, ']'); |
238 | } |
316 | } |
239 | |
317 | |
240 | static void |
318 | static void |
241 | encode_he (enc_t *enc, HE *he) |
319 | encode_he (enc_t *enc, HE *he) |
242 | { |
320 | { |
… | |
… | |
256 | else |
334 | else |
257 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
335 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
258 | |
336 | |
259 | encode_ch (enc, '"'); |
337 | encode_ch (enc, '"'); |
260 | |
338 | |
261 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
339 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
262 | encode_ch (enc, ':'); |
340 | encode_ch (enc, ':'); |
263 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
341 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
264 | encode_sv (enc, HeVAL (he)); |
342 | encode_sv (enc, HeVAL (he)); |
265 | } |
343 | } |
266 | |
344 | |
267 | // compare hash entries, used when all keys are bytestrings |
345 | // compare hash entries, used when all keys are bytestrings |
268 | static int |
346 | static int |
… | |
… | |
274 | HE *b = *(HE **)b_; |
352 | HE *b = *(HE **)b_; |
275 | |
353 | |
276 | STRLEN la = HeKLEN (a); |
354 | STRLEN la = HeKLEN (a); |
277 | STRLEN lb = HeKLEN (b); |
355 | STRLEN lb = HeKLEN (b); |
278 | |
356 | |
279 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
357 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
280 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
358 | cmp = la - lb; |
281 | |
359 | |
282 | return cmp; |
360 | return cmp; |
283 | } |
361 | } |
284 | |
362 | |
285 | // compare hash entries, used when some keys are sv's or utf-x |
363 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
292 | static void |
370 | static void |
293 | encode_hv (enc_t *enc, HV *hv) |
371 | encode_hv (enc_t *enc, HV *hv) |
294 | { |
372 | { |
295 | int count, i; |
373 | int count, i; |
296 | |
374 | |
|
|
375 | if (enc->indent >= enc->maxdepth) |
|
|
376 | croak ("data structure too deep (hit recursion limit)"); |
|
|
377 | |
297 | encode_ch (enc, '{'); NL; ++enc->indent; |
378 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
298 | |
379 | |
299 | if ((count = hv_iterinit (hv))) |
380 | if ((count = hv_iterinit (hv))) |
300 | { |
381 | { |
301 | // for canonical output we have to sort by keys first |
382 | // for canonical output we have to sort by keys first |
302 | // actually, this is mostly due to the stupid so-called |
383 | // actually, this is mostly due to the stupid so-called |
303 | // security workaround added somewhere in 5.8.x. |
384 | // security workaround added somewhere in 5.8.x. |
304 | // that randomises hash orderings |
385 | // that randomises hash orderings |
305 | if (enc->flags & F_CANONICAL) |
386 | if (enc->flags & F_CANONICAL) |
306 | { |
387 | { |
307 | HE *he, *hes [count]; |
|
|
308 | int fast = 1; |
388 | int fast = 1; |
|
|
389 | HE *he; |
|
|
390 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
391 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
392 | #else |
|
|
393 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
394 | #endif |
309 | |
395 | |
310 | i = 0; |
396 | i = 0; |
311 | while ((he = hv_iternext (hv))) |
397 | while ((he = hv_iternext (hv))) |
312 | { |
398 | { |
313 | hes [i++] = he; |
399 | hes [i++] = he; |
… | |
… | |
337 | LEAVE; |
423 | LEAVE; |
338 | } |
424 | } |
339 | |
425 | |
340 | for (i = 0; i < count; ++i) |
426 | for (i = 0; i < count; ++i) |
341 | { |
427 | { |
342 | INDENT; |
428 | encode_indent (enc); |
343 | encode_he (enc, hes [i]); |
429 | encode_he (enc, hes [i]); |
344 | |
430 | |
345 | if (i < count - 1) |
431 | if (i < count - 1) |
346 | COMMA; |
432 | encode_comma (enc); |
347 | } |
433 | } |
348 | |
434 | |
349 | NL; |
435 | encode_nl (enc); |
350 | } |
436 | } |
351 | else |
437 | else |
352 | { |
438 | { |
353 | SV *sv; |
|
|
354 | HE *he = hv_iternext (hv); |
439 | HE *he = hv_iternext (hv); |
355 | |
440 | |
356 | for (;;) |
441 | for (;;) |
357 | { |
442 | { |
358 | INDENT; |
443 | encode_indent (enc); |
359 | encode_he (enc, he); |
444 | encode_he (enc, he); |
360 | |
445 | |
361 | if (!(he = hv_iternext (hv))) |
446 | if (!(he = hv_iternext (hv))) |
362 | break; |
447 | break; |
363 | |
448 | |
364 | COMMA; |
449 | encode_comma (enc); |
365 | } |
450 | } |
366 | |
451 | |
367 | NL; |
452 | encode_nl (enc); |
368 | } |
453 | } |
369 | } |
454 | } |
370 | |
455 | |
371 | --enc->indent; INDENT; encode_ch (enc, '}'); |
456 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
457 | } |
|
|
458 | |
|
|
459 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
460 | static void |
|
|
461 | encode_rv (enc_t *enc, SV *sv) |
|
|
462 | { |
|
|
463 | svtype svt; |
|
|
464 | |
|
|
465 | SvGETMAGIC (sv); |
|
|
466 | svt = SvTYPE (sv); |
|
|
467 | |
|
|
468 | if (svt == SVt_PVHV) |
|
|
469 | encode_hv (enc, (HV *)sv); |
|
|
470 | else if (svt == SVt_PVAV) |
|
|
471 | encode_av (enc, (AV *)sv); |
|
|
472 | else if (svt < SVt_PVAV) |
|
|
473 | { |
|
|
474 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
475 | encode_str (enc, "false", 5, 0); |
|
|
476 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
477 | encode_str (enc, "true", 4, 0); |
|
|
478 | else |
|
|
479 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
480 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
481 | } |
|
|
482 | else |
|
|
483 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
484 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
372 | } |
485 | } |
373 | |
486 | |
374 | static void |
487 | static void |
375 | encode_sv (enc_t *enc, SV *sv) |
488 | encode_sv (enc_t *enc, SV *sv) |
376 | { |
489 | { |
… | |
… | |
384 | encode_str (enc, str, len, SvUTF8 (sv)); |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
385 | encode_ch (enc, '"'); |
498 | encode_ch (enc, '"'); |
386 | } |
499 | } |
387 | else if (SvNOKp (sv)) |
500 | else if (SvNOKp (sv)) |
388 | { |
501 | { |
|
|
502 | // trust that perl will do the right thing w.r.t. JSON syntax. |
389 | need (enc, NV_DIG + 32); |
503 | need (enc, NV_DIG + 32); |
390 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
391 | enc->cur += strlen (enc->cur); |
505 | enc->cur += strlen (enc->cur); |
392 | } |
506 | } |
393 | else if (SvIOKp (sv)) |
507 | else if (SvIOKp (sv)) |
394 | { |
508 | { |
395 | need (enc, 64); |
509 | // we assume we can always read an IV as a UV |
|
|
510 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
511 | { |
|
|
512 | // large integer, use the (rather slow) snprintf way. |
|
|
513 | need (enc, sizeof (UV) * 3); |
396 | enc->cur += |
514 | enc->cur += |
397 | SvIsUV(sv) |
515 | SvIsUV(sv) |
398 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
516 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
399 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
517 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
518 | } |
|
|
519 | else |
|
|
520 | { |
|
|
521 | // optimise the "small number case" |
|
|
522 | // code will likely be branchless and use only a single multiplication |
|
|
523 | I32 i = SvIV (sv); |
|
|
524 | U32 u; |
|
|
525 | char digit, nz = 0; |
|
|
526 | |
|
|
527 | need (enc, 6); |
|
|
528 | |
|
|
529 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
530 | u = i < 0 ? -i : i; |
|
|
531 | |
|
|
532 | // convert to 4.28 fixed-point representation |
|
|
533 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
534 | |
|
|
535 | // now output digit by digit, each time masking out the integer part |
|
|
536 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
537 | // resulting in a net multiplication by 10. |
|
|
538 | // we always write the digit to memory but conditionally increment |
|
|
539 | // the pointer, to ease the usage of conditional move instructions. |
|
|
540 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
541 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
542 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
543 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
544 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
545 | } |
400 | } |
546 | } |
401 | else if (SvROK (sv)) |
547 | else if (SvROK (sv)) |
402 | { |
548 | encode_rv (enc, SvRV (sv)); |
403 | SV *rv = SvRV (sv); |
|
|
404 | |
|
|
405 | if (!--enc->max_recurse) |
|
|
406 | croak ("data structure too deep (hit recursion limit)"); |
|
|
407 | |
|
|
408 | switch (SvTYPE (rv)) |
|
|
409 | { |
|
|
410 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
411 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
412 | |
|
|
413 | default: |
|
|
414 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
415 | SvPV_nolen (sv)); |
|
|
416 | } |
|
|
417 | } |
|
|
418 | else if (!SvOK (sv)) |
549 | else if (!SvOK (sv)) |
419 | encode_str (enc, "null", 4, 0); |
550 | encode_str (enc, "null", 4, 0); |
420 | else |
551 | else |
421 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
552 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
422 | SvPV_nolen (sv), SvFLAGS (sv)); |
553 | SvPV_nolen (sv), SvFLAGS (sv)); |
423 | } |
554 | } |
424 | |
555 | |
425 | static SV * |
556 | static SV * |
426 | encode_json (SV *scalar, UV flags) |
557 | encode_json (SV *scalar, U32 flags) |
427 | { |
558 | { |
|
|
559 | enc_t enc; |
|
|
560 | |
428 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
561 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
562 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
430 | |
563 | |
431 | enc_t enc; |
|
|
432 | enc.flags = flags; |
564 | enc.flags = flags; |
433 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
565 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.cur = SvPVX (enc.sv); |
566 | enc.cur = SvPVX (enc.sv); |
435 | enc.end = SvEND (enc.sv); |
567 | enc.end = SvEND (enc.sv); |
436 | enc.max_recurse = 0; |
|
|
437 | enc.indent = 0; |
568 | enc.indent = 0; |
|
|
569 | enc.maxdepth = DEC_DEPTH (flags); |
438 | |
570 | |
439 | SvPOK_only (enc.sv); |
571 | SvPOK_only (enc.sv); |
440 | encode_sv (&enc, scalar); |
572 | encode_sv (&enc, scalar); |
441 | |
573 | |
|
|
574 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
575 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
576 | |
442 | if (!(flags & (F_ASCII | F_UTF8))) |
577 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
443 | SvUTF8_on (enc.sv); |
578 | SvUTF8_on (enc.sv); |
444 | |
|
|
445 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
446 | |
579 | |
447 | if (enc.flags & F_SHRINK) |
580 | if (enc.flags & F_SHRINK) |
448 | shrink (enc.sv); |
581 | shrink (enc.sv); |
449 | |
582 | |
450 | return enc.sv; |
583 | return enc.sv; |
451 | } |
584 | } |
452 | |
585 | |
453 | ///////////////////////////////////////////////////////////////////////////// |
586 | ///////////////////////////////////////////////////////////////////////////// |
|
|
587 | // decoder |
454 | |
588 | |
455 | #define WS \ |
589 | // structure used for decoding JSON |
|
|
590 | typedef struct |
|
|
591 | { |
|
|
592 | char *cur; // current parser pointer |
|
|
593 | char *end; // end of input string |
|
|
594 | const char *err; // parse error, if != 0 |
|
|
595 | U32 flags; // F_* |
|
|
596 | U32 depth; // recursion depth |
|
|
597 | U32 maxdepth; // recursion depth limit |
|
|
598 | } dec_t; |
|
|
599 | |
|
|
600 | inline void |
|
|
601 | decode_ws (dec_t *dec) |
|
|
602 | { |
456 | for (;;) \ |
603 | for (;;) |
457 | { \ |
604 | { |
458 | char ch = *dec->cur; \ |
605 | char ch = *dec->cur; |
|
|
606 | |
459 | if (ch > 0x20 \ |
607 | if (ch > 0x20 |
460 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
608 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
461 | break; \ |
609 | break; |
|
|
610 | |
462 | ++dec->cur; \ |
611 | ++dec->cur; |
463 | } |
612 | } |
|
|
613 | } |
464 | |
614 | |
465 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
615 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
616 | |
466 | #define EXPECT_CH(ch) SB \ |
617 | #define EXPECT_CH(ch) SB \ |
467 | if (*dec->cur != ch) \ |
618 | if (*dec->cur != ch) \ |
468 | ERR (# ch " expected"); \ |
619 | ERR (# ch " expected"); \ |
469 | ++dec->cur; \ |
620 | ++dec->cur; \ |
470 | SE |
621 | SE |
471 | |
622 | |
|
|
623 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
624 | #define DEC_DEC_DEPTH --dec->depth |
|
|
625 | |
472 | static SV *decode_sv (dec_t *dec); |
626 | static SV *decode_sv (dec_t *dec); |
473 | |
627 | |
474 | static signed char decode_hexdigit[256]; |
628 | static signed char decode_hexdigit[256]; |
475 | |
629 | |
476 | static UV |
630 | static UV |
477 | decode_4hex (dec_t *dec) |
631 | decode_4hex (dec_t *dec) |
478 | { |
632 | { |
479 | signed char d1, d2, d3, d4; |
633 | signed char d1, d2, d3, d4; |
|
|
634 | unsigned char *cur = (unsigned char *)dec->cur; |
480 | |
635 | |
481 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
636 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
482 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
637 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
483 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
638 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
484 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
639 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
485 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
486 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
|
|
487 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
488 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
|
|
489 | |
640 | |
490 | dec->cur += 4; |
641 | dec->cur += 4; |
491 | |
642 | |
492 | return ((UV)d1) << 12 |
643 | return ((UV)d1) << 12 |
493 | | ((UV)d2) << 8 |
644 | | ((UV)d2) << 8 |
… | |
… | |
496 | |
647 | |
497 | fail: |
648 | fail: |
498 | return (UV)-1; |
649 | return (UV)-1; |
499 | } |
650 | } |
500 | |
651 | |
501 | #define APPEND_GROW(n) SB \ |
|
|
502 | if (cur + (n) >= end) \ |
|
|
503 | { \ |
|
|
504 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
505 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
506 | cur = SvPVX (sv) + ofs; \ |
|
|
507 | end = SvEND (sv); \ |
|
|
508 | } \ |
|
|
509 | SE |
|
|
510 | |
|
|
511 | #define APPEND_CH(ch) SB \ |
|
|
512 | APPEND_GROW (1); \ |
|
|
513 | *cur++ = (ch); \ |
|
|
514 | SE |
|
|
515 | |
|
|
516 | static SV * |
652 | static SV * |
517 | decode_str (dec_t *dec) |
653 | decode_str (dec_t *dec) |
518 | { |
654 | { |
519 | SV *sv = NEWSV (0,2); |
655 | SV *sv = 0; |
520 | int utf8 = 0; |
656 | int utf8 = 0; |
521 | char *cur = SvPVX (sv); |
657 | char *dec_cur = dec->cur; |
522 | char *end = SvEND (sv); |
|
|
523 | |
658 | |
524 | for (;;) |
659 | do |
525 | { |
660 | { |
526 | unsigned char ch = *(unsigned char *)dec->cur; |
661 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
662 | char *cur = buf; |
527 | |
663 | |
528 | if (ch == '"') |
664 | do |
529 | break; |
|
|
530 | else if (ch == '\\') |
|
|
531 | { |
665 | { |
532 | switch (*++dec->cur) |
666 | unsigned char ch = *(unsigned char *)dec_cur++; |
|
|
667 | |
|
|
668 | if (expect_false (ch == '"')) |
533 | { |
669 | { |
534 | case '\\': |
670 | --dec_cur; |
535 | case '/': |
671 | break; |
536 | case '"': APPEND_CH (*dec->cur++); break; |
672 | } |
537 | |
673 | else if (expect_false (ch == '\\')) |
538 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
674 | { |
539 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
675 | switch (*dec_cur) |
540 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
541 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
542 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
543 | |
|
|
544 | case 'u': |
|
|
545 | { |
676 | { |
546 | UV lo, hi; |
677 | case '\\': |
547 | ++dec->cur; |
678 | case '/': |
|
|
679 | case '"': *cur++ = *dec_cur++; break; |
548 | |
680 | |
549 | hi = decode_4hex (dec); |
681 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
550 | if (hi == (UV)-1) |
682 | case 't': ++dec_cur; *cur++ = '\011'; break; |
551 | goto fail; |
683 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
|
|
684 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
|
|
685 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
552 | |
686 | |
553 | // possibly a surrogate pair |
687 | case 'u': |
554 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
555 | { |
688 | { |
556 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
689 | UV lo, hi; |
557 | ERR ("missing low surrogate character in surrogate pair"); |
690 | ++dec_cur; |
558 | |
691 | |
559 | dec->cur += 2; |
692 | dec->cur = dec_cur; |
560 | |
|
|
561 | lo = decode_4hex (dec); |
693 | hi = decode_4hex (dec); |
|
|
694 | dec_cur = dec->cur; |
562 | if (lo == (UV)-1) |
695 | if (hi == (UV)-1) |
563 | goto fail; |
696 | goto fail; |
564 | |
697 | |
|
|
698 | // possibly a surrogate pair |
|
|
699 | if (hi >= 0xd800) |
|
|
700 | if (hi < 0xdc00) |
|
|
701 | { |
|
|
702 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
|
|
703 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
704 | |
|
|
705 | dec_cur += 2; |
|
|
706 | |
|
|
707 | dec->cur = dec_cur; |
|
|
708 | lo = decode_4hex (dec); |
|
|
709 | dec_cur = dec->cur; |
|
|
710 | if (lo == (UV)-1) |
|
|
711 | goto fail; |
|
|
712 | |
565 | if (lo < 0xdc00 || lo >= 0xe000) |
713 | if (lo < 0xdc00 || lo >= 0xe000) |
566 | ERR ("surrogate pair expected"); |
714 | ERR ("surrogate pair expected"); |
567 | |
715 | |
568 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
716 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
717 | } |
|
|
718 | else if (hi < 0xe000) |
|
|
719 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
720 | |
|
|
721 | if (hi >= 0x80) |
|
|
722 | { |
|
|
723 | utf8 = 1; |
|
|
724 | |
|
|
725 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
726 | } |
|
|
727 | else |
|
|
728 | *cur++ = hi; |
569 | } |
729 | } |
570 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
571 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
572 | |
|
|
573 | if (hi >= 0x80) |
|
|
574 | { |
730 | break; |
575 | utf8 = 1; |
|
|
576 | |
731 | |
577 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
578 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
579 | } |
|
|
580 | else |
732 | default: |
581 | APPEND_CH (hi); |
733 | --dec_cur; |
|
|
734 | ERR ("illegal backslash escape sequence in string"); |
582 | } |
735 | } |
583 | break; |
736 | } |
|
|
737 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
|
|
738 | *cur++ = ch; |
|
|
739 | else if (ch >= 0x80) |
|
|
740 | { |
|
|
741 | STRLEN clen; |
|
|
742 | UV uch; |
584 | |
743 | |
585 | default: |
|
|
586 | --dec->cur; |
744 | --dec_cur; |
587 | ERR ("illegal backslash escape sequence in string"); |
745 | |
|
|
746 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
|
|
747 | if (clen == (STRLEN)-1) |
|
|
748 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
749 | |
|
|
750 | do |
|
|
751 | *cur++ = *dec_cur++; |
|
|
752 | while (--clen); |
|
|
753 | |
|
|
754 | utf8 = 1; |
|
|
755 | } |
|
|
756 | else |
|
|
757 | { |
|
|
758 | --dec_cur; |
|
|
759 | |
|
|
760 | if (!ch) |
|
|
761 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
762 | else |
|
|
763 | ERR ("invalid character encountered while parsing JSON string"); |
588 | } |
764 | } |
589 | } |
765 | } |
590 | else if (ch >= 0x20 && ch <= 0x7f) |
766 | while (cur < buf + SHORT_STRING_LEN); |
591 | APPEND_CH (*dec->cur++); |
767 | |
592 | else if (ch >= 0x80) |
|
|
593 | { |
768 | { |
594 | STRLEN clen; |
769 | STRLEN len = cur - buf; |
595 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
|
|
596 | if (clen == (STRLEN)-1) |
|
|
597 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
598 | |
770 | |
599 | APPEND_GROW (clen); |
771 | if (sv) |
600 | do |
|
|
601 | { |
772 | { |
602 | *cur++ = *dec->cur++; |
773 | SvGROW (sv, SvCUR (sv) + len + 1); |
|
|
774 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
|
|
775 | SvCUR_set (sv, SvCUR (sv) + len); |
603 | } |
776 | } |
604 | while (--clen); |
|
|
605 | |
|
|
606 | utf8 = 1; |
|
|
607 | } |
|
|
608 | else if (dec->cur == dec->end) |
|
|
609 | ERR ("unexpected end of string while parsing json string"); |
|
|
610 | else |
777 | else |
611 | ERR ("invalid character encountered"); |
778 | sv = newSVpvn (buf, len); |
612 | } |
779 | } |
|
|
780 | } |
|
|
781 | while (*dec_cur != '"'); |
613 | |
782 | |
614 | ++dec->cur; |
783 | ++dec_cur; |
615 | |
784 | |
616 | SvCUR_set (sv, cur - SvPVX (sv)); |
785 | if (sv) |
617 | |
786 | { |
618 | SvPOK_only (sv); |
787 | SvPOK_only (sv); |
619 | *SvEND (sv) = 0; |
788 | *SvEND (sv) = 0; |
620 | |
789 | |
621 | if (utf8) |
790 | if (utf8) |
622 | SvUTF8_on (sv); |
791 | SvUTF8_on (sv); |
|
|
792 | } |
|
|
793 | else |
|
|
794 | sv = newSVpvn ("", 0); |
623 | |
795 | |
624 | if (dec->flags & F_SHRINK) |
796 | dec->cur = dec_cur; |
625 | shrink (sv); |
|
|
626 | |
|
|
627 | return sv; |
797 | return sv; |
628 | |
798 | |
629 | fail: |
799 | fail: |
630 | SvREFCNT_dec (sv); |
800 | dec->cur = dec_cur; |
631 | return 0; |
801 | return 0; |
632 | } |
802 | } |
633 | |
803 | |
634 | static SV * |
804 | static SV * |
635 | decode_num (dec_t *dec) |
805 | decode_num (dec_t *dec) |
… | |
… | |
693 | is_nv = 1; |
863 | is_nv = 1; |
694 | } |
864 | } |
695 | |
865 | |
696 | if (!is_nv) |
866 | if (!is_nv) |
697 | { |
867 | { |
698 | UV uv; |
868 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
699 | int numtype = grok_number (start, dec->cur - start, &uv); |
869 | if (*start == '-') |
700 | if (numtype & IS_NUMBER_IN_UV) |
870 | switch (dec->cur - start) |
701 | if (numtype & IS_NUMBER_NEG) |
|
|
702 | { |
871 | { |
703 | if (uv < (UV)IV_MIN) |
872 | case 2: return newSViv (-( start [1] - '0' )); |
704 | return newSViv (-(IV)uv); |
873 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
874 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
875 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
705 | } |
876 | } |
|
|
877 | else |
|
|
878 | switch (dec->cur - start) |
|
|
879 | { |
|
|
880 | case 1: return newSViv ( start [0] - '0' ); |
|
|
881 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
882 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
883 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
884 | } |
|
|
885 | |
|
|
886 | { |
|
|
887 | UV uv; |
|
|
888 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
889 | if (numtype & IS_NUMBER_IN_UV) |
|
|
890 | if (numtype & IS_NUMBER_NEG) |
|
|
891 | { |
|
|
892 | if (uv < (UV)IV_MIN) |
|
|
893 | return newSViv (-(IV)uv); |
|
|
894 | } |
706 | else |
895 | else |
707 | return newSVuv (uv); |
896 | return newSVuv (uv); |
|
|
897 | } |
708 | } |
898 | } |
709 | |
899 | |
710 | return newSVnv (Atof (start)); |
900 | return newSVnv (Atof (start)); |
711 | |
901 | |
712 | fail: |
902 | fail: |
… | |
… | |
716 | static SV * |
906 | static SV * |
717 | decode_av (dec_t *dec) |
907 | decode_av (dec_t *dec) |
718 | { |
908 | { |
719 | AV *av = newAV (); |
909 | AV *av = newAV (); |
720 | |
910 | |
721 | WS; |
911 | DEC_INC_DEPTH; |
|
|
912 | decode_ws (dec); |
|
|
913 | |
722 | if (*dec->cur == ']') |
914 | if (*dec->cur == ']') |
723 | ++dec->cur; |
915 | ++dec->cur; |
724 | else |
916 | else |
725 | for (;;) |
917 | for (;;) |
726 | { |
918 | { |
… | |
… | |
730 | if (!value) |
922 | if (!value) |
731 | goto fail; |
923 | goto fail; |
732 | |
924 | |
733 | av_push (av, value); |
925 | av_push (av, value); |
734 | |
926 | |
735 | WS; |
927 | decode_ws (dec); |
736 | |
928 | |
737 | if (*dec->cur == ']') |
929 | if (*dec->cur == ']') |
738 | { |
930 | { |
739 | ++dec->cur; |
931 | ++dec->cur; |
740 | break; |
932 | break; |
… | |
… | |
744 | ERR (", or ] expected while parsing array"); |
936 | ERR (", or ] expected while parsing array"); |
745 | |
937 | |
746 | ++dec->cur; |
938 | ++dec->cur; |
747 | } |
939 | } |
748 | |
940 | |
|
|
941 | DEC_DEC_DEPTH; |
749 | return newRV_noinc ((SV *)av); |
942 | return newRV_noinc ((SV *)av); |
750 | |
943 | |
751 | fail: |
944 | fail: |
752 | SvREFCNT_dec (av); |
945 | SvREFCNT_dec (av); |
|
|
946 | DEC_DEC_DEPTH; |
753 | return 0; |
947 | return 0; |
754 | } |
948 | } |
755 | |
949 | |
756 | static SV * |
950 | static SV * |
757 | decode_hv (dec_t *dec) |
951 | decode_hv (dec_t *dec) |
758 | { |
952 | { |
759 | HV *hv = newHV (); |
953 | HV *hv = newHV (); |
760 | |
954 | |
761 | WS; |
955 | DEC_INC_DEPTH; |
|
|
956 | decode_ws (dec); |
|
|
957 | |
762 | if (*dec->cur == '}') |
958 | if (*dec->cur == '}') |
763 | ++dec->cur; |
959 | ++dec->cur; |
764 | else |
960 | else |
765 | for (;;) |
961 | for (;;) |
766 | { |
962 | { |
767 | SV *key, *value; |
963 | SV *key, *value; |
768 | |
964 | |
769 | WS; EXPECT_CH ('"'); |
965 | decode_ws (dec); EXPECT_CH ('"'); |
770 | |
966 | |
771 | key = decode_str (dec); |
967 | key = decode_str (dec); |
772 | if (!key) |
968 | if (!key) |
773 | goto fail; |
969 | goto fail; |
774 | |
970 | |
775 | WS; EXPECT_CH (':'); |
971 | decode_ws (dec); EXPECT_CH (':'); |
776 | |
972 | |
777 | value = decode_sv (dec); |
973 | value = decode_sv (dec); |
778 | if (!value) |
974 | if (!value) |
779 | { |
975 | { |
780 | SvREFCNT_dec (key); |
976 | SvREFCNT_dec (key); |
781 | goto fail; |
977 | goto fail; |
782 | } |
978 | } |
783 | |
979 | |
784 | //TODO: optimise |
|
|
785 | hv_store_ent (hv, key, value, 0); |
980 | hv_store_ent (hv, key, value, 0); |
|
|
981 | SvREFCNT_dec (key); |
786 | |
982 | |
787 | WS; |
983 | decode_ws (dec); |
788 | |
984 | |
789 | if (*dec->cur == '}') |
985 | if (*dec->cur == '}') |
790 | { |
986 | { |
791 | ++dec->cur; |
987 | ++dec->cur; |
792 | break; |
988 | break; |
… | |
… | |
796 | ERR (", or } expected while parsing object/hash"); |
992 | ERR (", or } expected while parsing object/hash"); |
797 | |
993 | |
798 | ++dec->cur; |
994 | ++dec->cur; |
799 | } |
995 | } |
800 | |
996 | |
|
|
997 | DEC_DEC_DEPTH; |
801 | return newRV_noinc ((SV *)hv); |
998 | return newRV_noinc ((SV *)hv); |
802 | |
999 | |
803 | fail: |
1000 | fail: |
804 | SvREFCNT_dec (hv); |
1001 | SvREFCNT_dec (hv); |
|
|
1002 | DEC_DEC_DEPTH; |
805 | return 0; |
1003 | return 0; |
806 | } |
1004 | } |
807 | |
1005 | |
808 | static SV * |
1006 | static SV * |
809 | decode_sv (dec_t *dec) |
1007 | decode_sv (dec_t *dec) |
810 | { |
1008 | { |
811 | WS; |
1009 | decode_ws (dec); |
812 | switch (*dec->cur) |
1010 | switch (*dec->cur) |
813 | { |
1011 | { |
814 | case '"': ++dec->cur; return decode_str (dec); |
1012 | case '"': ++dec->cur; return decode_str (dec); |
815 | case '[': ++dec->cur; return decode_av (dec); |
1013 | case '[': ++dec->cur; return decode_av (dec); |
816 | case '{': ++dec->cur; return decode_hv (dec); |
1014 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
852 | ERR ("'null' expected"); |
1050 | ERR ("'null' expected"); |
853 | |
1051 | |
854 | break; |
1052 | break; |
855 | |
1053 | |
856 | default: |
1054 | default: |
857 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1055 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
858 | break; |
1056 | break; |
859 | } |
1057 | } |
860 | |
1058 | |
861 | fail: |
1059 | fail: |
862 | return 0; |
1060 | return 0; |
863 | } |
1061 | } |
864 | |
1062 | |
865 | static SV * |
1063 | static SV * |
866 | decode_json (SV *string, UV flags) |
1064 | decode_json (SV *string, U32 flags, UV *offset_return) |
867 | { |
1065 | { |
|
|
1066 | dec_t dec; |
|
|
1067 | UV offset; |
868 | SV *sv; |
1068 | SV *sv; |
|
|
1069 | |
|
|
1070 | SvGETMAGIC (string); |
|
|
1071 | SvUPGRADE (string, SVt_PV); |
869 | |
1072 | |
870 | if (flags & F_UTF8) |
1073 | if (flags & F_UTF8) |
871 | sv_utf8_downgrade (string, 0); |
1074 | sv_utf8_downgrade (string, 0); |
872 | else |
1075 | else |
873 | sv_utf8_upgrade (string); |
1076 | sv_utf8_upgrade (string); |
874 | |
1077 | |
875 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1078 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
876 | |
1079 | |
877 | dec_t dec; |
|
|
878 | dec.flags = flags; |
1080 | dec.flags = flags; |
879 | dec.cur = SvPVX (string); |
1081 | dec.cur = SvPVX (string); |
880 | dec.end = SvEND (string); |
1082 | dec.end = SvEND (string); |
881 | dec.err = 0; |
1083 | dec.err = 0; |
|
|
1084 | dec.depth = 0; |
|
|
1085 | dec.maxdepth = DEC_DEPTH (dec.flags); |
882 | |
1086 | |
|
|
1087 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
883 | sv = decode_sv (&dec); |
1088 | sv = decode_sv (&dec); |
884 | |
1089 | |
|
|
1090 | if (!(offset_return || !sv)) |
|
|
1091 | { |
|
|
1092 | // check for trailing garbage |
|
|
1093 | decode_ws (&dec); |
|
|
1094 | |
|
|
1095 | if (*dec.cur) |
|
|
1096 | { |
|
|
1097 | dec.err = "garbage after JSON object"; |
|
|
1098 | SvREFCNT_dec (sv); |
|
|
1099 | sv = 0; |
|
|
1100 | } |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | if (offset_return || !sv) |
|
|
1104 | { |
|
|
1105 | offset = dec.flags & F_UTF8 |
|
|
1106 | ? dec.cur - SvPVX (string) |
|
|
1107 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1108 | |
|
|
1109 | if (offset_return) |
|
|
1110 | *offset_return = offset; |
|
|
1111 | } |
|
|
1112 | |
885 | if (!sv) |
1113 | if (!sv) |
886 | { |
1114 | { |
887 | IV offset = dec.flags & F_UTF8 |
|
|
888 | ? dec.cur - SvPVX (string) |
|
|
889 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
890 | SV *uni = sv_newmortal (); |
1115 | SV *uni = sv_newmortal (); |
891 | |
1116 | |
892 | // horrible hack to silence warning inside pv_uni_display |
1117 | // horrible hack to silence warning inside pv_uni_display |
893 | COP cop = *PL_curcop; |
1118 | COP cop = *PL_curcop; |
894 | cop.cop_warnings = pWARN_NONE; |
1119 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
896 | SAVEVPTR (PL_curcop); |
1121 | SAVEVPTR (PL_curcop); |
897 | PL_curcop = &cop; |
1122 | PL_curcop = &cop; |
898 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1123 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
899 | LEAVE; |
1124 | LEAVE; |
900 | |
1125 | |
901 | croak ("%s, at character offset %d (%s)", |
1126 | croak ("%s, at character offset %d [\"%s\"]", |
902 | dec.err, |
1127 | dec.err, |
903 | (int)offset, |
1128 | (int)offset, |
904 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1129 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
905 | } |
1130 | } |
906 | |
1131 | |
… | |
… | |
910 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1135 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
911 | |
1136 | |
912 | return sv; |
1137 | return sv; |
913 | } |
1138 | } |
914 | |
1139 | |
|
|
1140 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1141 | // XS interface functions |
|
|
1142 | |
915 | MODULE = JSON::XS PACKAGE = JSON::XS |
1143 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | |
1144 | |
917 | BOOT: |
1145 | BOOT: |
918 | { |
1146 | { |
919 | int i; |
1147 | int i; |
920 | |
1148 | |
921 | memset (decode_hexdigit, 0xff, 256); |
1149 | memset (decode_hexdigit, 0xff, 256); |
|
|
1150 | |
922 | for (i = 10; i--; ) |
1151 | for (i = 0; i < 256; ++i) |
923 | decode_hexdigit ['0' + i] = i; |
1152 | decode_hexdigit [i] = |
924 | |
1153 | i >= '0' && i <= '9' ? i - '0' |
925 | for (i = 7; i--; ) |
1154 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
926 | { |
1155 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
927 | decode_hexdigit ['a' + i] = 10 + i; |
1156 | : -1; |
928 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
929 | } |
|
|
930 | |
1157 | |
931 | json_stash = gv_stashpv ("JSON::XS", 1); |
1158 | json_stash = gv_stashpv ("JSON::XS", 1); |
932 | } |
1159 | } |
933 | |
1160 | |
934 | PROTOTYPES: DISABLE |
1161 | PROTOTYPES: DISABLE |
… | |
… | |
940 | RETVAL |
1167 | RETVAL |
941 | |
1168 | |
942 | SV *ascii (SV *self, int enable = 1) |
1169 | SV *ascii (SV *self, int enable = 1) |
943 | ALIAS: |
1170 | ALIAS: |
944 | ascii = F_ASCII |
1171 | ascii = F_ASCII |
|
|
1172 | latin1 = F_LATIN1 |
945 | utf8 = F_UTF8 |
1173 | utf8 = F_UTF8 |
946 | indent = F_INDENT |
1174 | indent = F_INDENT |
947 | canonical = F_CANONICAL |
1175 | canonical = F_CANONICAL |
948 | space_before = F_SPACE_BEFORE |
1176 | space_before = F_SPACE_BEFORE |
949 | space_after = F_SPACE_AFTER |
1177 | space_after = F_SPACE_AFTER |
… | |
… | |
961 | RETVAL = newSVsv (self); |
1189 | RETVAL = newSVsv (self); |
962 | } |
1190 | } |
963 | OUTPUT: |
1191 | OUTPUT: |
964 | RETVAL |
1192 | RETVAL |
965 | |
1193 | |
|
|
1194 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1195 | CODE: |
|
|
1196 | { |
|
|
1197 | UV *uv = SvJSON (self); |
|
|
1198 | UV log2 = 0; |
|
|
1199 | |
|
|
1200 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1201 | |
|
|
1202 | while ((1UL << log2) < max_depth) |
|
|
1203 | ++log2; |
|
|
1204 | |
|
|
1205 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1206 | |
|
|
1207 | RETVAL = newSVsv (self); |
|
|
1208 | } |
|
|
1209 | OUTPUT: |
|
|
1210 | RETVAL |
|
|
1211 | |
966 | void encode (SV *self, SV *scalar) |
1212 | void encode (SV *self, SV *scalar) |
967 | PPCODE: |
1213 | PPCODE: |
968 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1214 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
969 | |
1215 | |
970 | void decode (SV *self, SV *jsonstr) |
1216 | void decode (SV *self, SV *jsonstr) |
971 | PPCODE: |
1217 | PPCODE: |
972 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1218 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1219 | |
|
|
1220 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1221 | PPCODE: |
|
|
1222 | { |
|
|
1223 | UV offset; |
|
|
1224 | EXTEND (SP, 2); |
|
|
1225 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1226 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1227 | } |
973 | |
1228 | |
974 | PROTOTYPES: ENABLE |
1229 | PROTOTYPES: ENABLE |
975 | |
1230 | |
976 | void to_json (SV *scalar) |
1231 | void to_json (SV *scalar) |
|
|
1232 | ALIAS: |
|
|
1233 | objToJson = 0 |
977 | PPCODE: |
1234 | PPCODE: |
978 | XPUSHs (encode_json (scalar, F_UTF8)); |
1235 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
979 | |
1236 | |
980 | void from_json (SV *jsonstr) |
1237 | void from_json (SV *jsonstr) |
|
|
1238 | ALIAS: |
|
|
1239 | jsonToObj = 0 |
981 | PPCODE: |
1240 | PPCODE: |
982 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1241 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
983 | |
1242 | |