… | |
… | |
100 | #define F_SHRINK 0x00000001UL |
100 | #define F_SHRINK 0x00000001UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
101 | #define F_ALLOW_UNKNOWN 0x00000002UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
102 | #define F_ALLOW_SHARING 0x00000004UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
103 | #define F_ALLOW_CYCLES 0x00000008UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
104 | #define F_PACK_STRINGS 0x00000010UL |
|
|
105 | #define F_TEXT_KEYS 0x00000020UL |
|
|
106 | #define F_TEXT_STRINGS 0x00000040UL |
105 | #define F_VALIDATE_UTF8 0x00000020UL |
107 | #define F_VALIDATE_UTF8 0x00000080UL |
106 | |
108 | |
107 | #define INIT_SIZE 32 // initial scalar size to be allocated |
109 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | |
110 | |
109 | #define SB do { |
111 | #define SB do { |
110 | #define SE } while (0) |
112 | #define SE } while (0) |
… | |
… | |
276 | encode_tag (enc_t *enc, UV tag) |
278 | encode_tag (enc_t *enc, UV tag) |
277 | { |
279 | { |
278 | encode_uint (enc, MAJOR_TAG, tag); |
280 | encode_uint (enc, MAJOR_TAG, tag); |
279 | } |
281 | } |
280 | |
282 | |
|
|
283 | // exceptional (hopefully) slow path for byte strings that need to be utf8-encoded |
|
|
284 | ecb_noinline static void |
|
|
285 | encode_str_utf8 (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
286 | { |
|
|
287 | STRLEN ulen = len; |
|
|
288 | U8 *p, *pend = (U8 *)str + len; |
|
|
289 | |
|
|
290 | for (p = (U8 *)str; p < pend; ++p) |
|
|
291 | ulen += *p >> 7; // count set high bits |
|
|
292 | |
|
|
293 | encode_uint (enc, MAJOR_TEXT, ulen); |
|
|
294 | |
|
|
295 | need (enc, ulen); |
|
|
296 | for (p = (U8 *)str; p < pend; ++p) |
|
|
297 | if (*p < 0x80) |
|
|
298 | *enc->cur++ = *p; |
|
|
299 | else |
|
|
300 | { |
|
|
301 | *enc->cur++ = 0xc0 + (*p >> 6); |
|
|
302 | *enc->cur++ = 0x80 + (*p & 63); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
281 | ecb_inline void |
306 | ecb_inline void |
282 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
307 | encode_str (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
283 | { |
308 | { |
|
|
309 | if (ecb_expect_false (upgrade_utf8)) |
|
|
310 | if (!utf8) |
|
|
311 | { |
|
|
312 | encode_str_utf8 (enc, utf8, str, len); |
|
|
313 | return; |
|
|
314 | } |
|
|
315 | |
284 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
316 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
285 | need (enc, len); |
317 | need (enc, len); |
286 | memcpy (enc->cur, str, len); |
318 | memcpy (enc->cur, str, len); |
287 | enc->cur += len; |
319 | enc->cur += len; |
288 | } |
320 | } |
289 | |
321 | |
290 | static void |
322 | ecb_inline void |
291 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
323 | encode_strref (enc_t *enc, int upgrade_utf8, int utf8, char *str, STRLEN len) |
292 | { |
324 | { |
293 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
325 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
294 | { |
326 | { |
295 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
327 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
296 | |
328 | |
… | |
… | |
307 | sv_setuv (*svp, enc->stringref_idx); |
339 | sv_setuv (*svp, enc->stringref_idx); |
308 | ++enc->stringref_idx; |
340 | ++enc->stringref_idx; |
309 | } |
341 | } |
310 | } |
342 | } |
311 | |
343 | |
312 | encode_str (enc, utf8, str, len); |
344 | encode_str (enc, upgrade_utf8, utf8, str, len); |
313 | } |
345 | } |
314 | |
346 | |
315 | static void encode_sv (enc_t *enc, SV *sv); |
347 | static void encode_sv (enc_t *enc, SV *sv); |
316 | |
348 | |
317 | static void |
349 | static void |
… | |
… | |
363 | while ((he = hv_iternext (hv))) |
395 | while ((he = hv_iternext (hv))) |
364 | { |
396 | { |
365 | if (HeKLEN (he) == HEf_SVKEY) |
397 | if (HeKLEN (he) == HEf_SVKEY) |
366 | encode_sv (enc, HeSVKEY (he)); |
398 | encode_sv (enc, HeSVKEY (he)); |
367 | else |
399 | else |
368 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
400 | encode_strref (enc, enc->cbor.flags & (F_TEXT_KEYS | F_TEXT_STRINGS), HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
369 | |
401 | |
370 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
402 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
371 | } |
403 | } |
372 | |
404 | |
373 | if (mg) |
405 | if (mg) |
… | |
… | |
491 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
523 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
492 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
524 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
493 | |
525 | |
494 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
495 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
496 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
497 | |
529 | |
498 | while (count) |
530 | while (count) |
499 | encode_sv (enc, SP[1 - count--]); |
531 | encode_sv (enc, SP[1 - count--]); |
500 | |
532 | |
501 | PUTBACK; |
533 | PUTBACK; |
… | |
… | |
560 | |
592 | |
561 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
562 | { |
594 | { |
563 | STRLEN len; |
595 | STRLEN len; |
564 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
565 | encode_strref (enc, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
566 | } |
598 | } |
567 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
568 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
569 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
570 | { |
602 | { |
… | |
… | |
633 | SV *decode_tagged; |
665 | SV *decode_tagged; |
634 | } dec_t; |
666 | } dec_t; |
635 | |
667 | |
636 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
668 | #define ERR(reason) SB if (!dec->err) dec->err = reason; goto fail; SE |
637 | |
669 | |
638 | #define WANT(len) if (ecb_expect_false (dec->cur + len > dec->end)) ERR ("unexpected end of CBOR data") |
670 | #define WANT(len) if (ecb_expect_false ((UV)(dec->end - dec->cur) < (UV)len)) ERR ("unexpected end of CBOR data") |
639 | |
671 | |
640 | #define DEC_INC_DEPTH if (++dec->depth > dec->cbor.max_depth) ERR (ERR_NESTING_EXCEEDED) |
672 | #define DEC_INC_DEPTH if (ecb_expect_false (++dec->depth > dec->cbor.max_depth)) ERR (ERR_NESTING_EXCEEDED) |
641 | #define DEC_DEC_DEPTH --dec->depth |
673 | #define DEC_DEC_DEPTH --dec->depth |
642 | |
674 | |
643 | static UV |
675 | static UV |
644 | decode_uint (dec_t *dec) |
676 | decode_uint (dec_t *dec) |
645 | { |
677 | { |
… | |
… | |
722 | av_push (av, decode_sv (dec)); |
754 | av_push (av, decode_sv (dec)); |
723 | } |
755 | } |
724 | } |
756 | } |
725 | else |
757 | else |
726 | { |
758 | { |
727 | int i, len = decode_uint (dec); |
759 | UV i, len = decode_uint (dec); |
728 | |
760 | |
729 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
761 | WANT (len); // complexity check for av_fill - need at least one byte per value, do not allow supersize arrays |
730 | av_fill (av, len - 1); |
762 | av_fill (av, len - 1); |
731 | |
763 | |
732 | for (i = 0; i < len; ++i) |
764 | for (i = 0; i < len; ++i) |
… | |
… | |
749 | // byte or utf-8 strings as keys, but only when !stringref |
781 | // byte or utf-8 strings as keys, but only when !stringref |
750 | |
782 | |
751 | if (ecb_expect_true (!dec->stringref)) |
783 | if (ecb_expect_true (!dec->stringref)) |
752 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
784 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
753 | { |
785 | { |
754 | I32 len = decode_uint (dec); |
786 | STRLEN len = decode_uint (dec); |
755 | char *key = (char *)dec->cur; |
787 | char *key = (char *)dec->cur; |
756 | |
788 | |
757 | WANT (len); |
789 | WANT (len); |
758 | dec->cur += len; |
790 | dec->cur += len; |
759 | |
791 | |
… | |
… | |
761 | |
793 | |
762 | return; |
794 | return; |
763 | } |
795 | } |
764 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
796 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
765 | { |
797 | { |
766 | I32 len = decode_uint (dec); |
798 | STRLEN len = decode_uint (dec); |
767 | char *key = (char *)dec->cur; |
799 | char *key = (char *)dec->cur; |
768 | |
800 | |
769 | WANT (len); |
801 | WANT (len); |
770 | dec->cur += len; |
802 | dec->cur += len; |
771 | |
803 | |
… | |
… | |
812 | decode_he (dec, hv); |
844 | decode_he (dec, hv); |
813 | } |
845 | } |
814 | } |
846 | } |
815 | else |
847 | else |
816 | { |
848 | { |
817 | int pairs = decode_uint (dec); |
849 | UV pairs = decode_uint (dec); |
|
|
850 | |
|
|
851 | WANT (pairs); // complexity check - need at least one byte per value, do not allow supersize hashes |
818 | |
852 | |
819 | while (pairs--) |
853 | while (pairs--) |
820 | decode_he (dec, hv); |
854 | decode_he (dec, hv); |
821 | } |
855 | } |
822 | |
856 | |
… | |
… | |
910 | sv = newRV_noinc (decode_sv (dec)); |
944 | sv = newRV_noinc (decode_sv (dec)); |
911 | break; |
945 | break; |
912 | |
946 | |
913 | case CBOR_TAG_STRINGREF_NAMESPACE: |
947 | case CBOR_TAG_STRINGREF_NAMESPACE: |
914 | { |
948 | { |
|
|
949 | // do nmot use SAVETMPS/FREETMPS, as these will |
|
|
950 | // erase mortalised caches, e.g. "shareable" |
915 | ENTER; SAVETMPS; |
951 | ENTER; |
916 | |
952 | |
917 | SAVESPTR (dec->stringref); |
953 | SAVESPTR (dec->stringref); |
918 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
954 | dec->stringref = (AV *)sv_2mortal ((SV *)newAV ()); |
919 | |
955 | |
920 | sv = decode_sv (dec); |
956 | sv = decode_sv (dec); |
921 | |
957 | |
922 | FREETMPS; LEAVE; |
958 | LEAVE; |
923 | } |
959 | } |
924 | break; |
960 | break; |
925 | |
961 | |
926 | case CBOR_TAG_STRINGREF: |
962 | case CBOR_TAG_STRINGREF: |
927 | { |
963 | { |
… | |
… | |
1360 | |
1396 | |
1361 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1397 | default_filter = newSVpv ("CBOR::XS::default_filter", 0); |
1362 | |
1398 | |
1363 | sv_cbor = newSVpv ("CBOR", 0); |
1399 | sv_cbor = newSVpv ("CBOR", 0); |
1364 | SvREADONLY_on (sv_cbor); |
1400 | SvREADONLY_on (sv_cbor); |
|
|
1401 | |
|
|
1402 | assert (("STRLEN must be an unsigned type", 0 <= (STRLEN)-1)); |
1365 | } |
1403 | } |
1366 | |
1404 | |
1367 | PROTOTYPES: DISABLE |
1405 | PROTOTYPES: DISABLE |
1368 | |
1406 | |
1369 | void CLONE (...) |
1407 | void CLONE (...) |
… | |
… | |
1390 | shrink = F_SHRINK |
1428 | shrink = F_SHRINK |
1391 | allow_unknown = F_ALLOW_UNKNOWN |
1429 | allow_unknown = F_ALLOW_UNKNOWN |
1392 | allow_sharing = F_ALLOW_SHARING |
1430 | allow_sharing = F_ALLOW_SHARING |
1393 | allow_cycles = F_ALLOW_CYCLES |
1431 | allow_cycles = F_ALLOW_CYCLES |
1394 | pack_strings = F_PACK_STRINGS |
1432 | pack_strings = F_PACK_STRINGS |
|
|
1433 | text_keys = F_TEXT_KEYS |
|
|
1434 | text_strings = F_TEXT_STRINGS |
1395 | validate_utf8 = F_VALIDATE_UTF8 |
1435 | validate_utf8 = F_VALIDATE_UTF8 |
1396 | PPCODE: |
1436 | PPCODE: |
1397 | { |
1437 | { |
1398 | if (enable) |
1438 | if (enable) |
1399 | self->flags |= ix; |
1439 | self->flags |= ix; |
… | |
… | |
1408 | get_shrink = F_SHRINK |
1448 | get_shrink = F_SHRINK |
1409 | get_allow_unknown = F_ALLOW_UNKNOWN |
1449 | get_allow_unknown = F_ALLOW_UNKNOWN |
1410 | get_allow_sharing = F_ALLOW_SHARING |
1450 | get_allow_sharing = F_ALLOW_SHARING |
1411 | get_allow_cycles = F_ALLOW_CYCLES |
1451 | get_allow_cycles = F_ALLOW_CYCLES |
1412 | get_pack_strings = F_PACK_STRINGS |
1452 | get_pack_strings = F_PACK_STRINGS |
|
|
1453 | get_text_keys = F_TEXT_KEYS |
|
|
1454 | get_text_strings = F_TEXT_STRINGS |
1413 | get_validate_utf8 = F_VALIDATE_UTF8 |
1455 | get_validate_utf8 = F_VALIDATE_UTF8 |
1414 | PPCODE: |
1456 | PPCODE: |
1415 | XPUSHs (boolSV (self->flags & ix)); |
1457 | XPUSHs (boolSV (self->flags & ix)); |
1416 | |
1458 | |
1417 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1459 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |