… | |
… | |
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) |
… | |
… | |
449 | |
481 | |
450 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
482 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
451 | { |
483 | { |
452 | dSP; |
484 | dSP; |
453 | |
485 | |
454 | ENTER; SAVETMPS; PUSHMARK (SP); |
486 | ENTER; SAVETMPS; |
|
|
487 | PUSHMARK (SP); |
455 | // we re-bless the reference to get overload and other niceties right |
488 | // we re-bless the reference to get overload and other niceties right |
456 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
489 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
457 | |
490 | |
458 | PUTBACK; |
491 | PUTBACK; |
459 | // G_SCALAR ensures that return value is 1 |
492 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
472 | } |
505 | } |
473 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
506 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
474 | { |
507 | { |
475 | dSP; |
508 | dSP; |
476 | |
509 | |
477 | ENTER; SAVETMPS; PUSHMARK (SP); |
510 | ENTER; SAVETMPS; |
|
|
511 | SAVESTACK_POS (); |
|
|
512 | PUSHMARK (SP); |
478 | EXTEND (SP, 2); |
513 | EXTEND (SP, 2); |
479 | // we re-bless the reference to get overload and other niceties right |
514 | // we re-bless the reference to get overload and other niceties right |
480 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
515 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
481 | PUSHs (sv_cbor); |
516 | PUSHs (sv_cbor); |
482 | |
517 | |
… | |
… | |
488 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
523 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
489 | 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)); |
490 | |
525 | |
491 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
526 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
492 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
527 | encode_uint (enc, MAJOR_ARRAY, count + 1); |
493 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
528 | encode_strref (enc, 0, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
494 | |
529 | |
495 | while (count) |
530 | while (count) |
496 | encode_sv (enc, SP[1 - count--]); |
531 | encode_sv (enc, SP[1 - count--]); |
497 | |
532 | |
498 | PUTBACK; |
533 | PUTBACK; |
… | |
… | |
557 | |
592 | |
558 | if (SvPOKp (sv)) |
593 | if (SvPOKp (sv)) |
559 | { |
594 | { |
560 | STRLEN len; |
595 | STRLEN len; |
561 | char *str = SvPV (sv, len); |
596 | char *str = SvPV (sv, len); |
562 | encode_strref (enc, SvUTF8 (sv), str, len); |
597 | encode_strref (enc, enc->cbor.flags & F_TEXT_STRINGS, SvUTF8 (sv), str, len); |
563 | } |
598 | } |
564 | else if (SvNOKp (sv)) |
599 | else if (SvNOKp (sv)) |
565 | encode_nv (enc, sv); |
600 | encode_nv (enc, sv); |
566 | else if (SvIOKp (sv)) |
601 | else if (SvIOKp (sv)) |
567 | { |
602 | { |
… | |
… | |
584 | } |
619 | } |
585 | |
620 | |
586 | static SV * |
621 | static SV * |
587 | encode_cbor (SV *scalar, CBOR *cbor) |
622 | encode_cbor (SV *scalar, CBOR *cbor) |
588 | { |
623 | { |
589 | enc_t enc = { }; |
624 | enc_t enc = { 0 }; |
590 | |
625 | |
591 | enc.cbor = *cbor; |
626 | enc.cbor = *cbor; |
592 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
593 | enc.cur = SvPVX (enc.sv); |
628 | enc.cur = SvPVX (enc.sv); |
594 | enc.end = SvEND (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
… | |
… | |
749 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
784 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
750 | { |
785 | { |
751 | I32 len = decode_uint (dec); |
786 | I32 len = decode_uint (dec); |
752 | char *key = (char *)dec->cur; |
787 | char *key = (char *)dec->cur; |
753 | |
788 | |
|
|
789 | WANT (len); |
754 | dec->cur += len; |
790 | dec->cur += len; |
755 | |
791 | |
756 | hv_store (hv, key, len, decode_sv (dec), 0); |
792 | hv_store (hv, key, len, decode_sv (dec), 0); |
757 | |
793 | |
758 | return; |
794 | return; |
759 | } |
795 | } |
760 | 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)) |
761 | { |
797 | { |
762 | I32 len = decode_uint (dec); |
798 | I32 len = decode_uint (dec); |
763 | char *key = (char *)dec->cur; |
799 | char *key = (char *)dec->cur; |
764 | |
800 | |
|
|
801 | WANT (len); |
765 | dec->cur += len; |
802 | dec->cur += len; |
766 | |
803 | |
767 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
804 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
768 | if (!is_utf8_string (key, len)) |
805 | if (!is_utf8_string (key, len)) |
769 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
806 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
992 | if (!method) |
1029 | if (!method) |
993 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1030 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
994 | |
1031 | |
995 | dSP; |
1032 | dSP; |
996 | |
1033 | |
997 | ENTER; SAVETMPS; PUSHMARK (SP); |
1034 | ENTER; SAVETMPS; |
|
|
1035 | PUSHMARK (SP); |
998 | EXTEND (SP, len + 1); |
1036 | EXTEND (SP, len + 1); |
999 | // we re-bless the reference to get overload and other niceties right |
1037 | // we re-bless the reference to get overload and other niceties right |
1000 | PUSHs (*av_fetch (av, 0, 1)); |
1038 | PUSHs (*av_fetch (av, 0, 1)); |
1001 | PUSHs (sv_cbor); |
1039 | PUSHs (sv_cbor); |
1002 | |
1040 | |
… | |
… | |
1027 | default: |
1065 | default: |
1028 | { |
1066 | { |
1029 | sv = decode_sv (dec); |
1067 | sv = decode_sv (dec); |
1030 | |
1068 | |
1031 | dSP; |
1069 | dSP; |
1032 | ENTER; SAVETMPS; PUSHMARK (SP); |
1070 | ENTER; SAVETMPS; |
|
|
1071 | SAVESTACK_POS (); |
|
|
1072 | PUSHMARK (SP); |
1033 | EXTEND (SP, 2); |
1073 | EXTEND (SP, 2); |
1034 | PUSHs (newSVuv (tag)); |
1074 | PUSHs (newSVuv (tag)); |
1035 | PUSHs (sv); |
1075 | PUSHs (sv); |
1036 | |
1076 | |
1037 | PUTBACK; |
1077 | PUTBACK; |
… | |
… | |
1163 | } |
1203 | } |
1164 | |
1204 | |
1165 | static SV * |
1205 | static SV * |
1166 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1206 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1167 | { |
1207 | { |
1168 | dec_t dec = { }; |
1208 | dec_t dec = { 0 }; |
1169 | SV *sv; |
1209 | SV *sv; |
1170 | STRLEN len; |
1210 | STRLEN len; |
1171 | char *data = SvPVbyte (string, len); |
1211 | char *data = SvPVbyte (string, len); |
1172 | |
1212 | |
1173 | if (len > cbor->max_size && cbor->max_size) |
1213 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1382 | shrink = F_SHRINK |
1422 | shrink = F_SHRINK |
1383 | allow_unknown = F_ALLOW_UNKNOWN |
1423 | allow_unknown = F_ALLOW_UNKNOWN |
1384 | allow_sharing = F_ALLOW_SHARING |
1424 | allow_sharing = F_ALLOW_SHARING |
1385 | allow_cycles = F_ALLOW_CYCLES |
1425 | allow_cycles = F_ALLOW_CYCLES |
1386 | pack_strings = F_PACK_STRINGS |
1426 | pack_strings = F_PACK_STRINGS |
|
|
1427 | text_keys = F_TEXT_KEYS |
|
|
1428 | text_strings = F_TEXT_STRINGS |
1387 | validate_utf8 = F_VALIDATE_UTF8 |
1429 | validate_utf8 = F_VALIDATE_UTF8 |
1388 | PPCODE: |
1430 | PPCODE: |
1389 | { |
1431 | { |
1390 | if (enable) |
1432 | if (enable) |
1391 | self->flags |= ix; |
1433 | self->flags |= ix; |
… | |
… | |
1400 | get_shrink = F_SHRINK |
1442 | get_shrink = F_SHRINK |
1401 | get_allow_unknown = F_ALLOW_UNKNOWN |
1443 | get_allow_unknown = F_ALLOW_UNKNOWN |
1402 | get_allow_sharing = F_ALLOW_SHARING |
1444 | get_allow_sharing = F_ALLOW_SHARING |
1403 | get_allow_cycles = F_ALLOW_CYCLES |
1445 | get_allow_cycles = F_ALLOW_CYCLES |
1404 | get_pack_strings = F_PACK_STRINGS |
1446 | get_pack_strings = F_PACK_STRINGS |
|
|
1447 | get_text_keys = F_TEXT_KEYS |
|
|
1448 | get_text_strings = F_TEXT_STRINGS |
1405 | get_validate_utf8 = F_VALIDATE_UTF8 |
1449 | get_validate_utf8 = F_VALIDATE_UTF8 |
1406 | PPCODE: |
1450 | PPCODE: |
1407 | XPUSHs (boolSV (self->flags & ix)); |
1451 | XPUSHs (boolSV (self->flags & ix)); |
1408 | |
1452 | |
1409 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1453 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |