… | |
… | |
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_SHAREABLE = 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 |
|
|
104 | #define F_VALIDATE_UTF8 0x00000020UL |
63 | |
105 | |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
106 | #define INIT_SIZE 32 // initial scalar size to be allocated |
65 | |
107 | |
66 | #define SB do { |
108 | #define SB do { |
67 | #define SE } while (0) |
109 | #define SE } while (0) |
… | |
… | |
79 | # define CBOR_SLOW 0 |
121 | # define CBOR_SLOW 0 |
80 | # define CBOR_STASH cbor_stash |
122 | # define CBOR_STASH cbor_stash |
81 | #endif |
123 | #endif |
82 | |
124 | |
83 | 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:: |
84 | static SV *types_true, *types_false, *types_error, *sv_cbor; |
126 | static SV *types_true, *types_false, *types_error, *sv_cbor, *default_filter; |
85 | |
127 | |
86 | typedef struct { |
128 | typedef struct { |
87 | U32 flags; |
129 | U32 flags; |
88 | U32 max_depth; |
130 | U32 max_depth; |
89 | STRLEN max_size; |
131 | STRLEN max_size; |
|
|
132 | SV *filter; |
90 | } CBOR; |
133 | } CBOR; |
91 | |
134 | |
92 | ecb_inline void |
135 | ecb_inline void |
93 | cbor_init (CBOR *cbor) |
136 | cbor_init (CBOR *cbor) |
94 | { |
137 | { |
95 | Zero (cbor, 1, CBOR); |
138 | Zero (cbor, 1, CBOR); |
96 | 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); |
97 | } |
146 | } |
98 | |
147 | |
99 | ///////////////////////////////////////////////////////////////////////////// |
148 | ///////////////////////////////////////////////////////////////////////////// |
100 | // utility functions |
149 | // utility functions |
101 | |
150 | |
… | |
… | |
131 | { |
180 | { |
132 | return idx > 23 |
181 | return idx > 23 |
133 | ? idx > 0xffU |
182 | ? idx > 0xffU |
134 | ? idx > 0xffffU |
183 | ? idx > 0xffffU |
135 | ? idx > 0xffffffffU |
184 | ? idx > 0xffffffffU |
|
|
185 | ? 11 |
136 | ? 7 |
186 | : 7 |
137 | : 6 |
|
|
138 | : 5 |
187 | : 5 |
139 | : 4 |
188 | : 4 |
140 | : 3; |
189 | : 3; |
141 | } |
190 | } |
142 | |
191 | |
… | |
… | |
179 | static void |
228 | static void |
180 | encode_uint (enc_t *enc, int major, UV len) |
229 | encode_uint (enc_t *enc, int major, UV len) |
181 | { |
230 | { |
182 | need (enc, 9); |
231 | need (enc, 9); |
183 | |
232 | |
184 | if (len < 24) |
233 | if (ecb_expect_true (len < LENGTH_EXT1)) |
185 | *enc->cur++ = major | len; |
234 | *enc->cur++ = major | len; |
186 | else if (len <= 0xff) |
235 | else if (ecb_expect_true (len <= 0xffU)) |
187 | { |
236 | { |
188 | *enc->cur++ = major | 24; |
237 | *enc->cur++ = major | LENGTH_EXT1; |
189 | *enc->cur++ = len; |
238 | *enc->cur++ = len; |
190 | } |
239 | } |
191 | else if (len <= 0xffff) |
240 | else if (len <= 0xffffU) |
192 | { |
241 | { |
193 | *enc->cur++ = major | 25; |
242 | *enc->cur++ = major | LENGTH_EXT2; |
194 | *enc->cur++ = len >> 8; |
243 | *enc->cur++ = len >> 8; |
195 | *enc->cur++ = len; |
244 | *enc->cur++ = len; |
196 | } |
245 | } |
197 | else if (len <= 0xffffffff) |
246 | else if (len <= 0xffffffffU) |
198 | { |
247 | { |
199 | *enc->cur++ = major | 26; |
248 | *enc->cur++ = major | LENGTH_EXT4; |
200 | *enc->cur++ = len >> 24; |
249 | *enc->cur++ = len >> 24; |
201 | *enc->cur++ = len >> 16; |
250 | *enc->cur++ = len >> 16; |
202 | *enc->cur++ = len >> 8; |
251 | *enc->cur++ = len >> 8; |
203 | *enc->cur++ = len; |
252 | *enc->cur++ = len; |
204 | } |
253 | } |
205 | else |
254 | else |
206 | { |
255 | { |
207 | *enc->cur++ = major | 27; |
256 | *enc->cur++ = major | LENGTH_EXT8; |
208 | *enc->cur++ = len >> 56; |
257 | *enc->cur++ = len >> 56; |
209 | *enc->cur++ = len >> 48; |
258 | *enc->cur++ = len >> 48; |
210 | *enc->cur++ = len >> 40; |
259 | *enc->cur++ = len >> 40; |
211 | *enc->cur++ = len >> 32; |
260 | *enc->cur++ = len >> 32; |
212 | *enc->cur++ = len >> 24; |
261 | *enc->cur++ = len >> 24; |
… | |
… | |
217 | } |
266 | } |
218 | |
267 | |
219 | ecb_inline void |
268 | ecb_inline void |
220 | encode_tag (enc_t *enc, UV tag) |
269 | encode_tag (enc_t *enc, UV tag) |
221 | { |
270 | { |
222 | encode_uint (enc, 0xc0, tag); |
271 | encode_uint (enc, MAJOR_TAG, tag); |
|
|
272 | } |
|
|
273 | |
|
|
274 | ecb_inline void |
|
|
275 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
276 | { |
|
|
277 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
|
|
278 | need (enc, len); |
|
|
279 | memcpy (enc->cur, str, len); |
|
|
280 | enc->cur += len; |
223 | } |
281 | } |
224 | |
282 | |
225 | static void |
283 | static void |
226 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
284 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
227 | { |
285 | { |
228 | if (ecb_expect_false (enc->cbor.flags & F_DEDUP_STRINGS)) |
286 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
229 | { |
287 | { |
230 | SV **svp = hv_fetch (enc->stringref[utf8], str, len, 1); |
288 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
231 | |
289 | |
232 | if (SvOK (*svp)) |
290 | if (SvOK (*svp)) |
233 | { |
291 | { |
234 | // already registered, use stringref |
292 | // already registered, use stringref |
235 | encode_tag (enc, CBOR_TAG_STRINGREF); |
293 | encode_tag (enc, CBOR_TAG_STRINGREF); |
236 | encode_uint (enc, 0x00, SvUV (*svp)); |
294 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
237 | return; |
295 | return; |
238 | } |
296 | } |
239 | else if (len >= minimum_string_length (enc->stringref_idx)) |
297 | else if (len >= minimum_string_length (enc->stringref_idx)) |
240 | { |
298 | { |
241 | // register only |
299 | // register only |
242 | sv_setuv (*svp, enc->stringref_idx); |
300 | sv_setuv (*svp, enc->stringref_idx); |
243 | ++enc->stringref_idx; |
301 | ++enc->stringref_idx; |
244 | } |
302 | } |
245 | } |
303 | } |
246 | |
304 | |
247 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
305 | encode_str (enc, utf8, str, len); |
248 | need (enc, len); |
|
|
249 | memcpy (enc->cur, str, len); |
|
|
250 | enc->cur += len; |
|
|
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); |
271 | } |
326 | } |
272 | |
327 | |
273 | --enc->depth; |
328 | --enc->depth; |
274 | } |
329 | } |
275 | |
330 | |
276 | ecb_inline void |
|
|
277 | encode_he (enc_t *enc, HE *he) |
|
|
278 | { |
|
|
279 | } |
|
|
280 | |
|
|
281 | static void |
331 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
332 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
333 | { |
284 | HE *he; |
334 | HE *he; |
285 | |
335 | |
… | |
… | |
290 | |
340 | |
291 | int pairs = hv_iterinit (hv); |
341 | int pairs = hv_iterinit (hv); |
292 | int mg = SvMAGICAL (hv); |
342 | int mg = SvMAGICAL (hv); |
293 | |
343 | |
294 | if (mg) |
344 | if (mg) |
295 | encode_ch (enc, 0xa0 | 31); |
345 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
296 | else |
346 | else |
297 | encode_uint (enc, 0xa0, pairs); |
347 | encode_uint (enc, MAJOR_MAP, pairs); |
298 | |
348 | |
299 | while ((he = hv_iternext (hv))) |
349 | while ((he = hv_iternext (hv))) |
300 | { |
350 | { |
301 | if (HeKLEN (he) == HEf_SVKEY) |
351 | if (HeKLEN (he) == HEf_SVKEY) |
302 | encode_sv (enc, HeSVKEY (he)); |
352 | encode_sv (enc, HeSVKEY (he)); |
303 | else |
353 | else |
304 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
354 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
305 | |
355 | |
306 | 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)); |
307 | } |
357 | } |
308 | |
358 | |
309 | if (mg) |
359 | if (mg) |
310 | encode_ch (enc, 0xe0 | 31); |
360 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
311 | |
361 | |
312 | --enc->depth; |
362 | --enc->depth; |
313 | } |
363 | } |
314 | |
364 | |
315 | // encode objects, arrays and special \0=false and \1=true values. |
365 | // encode objects, arrays and special \0=false and \1=true values. |
316 | static void |
366 | static void |
317 | encode_rv (enc_t *enc, SV *sv) |
367 | encode_rv (enc_t *enc, SV *sv) |
318 | { |
368 | { |
319 | SvGETMAGIC (sv); |
369 | SvGETMAGIC (sv); |
320 | |
|
|
321 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
322 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
323 | { |
|
|
324 | if (!enc->shareable) |
|
|
325 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
326 | |
|
|
327 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
328 | |
|
|
329 | if (SvOK (*svp)) |
|
|
330 | { |
|
|
331 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
332 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
333 | return; |
|
|
334 | } |
|
|
335 | else |
|
|
336 | { |
|
|
337 | sv_setuv (*svp, enc->shareable_idx); |
|
|
338 | ++enc->shareable_idx; |
|
|
339 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
340 | } |
|
|
341 | } |
|
|
342 | |
370 | |
343 | svtype svt = SvTYPE (sv); |
371 | svtype svt = SvTYPE (sv); |
344 | |
372 | |
345 | if (ecb_expect_false (SvOBJECT (sv))) |
373 | if (ecb_expect_false (SvOBJECT (sv))) |
346 | { |
374 | { |
… | |
… | |
353 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
381 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
354 | ? cbor_tagged_stash |
382 | ? cbor_tagged_stash |
355 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
383 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
356 | |
384 | |
357 | HV *stash = SvSTASH (sv); |
385 | HV *stash = SvSTASH (sv); |
358 | GV *method; |
|
|
359 | |
386 | |
360 | if (stash == boolean_stash) |
387 | if (stash == boolean_stash) |
361 | 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 | } |
362 | else if (stash == error_stash) |
392 | else if (stash == error_stash) |
363 | encode_ch (enc, 0xe0 | 23); |
393 | { |
|
|
394 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
395 | return; |
|
|
396 | } |
364 | else if (stash == tagged_stash) |
397 | else if (stash == tagged_stash) |
365 | { |
398 | { |
366 | if (svt != SVt_PVAV) |
399 | if (svt != SVt_PVAV) |
367 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
400 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
368 | |
401 | |
369 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
402 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
370 | 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)) |
371 | } |
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 | |
372 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
436 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
373 | { |
437 | { |
374 | dSP; |
438 | dSP; |
375 | |
439 | |
376 | ENTER; SAVETMPS; PUSHMARK (SP); |
440 | ENTER; SAVETMPS; PUSHMARK (SP); |
377 | // 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 |
… | |
… | |
409 | // catch this surprisingly common error |
473 | // catch this surprisingly common error |
410 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
474 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
411 | 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)); |
412 | |
476 | |
413 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
477 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
414 | encode_uint (enc, 0x80, count + 1); |
478 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
415 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
479 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
416 | |
480 | |
417 | while (count) |
481 | while (count) |
418 | encode_sv (enc, SP[1 - count--]); |
482 | encode_sv (enc, SP[1 - count--]); |
419 | |
483 | |
420 | PUTBACK; |
484 | PUTBACK; |
… | |
… | |
441 | { |
505 | { |
442 | double nv = SvNVX (sv); |
506 | double nv = SvNVX (sv); |
443 | |
507 | |
444 | need (enc, 9); |
508 | need (enc, 9); |
445 | |
509 | |
446 | if (ecb_expect_false (nv == (U32)nv)) |
510 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
447 | encode_uint (enc, 0x00, (U32)nv); |
511 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
448 | //TODO: maybe I32? |
512 | //TODO: maybe I32? |
449 | else if (ecb_expect_false (nv == (float)nv)) |
513 | else if (ecb_expect_false (nv == (float)nv)) |
450 | { |
514 | { |
451 | uint32_t fp = ecb_float_to_binary32 (nv); |
515 | uint32_t fp = ecb_float_to_binary32 (nv); |
452 | |
516 | |
453 | *enc->cur++ = 0xe0 | 26; |
517 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
454 | |
518 | |
455 | if (!ecb_big_endian ()) |
519 | if (!ecb_big_endian ()) |
456 | fp = ecb_bswap32 (fp); |
520 | fp = ecb_bswap32 (fp); |
457 | |
521 | |
458 | memcpy (enc->cur, &fp, 4); |
522 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
460 | } |
524 | } |
461 | else |
525 | else |
462 | { |
526 | { |
463 | uint64_t fp = ecb_double_to_binary64 (nv); |
527 | uint64_t fp = ecb_double_to_binary64 (nv); |
464 | |
528 | |
465 | *enc->cur++ = 0xe0 | 27; |
529 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
466 | |
530 | |
467 | if (!ecb_big_endian ()) |
531 | if (!ecb_big_endian ()) |
468 | fp = ecb_bswap64 (fp); |
532 | fp = ecb_bswap64 (fp); |
469 | |
533 | |
470 | memcpy (enc->cur, &fp, 8); |
534 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
479 | |
543 | |
480 | if (SvPOKp (sv)) |
544 | if (SvPOKp (sv)) |
481 | { |
545 | { |
482 | STRLEN len; |
546 | STRLEN len; |
483 | char *str = SvPV (sv, len); |
547 | char *str = SvPV (sv, len); |
484 | encode_str (enc, SvUTF8 (sv), str, len); |
548 | encode_strref (enc, SvUTF8 (sv), str, len); |
485 | } |
549 | } |
486 | else if (SvNOKp (sv)) |
550 | else if (SvNOKp (sv)) |
487 | encode_nv (enc, sv); |
551 | encode_nv (enc, sv); |
488 | else if (SvIOKp (sv)) |
552 | else if (SvIOKp (sv)) |
489 | { |
553 | { |
490 | if (SvIsUV (sv)) |
554 | if (SvIsUV (sv)) |
491 | encode_uint (enc, 0x00, SvUVX (sv)); |
555 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
492 | else if (SvIVX (sv) >= 0) |
556 | else if (SvIVX (sv) >= 0) |
493 | encode_uint (enc, 0x00, SvIVX (sv)); |
557 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
494 | else |
558 | else |
495 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
559 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
496 | } |
560 | } |
497 | else if (SvROK (sv)) |
561 | else if (SvROK (sv)) |
498 | encode_rv (enc, SvRV (sv)); |
562 | encode_rv (enc, SvRV (sv)); |
499 | else if (!SvOK (sv)) |
563 | else if (!SvOK (sv)) |
500 | encode_ch (enc, 0xe0 | 22); |
564 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
501 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
565 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
502 | encode_ch (enc, 0xe0 | 23); |
566 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
503 | else |
567 | else |
504 | 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", |
505 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
569 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
506 | } |
570 | } |
507 | |
571 | |
… | |
… | |
515 | enc.cur = SvPVX (enc.sv); |
579 | enc.cur = SvPVX (enc.sv); |
516 | enc.end = SvEND (enc.sv); |
580 | enc.end = SvEND (enc.sv); |
517 | |
581 | |
518 | SvPOK_only (enc.sv); |
582 | SvPOK_only (enc.sv); |
519 | |
583 | |
520 | if (cbor->flags & F_DEDUP_STRINGS) |
584 | if (cbor->flags & F_PACK_STRINGS) |
521 | { |
585 | { |
522 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
586 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
523 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
587 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
524 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
588 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
525 | } |
589 | } |
… | |
… | |
547 | CBOR cbor; |
611 | CBOR cbor; |
548 | U32 depth; // recursion depth |
612 | U32 depth; // recursion depth |
549 | U32 maxdepth; // recursion depth limit |
613 | U32 maxdepth; // recursion depth limit |
550 | AV *shareable; |
614 | AV *shareable; |
551 | AV *stringref; |
615 | AV *stringref; |
|
|
616 | SV *decode_tagged; |
552 | } dec_t; |
617 | } dec_t; |
553 | |
618 | |
554 | #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 |
555 | |
620 | |
556 | #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") |
… | |
… | |
559 | #define DEC_DEC_DEPTH --dec->depth |
624 | #define DEC_DEC_DEPTH --dec->depth |
560 | |
625 | |
561 | static UV |
626 | static UV |
562 | decode_uint (dec_t *dec) |
627 | decode_uint (dec_t *dec) |
563 | { |
628 | { |
564 | switch (*dec->cur & 31) |
629 | U8 m = *dec->cur & MINOR_MASK; |
565 | { |
630 | ++dec->cur; |
566 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|
567 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
|
|
568 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
|
|
569 | return *dec->cur++ & 31; |
|
|
570 | |
631 | |
571 | case 24: |
632 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
633 | return m; |
|
|
634 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
635 | { |
572 | WANT (2); |
636 | WANT (1); |
573 | dec->cur += 2; |
637 | dec->cur += 1; |
574 | return dec->cur[-1]; |
638 | return dec->cur[-1]; |
575 | |
639 | } |
576 | case 25: |
640 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
641 | { |
577 | WANT (3); |
642 | WANT (2); |
578 | dec->cur += 3; |
643 | dec->cur += 2; |
579 | return (((UV)dec->cur[-2]) << 8) |
644 | return (((UV)dec->cur[-2]) << 8) |
580 | | ((UV)dec->cur[-1]); |
645 | | ((UV)dec->cur[-1]); |
581 | |
646 | } |
582 | case 26: |
647 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
648 | { |
583 | WANT (5); |
649 | WANT (4); |
584 | dec->cur += 5; |
650 | dec->cur += 4; |
585 | return (((UV)dec->cur[-4]) << 24) |
651 | return (((UV)dec->cur[-4]) << 24) |
586 | | (((UV)dec->cur[-3]) << 16) |
652 | | (((UV)dec->cur[-3]) << 16) |
587 | | (((UV)dec->cur[-2]) << 8) |
653 | | (((UV)dec->cur[-2]) << 8) |
588 | | ((UV)dec->cur[-1]); |
654 | | ((UV)dec->cur[-1]); |
589 | |
655 | } |
590 | case 27: |
656 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
657 | { |
591 | WANT (9); |
658 | WANT (8); |
592 | dec->cur += 9; |
659 | dec->cur += 8; |
|
|
660 | |
|
|
661 | return |
|
|
662 | #if UVSIZE < 8 |
|
|
663 | 0 |
|
|
664 | #else |
593 | return (((UV)dec->cur[-8]) << 56) |
665 | (((UV)dec->cur[-8]) << 56) |
594 | | (((UV)dec->cur[-7]) << 48) |
666 | | (((UV)dec->cur[-7]) << 48) |
595 | | (((UV)dec->cur[-6]) << 40) |
667 | | (((UV)dec->cur[-6]) << 40) |
596 | | (((UV)dec->cur[-5]) << 32) |
668 | | (((UV)dec->cur[-5]) << 32) |
|
|
669 | #endif |
597 | | (((UV)dec->cur[-4]) << 24) |
670 | | (((UV)dec->cur[-4]) << 24) |
598 | | (((UV)dec->cur[-3]) << 16) |
671 | | (((UV)dec->cur[-3]) << 16) |
599 | | (((UV)dec->cur[-2]) << 8) |
672 | | (((UV)dec->cur[-2]) << 8) |
600 | | ((UV)dec->cur[-1]); |
673 | | ((UV)dec->cur[-1]); |
601 | |
674 | } |
602 | default: |
675 | else |
603 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
676 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
604 | } |
|
|
605 | |
677 | |
606 | fail: |
678 | fail: |
607 | return 0; |
679 | return 0; |
608 | } |
680 | } |
609 | |
681 | |
… | |
… | |
614 | { |
686 | { |
615 | AV *av = newAV (); |
687 | AV *av = newAV (); |
616 | |
688 | |
617 | DEC_INC_DEPTH; |
689 | DEC_INC_DEPTH; |
618 | |
690 | |
619 | if ((*dec->cur & 31) == 31) |
691 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
620 | { |
692 | { |
621 | ++dec->cur; |
693 | ++dec->cur; |
622 | |
694 | |
623 | for (;;) |
695 | for (;;) |
624 | { |
696 | { |
625 | WANT (1); |
697 | WANT (1); |
626 | |
698 | |
627 | if (*dec->cur == (0xe0 | 31)) |
699 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
628 | { |
700 | { |
629 | ++dec->cur; |
701 | ++dec->cur; |
630 | break; |
702 | break; |
631 | } |
703 | } |
632 | |
704 | |
… | |
… | |
635 | } |
707 | } |
636 | else |
708 | else |
637 | { |
709 | { |
638 | int i, len = decode_uint (dec); |
710 | int i, len = decode_uint (dec); |
639 | |
711 | |
|
|
712 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
640 | av_fill (av, len - 1); |
713 | av_fill (av, len - 1); |
641 | |
714 | |
642 | for (i = 0; i < len; ++i) |
715 | for (i = 0; i < len; ++i) |
643 | AvARRAY (av)[i] = decode_sv (dec); |
716 | AvARRAY (av)[i] = decode_sv (dec); |
644 | } |
717 | } |
… | |
… | |
656 | decode_he (dec_t *dec, HV *hv) |
729 | decode_he (dec_t *dec, HV *hv) |
657 | { |
730 | { |
658 | // for speed reasons, we specialcase single-string |
731 | // for speed reasons, we specialcase single-string |
659 | // byte or utf-8 strings as keys, but only when !stringref |
732 | // byte or utf-8 strings as keys, but only when !stringref |
660 | |
733 | |
661 | if (expect_true (!dec->stringref)) |
734 | if (ecb_expect_true (!dec->stringref)) |
662 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
735 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
663 | { |
736 | { |
664 | I32 len = decode_uint (dec); |
737 | I32 len = decode_uint (dec); |
665 | char *key = (char *)dec->cur; |
738 | char *key = (char *)dec->cur; |
666 | |
739 | |
667 | dec->cur += len; |
740 | dec->cur += len; |
668 | |
741 | |
669 | if (ecb_expect_false (dec->stringref)) |
|
|
670 | av_push (dec->stringref, newSVpvn (key, len)); |
|
|
671 | |
|
|
672 | hv_store (hv, key, len, decode_sv (dec), 0); |
742 | hv_store (hv, key, len, decode_sv (dec), 0); |
673 | |
743 | |
674 | return; |
744 | return; |
675 | } |
745 | } |
676 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
746 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
677 | { |
747 | { |
678 | I32 len = decode_uint (dec); |
748 | I32 len = decode_uint (dec); |
679 | char *key = (char *)dec->cur; |
749 | char *key = (char *)dec->cur; |
680 | |
750 | |
681 | dec->cur += len; |
751 | dec->cur += len; |
682 | |
752 | |
683 | if (ecb_expect_false (dec->stringref)) |
753 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
684 | av_push (dec->stringref, newSVpvn_utf8 (key, len, 1)); |
754 | if (!is_utf8_string (key, len)) |
|
|
755 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
685 | |
756 | |
686 | hv_store (hv, key, -len, decode_sv (dec), 0); |
757 | hv_store (hv, key, -len, decode_sv (dec), 0); |
687 | |
758 | |
688 | return; |
759 | return; |
689 | } |
760 | } |
… | |
… | |
691 | SV *k = decode_sv (dec); |
762 | SV *k = decode_sv (dec); |
692 | SV *v = decode_sv (dec); |
763 | SV *v = decode_sv (dec); |
693 | |
764 | |
694 | hv_store_ent (hv, k, v, 0); |
765 | hv_store_ent (hv, k, v, 0); |
695 | SvREFCNT_dec (k); |
766 | SvREFCNT_dec (k); |
|
|
767 | |
|
|
768 | fail: |
|
|
769 | ; |
696 | } |
770 | } |
697 | |
771 | |
698 | static SV * |
772 | static SV * |
699 | decode_hv (dec_t *dec) |
773 | decode_hv (dec_t *dec) |
700 | { |
774 | { |
701 | HV *hv = newHV (); |
775 | HV *hv = newHV (); |
702 | |
776 | |
703 | DEC_INC_DEPTH; |
777 | DEC_INC_DEPTH; |
704 | |
778 | |
705 | if ((*dec->cur & 31) == 31) |
779 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
706 | { |
780 | { |
707 | ++dec->cur; |
781 | ++dec->cur; |
708 | |
782 | |
709 | for (;;) |
783 | for (;;) |
710 | { |
784 | { |
711 | WANT (1); |
785 | WANT (1); |
712 | |
786 | |
713 | if (*dec->cur == (0xe0 | 31)) |
787 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
714 | { |
788 | { |
715 | ++dec->cur; |
789 | ++dec->cur; |
716 | break; |
790 | break; |
717 | } |
791 | } |
718 | |
792 | |
… | |
… | |
739 | static SV * |
813 | static SV * |
740 | decode_str (dec_t *dec, int utf8) |
814 | decode_str (dec_t *dec, int utf8) |
741 | { |
815 | { |
742 | SV *sv = 0; |
816 | SV *sv = 0; |
743 | |
817 | |
744 | if ((*dec->cur & 31) == 31) |
818 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
745 | { |
819 | { |
|
|
820 | // indefinite length strings |
746 | ++dec->cur; |
821 | ++dec->cur; |
747 | |
822 | |
|
|
823 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
824 | |
748 | sv = newSVpvn ("", 0); |
825 | sv = newSVpvn ("", 0); |
749 | |
826 | |
750 | // not very fast, and certainly not robust against illegal input |
|
|
751 | for (;;) |
827 | for (;;) |
752 | { |
828 | { |
753 | WANT (1); |
829 | WANT (1); |
754 | |
830 | |
755 | if (*dec->cur == (0xe0 | 31)) |
831 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
832 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
756 | { |
833 | { |
757 | ++dec->cur; |
834 | ++dec->cur; |
758 | break; |
835 | break; |
759 | } |
836 | } |
|
|
837 | else |
|
|
838 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
760 | |
839 | |
761 | 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; |
762 | } |
845 | } |
763 | } |
846 | } |
764 | else |
847 | else |
765 | { |
848 | { |
766 | STRLEN len = decode_uint (dec); |
849 | STRLEN len = decode_uint (dec); |
767 | |
850 | |
768 | WANT (len); |
851 | WANT (len); |
769 | sv = newSVpvn (dec->cur, len); |
852 | sv = newSVpvn (dec->cur, len); |
770 | 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)); |
771 | } |
858 | } |
772 | |
859 | |
773 | 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 | |
774 | SvUTF8_on (sv); |
866 | SvUTF8_on (sv); |
775 | |
867 | } |
776 | if (ecb_expect_false (dec->stringref) |
|
|
777 | && SvCUR (sv) >= minimum_string_length (AvFILLp (dec->stringref) + 1)) |
|
|
778 | av_push (dec->stringref, SvREFCNT_inc_NN (sv)); |
|
|
779 | |
868 | |
780 | return sv; |
869 | return sv; |
781 | |
870 | |
782 | fail: |
871 | fail: |
783 | SvREFCNT_dec (sv); |
872 | SvREFCNT_dec (sv); |
… | |
… | |
815 | } |
904 | } |
816 | break; |
905 | break; |
817 | |
906 | |
818 | case CBOR_TAG_STRINGREF: |
907 | case CBOR_TAG_STRINGREF: |
819 | { |
908 | { |
820 | if ((*dec->cur >> 5) != 0) |
909 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
821 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
910 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
822 | |
911 | |
823 | UV idx = decode_uint (dec); |
912 | UV idx = decode_uint (dec); |
824 | |
913 | |
825 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
914 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
… | |
… | |
832 | case CBOR_TAG_VALUE_SHAREABLE: |
921 | case CBOR_TAG_VALUE_SHAREABLE: |
833 | { |
922 | { |
834 | if (ecb_expect_false (!dec->shareable)) |
923 | if (ecb_expect_false (!dec->shareable)) |
835 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
924 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
836 | |
925 | |
|
|
926 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
927 | { |
837 | sv = newSV (0); |
928 | sv = newSV (0); |
838 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
929 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
839 | |
930 | |
840 | SV *osv = decode_sv (dec); |
931 | SV *osv = decode_sv (dec); |
841 | sv_setsv (sv, osv); |
932 | sv_setsv (sv, osv); |
842 | SvREFCNT_dec_NN (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 | } |
843 | } |
942 | } |
844 | break; |
943 | break; |
845 | |
944 | |
846 | case CBOR_TAG_VALUE_SHAREDREF: |
945 | case CBOR_TAG_VALUE_SHAREDREF: |
847 | { |
946 | { |
848 | if ((*dec->cur >> 5) != 0) |
947 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
849 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
948 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
850 | |
949 | |
851 | UV idx = decode_uint (dec); |
950 | UV idx = decode_uint (dec); |
852 | |
951 | |
853 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
952 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
854 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
953 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
855 | |
954 | |
856 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[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"); |
857 | } |
959 | } |
858 | break; |
960 | break; |
859 | |
961 | |
860 | case CBOR_TAG_PERL_OBJECT: |
962 | case CBOR_TAG_PERL_OBJECT: |
861 | { |
963 | { |
… | |
… | |
910 | |
1012 | |
911 | default: |
1013 | default: |
912 | { |
1014 | { |
913 | sv = decode_sv (dec); |
1015 | sv = decode_sv (dec); |
914 | |
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 | { |
915 | AV *av = newAV (); |
1040 | AV *av = newAV (); |
916 | av_push (av, newSVuv (tag)); |
1041 | av_push (av, newSVuv (tag)); |
917 | av_push (av, sv); |
1042 | av_push (av, sv); |
918 | |
1043 | |
919 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
1044 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
920 | ? cbor_tagged_stash |
1045 | ? cbor_tagged_stash |
921 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
1046 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
922 | |
|
|
923 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
1047 | sv = sv_bless (newRV_noinc ((SV *)av), tagged_stash); |
|
|
1048 | } |
|
|
1049 | |
|
|
1050 | PUTBACK; |
|
|
1051 | |
|
|
1052 | FREETMPS; LEAVE; |
924 | } |
1053 | } |
925 | break; |
1054 | break; |
926 | } |
1055 | } |
927 | |
1056 | |
928 | return sv; |
1057 | return sv; |
… | |
… | |
935 | static SV * |
1064 | static SV * |
936 | decode_sv (dec_t *dec) |
1065 | decode_sv (dec_t *dec) |
937 | { |
1066 | { |
938 | WANT (1); |
1067 | WANT (1); |
939 | |
1068 | |
940 | switch (*dec->cur >> 5) |
1069 | switch (*dec->cur >> MAJOR_SHIFT) |
941 | { |
1070 | { |
942 | case 0: // unsigned int |
1071 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
943 | return newSVuv (decode_uint (dec)); |
1072 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
944 | case 1: // negative int |
1073 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
945 | return newSViv (-1 - (IV)decode_uint (dec)); |
1074 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
946 | case 2: // octet string |
1075 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
947 | return decode_str (dec, 0); |
1076 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
948 | case 3: // utf-8 string |
1077 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
949 | return decode_str (dec, 1); |
1078 | |
950 | case 4: // array |
1079 | case MAJOR_MISC >> MAJOR_SHIFT: |
951 | return decode_av (dec); |
|
|
952 | case 5: // map |
|
|
953 | return decode_hv (dec); |
|
|
954 | case 6: // tag |
|
|
955 | return decode_tagged (dec); |
|
|
956 | case 7: // misc |
|
|
957 | switch (*dec->cur++ & 31) |
1080 | switch (*dec->cur++ & MINOR_MASK) |
958 | { |
1081 | { |
959 | case 20: |
1082 | case SIMPLE_FALSE: |
960 | #if CBOR_SLOW |
1083 | #if CBOR_SLOW |
961 | types_false = get_bool ("Types::Serialiser::false"); |
1084 | types_false = get_bool ("Types::Serialiser::false"); |
962 | #endif |
1085 | #endif |
963 | return newSVsv (types_false); |
1086 | return newSVsv (types_false); |
964 | case 21: |
1087 | case SIMPLE_TRUE: |
965 | #if CBOR_SLOW |
1088 | #if CBOR_SLOW |
966 | types_true = get_bool ("Types::Serialiser::true"); |
1089 | types_true = get_bool ("Types::Serialiser::true"); |
967 | #endif |
1090 | #endif |
968 | return newSVsv (types_true); |
1091 | return newSVsv (types_true); |
969 | case 22: |
1092 | case SIMPLE_NULL: |
970 | return newSVsv (&PL_sv_undef); |
1093 | return newSVsv (&PL_sv_undef); |
971 | case 23: |
1094 | case SIMPLE_UNDEF: |
972 | #if CBOR_SLOW |
1095 | #if CBOR_SLOW |
973 | types_error = get_bool ("Types::Serialiser::error"); |
1096 | types_error = get_bool ("Types::Serialiser::error"); |
974 | #endif |
1097 | #endif |
975 | return newSVsv (types_error); |
1098 | return newSVsv (types_error); |
976 | |
1099 | |
977 | case 25: |
1100 | case MISC_FLOAT16: |
978 | { |
1101 | { |
979 | WANT (2); |
1102 | WANT (2); |
980 | |
1103 | |
981 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1104 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
982 | dec->cur += 2; |
1105 | dec->cur += 2; |
983 | |
1106 | |
984 | return newSVnv (ecb_binary16_to_float (fp)); |
1107 | return newSVnv (ecb_binary16_to_float (fp)); |
985 | } |
1108 | } |
986 | |
1109 | |
987 | case 26: |
1110 | case MISC_FLOAT32: |
988 | { |
1111 | { |
989 | uint32_t fp; |
1112 | uint32_t fp; |
990 | WANT (4); |
1113 | WANT (4); |
991 | memcpy (&fp, dec->cur, 4); |
1114 | memcpy (&fp, dec->cur, 4); |
992 | dec->cur += 4; |
1115 | dec->cur += 4; |
… | |
… | |
995 | fp = ecb_bswap32 (fp); |
1118 | fp = ecb_bswap32 (fp); |
996 | |
1119 | |
997 | return newSVnv (ecb_binary32_to_float (fp)); |
1120 | return newSVnv (ecb_binary32_to_float (fp)); |
998 | } |
1121 | } |
999 | |
1122 | |
1000 | case 27: |
1123 | case MISC_FLOAT64: |
1001 | { |
1124 | { |
1002 | uint64_t fp; |
1125 | uint64_t fp; |
1003 | WANT (8); |
1126 | WANT (8); |
1004 | memcpy (&fp, dec->cur, 8); |
1127 | memcpy (&fp, dec->cur, 8); |
1005 | dec->cur += 8; |
1128 | dec->cur += 8; |
… | |
… | |
1008 | fp = ecb_bswap64 (fp); |
1131 | fp = ecb_bswap64 (fp); |
1009 | |
1132 | |
1010 | return newSVnv (ecb_binary64_to_double (fp)); |
1133 | return newSVnv (ecb_binary64_to_double (fp)); |
1011 | } |
1134 | } |
1012 | |
1135 | |
1013 | // 0..19 unassigned |
1136 | // 0..19 unassigned simple |
1014 | // 24 reserved + unassigned (reserved values are not encodable) |
1137 | // 24 reserved + unassigned (reserved values are not encodable) |
1015 | default: |
1138 | default: |
1016 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1139 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1017 | } |
1140 | } |
1018 | |
1141 | |
… | |
… | |
1073 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1196 | types_error_stash = gv_stashpv ("Types::Serialiser::Error" , 1); |
1074 | |
1197 | |
1075 | types_true = get_bool ("Types::Serialiser::true" ); |
1198 | types_true = get_bool ("Types::Serialiser::true" ); |
1076 | types_false = get_bool ("Types::Serialiser::false"); |
1199 | types_false = get_bool ("Types::Serialiser::false"); |
1077 | 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); |
1078 | |
1203 | |
1079 | sv_cbor = newSVpv ("CBOR", 0); |
1204 | sv_cbor = newSVpv ("CBOR", 0); |
1080 | SvREADONLY_on (sv_cbor); |
1205 | SvREADONLY_on (sv_cbor); |
1081 | } |
1206 | } |
1082 | |
1207 | |
… | |
… | |
1104 | void shrink (CBOR *self, int enable = 1) |
1229 | void shrink (CBOR *self, int enable = 1) |
1105 | ALIAS: |
1230 | ALIAS: |
1106 | shrink = F_SHRINK |
1231 | shrink = F_SHRINK |
1107 | allow_unknown = F_ALLOW_UNKNOWN |
1232 | allow_unknown = F_ALLOW_UNKNOWN |
1108 | allow_sharing = F_ALLOW_SHARING |
1233 | allow_sharing = F_ALLOW_SHARING |
|
|
1234 | allow_cycles = F_ALLOW_CYCLES |
1109 | dedup_strings = F_DEDUP_STRINGS |
1235 | pack_strings = F_PACK_STRINGS |
|
|
1236 | validate_utf8 = F_VALIDATE_UTF8 |
1110 | PPCODE: |
1237 | PPCODE: |
1111 | { |
1238 | { |
1112 | if (enable) |
1239 | if (enable) |
1113 | self->flags |= ix; |
1240 | self->flags |= ix; |
1114 | else |
1241 | else |
… | |
… | |
1120 | void get_shrink (CBOR *self) |
1247 | void get_shrink (CBOR *self) |
1121 | ALIAS: |
1248 | ALIAS: |
1122 | get_shrink = F_SHRINK |
1249 | get_shrink = F_SHRINK |
1123 | get_allow_unknown = F_ALLOW_UNKNOWN |
1250 | get_allow_unknown = F_ALLOW_UNKNOWN |
1124 | get_allow_sharing = F_ALLOW_SHARING |
1251 | get_allow_sharing = F_ALLOW_SHARING |
|
|
1252 | get_allow_cycles = F_ALLOW_CYCLES |
1125 | get_dedup_strings = F_DEDUP_STRINGS |
1253 | get_pack_strings = F_PACK_STRINGS |
|
|
1254 | get_validate_utf8 = F_VALIDATE_UTF8 |
1126 | PPCODE: |
1255 | PPCODE: |
1127 | XPUSHs (boolSV (self->flags & ix)); |
1256 | XPUSHs (boolSV (self->flags & ix)); |
1128 | |
1257 | |
1129 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1258 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1130 | PPCODE: |
1259 | PPCODE: |
… | |
… | |
1143 | XPUSHs (ST (0)); |
1272 | XPUSHs (ST (0)); |
1144 | |
1273 | |
1145 | int get_max_size (CBOR *self) |
1274 | int get_max_size (CBOR *self) |
1146 | CODE: |
1275 | CODE: |
1147 | 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); |
1148 | OUTPUT: |
1289 | OUTPUT: |
1149 | RETVAL |
1290 | RETVAL |
1150 | |
1291 | |
1151 | void encode (CBOR *self, SV *scalar) |
1292 | void encode (CBOR *self, SV *scalar) |
1152 | PPCODE: |
1293 | PPCODE: |
… | |
… | |
1167 | EXTEND (SP, 2); |
1308 | EXTEND (SP, 2); |
1168 | PUSHs (sv); |
1309 | PUSHs (sv); |
1169 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1310 | PUSHs (sv_2mortal (newSVuv (offset - SvPVX (cborstr)))); |
1170 | } |
1311 | } |
1171 | |
1312 | |
|
|
1313 | void DESTROY (CBOR *self) |
|
|
1314 | PPCODE: |
|
|
1315 | cbor_free (self); |
|
|
1316 | |
1172 | PROTOTYPES: ENABLE |
1317 | PROTOTYPES: ENABLE |
1173 | |
1318 | |
1174 | void encode_cbor (SV *scalar) |
1319 | void encode_cbor (SV *scalar) |
|
|
1320 | ALIAS: |
|
|
1321 | encode_cbor = 0 |
|
|
1322 | encode_cbor_sharing = F_ALLOW_SHARING |
1175 | PPCODE: |
1323 | PPCODE: |
1176 | { |
1324 | { |
1177 | CBOR cbor; |
1325 | CBOR cbor; |
1178 | cbor_init (&cbor); |
1326 | cbor_init (&cbor); |
|
|
1327 | cbor.flags |= ix; |
1179 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1328 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1180 | XPUSHs (scalar); |
1329 | XPUSHs (scalar); |
1181 | } |
1330 | } |
1182 | |
1331 | |
1183 | void decode_cbor (SV *cborstr) |
1332 | void decode_cbor (SV *cborstr) |