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