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