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