… | |
… | |
23 | #endif |
23 | #endif |
24 | #ifndef SvREFCNT_dec_NN |
24 | #ifndef SvREFCNT_dec_NN |
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
25 | # define SvREFCNT_dec_NN(sv) SvREFCNT_dec (sv) |
26 | #endif |
26 | #endif |
27 | |
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 | }; |
|
|
64 | |
28 | // known tags |
65 | // known tags |
29 | enum cbor_tag |
66 | enum cbor_tag |
30 | { |
67 | { |
31 | // extensions |
68 | // extensions |
32 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
69 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
33 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
70 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
34 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
71 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
35 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
72 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
36 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
73 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
37 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
74 | CBOR_TAG_STRINGREF_NAMESPACE = 256, // http://cbor.schmorp.de/stringref |
38 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
75 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
39 | |
76 | |
40 | // rfc7049 |
77 | // rfc7049 |
41 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
78 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
42 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
79 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
43 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
80 | CBOR_TAG_POS_BIGNUM = 2, // byte string |
44 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
81 | CBOR_TAG_NEG_BIGNUM = 3, // byte string |
45 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
82 | CBOR_TAG_DECIMAL = 4, // decimal fraction, array |
46 | CBOR_TAG_BIGFLOAT = 5, // array |
83 | CBOR_TAG_BIGFLOAT = 5, // array |
47 | |
84 | |
48 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
85 | CBOR_TAG_CONV_B64U = 21, // base64url, any |
49 | CBOR_TAG_CONV_B64 = 22, // base64, any |
86 | CBOR_TAG_CONV_B64 = 22, // base64, any |
50 | CBOR_TAG_CONV_HEX = 23, // base16, any |
87 | CBOR_TAG_CONV_HEX = 23, // base16, any |
51 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
88 | CBOR_TAG_CBOR = 24, // embedded cbor, byte string |
52 | |
89 | |
53 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
90 | CBOR_TAG_URI = 32, // URI rfc3986, utf-8 |
54 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
91 | CBOR_TAG_B64U = 33, // base64url rfc4648, utf-8 |
55 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
92 | CBOR_TAG_B64 = 34, // base6 rfc46484, utf-8 |
56 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
93 | CBOR_TAG_REGEX = 35, // regex pcre/ecma262, utf-8 |
57 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
94 | CBOR_TAG_MIME = 36, // mime message rfc2045, utf-8 |
58 | |
95 | |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
96 | CBOR_TAG_MAGIC = 55799, // self-describe cbor |
60 | }; |
97 | }; |
61 | |
98 | |
62 | #define F_SHRINK 0x00000001UL |
99 | #define F_SHRINK 0x00000001UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
100 | #define F_ALLOW_UNKNOWN 0x00000002UL |
64 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
101 | #define F_ALLOW_SHARING 0x00000004UL |
65 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
102 | #define F_ALLOW_CYCLES 0x00000008UL |
|
|
103 | #define F_PACK_STRINGS 0x00000010UL |
66 | |
104 | |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
105 | #define INIT_SIZE 32 // initial scalar size to be allocated |
68 | |
106 | |
69 | #define SB do { |
107 | #define SB do { |
70 | #define SE } while (0) |
108 | #define SE } while (0) |
… | |
… | |
189 | static void |
227 | static void |
190 | encode_uint (enc_t *enc, int major, UV len) |
228 | encode_uint (enc_t *enc, int major, UV len) |
191 | { |
229 | { |
192 | need (enc, 9); |
230 | need (enc, 9); |
193 | |
231 | |
194 | if (len < 24) |
232 | if (ecb_expect_true (len < LENGTH_EXT1)) |
195 | *enc->cur++ = major | len; |
233 | *enc->cur++ = major | len; |
196 | else if (len <= 0xff) |
234 | else if (ecb_expect_true (len <= 0xffU)) |
197 | { |
235 | { |
198 | *enc->cur++ = major | 24; |
236 | *enc->cur++ = major | LENGTH_EXT1; |
199 | *enc->cur++ = len; |
237 | *enc->cur++ = len; |
200 | } |
238 | } |
201 | else if (len <= 0xffff) |
239 | else if (len <= 0xffffU) |
202 | { |
240 | { |
203 | *enc->cur++ = major | 25; |
241 | *enc->cur++ = major | LENGTH_EXT2; |
204 | *enc->cur++ = len >> 8; |
242 | *enc->cur++ = len >> 8; |
205 | *enc->cur++ = len; |
243 | *enc->cur++ = len; |
206 | } |
244 | } |
207 | else if (len <= 0xffffffff) |
245 | else if (len <= 0xffffffffU) |
208 | { |
246 | { |
209 | *enc->cur++ = major | 26; |
247 | *enc->cur++ = major | LENGTH_EXT4; |
210 | *enc->cur++ = len >> 24; |
248 | *enc->cur++ = len >> 24; |
211 | *enc->cur++ = len >> 16; |
249 | *enc->cur++ = len >> 16; |
212 | *enc->cur++ = len >> 8; |
250 | *enc->cur++ = len >> 8; |
213 | *enc->cur++ = len; |
251 | *enc->cur++ = len; |
214 | } |
252 | } |
215 | else |
253 | else |
216 | { |
254 | { |
217 | *enc->cur++ = major | 27; |
255 | *enc->cur++ = major | LENGTH_EXT8; |
218 | *enc->cur++ = len >> 56; |
256 | *enc->cur++ = len >> 56; |
219 | *enc->cur++ = len >> 48; |
257 | *enc->cur++ = len >> 48; |
220 | *enc->cur++ = len >> 40; |
258 | *enc->cur++ = len >> 40; |
221 | *enc->cur++ = len >> 32; |
259 | *enc->cur++ = len >> 32; |
222 | *enc->cur++ = len >> 24; |
260 | *enc->cur++ = len >> 24; |
… | |
… | |
227 | } |
265 | } |
228 | |
266 | |
229 | ecb_inline void |
267 | ecb_inline void |
230 | encode_tag (enc_t *enc, UV tag) |
268 | encode_tag (enc_t *enc, UV tag) |
231 | { |
269 | { |
232 | encode_uint (enc, 0xc0, tag); |
270 | encode_uint (enc, MAJOR_TAG, tag); |
233 | } |
271 | } |
234 | |
272 | |
235 | ecb_inline void |
273 | ecb_inline void |
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
274 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
237 | { |
275 | { |
238 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
276 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
239 | need (enc, len); |
277 | need (enc, len); |
240 | memcpy (enc->cur, str, len); |
278 | memcpy (enc->cur, str, len); |
241 | enc->cur += len; |
279 | enc->cur += len; |
242 | } |
280 | } |
243 | |
281 | |
244 | static void |
282 | static void |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
246 | { |
284 | { |
247 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
285 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
248 | { |
286 | { |
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
287 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
250 | |
288 | |
251 | if (SvOK (*svp)) |
289 | if (SvOK (*svp)) |
252 | { |
290 | { |
253 | // already registered, use stringref |
291 | // already registered, use stringref |
254 | encode_tag (enc, CBOR_TAG_STRINGREF); |
292 | encode_tag (enc, CBOR_TAG_STRINGREF); |
255 | encode_uint (enc, 0x00, SvUV (*svp)); |
293 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
256 | return; |
294 | return; |
257 | } |
295 | } |
258 | else if (len >= minimum_string_length (enc->stringref_idx)) |
296 | else if (len >= minimum_string_length (enc->stringref_idx)) |
259 | { |
297 | { |
260 | // register only |
298 | // register only |
… | |
… | |
276 | if (enc->depth >= enc->cbor.max_depth) |
314 | if (enc->depth >= enc->cbor.max_depth) |
277 | croak (ERR_NESTING_EXCEEDED); |
315 | croak (ERR_NESTING_EXCEEDED); |
278 | |
316 | |
279 | ++enc->depth; |
317 | ++enc->depth; |
280 | |
318 | |
281 | encode_uint (enc, 0x80, len + 1); |
319 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
282 | |
320 | |
283 | for (i = 0; i <= len; ++i) |
321 | for (i = 0; i <= len; ++i) |
284 | { |
322 | { |
285 | SV **svp = av_fetch (av, i, 0); |
323 | SV **svp = av_fetch (av, i, 0); |
286 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
324 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
… | |
… | |
301 | |
339 | |
302 | int pairs = hv_iterinit (hv); |
340 | int pairs = hv_iterinit (hv); |
303 | int mg = SvMAGICAL (hv); |
341 | int mg = SvMAGICAL (hv); |
304 | |
342 | |
305 | if (mg) |
343 | if (mg) |
306 | encode_ch (enc, 0xa0 | 31); |
344 | encode_ch (enc, MAJOR_MAP | MINOR_INDEF); |
307 | else |
345 | else |
308 | encode_uint (enc, 0xa0, pairs); |
346 | encode_uint (enc, MAJOR_MAP, pairs); |
309 | |
347 | |
310 | while ((he = hv_iternext (hv))) |
348 | while ((he = hv_iternext (hv))) |
311 | { |
349 | { |
312 | if (HeKLEN (he) == HEf_SVKEY) |
350 | if (HeKLEN (he) == HEf_SVKEY) |
313 | encode_sv (enc, HeSVKEY (he)); |
351 | encode_sv (enc, HeSVKEY (he)); |
… | |
… | |
316 | |
354 | |
317 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
355 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
318 | } |
356 | } |
319 | |
357 | |
320 | if (mg) |
358 | if (mg) |
321 | encode_ch (enc, 0xe0 | 31); |
359 | encode_ch (enc, MAJOR_MISC | MINOR_INDEF); |
322 | |
360 | |
323 | --enc->depth; |
361 | --enc->depth; |
324 | } |
362 | } |
325 | |
363 | |
326 | // encode objects, arrays and special \0=false and \1=true values. |
364 | // encode objects, arrays and special \0=false and \1=true values. |
327 | static void |
365 | static void |
328 | encode_rv (enc_t *enc, SV *sv) |
366 | encode_rv (enc_t *enc, SV *sv) |
329 | { |
367 | { |
330 | SvGETMAGIC (sv); |
368 | SvGETMAGIC (sv); |
331 | |
|
|
332 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
333 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
334 | { |
|
|
335 | if (!enc->shareable) |
|
|
336 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
337 | |
|
|
338 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
339 | |
|
|
340 | if (SvOK (*svp)) |
|
|
341 | { |
|
|
342 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
343 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
344 | return; |
|
|
345 | } |
|
|
346 | else |
|
|
347 | { |
|
|
348 | sv_setuv (*svp, enc->shareable_idx); |
|
|
349 | ++enc->shareable_idx; |
|
|
350 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
351 | } |
|
|
352 | } |
|
|
353 | |
369 | |
354 | svtype svt = SvTYPE (sv); |
370 | svtype svt = SvTYPE (sv); |
355 | |
371 | |
356 | if (ecb_expect_false (SvOBJECT (sv))) |
372 | if (ecb_expect_false (SvOBJECT (sv))) |
357 | { |
373 | { |
… | |
… | |
364 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
380 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
365 | ? cbor_tagged_stash |
381 | ? cbor_tagged_stash |
366 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
382 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
367 | |
383 | |
368 | HV *stash = SvSTASH (sv); |
384 | HV *stash = SvSTASH (sv); |
369 | GV *method; |
|
|
370 | |
385 | |
371 | if (stash == boolean_stash) |
386 | if (stash == boolean_stash) |
372 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
387 | { |
|
|
388 | encode_ch (enc, SvIV (sv) ? MAJOR_MISC | SIMPLE_TRUE : MAJOR_MISC | SIMPLE_FALSE); |
|
|
389 | return; |
|
|
390 | } |
373 | else if (stash == error_stash) |
391 | else if (stash == error_stash) |
374 | encode_ch (enc, 0xe0 | 23); |
392 | { |
|
|
393 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
|
|
394 | return; |
|
|
395 | } |
375 | else if (stash == tagged_stash) |
396 | else if (stash == tagged_stash) |
376 | { |
397 | { |
377 | if (svt != SVt_PVAV) |
398 | if (svt != SVt_PVAV) |
378 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
399 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
379 | |
400 | |
380 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
401 | encode_uint (enc, MAJOR_TAG, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
381 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
402 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
403 | |
|
|
404 | return; |
|
|
405 | } |
|
|
406 | } |
|
|
407 | |
|
|
408 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
409 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
410 | { |
|
|
411 | if (!enc->shareable) |
|
|
412 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
413 | |
|
|
414 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
415 | |
|
|
416 | if (SvOK (*svp)) |
382 | } |
417 | { |
|
|
418 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
419 | encode_uint (enc, MAJOR_POS_INT, SvUV (*svp)); |
|
|
420 | return; |
|
|
421 | } |
|
|
422 | else |
|
|
423 | { |
|
|
424 | sv_setuv (*svp, enc->shareable_idx); |
|
|
425 | ++enc->shareable_idx; |
|
|
426 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
427 | } |
|
|
428 | } |
|
|
429 | |
|
|
430 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
431 | { |
|
|
432 | HV *stash = SvSTASH (sv); |
|
|
433 | GV *method; |
|
|
434 | |
383 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
435 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
384 | { |
436 | { |
385 | dSP; |
437 | dSP; |
386 | |
438 | |
387 | ENTER; SAVETMPS; PUSHMARK (SP); |
439 | ENTER; SAVETMPS; PUSHMARK (SP); |
388 | // we re-bless the reference to get overload and other niceties right |
440 | // we re-bless the reference to get overload and other niceties right |
… | |
… | |
420 | // catch this surprisingly common error |
472 | // catch this surprisingly common error |
421 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
473 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
422 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
474 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
423 | |
475 | |
424 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
476 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
425 | encode_uint (enc, 0x80, count + 1); |
477 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
426 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
478 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
427 | |
479 | |
428 | while (count) |
480 | while (count) |
429 | encode_sv (enc, SP[1 - count--]); |
481 | encode_sv (enc, SP[1 - count--]); |
430 | |
482 | |
… | |
… | |
452 | { |
504 | { |
453 | double nv = SvNVX (sv); |
505 | double nv = SvNVX (sv); |
454 | |
506 | |
455 | need (enc, 9); |
507 | need (enc, 9); |
456 | |
508 | |
457 | if (ecb_expect_false (nv == (U32)nv)) |
509 | if (ecb_expect_false (nv == (NV)(U32)nv)) |
458 | encode_uint (enc, 0x00, (U32)nv); |
510 | encode_uint (enc, MAJOR_POS_INT, (U32)nv); |
459 | //TODO: maybe I32? |
511 | //TODO: maybe I32? |
460 | else if (ecb_expect_false (nv == (float)nv)) |
512 | else if (ecb_expect_false (nv == (float)nv)) |
461 | { |
513 | { |
462 | uint32_t fp = ecb_float_to_binary32 (nv); |
514 | uint32_t fp = ecb_float_to_binary32 (nv); |
463 | |
515 | |
464 | *enc->cur++ = 0xe0 | 26; |
516 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT32; |
465 | |
517 | |
466 | if (!ecb_big_endian ()) |
518 | if (!ecb_big_endian ()) |
467 | fp = ecb_bswap32 (fp); |
519 | fp = ecb_bswap32 (fp); |
468 | |
520 | |
469 | memcpy (enc->cur, &fp, 4); |
521 | memcpy (enc->cur, &fp, 4); |
… | |
… | |
471 | } |
523 | } |
472 | else |
524 | else |
473 | { |
525 | { |
474 | uint64_t fp = ecb_double_to_binary64 (nv); |
526 | uint64_t fp = ecb_double_to_binary64 (nv); |
475 | |
527 | |
476 | *enc->cur++ = 0xe0 | 27; |
528 | *enc->cur++ = MAJOR_MISC | MISC_FLOAT64; |
477 | |
529 | |
478 | if (!ecb_big_endian ()) |
530 | if (!ecb_big_endian ()) |
479 | fp = ecb_bswap64 (fp); |
531 | fp = ecb_bswap64 (fp); |
480 | |
532 | |
481 | memcpy (enc->cur, &fp, 8); |
533 | memcpy (enc->cur, &fp, 8); |
… | |
… | |
497 | else if (SvNOKp (sv)) |
549 | else if (SvNOKp (sv)) |
498 | encode_nv (enc, sv); |
550 | encode_nv (enc, sv); |
499 | else if (SvIOKp (sv)) |
551 | else if (SvIOKp (sv)) |
500 | { |
552 | { |
501 | if (SvIsUV (sv)) |
553 | if (SvIsUV (sv)) |
502 | encode_uint (enc, 0x00, SvUVX (sv)); |
554 | encode_uint (enc, MAJOR_POS_INT, SvUVX (sv)); |
503 | else if (SvIVX (sv) >= 0) |
555 | else if (SvIVX (sv) >= 0) |
504 | encode_uint (enc, 0x00, SvIVX (sv)); |
556 | encode_uint (enc, MAJOR_POS_INT, SvIVX (sv)); |
505 | else |
557 | else |
506 | encode_uint (enc, 0x20, -(SvIVX (sv) + 1)); |
558 | encode_uint (enc, MAJOR_NEG_INT, -(SvIVX (sv) + 1)); |
507 | } |
559 | } |
508 | else if (SvROK (sv)) |
560 | else if (SvROK (sv)) |
509 | encode_rv (enc, SvRV (sv)); |
561 | encode_rv (enc, SvRV (sv)); |
510 | else if (!SvOK (sv)) |
562 | else if (!SvOK (sv)) |
511 | encode_ch (enc, 0xe0 | 22); |
563 | encode_ch (enc, MAJOR_MISC | SIMPLE_NULL); |
512 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
564 | else if (enc->cbor.flags & F_ALLOW_UNKNOWN) |
513 | encode_ch (enc, 0xe0 | 23); |
565 | encode_ch (enc, MAJOR_MISC | SIMPLE_UNDEF); |
514 | else |
566 | else |
515 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
567 | croak ("encountered perl type (%s,0x%x) that CBOR cannot handle, check your input data", |
516 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
568 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
517 | } |
569 | } |
518 | |
570 | |
… | |
… | |
526 | enc.cur = SvPVX (enc.sv); |
578 | enc.cur = SvPVX (enc.sv); |
527 | enc.end = SvEND (enc.sv); |
579 | enc.end = SvEND (enc.sv); |
528 | |
580 | |
529 | SvPOK_only (enc.sv); |
581 | SvPOK_only (enc.sv); |
530 | |
582 | |
531 | if (cbor->flags & F_ALLOW_STRINGREF) |
583 | if (cbor->flags & F_PACK_STRINGS) |
532 | { |
584 | { |
533 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
585 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
534 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
586 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
535 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
587 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
536 | } |
588 | } |
… | |
… | |
571 | #define DEC_DEC_DEPTH --dec->depth |
623 | #define DEC_DEC_DEPTH --dec->depth |
572 | |
624 | |
573 | static UV |
625 | static UV |
574 | decode_uint (dec_t *dec) |
626 | decode_uint (dec_t *dec) |
575 | { |
627 | { |
576 | switch (*dec->cur & 31) |
628 | U8 m = *dec->cur & MINOR_MASK; |
577 | { |
629 | ++dec->cur; |
578 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|
579 | case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: |
|
|
580 | case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: |
|
|
581 | return *dec->cur++ & 31; |
|
|
582 | |
630 | |
583 | case 24: |
631 | if (ecb_expect_true (m < LENGTH_EXT1)) |
|
|
632 | return m; |
|
|
633 | else if (ecb_expect_true (m == LENGTH_EXT1)) |
|
|
634 | { |
584 | WANT (2); |
635 | WANT (1); |
585 | dec->cur += 2; |
636 | dec->cur += 1; |
586 | return dec->cur[-1]; |
637 | return dec->cur[-1]; |
587 | |
638 | } |
588 | case 25: |
639 | else if (ecb_expect_true (m == LENGTH_EXT2)) |
|
|
640 | { |
589 | WANT (3); |
641 | WANT (2); |
590 | dec->cur += 3; |
642 | dec->cur += 2; |
591 | return (((UV)dec->cur[-2]) << 8) |
643 | return (((UV)dec->cur[-2]) << 8) |
592 | | ((UV)dec->cur[-1]); |
644 | | ((UV)dec->cur[-1]); |
593 | |
645 | } |
594 | case 26: |
646 | else if (ecb_expect_true (m == LENGTH_EXT4)) |
|
|
647 | { |
595 | WANT (5); |
648 | WANT (4); |
596 | dec->cur += 5; |
649 | dec->cur += 4; |
597 | return (((UV)dec->cur[-4]) << 24) |
650 | return (((UV)dec->cur[-4]) << 24) |
598 | | (((UV)dec->cur[-3]) << 16) |
651 | | (((UV)dec->cur[-3]) << 16) |
599 | | (((UV)dec->cur[-2]) << 8) |
652 | | (((UV)dec->cur[-2]) << 8) |
600 | | ((UV)dec->cur[-1]); |
653 | | ((UV)dec->cur[-1]); |
601 | |
654 | } |
602 | case 27: |
655 | else if (ecb_expect_true (m == LENGTH_EXT8)) |
|
|
656 | { |
603 | WANT (9); |
657 | WANT (8); |
604 | dec->cur += 9; |
658 | dec->cur += 8; |
|
|
659 | |
|
|
660 | return |
|
|
661 | #if UVSIZE < 8 |
|
|
662 | 0 |
|
|
663 | #else |
605 | return (((UV)dec->cur[-8]) << 56) |
664 | (((UV)dec->cur[-8]) << 56) |
606 | | (((UV)dec->cur[-7]) << 48) |
665 | | (((UV)dec->cur[-7]) << 48) |
607 | | (((UV)dec->cur[-6]) << 40) |
666 | | (((UV)dec->cur[-6]) << 40) |
608 | | (((UV)dec->cur[-5]) << 32) |
667 | | (((UV)dec->cur[-5]) << 32) |
|
|
668 | #endif |
609 | | (((UV)dec->cur[-4]) << 24) |
669 | | (((UV)dec->cur[-4]) << 24) |
610 | | (((UV)dec->cur[-3]) << 16) |
670 | | (((UV)dec->cur[-3]) << 16) |
611 | | (((UV)dec->cur[-2]) << 8) |
671 | | (((UV)dec->cur[-2]) << 8) |
612 | | ((UV)dec->cur[-1]); |
672 | | ((UV)dec->cur[-1]); |
613 | |
673 | } |
614 | default: |
674 | else |
615 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
675 | ERR ("corrupted CBOR data (unsupported integer minor encoding)"); |
616 | } |
|
|
617 | |
676 | |
618 | fail: |
677 | fail: |
619 | return 0; |
678 | return 0; |
620 | } |
679 | } |
621 | |
680 | |
… | |
… | |
626 | { |
685 | { |
627 | AV *av = newAV (); |
686 | AV *av = newAV (); |
628 | |
687 | |
629 | DEC_INC_DEPTH; |
688 | DEC_INC_DEPTH; |
630 | |
689 | |
631 | if ((*dec->cur & 31) == 31) |
690 | if (*dec->cur == (MAJOR_ARRAY | MINOR_INDEF)) |
632 | { |
691 | { |
633 | ++dec->cur; |
692 | ++dec->cur; |
634 | |
693 | |
635 | for (;;) |
694 | for (;;) |
636 | { |
695 | { |
637 | WANT (1); |
696 | WANT (1); |
638 | |
697 | |
639 | if (*dec->cur == (0xe0 | 31)) |
698 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
640 | { |
699 | { |
641 | ++dec->cur; |
700 | ++dec->cur; |
642 | break; |
701 | break; |
643 | } |
702 | } |
644 | |
703 | |
… | |
… | |
647 | } |
706 | } |
648 | else |
707 | else |
649 | { |
708 | { |
650 | int i, len = decode_uint (dec); |
709 | int i, len = decode_uint (dec); |
651 | |
710 | |
|
|
711 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
652 | av_fill (av, len - 1); |
712 | av_fill (av, len - 1); |
653 | |
713 | |
654 | for (i = 0; i < len; ++i) |
714 | for (i = 0; i < len; ++i) |
655 | AvARRAY (av)[i] = decode_sv (dec); |
715 | AvARRAY (av)[i] = decode_sv (dec); |
656 | } |
716 | } |
… | |
… | |
669 | { |
729 | { |
670 | // for speed reasons, we specialcase single-string |
730 | // for speed reasons, we specialcase single-string |
671 | // byte or utf-8 strings as keys, but only when !stringref |
731 | // byte or utf-8 strings as keys, but only when !stringref |
672 | |
732 | |
673 | if (ecb_expect_true (!dec->stringref)) |
733 | if (ecb_expect_true (!dec->stringref)) |
674 | if (*dec->cur >= 0x40 && *dec->cur <= 0x40 + 27) |
734 | if (ecb_expect_true ((*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
675 | { |
735 | { |
676 | I32 len = decode_uint (dec); |
736 | I32 len = decode_uint (dec); |
677 | char *key = (char *)dec->cur; |
737 | char *key = (char *)dec->cur; |
678 | |
738 | |
679 | dec->cur += len; |
739 | dec->cur += len; |
… | |
… | |
683 | |
743 | |
684 | hv_store (hv, key, len, decode_sv (dec), 0); |
744 | hv_store (hv, key, len, decode_sv (dec), 0); |
685 | |
745 | |
686 | return; |
746 | return; |
687 | } |
747 | } |
688 | else if (*dec->cur >= 0x60 && *dec->cur <= 0x60 + 27) |
748 | else if (ecb_expect_true ((*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
689 | { |
749 | { |
690 | I32 len = decode_uint (dec); |
750 | I32 len = decode_uint (dec); |
691 | char *key = (char *)dec->cur; |
751 | char *key = (char *)dec->cur; |
692 | |
752 | |
693 | dec->cur += len; |
753 | dec->cur += len; |
… | |
… | |
712 | { |
772 | { |
713 | HV *hv = newHV (); |
773 | HV *hv = newHV (); |
714 | |
774 | |
715 | DEC_INC_DEPTH; |
775 | DEC_INC_DEPTH; |
716 | |
776 | |
717 | if ((*dec->cur & 31) == 31) |
777 | if (*dec->cur == (MAJOR_MAP | MINOR_INDEF)) |
718 | { |
778 | { |
719 | ++dec->cur; |
779 | ++dec->cur; |
720 | |
780 | |
721 | for (;;) |
781 | for (;;) |
722 | { |
782 | { |
723 | WANT (1); |
783 | WANT (1); |
724 | |
784 | |
725 | if (*dec->cur == (0xe0 | 31)) |
785 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
726 | { |
786 | { |
727 | ++dec->cur; |
787 | ++dec->cur; |
728 | break; |
788 | break; |
729 | } |
789 | } |
730 | |
790 | |
… | |
… | |
751 | static SV * |
811 | static SV * |
752 | decode_str (dec_t *dec, int utf8) |
812 | decode_str (dec_t *dec, int utf8) |
753 | { |
813 | { |
754 | SV *sv = 0; |
814 | SV *sv = 0; |
755 | |
815 | |
756 | if ((*dec->cur & 31) == 31) |
816 | if ((*dec->cur & MINOR_MASK) == MINOR_INDEF) |
757 | { |
817 | { |
|
|
818 | // indefinite length strings |
758 | ++dec->cur; |
819 | ++dec->cur; |
759 | |
820 | |
|
|
821 | U8 major = *dec->cur & MAJOR_MISC; |
|
|
822 | |
760 | sv = newSVpvn ("", 0); |
823 | sv = newSVpvn ("", 0); |
761 | |
824 | |
762 | // not very fast, and certainly not robust against illegal input |
|
|
763 | for (;;) |
825 | for (;;) |
764 | { |
826 | { |
765 | WANT (1); |
827 | WANT (1); |
766 | |
828 | |
767 | if (*dec->cur == (0xe0 | 31)) |
829 | if ((*dec->cur - major) > LENGTH_EXT8) |
|
|
830 | if (*dec->cur == (MAJOR_MISC | MINOR_INDEF)) |
768 | { |
831 | { |
769 | ++dec->cur; |
832 | ++dec->cur; |
770 | break; |
833 | break; |
771 | } |
834 | } |
|
|
835 | else |
|
|
836 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
772 | |
837 | |
773 | sv_catsv (sv, decode_sv (dec)); |
838 | STRLEN len = decode_uint (dec); |
|
|
839 | |
|
|
840 | WANT (len); |
|
|
841 | sv_catpvn (sv, dec->cur, len); |
|
|
842 | dec->cur += len; |
774 | } |
843 | } |
775 | } |
844 | } |
776 | else |
845 | else |
777 | { |
846 | { |
778 | STRLEN len = decode_uint (dec); |
847 | STRLEN len = decode_uint (dec); |
… | |
… | |
827 | } |
896 | } |
828 | break; |
897 | break; |
829 | |
898 | |
830 | case CBOR_TAG_STRINGREF: |
899 | case CBOR_TAG_STRINGREF: |
831 | { |
900 | { |
832 | if ((*dec->cur >> 5) != 0) |
901 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
833 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
902 | ERR ("corrupted CBOR data (stringref index not an unsigned integer)"); |
834 | |
903 | |
835 | UV idx = decode_uint (dec); |
904 | UV idx = decode_uint (dec); |
836 | |
905 | |
837 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
906 | if (!dec->stringref || (int)idx > AvFILLp (dec->stringref)) |
… | |
… | |
844 | case CBOR_TAG_VALUE_SHAREABLE: |
913 | case CBOR_TAG_VALUE_SHAREABLE: |
845 | { |
914 | { |
846 | if (ecb_expect_false (!dec->shareable)) |
915 | if (ecb_expect_false (!dec->shareable)) |
847 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
916 | dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); |
848 | |
917 | |
|
|
918 | if (dec->cbor.flags & F_ALLOW_CYCLES) |
|
|
919 | { |
849 | sv = newSV (0); |
920 | sv = newSV (0); |
850 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
921 | av_push (dec->shareable, SvREFCNT_inc_NN (sv)); |
851 | |
922 | |
852 | SV *osv = decode_sv (dec); |
923 | SV *osv = decode_sv (dec); |
853 | sv_setsv (sv, osv); |
924 | sv_setsv (sv, osv); |
854 | SvREFCNT_dec_NN (osv); |
925 | SvREFCNT_dec_NN (osv); |
|
|
926 | } |
|
|
927 | else |
|
|
928 | { |
|
|
929 | av_push (dec->shareable, &PL_sv_undef); |
|
|
930 | int idx = AvFILLp (dec->shareable); |
|
|
931 | sv = decode_sv (dec); |
|
|
932 | av_store (dec->shareable, idx, SvREFCNT_inc_NN (sv)); |
|
|
933 | } |
855 | } |
934 | } |
856 | break; |
935 | break; |
857 | |
936 | |
858 | case CBOR_TAG_VALUE_SHAREDREF: |
937 | case CBOR_TAG_VALUE_SHAREDREF: |
859 | { |
938 | { |
860 | if ((*dec->cur >> 5) != 0) |
939 | if ((*dec->cur >> MAJOR_SHIFT) != (MAJOR_POS_INT >> MAJOR_SHIFT)) |
861 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
940 | ERR ("corrupted CBOR data (sharedref index not an unsigned integer)"); |
862 | |
941 | |
863 | UV idx = decode_uint (dec); |
942 | UV idx = decode_uint (dec); |
864 | |
943 | |
865 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
944 | if (!dec->shareable || (int)idx > AvFILLp (dec->shareable)) |
866 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
945 | ERR ("corrupted CBOR data (sharedref index out of bounds)"); |
867 | |
946 | |
868 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
947 | sv = SvREFCNT_inc_NN (AvARRAY (dec->shareable)[idx]); |
|
|
948 | |
|
|
949 | if (sv == &PL_sv_undef) |
|
|
950 | ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); |
869 | } |
951 | } |
870 | break; |
952 | break; |
871 | |
953 | |
872 | case CBOR_TAG_PERL_OBJECT: |
954 | case CBOR_TAG_PERL_OBJECT: |
873 | { |
955 | { |
… | |
… | |
974 | static SV * |
1056 | static SV * |
975 | decode_sv (dec_t *dec) |
1057 | decode_sv (dec_t *dec) |
976 | { |
1058 | { |
977 | WANT (1); |
1059 | WANT (1); |
978 | |
1060 | |
979 | switch (*dec->cur >> 5) |
1061 | switch (*dec->cur >> MAJOR_SHIFT) |
980 | { |
1062 | { |
981 | case 0: // unsigned int |
1063 | case MAJOR_POS_INT >> MAJOR_SHIFT: return newSVuv (decode_uint (dec)); |
982 | return newSVuv (decode_uint (dec)); |
1064 | case MAJOR_NEG_INT >> MAJOR_SHIFT: return newSViv (-1 - (IV)decode_uint (dec)); |
983 | case 1: // negative int |
1065 | case MAJOR_BYTES >> MAJOR_SHIFT: return decode_str (dec, 0); |
984 | return newSViv (-1 - (IV)decode_uint (dec)); |
1066 | case MAJOR_TEXT >> MAJOR_SHIFT: return decode_str (dec, 1); |
985 | case 2: // octet string |
1067 | case MAJOR_ARRAY >> MAJOR_SHIFT: return decode_av (dec); |
986 | return decode_str (dec, 0); |
1068 | case MAJOR_MAP >> MAJOR_SHIFT: return decode_hv (dec); |
987 | case 3: // utf-8 string |
1069 | case MAJOR_TAG >> MAJOR_SHIFT: return decode_tagged (dec); |
988 | return decode_str (dec, 1); |
1070 | |
989 | case 4: // array |
1071 | case MAJOR_MISC >> MAJOR_SHIFT: |
990 | return decode_av (dec); |
|
|
991 | case 5: // map |
|
|
992 | return decode_hv (dec); |
|
|
993 | case 6: // tag |
|
|
994 | return decode_tagged (dec); |
|
|
995 | case 7: // misc |
|
|
996 | switch (*dec->cur++ & 31) |
1072 | switch (*dec->cur++ & MINOR_MASK) |
997 | { |
1073 | { |
998 | case 20: |
1074 | case SIMPLE_FALSE: |
999 | #if CBOR_SLOW |
1075 | #if CBOR_SLOW |
1000 | types_false = get_bool ("Types::Serialiser::false"); |
1076 | types_false = get_bool ("Types::Serialiser::false"); |
1001 | #endif |
1077 | #endif |
1002 | return newSVsv (types_false); |
1078 | return newSVsv (types_false); |
1003 | case 21: |
1079 | case SIMPLE_TRUE: |
1004 | #if CBOR_SLOW |
1080 | #if CBOR_SLOW |
1005 | types_true = get_bool ("Types::Serialiser::true"); |
1081 | types_true = get_bool ("Types::Serialiser::true"); |
1006 | #endif |
1082 | #endif |
1007 | return newSVsv (types_true); |
1083 | return newSVsv (types_true); |
1008 | case 22: |
1084 | case SIMPLE_NULL: |
1009 | return newSVsv (&PL_sv_undef); |
1085 | return newSVsv (&PL_sv_undef); |
1010 | case 23: |
1086 | case SIMPLE_UNDEF: |
1011 | #if CBOR_SLOW |
1087 | #if CBOR_SLOW |
1012 | types_error = get_bool ("Types::Serialiser::error"); |
1088 | types_error = get_bool ("Types::Serialiser::error"); |
1013 | #endif |
1089 | #endif |
1014 | return newSVsv (types_error); |
1090 | return newSVsv (types_error); |
1015 | |
1091 | |
1016 | case 25: |
1092 | case MISC_FLOAT16: |
1017 | { |
1093 | { |
1018 | WANT (2); |
1094 | WANT (2); |
1019 | |
1095 | |
1020 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1096 | uint16_t fp = (dec->cur[0] << 8) | dec->cur[1]; |
1021 | dec->cur += 2; |
1097 | dec->cur += 2; |
1022 | |
1098 | |
1023 | return newSVnv (ecb_binary16_to_float (fp)); |
1099 | return newSVnv (ecb_binary16_to_float (fp)); |
1024 | } |
1100 | } |
1025 | |
1101 | |
1026 | case 26: |
1102 | case MISC_FLOAT32: |
1027 | { |
1103 | { |
1028 | uint32_t fp; |
1104 | uint32_t fp; |
1029 | WANT (4); |
1105 | WANT (4); |
1030 | memcpy (&fp, dec->cur, 4); |
1106 | memcpy (&fp, dec->cur, 4); |
1031 | dec->cur += 4; |
1107 | dec->cur += 4; |
… | |
… | |
1034 | fp = ecb_bswap32 (fp); |
1110 | fp = ecb_bswap32 (fp); |
1035 | |
1111 | |
1036 | return newSVnv (ecb_binary32_to_float (fp)); |
1112 | return newSVnv (ecb_binary32_to_float (fp)); |
1037 | } |
1113 | } |
1038 | |
1114 | |
1039 | case 27: |
1115 | case MISC_FLOAT64: |
1040 | { |
1116 | { |
1041 | uint64_t fp; |
1117 | uint64_t fp; |
1042 | WANT (8); |
1118 | WANT (8); |
1043 | memcpy (&fp, dec->cur, 8); |
1119 | memcpy (&fp, dec->cur, 8); |
1044 | dec->cur += 8; |
1120 | dec->cur += 8; |
… | |
… | |
1047 | fp = ecb_bswap64 (fp); |
1123 | fp = ecb_bswap64 (fp); |
1048 | |
1124 | |
1049 | return newSVnv (ecb_binary64_to_double (fp)); |
1125 | return newSVnv (ecb_binary64_to_double (fp)); |
1050 | } |
1126 | } |
1051 | |
1127 | |
1052 | // 0..19 unassigned |
1128 | // 0..19 unassigned simple |
1053 | // 24 reserved + unassigned (reserved values are not encodable) |
1129 | // 24 reserved + unassigned (reserved values are not encodable) |
1054 | default: |
1130 | default: |
1055 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1131 | ERR ("corrupted CBOR data (reserved/unassigned major 7 value)"); |
1056 | } |
1132 | } |
1057 | |
1133 | |
… | |
… | |
1145 | void shrink (CBOR *self, int enable = 1) |
1221 | void shrink (CBOR *self, int enable = 1) |
1146 | ALIAS: |
1222 | ALIAS: |
1147 | shrink = F_SHRINK |
1223 | shrink = F_SHRINK |
1148 | allow_unknown = F_ALLOW_UNKNOWN |
1224 | allow_unknown = F_ALLOW_UNKNOWN |
1149 | allow_sharing = F_ALLOW_SHARING |
1225 | allow_sharing = F_ALLOW_SHARING |
1150 | allow_stringref = F_ALLOW_STRINGREF |
1226 | allow_cycles = F_ALLOW_CYCLES |
|
|
1227 | pack_strings = F_PACK_STRINGS |
1151 | PPCODE: |
1228 | PPCODE: |
1152 | { |
1229 | { |
1153 | if (enable) |
1230 | if (enable) |
1154 | self->flags |= ix; |
1231 | self->flags |= ix; |
1155 | else |
1232 | else |
… | |
… | |
1161 | void get_shrink (CBOR *self) |
1238 | void get_shrink (CBOR *self) |
1162 | ALIAS: |
1239 | ALIAS: |
1163 | get_shrink = F_SHRINK |
1240 | get_shrink = F_SHRINK |
1164 | get_allow_unknown = F_ALLOW_UNKNOWN |
1241 | get_allow_unknown = F_ALLOW_UNKNOWN |
1165 | get_allow_sharing = F_ALLOW_SHARING |
1242 | get_allow_sharing = F_ALLOW_SHARING |
1166 | get_allow_stringref = F_ALLOW_STRINGREF |
1243 | get_allow_cycles = F_ALLOW_CYCLES |
|
|
1244 | get_pack_strings = F_PACK_STRINGS |
1167 | PPCODE: |
1245 | PPCODE: |
1168 | XPUSHs (boolSV (self->flags & ix)); |
1246 | XPUSHs (boolSV (self->flags & ix)); |
1169 | |
1247 | |
1170 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1248 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1171 | PPCODE: |
1249 | PPCODE: |
… | |
… | |
1227 | cbor_free (self); |
1305 | cbor_free (self); |
1228 | |
1306 | |
1229 | PROTOTYPES: ENABLE |
1307 | PROTOTYPES: ENABLE |
1230 | |
1308 | |
1231 | void encode_cbor (SV *scalar) |
1309 | void encode_cbor (SV *scalar) |
|
|
1310 | ALIAS: |
|
|
1311 | encode_cbor = 0 |
|
|
1312 | encode_cbor_sharing = F_ALLOW_SHARING |
1232 | PPCODE: |
1313 | PPCODE: |
1233 | { |
1314 | { |
1234 | CBOR cbor; |
1315 | CBOR cbor; |
1235 | cbor_init (&cbor); |
1316 | cbor_init (&cbor); |
|
|
1317 | cbor.flags |= ix; |
1236 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1318 | PUTBACK; scalar = encode_cbor (scalar, &cbor); SPAGAIN; |
1237 | XPUSHs (scalar); |
1319 | XPUSHs (scalar); |
1238 | } |
1320 | } |
1239 | |
1321 | |
1240 | void decode_cbor (SV *cborstr) |
1322 | void decode_cbor (SV *cborstr) |