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