… | |
… | |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
19 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
20 | #endif |
20 | #endif |
21 | #ifndef HvNAMEUTF8 |
21 | #ifndef HvNAMEUTF8 |
22 | # define HvNAMEUTF8(hv) 0 |
22 | # define HvNAMEUTF8(hv) 0 |
23 | #endif |
23 | #endif |
|
|
24 | #ifndef SvREFCNT_dec_NN |
|
|
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
|
|
26 | #endif |
|
|
27 | |
|
|
28 | // known major and minor types |
|
|
29 | enum cbor_type |
|
|
30 | { |
|
|
31 | MAJOR_SHIFT = 5, |
|
|
32 | MINOR_MASK = 0x1f, |
|
|
33 | |
|
|
34 | MAJOR_POS_INT = 0 << MAJOR_SHIFT, |
|
|
35 | MAJOR_NEG_INT = 1 << MAJOR_SHIFT, |
|
|
36 | MAJOR_BYTES = 2 << MAJOR_SHIFT, |
|
|
37 | MAJOR_TEXT = 3 << MAJOR_SHIFT, |
|
|
38 | MAJOR_ARRAY = 4 << MAJOR_SHIFT, |
|
|
39 | MAJOR_MAP = 5 << MAJOR_SHIFT, |
|
|
40 | MAJOR_TAG = 6 << MAJOR_SHIFT, |
|
|
41 | MAJOR_MISC = 7 << MAJOR_SHIFT, |
|
|
42 | |
|
|
43 | // INT/STRING/ARRAY/MAP subtypes |
|
|
44 | LENGTH_EXT1 = 24, |
|
|
45 | LENGTH_EXT2 = 25, |
|
|
46 | LENGTH_EXT4 = 26, |
|
|
47 | LENGTH_EXT8 = 27, |
|
|
48 | |
|
|
49 | // SIMPLE types (effectively MISC subtypes) |
|
|
50 | SIMPLE_FALSE = 20, |
|
|
51 | SIMPLE_TRUE = 21, |
|
|
52 | SIMPLE_NULL = 22, |
|
|
53 | SIMPLE_UNDEF = 23, |
|
|
54 | |
|
|
55 | // MISC subtype (unused) |
|
|
56 | MISC_EXT1 = 24, |
|
|
57 | MISC_FLOAT16 = 25, |
|
|
58 | MISC_FLOAT32 = 26, |
|
|
59 | MISC_FLOAT64 = 27, |
|
|
60 | |
|
|
61 | // BYTES/TEXT/ARRAY/MAP |
|
|
62 | MINOR_INDEF = 31, |
|
|
63 | }; |
24 | |
64 | |
25 | // known tags |
65 | // known tags |
26 | enum cbor_tag |
66 | enum cbor_tag |
27 | { |
67 | { |
28 | // inofficial extensions (pending iana registration) |
68 | // extensions |
|
|
69 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
70 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
71 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHARABLE = 26, // http://cbor.schmorp.de/value-sharing |
72 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
73 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
74 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
34 | CBOR_TAG_STRINGREF = 28, // http://cbor.schmorp.de/stringref |
|
|
35 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
75 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
76 | |
37 | // rfc7049 |
77 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
78 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
79 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
40 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
80 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
41 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
81 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
42 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
82 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
43 | CBOR_TAG_BIGFLOAT = 5, // array |
83 | CBOR_TAG_BIGFLOAT = 5, // array |
44 | |
84 | |
45 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
85 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
46 | CBOR_TAG_CONV_B64 = 22, // base64, any |
86 | CBOR_TAG_CONV_B64 = 22, // base64, any |
47 | CBOR_TAG_CONV_HEX = 23, // base16, any |
87 | CBOR_TAG_CONV_HEX = 23, // base16, any |
48 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
88 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
49 | |
89 | |
50 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
90 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
51 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
91 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
52 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
92 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
53 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
93 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
54 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
94 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
55 | |
95 | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
96 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
57 | }; |
97 | }; |
58 | |
98 | |
59 | #define F_SHRINK 0x00000001UL |
99 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
101 | #define F_ALLOW_SHARING 0x00000004UL |
|
|
102 | #define F_ALLOW_CYCLES 0x00000008UL |
62 | #define F_DEDUP_STRINGS 0x00000008UL //TODO |
103 | #define F_PACK_STRINGS 0x00000010UL |
63 | #define F_DEDUP_KEYS 0x00000010UL //TODO |
104 | #define F_VALIDATE_UTF8 0x00000020UL |
64 | |
105 | |
65 | #define INIT_SIZE 32 // initial scalar size to be allocated |
106 | #define INIT_SIZE 32 // initial scalar size to be allocated |
66 | |
107 | |
67 | #define SB do { |
108 | #define SB do { |
68 | #define SE } while (0) |
109 | #define SE } while (0) |
… | |
… | |
80 | # define CBOR_SLOW 0 |
121 | # define CBOR_SLOW 0 |
81 | # define CBOR_STASH cbor_stash |
122 | # define CBOR_STASH cbor_stash |
82 | #endif |
123 | #endif |
83 | |
124 | |
84 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
125 | static HV *cbor_stash, *types_boolean_stash, *types_error_stash, *cbor_tagged_stash; // CBOR::XS:: |
85 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
126 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
86 | |
127 | |
87 | typedef struct { |
128 | typedef struct { |
88 | U32 flags; |
129 | U32 flags; |
89 | U32 max_depth; |
130 | U32 max_depth; |
90 | STRLEN max_size; |
131 | STRLEN max_size; |
|
|
132 | SV *filter; |
91 | } CBOR; |
133 | } CBOR; |
92 | |
134 | |
93 | ecb_inline void |
135 | ecb_inline void |
94 | cbor_init (CBOR *cbor) |
136 | cbor_init (CBOR *cbor) |
95 | { |
137 | { |
96 | Zero (cbor, 1, CBOR); |
138 | Zero (cbor, 1, CBOR); |
97 | cbor->max_depth = 512; |
139 | cbor->max_depth = 512; |
|
|
140 | } |
|
|
141 | |
|
|
142 | ecb_inline void |
|
|
143 | cbor_free (CBOR *cbor) |
|
|
144 | { |
|
|
145 | SvREFCNT_dec (cbor->filter); |
98 | } |
146 | } |
99 | |
147 | |
100 | ///////////////////////////////////////////////////////////////////////////// |
148 | ///////////////////////////////////////////////////////////////////////////// |
101 | // utility functions |
149 | // utility functions |
102 | |
150 | |
… | |
… | |
124 | SvPV_renew (sv, SvCUR (sv) + 1); |
172 | SvPV_renew (sv, SvCUR (sv) + 1); |
125 | #endif |
173 | #endif |
126 | } |
174 | } |
127 | } |
175 | } |
128 | |
176 | |
|
|
177 | // minimum length of a string to be registered for stringref |
|
|
178 | ecb_inline int |
|
|
179 | minimum_string_length (UV idx) |
|
|
180 | { |
|
|
181 | return idx > 23 |
|
|
182 | ? idx > 0xffU |
|
|
183 | ? idx > 0xffffU |
|
|
184 | ? idx > 0xffffffffU |
|
|
185 | ? 11 |
|
|
186 | : 7 |
|
|
187 | : 5 |
|
|
188 | : 4 |
|
|
189 | : 3; |
|
|
190 | } |
|
|
191 | |
129 | ///////////////////////////////////////////////////////////////////////////// |
192 | ///////////////////////////////////////////////////////////////////////////// |
130 | // encoder |
193 | // encoder |
131 | |
194 | |
132 | // structure used for encoding CBOR |
195 | // structure used for encoding CBOR |
133 | typedef struct |
196 | typedef struct |
… | |
… | |
135 | char *cur; // SvPVX (sv) + current output position |
198 | char *cur; // SvPVX (sv) + current output position |
136 | char *end; // SvEND (sv) |
199 | char *end; // SvEND (sv) |
137 | SV *sv; // result scalar |
200 | SV *sv; // result scalar |
138 | CBOR cbor; |
201 | CBOR cbor; |
139 | U32 depth; // recursion level |
202 | U32 depth; // recursion level |
140 | HV *stringref; // string => index, or 0 |
203 | HV *stringref[2]; // string => index, or 0 ([0] = bytes, [1] = utf-8) |
|
|
204 | UV stringref_idx; |
141 | HV *sharable; // ptr => index, or 0 |
205 | HV *shareable; // ptr => index, or 0 |
142 | HV *sharable_idx; |
206 | UV shareable_idx; |
143 | } enc_t; |
207 | } enc_t; |
144 | |
208 | |
145 | ecb_inline void |
209 | ecb_inline void |
146 | need (enc_t *enc, STRLEN len) |
210 | need (enc_t *enc, STRLEN len) |
147 | { |
211 | { |
… | |
… | |
164 | static void |
228 | static void |
165 | encode_uint (enc_t *enc, int major, UV len) |
229 | encode_uint (enc_t *enc, int major, UV len) |
166 | { |
230 | { |
167 | need (enc, 9); |
231 | need (enc, 9); |
168 | |
232 | |
169 | if (len < 24) |
233 | if (ecb_expect_true (len < LENGTH_EXT1)) |
170 | *enc->cur++ = major | len; |
234 | *enc->cur++ = major | len; |
171 | else if (len <= 0xff) |
235 | else if (ecb_expect_true (len <= 0xffU)) |
172 | { |
236 | { |
173 | *enc->cur++ = major | 24; |
237 | *enc->cur++ = major | LENGTH_EXT1; |
174 | *enc->cur++ = len; |
238 | *enc->cur++ = len; |
175 | } |
239 | } |
176 | else if (len <= 0xffff) |
240 | else if (len <= 0xffffU) |
177 | { |
241 | { |
178 | *enc->cur++ = major | 25; |
242 | *enc->cur++ = major | LENGTH_EXT2; |
179 | *enc->cur++ = len >> 8; |
243 | *enc->cur++ = len >> 8; |
180 | *enc->cur++ = len; |
244 | *enc->cur++ = len; |
181 | } |
245 | } |
182 | else if (len <= 0xffffffff) |
246 | else if (len <= 0xffffffffU) |
183 | { |
247 | { |
184 | *enc->cur++ = major | 26; |
248 | *enc->cur++ = major | LENGTH_EXT4; |
185 | *enc->cur++ = len >> 24; |
249 | *enc->cur++ = len >> 24; |
186 | *enc->cur++ = len >> 16; |
250 | *enc->cur++ = len >> 16; |
187 | *enc->cur++ = len >> 8; |
251 | *enc->cur++ = len >> 8; |
188 | *enc->cur++ = len; |
252 | *enc->cur++ = len; |
189 | } |
253 | } |
190 | else |
254 | else |
191 | { |
255 | { |
192 | *enc->cur++ = major | 27; |
256 | *enc->cur++ = major | LENGTH_EXT8; |
193 | *enc->cur++ = len >> 56; |
257 | *enc->cur++ = len >> 56; |
194 | *enc->cur++ = len >> 48; |
258 | *enc->cur++ = len >> 48; |
195 | *enc->cur++ = len >> 40; |
259 | *enc->cur++ = len >> 40; |
196 | *enc->cur++ = len >> 32; |
260 | *enc->cur++ = len >> 32; |
197 | *enc->cur++ = len >> 24; |
261 | *enc->cur++ = len >> 24; |
… | |
… | |
199 | *enc->cur++ = len >> 8; |
263 | *enc->cur++ = len >> 8; |
200 | *enc->cur++ = len; |
264 | *enc->cur++ = len; |
201 | } |
265 | } |
202 | } |
266 | } |
203 | |
267 | |
204 | static void |
268 | ecb_inline void |
|
|
269 | encode_tag (enc_t *enc, UV tag) |
|
|
270 | { |
|
|
271 | encode_uint (enc, MAJOR_TAG, tag); |
|
|
272 | } |
|
|
273 | |
|
|
274 | ecb_inline void |
205 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
275 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
206 | { |
276 | { |
207 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
277 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
208 | need (enc, len); |
278 | need (enc, len); |
209 | memcpy (enc->cur, str, len); |
279 | memcpy (enc->cur, str, len); |
210 | enc->cur += len; |
280 | enc->cur += len; |
211 | } |
281 | } |
212 | |
282 | |
213 | ecb_inline void |
|
|
214 | encode_tag (enc_t *enc, UV tag) |
|
|
215 | { |
|
|
216 | encode_uint (enc, 0xc0, tag); |
|
|
217 | } |
|
|
218 | |
|
|
219 | static int |
283 | static void |
220 | encode_sharable2 (enc_t *enc, SV *sv) |
284 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
221 | { |
285 | { |
222 | if (!enc->sharable) |
286 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
223 | enc->sharable = (HV *)sv_2mortal ((SV *)newHV ()); |
287 | { |
|
|
288 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
224 | |
289 | |
225 | SV **svp = hv_fetch (enc->sharable, &sv, sizeof (sv), 1); |
|
|
226 | |
|
|
227 | if (SvOK (*svp)) |
290 | if (SvOK (*svp)) |
228 | { |
291 | { |
|
|
292 | // already registered, use stringref |
229 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
293 | encode_tag (enc, CBOR_TAG_STRINGREF); |
230 | encode_uint (enc, 0x00, SvUV (*svp)); |
294 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
231 | |
|
|
232 | return 1; |
295 | return; |
233 | } |
296 | } |
234 | else |
297 | else if (len >= minimum_string_length (enc->stringref_idx)) |
235 | { |
298 | { |
|
|
299 | // register only |
236 | sv_setuv (*svp, enc->sharable_idx++); |
300 | sv_setuv (*svp, enc->stringref_idx); |
237 | encode_tag (enc, CBOR_TAG_VALUE_SHARABLE); |
301 | ++enc->stringref_idx; |
238 | |
302 | } |
239 | return 0; |
|
|
240 | } |
303 | } |
241 | } |
|
|
242 | |
304 | |
243 | ecb_inline int |
305 | encode_str (enc, utf8, str, len); |
244 | encode_sharable (enc_t *enc, SV *sv) |
|
|
245 | { |
|
|
246 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
247 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
248 | return encode_sharable2 (enc, sv); |
|
|
249 | |
|
|
250 | return 0; |
|
|
251 | } |
306 | } |
252 | |
307 | |
253 | static void encode_sv (enc_t *enc, SV *sv); |
308 | static void encode_sv (enc_t *enc, SV *sv); |
254 | |
309 | |
255 | static void |
310 | static void |
… | |
… | |
260 | if (enc->depth >= enc->cbor.max_depth) |
315 | if (enc->depth >= enc->cbor.max_depth) |
261 | croak (ERR_NESTING_EXCEEDED); |
316 | croak (ERR_NESTING_EXCEEDED); |
262 | |
317 | |
263 | ++enc->depth; |
318 | ++enc->depth; |
264 | |
319 | |
265 | encode_uint (enc, 0x80, len + 1); |
320 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
266 | |
321 | |
267 | for (i = 0; i <= len; ++i) |
322 | for (i = 0; i <= len; ++i) |
268 | { |
323 | { |
269 | SV **svp = av_fetch (av, i, 0); |
324 | SV **svp = av_fetch (av, i, 0); |
270 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
325 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
… | |
… | |
285 | |
340 | |
286 | int pairs = hv_iterinit (hv); |
341 | int pairs = hv_iterinit (hv); |
287 | int mg = SvMAGICAL (hv); |
342 | int mg = SvMAGICAL (hv); |
288 | |
343 | |
289 | if (mg) |
344 | if (mg) |
290 | encode_ch (enc, 0xa0 | 31); |
345 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
291 | else |
346 | else |
292 | encode_uint (enc, 0xa0, pairs); |
347 | encode_uint (enc, MAJOR_MAP, pairs); |
293 | |
348 | |
294 | while ((he = hv_iternext (hv))) |
349 | while ((he = hv_iternext (hv))) |
295 | { |
350 | { |
296 | if (HeKLEN (he) == HEf_SVKEY) |
351 | if (HeKLEN (he) == HEf_SVKEY) |
297 | encode_sv (enc, HeSVKEY (he)); |
352 | encode_sv (enc, HeSVKEY (he)); |
298 | else |
353 | else |
299 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
354 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
300 | |
355 | |
301 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
356 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
302 | } |
357 | } |
303 | |
358 | |
304 | if (mg) |
359 | if (mg) |
305 | encode_ch (enc, 0xe0 | 31); |
360 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
306 | |
361 | |
307 | --enc->depth; |
362 | --enc->depth; |
308 | } |
363 | } |
309 | |
364 | |
310 | // encode objects, arrays and special \0=false and \1=true values. |
365 | // encode objects, arrays and special \0=false and \1=true values. |
311 | static void |
366 | static void |
312 | encode_rv (enc_t *enc, SV *sv) |
367 | encode_rv (enc_t *enc, SV *sv) |
313 | { |
368 | { |
314 | svtype svt; |
|
|
315 | |
|
|
316 | SvGETMAGIC (sv); |
369 | SvGETMAGIC (sv); |
|
|
370 | |
317 | svt = SvTYPE (sv); |
371 | svtype svt = SvTYPE (sv); |
318 | |
|
|
319 | if (encode_sharable (enc, sv)) |
|
|
320 | return; |
|
|
321 | |
372 | |
322 | if (ecb_expect_false (SvOBJECT (sv))) |
373 | if (ecb_expect_false (SvOBJECT (sv))) |
323 | { |
374 | { |
324 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
375 | HV *boolean_stash = !CBOR_SLOW || types_boolean_stash |
325 | ? types_boolean_stash |
376 | ? types_boolean_stash |
… | |
… | |
330 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
381 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
331 | ? cbor_tagged_stash |
382 | ? cbor_tagged_stash |
332 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
383 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
333 | |
384 | |
334 | HV *stash = SvSTASH (sv); |
385 | HV *stash = SvSTASH (sv); |
335 | GV *method; |
|
|
336 | |
386 | |
337 | if (stash == boolean_stash) |
387 | if (stash == boolean_stash) |
338 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
388 | { |
|
|
389 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
390 | return; |
|
|
391 | } |
339 | else if (stash == error_stash) |
392 | else if (stash == error_stash) |
340 | encode_ch (enc, 0xe0 | 23); |
393 | { |
|
|
394 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
395 | return; |
|
|
396 | } |
341 | else if (stash == tagged_stash) |
397 | else if (stash == tagged_stash) |
342 | { |
398 | { |
343 | if (svt != SVt_PVAV) |
399 | if (svt != SVt_PVAV) |
344 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
400 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
345 | |
401 | |
346 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
402 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
347 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
403 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
404 | |
|
|
405 | return; |
|
|
406 | } |
|
|
407 | } |
|
|
408 | |
|
|
409 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
410 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
411 | { |
|
|
412 | if (!enc->shareable) |
|
|
413 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
414 | |
|
|
415 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
416 | |
|
|
417 | if (SvOK (*svp)) |
348 | } |
418 | { |
|
|
419 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
420 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
421 | return; |
|
|
422 | } |
|
|
423 | else |
|
|
424 | { |
|
|
425 | sv_setuv (*svp, enc->shareable_idx); |
|
|
426 | ++enc->shareable_idx; |
|
|
427 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
428 | } |
|
|
429 | } |
|
|
430 | |
|
|
431 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
432 | { |
|
|
433 | HV *stash = SvSTASH (sv); |
|
|
434 | GV *method; |
|
|
435 | |
349 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
436 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
350 | { |
437 | { |
351 | dSP; |
438 | dSP; |
352 | |
439 | |
353 | ENTER; SAVETMPS; PUSHMARK (SP); |
440 | ENTER; SAVETMPS; PUSHMARK (SP); |
354 | // we re-bless the reference to get overload and other niceties right |
441 | // we re-bless the reference to get overload and other niceties right |
… | |
… | |
386 | // catch this surprisingly common error |
473 | // catch this surprisingly common error |
387 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
474 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
388 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
475 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
389 | |
476 | |
390 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
477 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
391 | encode_uint (enc, 0x80, count + 1); |
478 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
392 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
479 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
393 | |
480 | |
394 | while (count) |
481 | while (count) |
395 | encode_sv (enc, SP[1 - count--]); |
482 | encode_sv (enc, SP[1 - count--]); |
396 | |
483 | |
397 | PUTBACK; |
484 | PUTBACK; |
… | |
… | |
418 | { |
505 | { |
419 | double nv = SvNVX (sv); |
506 | double nv = SvNVX (sv); |
420 | |
507 | |
421 | need (enc, 9); |
508 | need (enc, 9); |
422 | |
509 | |
423 | if (ecb_expect_false (nv == (U32)nv)) |
510 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
424 | encode_uint (enc, 0x00, (U32)nv); |
511 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
425 | //TODO: maybe I32? |
512 | //TODO: maybe I32? |
426 | else if (ecb_expect_false (nv == (float)nv)) |
513 | else if (ecb_expect_false (nv == (float)nv)) |
427 | { |
514 | { |
428 | uint32_t fp = ecb_float_to_binary32 (nv); |
515 | uint32_t fp = ecb_float_to_binary32 (nv); |
429 | |
516 | |
430 | *enc->cur++ = 0xe0 | 26; |
517 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
431 | |
518 | |
432 | if (!ecb_big_endian ()) |
519 | if (!ecb_big_endian ()) |
433 | fp = ecb_bswap32 (fp); |
520 | fp = ecb_bswap32 (fp); |
434 | |
521 | |
435 | memcpy (enc->cur, &fp, 4); |
522 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
437 | } |
524 | } |
438 | else |
525 | else |
439 | { |
526 | { |
440 | uint64_t fp = ecb_double_to_binary64 (nv); |
527 | uint64_t fp = ecb_double_to_binary64 (nv); |
441 | |
528 | |
442 | *enc->cur++ = 0xe0 | 27; |
529 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
443 | |
530 | |
444 | if (!ecb_big_endian ()) |
531 | if (!ecb_big_endian ()) |
445 | fp = ecb_bswap64 (fp); |
532 | fp = ecb_bswap64 (fp); |
446 | |
533 | |
447 | memcpy (enc->cur, &fp, 8); |
534 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
452 | static void |
539 | static void |
453 | encode_sv (enc_t *enc, SV *sv) |
540 | encode_sv (enc_t *enc, SV *sv) |
454 | { |
541 | { |
455 | SvGETMAGIC (sv); |
542 | SvGETMAGIC (sv); |
456 | |
543 | |
457 | if (encode_sharable (enc, sv)) |
|
|
458 | return; |
|
|
459 | |
|
|
460 | if (SvPOKp (sv)) |
544 | if (SvPOKp (sv)) |
461 | { |
545 | { |
462 | STRLEN len; |
546 | STRLEN len; |
463 | char *str = SvPV (sv, len); |
547 | char *str = SvPV (sv, len); |
464 | encode_str (enc, SvUTF8 (sv), str, len); |
548 | encode_strref (enc, SvUTF8 (sv), str, len); |
465 | } |
549 | } |
466 | else if (SvNOKp (sv)) |
550 | else if (SvNOKp (sv)) |
467 | encode_nv (enc, sv); |
551 | encode_nv (enc, sv); |
468 | else if (SvIOKp (sv)) |
552 | else if (SvIOKp (sv)) |
469 | { |
553 | { |
470 | if (SvIsUV (sv)) |
554 | if (SvIsUV (sv)) |
471 | encode_uint (enc, 0x00, SvUVX (sv)); |
555 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
472 | else if (SvIVX (sv) >= 0) |
556 | else if (SvIVX (sv) >= 0) |
473 | encode_uint (enc, 0x00, SvIVX (sv)); |
557 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
474 | else |
558 | else |
475 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
559 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
476 | } |
560 | } |
477 | else if (SvROK (sv)) |
561 | else if (SvROK (sv)) |
478 | encode_rv (enc, SvRV (sv)); |
562 | encode_rv (enc, SvRV (sv)); |
479 | else if (!SvOK (sv)) |
563 | else if (!SvOK (sv)) |
480 | encode_ch (enc, 0xe0 | 22); |
564 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
481 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
565 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
482 | encode_ch (enc, 0xe0 | 23); |
566 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
483 | else |
567 | else |
484 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
568 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
485 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
569 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
486 | } |
570 | } |
487 | |
571 | |
… | |
… | |
494 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
578 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
495 | enc.cur = SvPVX (enc.sv); |
579 | enc.cur = SvPVX (enc.sv); |
496 | enc.end = SvEND (enc.sv); |
580 | enc.end = SvEND (enc.sv); |
497 | |
581 | |
498 | SvPOK_only (enc.sv); |
582 | SvPOK_only (enc.sv); |
|
|
583 | |
|
|
584 | if (cbor->flags & F_PACK_STRINGS) |
|
|
585 | { |
|
|
586 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
|
|
587 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
588 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
589 | } |
|
|
590 | |
499 | encode_sv (&enc, scalar); |
591 | encode_sv (&enc, scalar); |
500 | |
592 | |
501 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
593 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
502 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
594 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
503 | |
595 | |
… | |
… | |
517 | U8 *end; // end of input string |
609 | U8 *end; // end of input string |
518 | const char *err; // parse error, if != 0 |
610 | const char *err; // parse error, if != 0 |
519 | CBOR cbor; |
611 | CBOR cbor; |
520 | U32 depth; // recursion depth |
612 | U32 depth; // recursion depth |
521 | U32 maxdepth; // recursion depth limit |
613 | U32 maxdepth; // recursion depth limit |
522 | AV *sharable; |
614 | AV *shareable; |
|
|
615 | AV *stringref; |
|
|
616 | SV *decode_tagged; |
523 | } dec_t; |
617 | } dec_t; |
524 | |
618 | |
525 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
619 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
526 | |
620 | |
527 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
621 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
… | |
… | |
530 | #define DEC_DEC_DEPTH --dec->depth |
624 | #define DEC_DEC_DEPTH --dec->depth |
531 | |
625 | |
532 | static UV |
626 | static UV |
533 | decode_uint (dec_t *dec) |
627 | decode_uint (dec_t *dec) |
534 | { |
628 | { |
535 | switch (*dec->cur & 31) |
629 | U8 m = *dec->cur & MINOR_MASK; |
536 | { |
630 | ++dec->cur; |
537 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|
538 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
|
|
539 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
|
|
540 | return *dec->cur++ & 31; |
|
|
541 | |
631 | |
542 | case 24: |
632 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
633 | return m; |
|
|
634 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
635 | { |
543 | WANT (2); |
636 | WANT (1); |
544 | dec->cur += 2; |
637 | dec->cur += 1; |
545 | return dec->cur[-1]; |
638 | return dec->cur[-1]; |
546 | |
639 | } |
547 | case 25: |
640 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
641 | { |
548 | WANT (3); |
642 | WANT (2); |
549 | dec->cur += 3; |
643 | dec->cur += 2; |
550 | return (((UV)dec->cur[-2]) << 8) |
644 | return (((UV)dec->cur[-2]) << 8) |
551 | | ((UV)dec->cur[-1]); |
645 | | ((UV)dec->cur[-1]); |
552 | |
646 | } |
553 | case 26: |
647 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
648 | { |
554 | WANT (5); |
649 | WANT (4); |
555 | dec->cur += 5; |
650 | dec->cur += 4; |
556 | return (((UV)dec->cur[-4]) << 24) |
651 | return (((UV)dec->cur[-4]) << 24) |
557 | | (((UV)dec->cur[-3]) << 16) |
652 | | (((UV)dec->cur[-3]) << 16) |
558 | | (((UV)dec->cur[-2]) << 8) |
653 | | (((UV)dec->cur[-2]) << 8) |
559 | | ((UV)dec->cur[-1]); |
654 | | ((UV)dec->cur[-1]); |
560 | |
655 | } |
561 | case 27: |
656 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
657 | { |
562 | WANT (9); |
658 | WANT (8); |
563 | dec->cur += 9; |
659 | dec->cur += 8; |
|
|
660 | |
|
|
661 | return |
|
|
662 | #if UVSIZE < 8 |
|
|
663 | 0 |
|
|
664 | #else |
564 | return (((UV)dec->cur[-8]) << 56) |
665 | (((UV)dec->cur[-8]) << 56) |
565 | | (((UV)dec->cur[-7]) << 48) |
666 | | (((UV)dec->cur[-7]) << 48) |
566 | | (((UV)dec->cur[-6]) << 40) |
667 | | (((UV)dec->cur[-6]) << 40) |
567 | | (((UV)dec->cur[-5]) << 32) |
668 | | (((UV)dec->cur[-5]) << 32) |
|
|
669 | #endif |
568 | | (((UV)dec->cur[-4]) << 24) |
670 | | (((UV)dec->cur[-4]) << 24) |
569 | | (((UV)dec->cur[-3]) << 16) |
671 | | (((UV)dec->cur[-3]) << 16) |
570 | | (((UV)dec->cur[-2]) << 8) |
672 | | (((UV)dec->cur[-2]) << 8) |
571 | | ((UV)dec->cur[-1]); |
673 | | ((UV)dec->cur[-1]); |
572 | |
674 | } |
573 | default: |
675 | else |
574 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
676 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
575 | } |
|
|
576 | |
677 | |
577 | fail: |
678 | fail: |
578 | return 0; |
679 | return 0; |
579 | } |
680 | } |
580 | |
681 | |
… | |
… | |
585 | { |
686 | { |
586 | AV *av = newAV (); |
687 | AV *av = newAV (); |
587 | |
688 | |
588 | DEC_INC_DEPTH; |
689 | DEC_INC_DEPTH; |
589 | |
690 | |
590 | if ((*dec->cur & 31) == 31) |
691 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
591 | { |
692 | { |
592 | ++dec->cur; |
693 | ++dec->cur; |
593 | |
694 | |
594 | for (;;) |
695 | for (;;) |
595 | { |
696 | { |
596 | WANT (1); |
697 | WANT (1); |
597 | |
698 | |
598 | if (*dec->cur == (0xe0 | 31)) |
699 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
599 | { |
700 | { |
600 | ++dec->cur; |
701 | ++dec->cur; |
601 | break; |
702 | break; |
602 | } |
703 | } |
603 | |
704 | |
… | |
… | |
606 | } |
707 | } |
607 | else |
708 | else |
608 | { |
709 | { |
609 | int i, len = decode_uint (dec); |
710 | int i, len = decode_uint (dec); |
610 | |
711 | |
|
|
712 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
611 | av_fill (av, len - 1); |
713 | av_fill (av, len - 1); |
612 | |
714 | |
613 | for (i = 0; i < len; ++i) |
715 | for (i = 0; i < len; ++i) |
614 | AvARRAY (av)[i] = decode_sv (dec); |
716 | AvARRAY (av)[i] = decode_sv (dec); |
615 | } |
717 | } |
… | |
… | |
625 | |
727 | |
626 | static void |
728 | static void |
627 | decode_he (dec_t *dec, HV *hv) |
729 | decode_he (dec_t *dec, HV *hv) |
628 | { |
730 | { |
629 | // for speed reasons, we specialcase single-string |
731 | // for speed reasons, we specialcase single-string |
630 | // byte or utf-8 strings as keys. |
732 | // byte or utf-8 strings as keys, but only when !stringref |
631 | |
733 | |
632 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
734 | if (ecb_expect_true (!dec->stringref)) |
|
|
735 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
633 | { |
736 | { |
634 | I32 len = decode_uint (dec); |
737 | I32 len = decode_uint (dec); |
635 | char *key = (char *)dec->cur; |
738 | char *key = (char *)dec->cur; |
636 | |
739 | |
637 | dec->cur += len; |
740 | dec->cur += len; |
638 | |
741 | |
639 | hv_store (hv, key, len, decode_sv (dec), 0); |
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
|
|
743 | |
|
|
744 | return; |
640 | } |
745 | } |
641 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
746 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
642 | { |
747 | { |
643 | I32 len = decode_uint (dec); |
748 | I32 len = decode_uint (dec); |
644 | char *key = (char *)dec->cur; |
749 | char *key = (char *)dec->cur; |
645 | |
750 | |
646 | dec->cur += len; |
751 | dec->cur += len; |
647 | |
752 | |
|
|
753 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
754 | if (!is_utf8_string (key, len)) |
|
|
755 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
|
|
756 | |
648 | hv_store (hv, key, -len, decode_sv (dec), 0); |
757 | hv_store (hv, key, -len, decode_sv (dec), 0); |
|
|
758 | |
|
|
759 | return; |
649 | } |
760 | } |
650 | else |
761 | |
651 | { |
|
|
652 | SV *k = decode_sv (dec); |
762 | SV *k = decode_sv (dec); |
653 | SV *v = decode_sv (dec); |
763 | SV *v = decode_sv (dec); |
654 | |
764 | |
655 | hv_store_ent (hv, k, v, 0); |
765 | hv_store_ent (hv, k, v, 0); |
656 | SvREFCNT_dec (k); |
766 | SvREFCNT_dec (k); |
657 | } |
767 | |
|
|
768 | fail: |
|
|
769 | ; |
658 | } |
770 | } |
659 | |
771 | |
660 | static SV * |
772 | static SV * |
661 | decode_hv (dec_t *dec) |
773 | decode_hv (dec_t *dec) |
662 | { |
774 | { |
663 | HV *hv = newHV (); |
775 | HV *hv = newHV (); |
664 | |
776 | |
665 | DEC_INC_DEPTH; |
777 | DEC_INC_DEPTH; |
666 | |
778 | |
667 | if ((*dec->cur & 31) == 31) |
779 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
668 | { |
780 | { |
669 | ++dec->cur; |
781 | ++dec->cur; |
670 | |
782 | |
671 | for (;;) |
783 | for (;;) |
672 | { |
784 | { |
673 | WANT (1); |
785 | WANT (1); |
674 | |
786 | |
675 | if (*dec->cur == (0xe0 | 31)) |
787 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
676 | { |
788 | { |
677 | ++dec->cur; |
789 | ++dec->cur; |
678 | break; |
790 | break; |
679 | } |
791 | } |
680 | |
792 | |
… | |
… | |
701 | static SV * |
813 | static SV * |
702 | decode_str (dec_t *dec, int utf8) |
814 | decode_str (dec_t *dec, int utf8) |
703 | { |
815 | { |
704 | SV *sv = 0; |
816 | SV *sv = 0; |
705 | |
817 | |
706 | if ((*dec->cur & 31) == 31) |
818 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
707 | { |
819 | { |
|
|
820 | // indefinite length strings |
708 | ++dec->cur; |
821 | ++dec->cur; |
709 | |
822 | |
|
|
823 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
824 | |
710 | sv = newSVpvn ("", 0); |
825 | sv = newSVpvn ("", 0); |
711 | |
826 | |
712 | // not very fast, and certainly not robust against illegal input |
|
|
713 | for (;;) |
827 | for (;;) |
714 | { |
828 | { |
715 | WANT (1); |
829 | WANT (1); |
716 | |
830 | |
717 | if (*dec->cur == (0xe0 | 31)) |
831 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
832 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
718 | { |
833 | { |
719 | ++dec->cur; |
834 | ++dec->cur; |
720 | break; |
835 | break; |
721 | } |
836 | } |
|
|
837 | else |
|
|
838 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
722 | |
839 | |
723 | sv_catsv (sv, decode_sv (dec)); |
840 | STRLEN len = decode_uint (dec); |
|
|
841 | |
|
|
842 | WANT (len); |
|
|
843 | sv_catpvn (sv, dec->cur, len); |
|
|
844 | dec->cur += len; |
724 | } |
845 | } |
725 | } |
846 | } |
726 | else |
847 | else |
727 | { |
848 | { |
728 | STRLEN len = decode_uint (dec); |
849 | STRLEN len = decode_uint (dec); |
729 | |
850 | |
730 | WANT (len); |
851 | WANT (len); |
731 | sv = newSVpvn (dec->cur, len); |
852 | sv = newSVpvn (dec->cur, len); |
732 | dec->cur += len; |
853 | dec->cur += len; |
|
|
854 | |
|
|
855 | if (ecb_expect_false (dec->stringref) |
|
|
856 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
857 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
733 | } |
858 | } |
734 | |
859 | |
735 | if (utf8) |
860 | if (utf8) |
|
|
861 | { |
|
|
862 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
|
|
863 | if (!is_utf8_string (SvPVX (sv), SvCUR (sv))) |
|
|
864 | ERR ("corrupted CBOR data (invalid UTF-8 in text string)"); |
|
|
865 | |
736 | SvUTF8_on (sv); |
866 | SvUTF8_on (sv); |
|
|
867 | } |
737 | |
868 | |
738 | return sv; |
869 | return sv; |
739 | |
870 | |
740 | fail: |
871 | fail: |
741 | SvREFCNT_dec (sv); |
872 | SvREFCNT_dec (sv); |
… | |
… | |
743 | } |
874 | } |
744 | |
875 | |
745 | static SV * |
876 | static SV * |
746 | decode_tagged (dec_t *dec) |
877 | decode_tagged (dec_t *dec) |
747 | { |
878 | { |
|
|
879 | SV *sv = 0; |
748 | UV tag = decode_uint (dec); |
880 | UV tag = decode_uint (dec); |
749 | SV *sv = decode_sv (dec); |
881 | |
|
|
882 | WANT (1); |
750 | |
883 | |
751 | switch (tag) |
884 | switch (tag) |
752 | { |
885 | { |
753 | case CBOR_TAG_MAGIC: |
886 | case CBOR_TAG_MAGIC: |
754 | return sv; |
887 | sv = decode_sv (dec); |
|
|
888 | break; |
755 | |
889 | |
756 | case CBOR_TAG_INDIRECTION: |
890 | case CBOR_TAG_INDIRECTION: |
757 | return newRV_noinc (sv); |
891 | sv = newRV_noinc (decode_sv (dec)); |
|
|
892 | break; |
758 | |
893 | |
|
|
894 | case CBOR_TAG_STRINGREF_NAMESPACE: |
|
|
895 | { |
|
|
896 | ENTER; SAVETMPS; |
|
|
897 | |
|
|
898 | SAVESPTR (dec->stringref); |
|
|
899 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
|
|
900 | |
|
|
901 | sv = decode_sv (dec); |
|
|
902 | |
|
|
903 | FREETMPS; LEAVE; |
|
|
904 | } |
|
|
905 | break; |
|
|
906 | |
|
|
907 | case CBOR_TAG_STRINGREF: |
|
|
908 | { |
|
|
909 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
|
|
910 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
|
|
911 | |
|
|
912 | UV idx = decode_uint (dec); |
|
|
913 | |
|
|
914 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
|
|
915 | ERR ("corrupted CBOR data (stringref index out of bounds or outside namespace)"); |
|
|
916 | |
|
|
917 | sv = newSVsv (AvARRAY (dec->stringref)[idx]); |
|
|
918 | } |
|
|
919 | break; |
|
|
920 | |
759 | case CBOR_TAG_VALUE_SHARABLE: |
921 | case CBOR_TAG_VALUE_SHAREABLE: |
|
|
922 | { |
760 | if (ecb_expect_false (!dec->sharable)) |
923 | if (ecb_expect_false (!dec->shareable)) |
761 | dec->sharable = (AV *)sv_2mortal ((SV *)newAV ()); |
924 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
762 | |
925 | |
|
|
926 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
927 | { |
|
|
928 | sv = newSV (0); |
763 | av_push (dec->sharable, SvREFCNT_inc_NN (sv)); |
929 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
764 | |
930 | |
765 | return sv; |
931 | SV *osv = decode_sv (dec); |
|
|
932 | sv_setsv (sv, osv); |
|
|
933 | SvREFCNT_dec_NN (osv); |
|
|
934 | } |
|
|
935 | else |
|
|
936 | { |
|
|
937 | av_push (dec->shareable, &PL_sv_undef); |
|
|
938 | int idx = AvFILLp (dec->shareable); |
|
|
939 | sv = decode_sv (dec); |
|
|
940 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
941 | } |
|
|
942 | } |
|
|
943 | break; |
766 | |
944 | |
767 | case CBOR_TAG_VALUE_SHAREDREF: |
945 | case CBOR_TAG_VALUE_SHAREDREF: |
768 | { |
946 | { |
769 | // TODO: should verify that the sv atcually was a CBOR unsigned integer |
947 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
770 | UV idx = SvUV (sv); |
948 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
771 | |
949 | |
|
|
950 | UV idx = decode_uint (dec); |
|
|
951 | |
772 | if (!dec->sharable || idx > AvFILLp (dec->sharable)) |
952 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
773 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
953 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
774 | |
954 | |
775 | SvREFCNT_dec (sv); |
|
|
776 | |
|
|
777 | return SvREFCNT_inc_NN (AvARRAY (dec->sharable)[idx]); |
955 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
956 | |
|
|
957 | if (sv == &PL_sv_undef) |
|
|
958 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
778 | } |
959 | } |
|
|
960 | break; |
779 | |
961 | |
780 | case CBOR_TAG_PERL_OBJECT: |
962 | case CBOR_TAG_PERL_OBJECT: |
781 | { |
963 | { |
|
|
964 | sv = decode_sv (dec); |
|
|
965 | |
782 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
966 | if (!SvROK (sv) || SvTYPE (SvRV (sv)) != SVt_PVAV) |
783 | ERR ("corrupted CBOR data (non-array perl object)"); |
967 | ERR ("corrupted CBOR data (non-array perl object)"); |
784 | |
968 | |
785 | AV *av = (AV *)SvRV (sv); |
969 | AV *av = (AV *)SvRV (sv); |
786 | int len = av_len (av) + 1; |
970 | int len = av_len (av) + 1; |
… | |
… | |
821 | sv = SvREFCNT_inc (POPs); |
1005 | sv = SvREFCNT_inc (POPs); |
822 | |
1006 | |
823 | PUTBACK; |
1007 | PUTBACK; |
824 | |
1008 | |
825 | FREETMPS; LEAVE; |
1009 | FREETMPS; LEAVE; |
826 | |
|
|
827 | return sv; |
|
|
828 | } |
1010 | } |
|
|
1011 | break; |
829 | |
1012 | |
830 | default: |
1013 | default: |
831 | { |
1014 | { |
|
|
1015 | sv = decode_sv (dec); |
|
|
1016 | |
|
|
1017 | dSP; |
|
|
1018 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1019 | EXTEND (SP, 2); |
|
|
1020 | PUSHs (newSVuv (tag)); |
|
|
1021 | PUSHs (sv); |
|
|
1022 | |
|
|
1023 | PUTBACK; |
|
|
1024 | int count = call_sv (dec->cbor.filter ? dec->cbor.filter : default_filter, G_ARRAY | G_EVAL); |
|
|
1025 | SPAGAIN; |
|
|
1026 | |
|
|
1027 | if (SvTRUE (ERRSV)) |
|
|
1028 | { |
|
|
1029 | FREETMPS; LEAVE; |
|
|
1030 | ERR (SvPVutf8_nolen (sv_2mortal (SvREFCNT_inc (ERRSV)))); |
|
|
1031 | } |
|
|
1032 | |
|
|
1033 | if (count) |
|
|
1034 | { |
|
|
1035 | SvREFCNT_dec (sv); |
|
|
1036 | sv = SvREFCNT_inc (POPs); |
|
|
1037 | } |
|
|
1038 | else |
|
|
1039 | { |
832 | AV *av = newAV (); |
1040 | AV *av = newAV (); |
833 | av_push (av, newSVuv (tag)); |
1041 | av_push (av, newSVuv (tag)); |
834 | av_push (av, sv); |
1042 | av_push (av, sv); |
835 | |
1043 | |
836 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1044 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
837 | ? cbor_tagged_stash |
1045 | ? cbor_tagged_stash |
838 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1046 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
839 | |
|
|
840 | return sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
1047 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
841 | } |
1048 | } |
|
|
1049 | |
|
|
1050 | PUTBACK; |
|
|
1051 | |
|
|
1052 | FREETMPS; LEAVE; |
|
|
1053 | } |
|
|
1054 | break; |
842 | } |
1055 | } |
|
|
1056 | |
|
|
1057 | return sv; |
843 | |
1058 | |
844 | fail: |
1059 | fail: |
845 | SvREFCNT_dec (sv); |
1060 | SvREFCNT_dec (sv); |
846 | return &PL_sv_undef; |
1061 | return &PL_sv_undef; |
847 | } |
1062 | } |
… | |
… | |
849 | static SV * |
1064 | static SV * |
850 | decode_sv (dec_t *dec) |
1065 | decode_sv (dec_t *dec) |
851 | { |
1066 | { |
852 | WANT (1); |
1067 | WANT (1); |
853 | |
1068 | |
854 | switch (*dec->cur >> 5) |
1069 | switch (*dec->cur >> MAJOR_SHIFT) |
855 | { |
1070 | { |
856 | case 0: // unsigned int |
1071 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
857 | return newSVuv (decode_uint (dec)); |
1072 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
858 | case 1: // negative int |
1073 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
859 | return newSViv (-1 - (IV)decode_uint (dec)); |
1074 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
860 | case 2: // octet string |
1075 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
861 | return decode_str (dec, 0); |
1076 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
862 | case 3: // utf-8 string |
1077 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
863 | return decode_str (dec, 1); |
1078 | |
864 | case 4: // array |
1079 | case MAJOR_MISC >> MAJOR_SHIFT: |
865 | return decode_av (dec); |
|
|
866 | case 5: // map |
|
|
867 | return decode_hv (dec); |
|
|
868 | case 6: // tag |
|
|
869 | return decode_tagged (dec); |
|
|
870 | case 7: // misc |
|
|
871 | switch (*dec->cur++ & 31) |
1080 | switch (*dec->cur++ & MINOR_MASK) |
872 | { |
1081 | { |
873 | case 20: |
1082 | case SIMPLE_FALSE: |
874 | #if CBOR_SLOW |
1083 | #if CBOR_SLOW |
875 | types_false = get_bool ("Types::Serialiser::false"); |
1084 | types_false = get_bool ("Types::Serialiser::false"); |
876 | #endif |
1085 | #endif |
877 | return newSVsv (types_false); |
1086 | return newSVsv (types_false); |
878 | case 21: |
1087 | case SIMPLE_TRUE: |
879 | #if CBOR_SLOW |
1088 | #if CBOR_SLOW |
880 | types_true = get_bool ("Types::Serialiser::true"); |
1089 | types_true = get_bool ("Types::Serialiser::true"); |
881 | #endif |
1090 | #endif |
882 | return newSVsv (types_true); |
1091 | return newSVsv (types_true); |
883 | case 22: |
1092 | case SIMPLE_NULL: |
884 | return newSVsv (&PL_sv_undef); |
1093 | return newSVsv (&PL_sv_undef); |
885 | case 23: |
1094 | case SIMPLE_UNDEF: |
886 | #if CBOR_SLOW |
1095 | #if CBOR_SLOW |
887 | types_error = get_bool ("Types::Serialiser::error"); |
1096 | types_error = get_bool ("Types::Serialiser::error"); |
888 | #endif |
1097 | #endif |
889 | return newSVsv (types_error); |
1098 | return newSVsv (types_error); |
890 | |
1099 | |
891 | case 25: |
1100 | case MISC_FLOAT16: |
892 | { |
1101 | { |
893 | WANT (2); |
1102 | WANT (2); |
894 | |
1103 | |
895 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1104 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
896 | dec->cur += 2; |
1105 | dec->cur += 2; |
897 | |
1106 | |
898 | return newSVnv (ecb_binary16_to_float (fp)); |
1107 | return newSVnv (ecb_binary16_to_float (fp)); |
899 | } |
1108 | } |
900 | |
1109 | |
901 | case 26: |
1110 | case MISC_FLOAT32: |
902 | { |
1111 | { |
903 | uint32_t fp; |
1112 | uint32_t fp; |
904 | WANT (4); |
1113 | WANT (4); |
905 | memcpy (&fp, dec->cur, 4); |
1114 | memcpy (&fp, dec->cur, 4); |
906 | dec->cur += 4; |
1115 | dec->cur += 4; |
… | |
… | |
909 | fp = ecb_bswap32 (fp); |
1118 | fp = ecb_bswap32 (fp); |
910 | |
1119 | |
911 | return newSVnv (ecb_binary32_to_float (fp)); |
1120 | return newSVnv (ecb_binary32_to_float (fp)); |
912 | } |
1121 | } |
913 | |
1122 | |
914 | case 27: |
1123 | case MISC_FLOAT64: |
915 | { |
1124 | { |
916 | uint64_t fp; |
1125 | uint64_t fp; |
917 | WANT (8); |
1126 | WANT (8); |
918 | memcpy (&fp, dec->cur, 8); |
1127 | memcpy (&fp, dec->cur, 8); |
919 | dec->cur += 8; |
1128 | dec->cur += 8; |
… | |
… | |
922 | fp = ecb_bswap64 (fp); |
1131 | fp = ecb_bswap64 (fp); |
923 | |
1132 | |
924 | return newSVnv (ecb_binary64_to_double (fp)); |
1133 | return newSVnv (ecb_binary64_to_double (fp)); |
925 | } |
1134 | } |
926 | |
1135 | |
927 | // 0..19 unassigned |
1136 | // 0..19 unassigned simple |
928 | // 24 reserved + unassigned (reserved values are not encodable) |
1137 | // 24 reserved + unassigned (reserved values are not encodable) |
929 | default: |
1138 | default: |
930 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1139 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
931 | } |
1140 | } |
932 | |
1141 | |
… | |
… | |
987 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1196 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
988 | |
1197 | |
989 | types_true = get_bool ("Types::Serialiser::true" ); |
1198 | types_true = get_bool ("Types::Serialiser::true" ); |
990 | types_false = get_bool ("Types::Serialiser::false"); |
1199 | types_false = get_bool ("Types::Serialiser::false"); |
991 | types_error = get_bool ("Types::Serialiser::error"); |
1200 | types_error = get_bool ("Types::Serialiser::error"); |
|
|
1201 | |
|
|
1202 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
992 | |
1203 | |
993 | sv_cbor = newSVpv ("CBOR", 0); |
1204 | sv_cbor = newSVpv ("CBOR", 0); |
994 | SvREADONLY_on (sv_cbor); |
1205 | SvREADONLY_on (sv_cbor); |
995 | } |
1206 | } |
996 | |
1207 | |
… | |
… | |
1018 | void shrink (CBOR *self, int enable = 1) |
1229 | void shrink (CBOR *self, int enable = 1) |
1019 | ALIAS: |
1230 | ALIAS: |
1020 | shrink = F_SHRINK |
1231 | shrink = F_SHRINK |
1021 | allow_unknown = F_ALLOW_UNKNOWN |
1232 | allow_unknown = F_ALLOW_UNKNOWN |
1022 | allow_sharing = F_ALLOW_SHARING |
1233 | allow_sharing = F_ALLOW_SHARING |
1023 | dedup_keys = F_DEDUP_KEYS |
1234 | allow_cycles = F_ALLOW_CYCLES |
1024 | dedup_strings = F_DEDUP_STRINGS |
1235 | pack_strings = F_PACK_STRINGS |
|
|
1236 | validate_utf8 = F_VALIDATE_UTF8 |
1025 | PPCODE: |
1237 | PPCODE: |
1026 | { |
1238 | { |
1027 | if (enable) |
1239 | if (enable) |
1028 | self->flags |= ix; |
1240 | self->flags |= ix; |
1029 | else |
1241 | else |
… | |
… | |
1035 | void get_shrink (CBOR *self) |
1247 | void get_shrink (CBOR *self) |
1036 | ALIAS: |
1248 | ALIAS: |
1037 | get_shrink = F_SHRINK |
1249 | get_shrink = F_SHRINK |
1038 | get_allow_unknown = F_ALLOW_UNKNOWN |
1250 | get_allow_unknown = F_ALLOW_UNKNOWN |
1039 | get_allow_sharing = F_ALLOW_SHARING |
1251 | get_allow_sharing = F_ALLOW_SHARING |
1040 | get_dedup_keys = F_DEDUP_KEYS |
1252 | get_allow_cycles = F_ALLOW_CYCLES |
1041 | get_dedup_strings = F_DEDUP_STRINGS |
1253 | get_pack_strings = F_PACK_STRINGS |
|
|
1254 | get_validate_utf8 = F_VALIDATE_UTF8 |
1042 | PPCODE: |
1255 | PPCODE: |
1043 | XPUSHs (boolSV (self->flags & ix)); |
1256 | XPUSHs (boolSV (self->flags & ix)); |
1044 | |
1257 | |
1045 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1258 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1046 | PPCODE: |
1259 | PPCODE: |
… | |
… | |
1059 | XPUSHs (ST (0)); |
1272 | XPUSHs (ST (0)); |
1060 | |
1273 | |
1061 | int get_max_size (CBOR *self) |
1274 | int get_max_size (CBOR *self) |
1062 | CODE: |
1275 | CODE: |
1063 | RETVAL = self->max_size; |
1276 | RETVAL = self->max_size; |
|
|
1277 | OUTPUT: |
|
|
1278 | RETVAL |
|
|
1279 | |
|
|
1280 | void filter (CBOR *self, SV *filter = 0) |
|
|
1281 | PPCODE: |
|
|
1282 | SvREFCNT_dec (self->filter); |
|
|
1283 | self->filter = filter ? newSVsv (filter) : filter; |
|
|
1284 | XPUSHs (ST (0)); |
|
|
1285 | |
|
|
1286 | SV *get_filter (CBOR *self) |
|
|
1287 | CODE: |
|
|
1288 | RETVAL = self->filter ? self->filter : NEWSV (0, 0); |
1064 | OUTPUT: |
1289 | OUTPUT: |
1065 | RETVAL |
1290 | RETVAL |
1066 | |
1291 | |
1067 | void encode (CBOR *self, SV *scalar) |
1292 | void encode (CBOR *self, SV *scalar) |
1068 | PPCODE: |
1293 | PPCODE: |
… | |
… | |
1083 | EXTEND (SP, 2); |
1308 | EXTEND (SP, 2); |
1084 | PUSHs (sv); |
1309 | PUSHs (sv); |
1085 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1310 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1086 | } |
1311 | } |
1087 | |
1312 | |
|
|
1313 | void DESTROY (CBOR *self) |
|
|
1314 | PPCODE: |
|
|
1315 | cbor_free (self); |
|
|
1316 | |
1088 | PROTOTYPES: ENABLE |
1317 | PROTOTYPES: ENABLE |
1089 | |
1318 | |
1090 | void encode_cbor (SV *scalar) |
1319 | void encode_cbor (SV *scalar) |
|
|
1320 | ALIAS: |
|
|
1321 | encode_cbor = 0 |
|
|
1322 | encode_cbor_sharing = F_ALLOW_SHARING |
1091 | PPCODE: |
1323 | PPCODE: |
1092 | { |
1324 | { |
1093 | CBOR cbor; |
1325 | CBOR cbor; |
1094 | cbor_init (&cbor); |
1326 | cbor_init (&cbor); |
|
|
1327 | cbor.flags |= ix; |
1095 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1328 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1096 | XPUSHs (scalar); |
1329 | XPUSHs (scalar); |
1097 | } |
1330 | } |
1098 | |
1331 | |
1099 | void decode_cbor (SV *cborstr) |
1332 | void decode_cbor (SV *cborstr) |