… | |
… | |
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 |
24 | |
27 | |
25 | // known tags |
28 | // known tags |
26 | enum cbor_tag |
29 | enum cbor_tag |
27 | { |
30 | { |
28 | // inofficial extensions (pending iana registration) |
31 | // extensions |
|
|
32 | CBOR_TAG_STRINGREF = 25, // http://cbor.schmorp.de/stringref |
29 | CBOR_TAG_PERL_OBJECT = 24, // http://cbor.schmorp.de/perl-object |
33 | CBOR_TAG_PERL_OBJECT = 26, // http://cbor.schmorp.de/perl-object |
30 | CBOR_TAG_GENERIC_OBJECT = 25, // http://cbor.schmorp.de/generic-object |
34 | CBOR_TAG_GENERIC_OBJECT = 27, // http://cbor.schmorp.de/generic-object |
31 | CBOR_TAG_VALUE_SHAREABLE = 26, // http://cbor.schmorp.de/value-sharing |
35 | CBOR_TAG_VALUE_SHAREABLE = 28, // http://cbor.schmorp.de/value-sharing |
32 | CBOR_TAG_VALUE_SHAREDREF = 27, // http://cbor.schmorp.de/value-sharing |
36 | CBOR_TAG_VALUE_SHAREDREF = 29, // http://cbor.schmorp.de/value-sharing |
33 | CBOR_TAG_STRINGREF_NAMESPACE = 65537, // http://cbor.schmorp.de/stringref |
37 | 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 |
38 | CBOR_TAG_INDIRECTION = 22098, // http://cbor.schmorp.de/indirection |
36 | |
39 | |
37 | // rfc7049 |
40 | // rfc7049 |
38 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
41 | CBOR_TAG_DATETIME = 0, // rfc4287, utf-8 |
39 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
42 | CBOR_TAG_TIMESTAMP = 1, // unix timestamp, any |
… | |
… | |
56 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
57 | }; |
60 | }; |
58 | |
61 | |
59 | #define F_SHRINK 0x00000001UL |
62 | #define F_SHRINK 0x00000001UL |
60 | #define F_ALLOW_UNKNOWN 0x00000002UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
61 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
64 | #define F_ALLOW_SHARING 0x00000004UL |
62 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
65 | #define F_PACK_STRINGS 0x00000008UL |
63 | |
66 | |
64 | #define INIT_SIZE 32 // initial scalar size to be allocated |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
65 | |
68 | |
66 | #define SB do { |
69 | #define SB do { |
67 | #define SE } while (0) |
70 | #define SE } while (0) |
… | |
… | |
138 | { |
141 | { |
139 | return idx > 23 |
142 | return idx > 23 |
140 | ? idx > 0xffU |
143 | ? idx > 0xffU |
141 | ? idx > 0xffffU |
144 | ? idx > 0xffffU |
142 | ? idx > 0xffffffffU |
145 | ? idx > 0xffffffffU |
|
|
146 | ? 11 |
143 | ? 7 |
147 | : 7 |
144 | : 6 |
|
|
145 | : 5 |
148 | : 5 |
146 | : 4 |
149 | : 4 |
147 | : 3; |
150 | : 3; |
148 | } |
151 | } |
149 | |
152 | |
… | |
… | |
186 | static void |
189 | static void |
187 | encode_uint (enc_t *enc, int major, UV len) |
190 | encode_uint (enc_t *enc, int major, UV len) |
188 | { |
191 | { |
189 | need (enc, 9); |
192 | need (enc, 9); |
190 | |
193 | |
191 | if (len < 24) |
194 | if (ecb_expect_true (len < 24)) |
192 | *enc->cur++ = major | len; |
195 | *enc->cur++ = major | len; |
193 | else if (len <= 0xff) |
196 | else if (ecb_expect_true (len <= 0xff)) |
194 | { |
197 | { |
195 | *enc->cur++ = major | 24; |
198 | *enc->cur++ = major | 24; |
196 | *enc->cur++ = len; |
199 | *enc->cur++ = len; |
197 | } |
200 | } |
198 | else if (len <= 0xffff) |
201 | else if (len <= 0xffff) |
… | |
… | |
227 | encode_tag (enc_t *enc, UV tag) |
230 | encode_tag (enc_t *enc, UV tag) |
228 | { |
231 | { |
229 | encode_uint (enc, 0xc0, tag); |
232 | encode_uint (enc, 0xc0, tag); |
230 | } |
233 | } |
231 | |
234 | |
|
|
235 | ecb_inline void |
|
|
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
|
|
237 | { |
|
|
238 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
|
|
239 | need (enc, len); |
|
|
240 | memcpy (enc->cur, str, len); |
|
|
241 | enc->cur += len; |
|
|
242 | } |
|
|
243 | |
232 | static void |
244 | static void |
233 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
234 | { |
246 | { |
235 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
247 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
236 | { |
248 | { |
237 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
238 | |
250 | |
239 | if (SvOK (*svp)) |
251 | if (SvOK (*svp)) |
240 | { |
252 | { |
… | |
… | |
249 | sv_setuv (*svp, enc->stringref_idx); |
261 | sv_setuv (*svp, enc->stringref_idx); |
250 | ++enc->stringref_idx; |
262 | ++enc->stringref_idx; |
251 | } |
263 | } |
252 | } |
264 | } |
253 | |
265 | |
254 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
266 | encode_str (enc, utf8, str, len); |
255 | need (enc, len); |
|
|
256 | memcpy (enc->cur, str, len); |
|
|
257 | enc->cur += len; |
|
|
258 | } |
267 | } |
259 | |
268 | |
260 | static void encode_sv (enc_t *enc, SV *sv); |
269 | static void encode_sv (enc_t *enc, SV *sv); |
261 | |
270 | |
262 | static void |
271 | static void |
… | |
… | |
301 | while ((he = hv_iternext (hv))) |
310 | while ((he = hv_iternext (hv))) |
302 | { |
311 | { |
303 | if (HeKLEN (he) == HEf_SVKEY) |
312 | if (HeKLEN (he) == HEf_SVKEY) |
304 | encode_sv (enc, HeSVKEY (he)); |
313 | encode_sv (enc, HeSVKEY (he)); |
305 | else |
314 | else |
306 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
315 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
307 | |
316 | |
308 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
317 | encode_sv (enc, ecb_expect_false (mg) ? hv_iterval (hv, he) : HeVAL (he)); |
309 | } |
318 | } |
310 | |
319 | |
311 | if (mg) |
320 | if (mg) |
… | |
… | |
317 | // encode objects, arrays and special \0=false and \1=true values. |
326 | // encode objects, arrays and special \0=false and \1=true values. |
318 | static void |
327 | static void |
319 | encode_rv (enc_t *enc, SV *sv) |
328 | encode_rv (enc_t *enc, SV *sv) |
320 | { |
329 | { |
321 | SvGETMAGIC (sv); |
330 | SvGETMAGIC (sv); |
322 | |
|
|
323 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
324 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
325 | { |
|
|
326 | if (!enc->shareable) |
|
|
327 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
328 | |
|
|
329 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
330 | |
|
|
331 | if (SvOK (*svp)) |
|
|
332 | { |
|
|
333 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
334 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
335 | return; |
|
|
336 | } |
|
|
337 | else |
|
|
338 | { |
|
|
339 | sv_setuv (*svp, enc->shareable_idx); |
|
|
340 | ++enc->shareable_idx; |
|
|
341 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
342 | } |
|
|
343 | } |
|
|
344 | |
331 | |
345 | svtype svt = SvTYPE (sv); |
332 | svtype svt = SvTYPE (sv); |
346 | |
333 | |
347 | if (ecb_expect_false (SvOBJECT (sv))) |
334 | if (ecb_expect_false (SvOBJECT (sv))) |
348 | { |
335 | { |
… | |
… | |
355 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
342 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
356 | ? cbor_tagged_stash |
343 | ? cbor_tagged_stash |
357 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
344 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
358 | |
345 | |
359 | HV *stash = SvSTASH (sv); |
346 | HV *stash = SvSTASH (sv); |
360 | GV *method; |
|
|
361 | |
347 | |
362 | if (stash == boolean_stash) |
348 | if (stash == boolean_stash) |
|
|
349 | { |
363 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
350 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
351 | return; |
|
|
352 | } |
364 | else if (stash == error_stash) |
353 | else if (stash == error_stash) |
|
|
354 | { |
365 | encode_ch (enc, 0xe0 | 23); |
355 | encode_ch (enc, 0xe0 | 23); |
|
|
356 | return; |
|
|
357 | } |
366 | else if (stash == tagged_stash) |
358 | else if (stash == tagged_stash) |
367 | { |
359 | { |
368 | if (svt != SVt_PVAV) |
360 | if (svt != SVt_PVAV) |
369 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
361 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
370 | |
362 | |
371 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
363 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
372 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
364 | encode_sv (enc, *av_fetch ((AV *)sv, 1, 1)); |
|
|
365 | |
|
|
366 | return; |
|
|
367 | } |
|
|
368 | } |
|
|
369 | |
|
|
370 | if (ecb_expect_false (SvREFCNT (sv) > 1) |
|
|
371 | && ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING)) |
|
|
372 | { |
|
|
373 | if (!enc->shareable) |
|
|
374 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
375 | |
|
|
376 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
377 | |
|
|
378 | if (SvOK (*svp)) |
373 | } |
379 | { |
|
|
380 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
381 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
382 | return; |
|
|
383 | } |
|
|
384 | else |
|
|
385 | { |
|
|
386 | sv_setuv (*svp, enc->shareable_idx); |
|
|
387 | ++enc->shareable_idx; |
|
|
388 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
389 | } |
|
|
390 | } |
|
|
391 | |
|
|
392 | if (ecb_expect_false (SvOBJECT (sv))) |
|
|
393 | { |
|
|
394 | HV *stash = SvSTASH (sv); |
|
|
395 | GV *method; |
|
|
396 | |
374 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
397 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
375 | { |
398 | { |
376 | dSP; |
399 | dSP; |
377 | |
400 | |
378 | ENTER; SAVETMPS; PUSHMARK (SP); |
401 | ENTER; SAVETMPS; PUSHMARK (SP); |
379 | // we re-bless the reference to get overload and other niceties right |
402 | // we re-bless the reference to get overload and other niceties right |
… | |
… | |
412 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
435 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
413 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
436 | croak ("%s::FREEZE(CBOR) method returned same object as was passed instead of a new one", HvNAME (stash)); |
414 | |
437 | |
415 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
438 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
416 | encode_uint (enc, 0x80, count + 1); |
439 | encode_uint (enc, 0x80, count + 1); |
417 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
440 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
418 | |
441 | |
419 | while (count) |
442 | while (count) |
420 | encode_sv (enc, SP[1 - count--]); |
443 | encode_sv (enc, SP[1 - count--]); |
421 | |
444 | |
422 | PUTBACK; |
445 | PUTBACK; |
… | |
… | |
481 | |
504 | |
482 | if (SvPOKp (sv)) |
505 | if (SvPOKp (sv)) |
483 | { |
506 | { |
484 | STRLEN len; |
507 | STRLEN len; |
485 | char *str = SvPV (sv, len); |
508 | char *str = SvPV (sv, len); |
486 | encode_str (enc, SvUTF8 (sv), str, len); |
509 | encode_strref (enc, SvUTF8 (sv), str, len); |
487 | } |
510 | } |
488 | else if (SvNOKp (sv)) |
511 | else if (SvNOKp (sv)) |
489 | encode_nv (enc, sv); |
512 | encode_nv (enc, sv); |
490 | else if (SvIOKp (sv)) |
513 | else if (SvIOKp (sv)) |
491 | { |
514 | { |
… | |
… | |
517 | enc.cur = SvPVX (enc.sv); |
540 | enc.cur = SvPVX (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
541 | enc.end = SvEND (enc.sv); |
519 | |
542 | |
520 | SvPOK_only (enc.sv); |
543 | SvPOK_only (enc.sv); |
521 | |
544 | |
522 | if (cbor->flags & F_ALLOW_STRINGREF) |
545 | if (cbor->flags & F_PACK_STRINGS) |
523 | { |
546 | { |
524 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
547 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
525 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
548 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
526 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
549 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
527 | } |
550 | } |
… | |
… | |
591 | | ((UV)dec->cur[-1]); |
614 | | ((UV)dec->cur[-1]); |
592 | |
615 | |
593 | case 27: |
616 | case 27: |
594 | WANT (9); |
617 | WANT (9); |
595 | dec->cur += 9; |
618 | dec->cur += 9; |
|
|
619 | |
|
|
620 | return |
|
|
621 | #if UVSIZE < 8 |
|
|
622 | 0 |
|
|
623 | #else |
596 | return (((UV)dec->cur[-8]) << 56) |
624 | (((UV)dec->cur[-8]) << 56) |
597 | | (((UV)dec->cur[-7]) << 48) |
625 | | (((UV)dec->cur[-7]) << 48) |
598 | | (((UV)dec->cur[-6]) << 40) |
626 | | (((UV)dec->cur[-6]) << 40) |
599 | | (((UV)dec->cur[-5]) << 32) |
627 | | (((UV)dec->cur[-5]) << 32) |
|
|
628 | #endif |
600 | | (((UV)dec->cur[-4]) << 24) |
629 | | (((UV)dec->cur[-4]) << 24) |
601 | | (((UV)dec->cur[-3]) << 16) |
630 | | (((UV)dec->cur[-3]) << 16) |
602 | | (((UV)dec->cur[-2]) << 8) |
631 | | (((UV)dec->cur[-2]) << 8) |
603 | | ((UV)dec->cur[-1]); |
632 | | ((UV)dec->cur[-1]); |
604 | |
633 | |
… | |
… | |
744 | { |
773 | { |
745 | SV *sv = 0; |
774 | SV *sv = 0; |
746 | |
775 | |
747 | if ((*dec->cur & 31) == 31) |
776 | if ((*dec->cur & 31) == 31) |
748 | { |
777 | { |
|
|
778 | // indefinite length strings |
749 | ++dec->cur; |
779 | ++dec->cur; |
750 | |
780 | |
|
|
781 | unsigned char major = *dec->cur & 0xe0; |
|
|
782 | |
751 | sv = newSVpvn ("", 0); |
783 | sv = newSVpvn ("", 0); |
752 | |
784 | |
753 | // not very fast, and certainly not robust against illegal input |
|
|
754 | for (;;) |
785 | for (;;) |
755 | { |
786 | { |
756 | WANT (1); |
787 | WANT (1); |
757 | |
788 | |
|
|
789 | if ((*dec->cur ^ major) >= 31) |
758 | if (*dec->cur == (0xe0 | 31)) |
790 | if (*dec->cur == (0xe0 | 31)) |
759 | { |
791 | { |
760 | ++dec->cur; |
792 | ++dec->cur; |
761 | break; |
793 | break; |
762 | } |
794 | } |
|
|
795 | else |
|
|
796 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
763 | |
797 | |
764 | sv_catsv (sv, decode_sv (dec)); |
798 | STRLEN len = decode_uint (dec); |
|
|
799 | |
|
|
800 | WANT (len); |
|
|
801 | sv_catpvn (sv, dec->cur, len); |
|
|
802 | dec->cur += len; |
765 | } |
803 | } |
766 | } |
804 | } |
767 | else |
805 | else |
768 | { |
806 | { |
769 | STRLEN len = decode_uint (dec); |
807 | STRLEN len = decode_uint (dec); |
… | |
… | |
1136 | void shrink (CBOR *self, int enable = 1) |
1174 | void shrink (CBOR *self, int enable = 1) |
1137 | ALIAS: |
1175 | ALIAS: |
1138 | shrink = F_SHRINK |
1176 | shrink = F_SHRINK |
1139 | allow_unknown = F_ALLOW_UNKNOWN |
1177 | allow_unknown = F_ALLOW_UNKNOWN |
1140 | allow_sharing = F_ALLOW_SHARING |
1178 | allow_sharing = F_ALLOW_SHARING |
1141 | allow_stringref = F_ALLOW_STRINGREF |
1179 | pack_strings = F_PACK_STRINGS |
1142 | PPCODE: |
1180 | PPCODE: |
1143 | { |
1181 | { |
1144 | if (enable) |
1182 | if (enable) |
1145 | self->flags |= ix; |
1183 | self->flags |= ix; |
1146 | else |
1184 | else |
… | |
… | |
1152 | void get_shrink (CBOR *self) |
1190 | void get_shrink (CBOR *self) |
1153 | ALIAS: |
1191 | ALIAS: |
1154 | get_shrink = F_SHRINK |
1192 | get_shrink = F_SHRINK |
1155 | get_allow_unknown = F_ALLOW_UNKNOWN |
1193 | get_allow_unknown = F_ALLOW_UNKNOWN |
1156 | get_allow_sharing = F_ALLOW_SHARING |
1194 | get_allow_sharing = F_ALLOW_SHARING |
1157 | get_allow_stringref = F_ALLOW_STRINGREF |
1195 | get_pack_strings = F_PACK_STRINGS |
1158 | PPCODE: |
1196 | PPCODE: |
1159 | XPUSHs (boolSV (self->flags & ix)); |
1197 | XPUSHs (boolSV (self->flags & ix)); |
1160 | |
1198 | |
1161 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1199 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1162 | PPCODE: |
1200 | PPCODE: |