… | |
… | |
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_UTF8_STRINGS 0x00000020UL |
105 | #define F_VALIDATE_UTF8 0x00000020UL |
106 | #define F_VALIDATE_UTF8 0x00000040UL |
106 | |
107 | |
107 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | #define INIT_SIZE 32 // initial scalar size to be allocated |
108 | |
109 | |
109 | #define SB do { |
110 | #define SB do { |
110 | #define SE } while (0) |
111 | #define SE } while (0) |
… | |
… | |
279 | } |
280 | } |
280 | |
281 | |
281 | ecb_inline void |
282 | ecb_inline void |
282 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
283 | { |
284 | { |
|
|
285 | if (ecb_expect_false (enc->cbor.flags & F_UTF8_STRINGS)) |
|
|
286 | if (!utf8) |
|
|
287 | { |
|
|
288 | SV *sv = sv_newmortal (); |
|
|
289 | char *s; STRLEN l; |
|
|
290 | |
|
|
291 | sv_setpvn (sv, str, len); |
|
|
292 | |
|
|
293 | s = SvPVutf8 (sv, l); |
|
|
294 | encode_str (enc, 1, s, l); |
|
|
295 | return; |
|
|
296 | } |
|
|
297 | |
284 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
298 | encode_uint (enc, utf8 ? MAJOR_TEXT : MAJOR_BYTES, len); |
285 | need (enc, len); |
299 | need (enc, len); |
286 | memcpy (enc->cur, str, len); |
300 | memcpy (enc->cur, str, len); |
287 | enc->cur += len; |
301 | enc->cur += len; |
288 | } |
302 | } |
… | |
… | |
324 | |
338 | |
325 | ++enc->depth; |
339 | ++enc->depth; |
326 | |
340 | |
327 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
341 | encode_uint (enc, MAJOR_ARRAY, len + 1); |
328 | |
342 | |
|
|
343 | if (SvMAGICAL (av)) |
329 | for (i = 0; i <= len; ++i) |
344 | for (i = 0; i <= len; ++i) |
330 | { |
345 | { |
331 | SV **svp = av_fetch (av, i, 0); |
346 | SV **svp = av_fetch (av, i, 0); |
332 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
347 | encode_sv (enc, svp ? *svp : &PL_sv_undef); |
333 | } |
348 | } |
|
|
349 | else |
|
|
350 | for (i = 0; i <= len; ++i) |
|
|
351 | { |
|
|
352 | SV *sv = AvARRAY (av)[i]; |
|
|
353 | encode_sv (enc, sv ? sv : &PL_sv_undef); |
|
|
354 | } |
334 | |
355 | |
335 | --enc->depth; |
356 | --enc->depth; |
336 | } |
357 | } |
337 | |
358 | |
338 | static void |
359 | static void |
… | |
… | |
442 | |
463 | |
443 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
464 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
444 | { |
465 | { |
445 | dSP; |
466 | dSP; |
446 | |
467 | |
447 | ENTER; SAVETMPS; PUSHMARK (SP); |
468 | ENTER; SAVETMPS; |
|
|
469 | PUSHMARK (SP); |
448 | // we re-bless the reference to get overload and other niceties right |
470 | // we re-bless the reference to get overload and other niceties right |
449 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
471 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
450 | |
472 | |
451 | PUTBACK; |
473 | PUTBACK; |
452 | // G_SCALAR ensures that return value is 1 |
474 | // G_SCALAR ensures that return value is 1 |
… | |
… | |
465 | } |
487 | } |
466 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
488 | else if ((method = gv_fetchmethod_autoload (stash, "FREEZE", 0)) != 0) |
467 | { |
489 | { |
468 | dSP; |
490 | dSP; |
469 | |
491 | |
470 | ENTER; SAVETMPS; PUSHMARK (SP); |
492 | ENTER; SAVETMPS; |
|
|
493 | SAVESTACK_POS (); |
|
|
494 | PUSHMARK (SP); |
471 | EXTEND (SP, 2); |
495 | EXTEND (SP, 2); |
472 | // we re-bless the reference to get overload and other niceties right |
496 | // we re-bless the reference to get overload and other niceties right |
473 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
497 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
474 | PUSHs (sv_cbor); |
498 | PUSHs (sv_cbor); |
475 | |
499 | |
… | |
… | |
577 | } |
601 | } |
578 | |
602 | |
579 | static SV * |
603 | static SV * |
580 | encode_cbor (SV *scalar, CBOR *cbor) |
604 | encode_cbor (SV *scalar, CBOR *cbor) |
581 | { |
605 | { |
582 | enc_t enc = { }; |
606 | enc_t enc = { 0 }; |
583 | |
607 | |
584 | enc.cbor = *cbor; |
608 | enc.cbor = *cbor; |
585 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
609 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
586 | enc.cur = SvPVX (enc.sv); |
610 | enc.cur = SvPVX (enc.sv); |
587 | enc.end = SvEND (enc.sv); |
611 | enc.end = SvEND (enc.sv); |
… | |
… | |
742 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
766 | if (ecb_expect_true ((U8)(*dec->cur - MAJOR_BYTES) <= LENGTH_EXT8)) |
743 | { |
767 | { |
744 | I32 len = decode_uint (dec); |
768 | I32 len = decode_uint (dec); |
745 | char *key = (char *)dec->cur; |
769 | char *key = (char *)dec->cur; |
746 | |
770 | |
|
|
771 | WANT (len); |
747 | dec->cur += len; |
772 | dec->cur += len; |
748 | |
773 | |
749 | hv_store (hv, key, len, decode_sv (dec), 0); |
774 | hv_store (hv, key, len, decode_sv (dec), 0); |
750 | |
775 | |
751 | return; |
776 | return; |
752 | } |
777 | } |
753 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
778 | else if (ecb_expect_true ((U8)(*dec->cur - MAJOR_TEXT) <= LENGTH_EXT8)) |
754 | { |
779 | { |
755 | I32 len = decode_uint (dec); |
780 | I32 len = decode_uint (dec); |
756 | char *key = (char *)dec->cur; |
781 | char *key = (char *)dec->cur; |
757 | |
782 | |
|
|
783 | WANT (len); |
758 | dec->cur += len; |
784 | dec->cur += len; |
759 | |
785 | |
760 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
786 | if (ecb_expect_false (dec->cbor.flags & F_VALIDATE_UTF8)) |
761 | if (!is_utf8_string (key, len)) |
787 | if (!is_utf8_string (key, len)) |
762 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
788 | ERR ("corrupted CBOR data (invalid UTF-8 in map key)"); |
… | |
… | |
985 | if (!method) |
1011 | if (!method) |
986 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1012 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
987 | |
1013 | |
988 | dSP; |
1014 | dSP; |
989 | |
1015 | |
990 | ENTER; SAVETMPS; PUSHMARK (SP); |
1016 | ENTER; SAVETMPS; |
|
|
1017 | PUSHMARK (SP); |
991 | EXTEND (SP, len + 1); |
1018 | EXTEND (SP, len + 1); |
992 | // we re-bless the reference to get overload and other niceties right |
1019 | // we re-bless the reference to get overload and other niceties right |
993 | PUSHs (*av_fetch (av, 0, 1)); |
1020 | PUSHs (*av_fetch (av, 0, 1)); |
994 | PUSHs (sv_cbor); |
1021 | PUSHs (sv_cbor); |
995 | |
1022 | |
… | |
… | |
1020 | default: |
1047 | default: |
1021 | { |
1048 | { |
1022 | sv = decode_sv (dec); |
1049 | sv = decode_sv (dec); |
1023 | |
1050 | |
1024 | dSP; |
1051 | dSP; |
1025 | ENTER; SAVETMPS; PUSHMARK (SP); |
1052 | ENTER; SAVETMPS; |
|
|
1053 | SAVESTACK_POS (); |
|
|
1054 | PUSHMARK (SP); |
1026 | EXTEND (SP, 2); |
1055 | EXTEND (SP, 2); |
1027 | PUSHs (newSVuv (tag)); |
1056 | PUSHs (newSVuv (tag)); |
1028 | PUSHs (sv); |
1057 | PUSHs (sv); |
1029 | |
1058 | |
1030 | PUTBACK; |
1059 | PUTBACK; |
… | |
… | |
1156 | } |
1185 | } |
1157 | |
1186 | |
1158 | static SV * |
1187 | static SV * |
1159 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1188 | decode_cbor (SV *string, CBOR *cbor, char **offset_return) |
1160 | { |
1189 | { |
1161 | dec_t dec = { }; |
1190 | dec_t dec = { 0 }; |
1162 | SV *sv; |
1191 | SV *sv; |
1163 | STRLEN len; |
1192 | STRLEN len; |
1164 | char *data = SvPVbyte (string, len); |
1193 | char *data = SvPVbyte (string, len); |
1165 | |
1194 | |
1166 | if (len > cbor->max_size && cbor->max_size) |
1195 | if (len > cbor->max_size && cbor->max_size) |
… | |
… | |
1257 | |
1286 | |
1258 | int major = *p >> MAJOR_SHIFT; |
1287 | int major = *p >> MAJOR_SHIFT; |
1259 | |
1288 | |
1260 | switch (major) |
1289 | switch (major) |
1261 | { |
1290 | { |
|
|
1291 | case MAJOR_TAG >> MAJOR_SHIFT: |
|
|
1292 | ++count; // tags merely prefix another value |
|
|
1293 | break; |
|
|
1294 | |
1262 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1295 | case MAJOR_BYTES >> MAJOR_SHIFT: |
1263 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1296 | case MAJOR_TEXT >> MAJOR_SHIFT: |
1264 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1297 | case MAJOR_ARRAY >> MAJOR_SHIFT: |
1265 | case MAJOR_MAP >> MAJOR_SHIFT: |
1298 | case MAJOR_MAP >> MAJOR_SHIFT: |
1266 | { |
1299 | { |
… | |
… | |
1371 | shrink = F_SHRINK |
1404 | shrink = F_SHRINK |
1372 | allow_unknown = F_ALLOW_UNKNOWN |
1405 | allow_unknown = F_ALLOW_UNKNOWN |
1373 | allow_sharing = F_ALLOW_SHARING |
1406 | allow_sharing = F_ALLOW_SHARING |
1374 | allow_cycles = F_ALLOW_CYCLES |
1407 | allow_cycles = F_ALLOW_CYCLES |
1375 | pack_strings = F_PACK_STRINGS |
1408 | pack_strings = F_PACK_STRINGS |
|
|
1409 | utf8_strings = F_UTF8_STRINGS |
1376 | validate_utf8 = F_VALIDATE_UTF8 |
1410 | validate_utf8 = F_VALIDATE_UTF8 |
1377 | PPCODE: |
1411 | PPCODE: |
1378 | { |
1412 | { |
1379 | if (enable) |
1413 | if (enable) |
1380 | self->flags |= ix; |
1414 | self->flags |= ix; |