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