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