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