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