… | |
… | |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
59 | CBOR_TAG_MAGIC = 55799 // self-describe cbor |
60 | }; |
60 | }; |
61 | |
61 | |
62 | #define F_SHRINK 0x00000001UL |
62 | #define F_SHRINK 0x00000001UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
63 | #define F_ALLOW_UNKNOWN 0x00000002UL |
64 | #define F_ALLOW_SHARING 0x00000004UL //TODO |
64 | #define F_ALLOW_SHARING 0x00000004UL |
65 | #define F_ALLOW_STRINGREF 0x00000008UL //TODO |
65 | #define F_PACK_STRINGS 0x00000008UL |
66 | |
66 | |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
67 | #define INIT_SIZE 32 // initial scalar size to be allocated |
68 | |
68 | |
69 | #define SB do { |
69 | #define SB do { |
70 | #define SE } while (0) |
70 | #define SE } while (0) |
… | |
… | |
189 | static void |
189 | static void |
190 | encode_uint (enc_t *enc, int major, UV len) |
190 | encode_uint (enc_t *enc, int major, UV len) |
191 | { |
191 | { |
192 | need (enc, 9); |
192 | need (enc, 9); |
193 | |
193 | |
194 | if (len < 24) |
194 | if (ecb_expect_true (len < 24)) |
195 | *enc->cur++ = major | len; |
195 | *enc->cur++ = major | len; |
196 | else if (len <= 0xff) |
196 | else if (ecb_expect_true (len <= 0xff)) |
197 | { |
197 | { |
198 | *enc->cur++ = major | 24; |
198 | *enc->cur++ = major | 24; |
199 | *enc->cur++ = len; |
199 | *enc->cur++ = len; |
200 | } |
200 | } |
201 | else if (len <= 0xffff) |
201 | else if (len <= 0xffff) |
… | |
… | |
230 | encode_tag (enc_t *enc, UV tag) |
230 | encode_tag (enc_t *enc, UV tag) |
231 | { |
231 | { |
232 | encode_uint (enc, 0xc0, tag); |
232 | encode_uint (enc, 0xc0, tag); |
233 | } |
233 | } |
234 | |
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 | |
235 | static void |
244 | static void |
236 | encode_str (enc_t *enc, int utf8, char *str, STRLEN len) |
245 | encode_strref (enc_t *enc, int utf8, char *str, STRLEN len) |
237 | { |
246 | { |
238 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_STRINGREF)) |
247 | if (ecb_expect_false (enc->cbor.flags & F_PACK_STRINGS)) |
239 | { |
248 | { |
240 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
249 | SV **svp = hv_fetch (enc->stringref[!!utf8], str, len, 1); |
241 | |
250 | |
242 | if (SvOK (*svp)) |
251 | if (SvOK (*svp)) |
243 | { |
252 | { |
… | |
… | |
252 | sv_setuv (*svp, enc->stringref_idx); |
261 | sv_setuv (*svp, enc->stringref_idx); |
253 | ++enc->stringref_idx; |
262 | ++enc->stringref_idx; |
254 | } |
263 | } |
255 | } |
264 | } |
256 | |
265 | |
257 | encode_uint (enc, utf8 ? 0x60 : 0x40, len); |
266 | encode_str (enc, utf8, str, len); |
258 | need (enc, len); |
|
|
259 | memcpy (enc->cur, str, len); |
|
|
260 | enc->cur += len; |
|
|
261 | } |
267 | } |
262 | |
268 | |
263 | static void encode_sv (enc_t *enc, SV *sv); |
269 | static void encode_sv (enc_t *enc, SV *sv); |
264 | |
270 | |
265 | static void |
271 | static void |
… | |
… | |
304 | while ((he = hv_iternext (hv))) |
310 | while ((he = hv_iternext (hv))) |
305 | { |
311 | { |
306 | if (HeKLEN (he) == HEf_SVKEY) |
312 | if (HeKLEN (he) == HEf_SVKEY) |
307 | encode_sv (enc, HeSVKEY (he)); |
313 | encode_sv (enc, HeSVKEY (he)); |
308 | else |
314 | else |
309 | encode_str (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
315 | encode_strref (enc, HeKUTF8 (he), HeKEY (he), HeKLEN (he)); |
310 | |
316 | |
311 | 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)); |
312 | } |
318 | } |
313 | |
319 | |
314 | if (mg) |
320 | if (mg) |
… | |
… | |
320 | // encode objects, arrays and special \0=false and \1=true values. |
326 | // encode objects, arrays and special \0=false and \1=true values. |
321 | static void |
327 | static void |
322 | encode_rv (enc_t *enc, SV *sv) |
328 | encode_rv (enc_t *enc, SV *sv) |
323 | { |
329 | { |
324 | SvGETMAGIC (sv); |
330 | SvGETMAGIC (sv); |
325 | |
|
|
326 | if (ecb_expect_false (enc->cbor.flags & F_ALLOW_SHARING) |
|
|
327 | && ecb_expect_false (SvREFCNT (sv) > 1)) |
|
|
328 | { |
|
|
329 | if (!enc->shareable) |
|
|
330 | enc->shareable = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
331 | |
|
|
332 | SV **svp = hv_fetch (enc->shareable, (char *)&sv, sizeof (sv), 1); |
|
|
333 | |
|
|
334 | if (SvOK (*svp)) |
|
|
335 | { |
|
|
336 | encode_tag (enc, CBOR_TAG_VALUE_SHAREDREF); |
|
|
337 | encode_uint (enc, 0x00, SvUV (*svp)); |
|
|
338 | return; |
|
|
339 | } |
|
|
340 | else |
|
|
341 | { |
|
|
342 | sv_setuv (*svp, enc->shareable_idx); |
|
|
343 | ++enc->shareable_idx; |
|
|
344 | encode_tag (enc, CBOR_TAG_VALUE_SHAREABLE); |
|
|
345 | } |
|
|
346 | } |
|
|
347 | |
331 | |
348 | svtype svt = SvTYPE (sv); |
332 | svtype svt = SvTYPE (sv); |
349 | |
333 | |
350 | if (ecb_expect_false (SvOBJECT (sv))) |
334 | if (ecb_expect_false (SvOBJECT (sv))) |
351 | { |
335 | { |
… | |
… | |
358 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
342 | HV *tagged_stash = !CBOR_SLOW || cbor_tagged_stash |
359 | ? cbor_tagged_stash |
343 | ? cbor_tagged_stash |
360 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
344 | : gv_stashpv ("CBOR::XS::Tagged" , 1); |
361 | |
345 | |
362 | HV *stash = SvSTASH (sv); |
346 | HV *stash = SvSTASH (sv); |
363 | GV *method; |
|
|
364 | |
347 | |
365 | if (stash == boolean_stash) |
348 | if (stash == boolean_stash) |
|
|
349 | { |
366 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
350 | encode_ch (enc, SvIV (sv) ? 0xe0 | 21 : 0xe0 | 20); |
|
|
351 | return; |
|
|
352 | } |
367 | else if (stash == error_stash) |
353 | else if (stash == error_stash) |
|
|
354 | { |
368 | encode_ch (enc, 0xe0 | 23); |
355 | encode_ch (enc, 0xe0 | 23); |
|
|
356 | return; |
|
|
357 | } |
369 | else if (stash == tagged_stash) |
358 | else if (stash == tagged_stash) |
370 | { |
359 | { |
371 | if (svt != SVt_PVAV) |
360 | if (svt != SVt_PVAV) |
372 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
361 | croak ("encountered CBOR::XS::Tagged object that isn't an array"); |
373 | |
362 | |
374 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
363 | encode_uint (enc, 0xc0, SvUV (*av_fetch ((AV *)sv, 0, 1))); |
375 | 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)) |
376 | } |
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 | |
377 | else if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
397 | if ((method = gv_fetchmethod_autoload (stash, "TO_CBOR", 0))) |
378 | { |
398 | { |
379 | dSP; |
399 | dSP; |
380 | |
400 | |
381 | ENTER; SAVETMPS; PUSHMARK (SP); |
401 | ENTER; SAVETMPS; PUSHMARK (SP); |
382 | // 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 |
… | |
… | |
415 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
435 | if (count == 1 && SvROK (TOPs) && SvRV (TOPs) == sv) |
416 | 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)); |
417 | |
437 | |
418 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
438 | encode_tag (enc, CBOR_TAG_PERL_OBJECT); |
419 | encode_uint (enc, 0x80, count + 1); |
439 | encode_uint (enc, 0x80, count + 1); |
420 | encode_str (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
440 | encode_strref (enc, HvNAMEUTF8 (stash), HvNAME (stash), HvNAMELEN (stash)); |
421 | |
441 | |
422 | while (count) |
442 | while (count) |
423 | encode_sv (enc, SP[1 - count--]); |
443 | encode_sv (enc, SP[1 - count--]); |
424 | |
444 | |
425 | PUTBACK; |
445 | PUTBACK; |
… | |
… | |
484 | |
504 | |
485 | if (SvPOKp (sv)) |
505 | if (SvPOKp (sv)) |
486 | { |
506 | { |
487 | STRLEN len; |
507 | STRLEN len; |
488 | char *str = SvPV (sv, len); |
508 | char *str = SvPV (sv, len); |
489 | encode_str (enc, SvUTF8 (sv), str, len); |
509 | encode_strref (enc, SvUTF8 (sv), str, len); |
490 | } |
510 | } |
491 | else if (SvNOKp (sv)) |
511 | else if (SvNOKp (sv)) |
492 | encode_nv (enc, sv); |
512 | encode_nv (enc, sv); |
493 | else if (SvIOKp (sv)) |
513 | else if (SvIOKp (sv)) |
494 | { |
514 | { |
… | |
… | |
520 | enc.cur = SvPVX (enc.sv); |
540 | enc.cur = SvPVX (enc.sv); |
521 | enc.end = SvEND (enc.sv); |
541 | enc.end = SvEND (enc.sv); |
522 | |
542 | |
523 | SvPOK_only (enc.sv); |
543 | SvPOK_only (enc.sv); |
524 | |
544 | |
525 | if (cbor->flags & F_ALLOW_STRINGREF) |
545 | if (cbor->flags & F_PACK_STRINGS) |
526 | { |
546 | { |
527 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
547 | encode_tag (&enc, CBOR_TAG_STRINGREF_NAMESPACE); |
528 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
548 | enc.stringref[0]= (HV *)sv_2mortal ((SV *)newHV ()); |
529 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
549 | enc.stringref[1]= (HV *)sv_2mortal ((SV *)newHV ()); |
530 | } |
550 | } |
… | |
… | |
594 | | ((UV)dec->cur[-1]); |
614 | | ((UV)dec->cur[-1]); |
595 | |
615 | |
596 | case 27: |
616 | case 27: |
597 | WANT (9); |
617 | WANT (9); |
598 | dec->cur += 9; |
618 | dec->cur += 9; |
|
|
619 | |
|
|
620 | return |
|
|
621 | #if UVSIZE < 8 |
|
|
622 | 0 |
|
|
623 | #else |
599 | return (((UV)dec->cur[-8]) << 56) |
624 | (((UV)dec->cur[-8]) << 56) |
600 | | (((UV)dec->cur[-7]) << 48) |
625 | | (((UV)dec->cur[-7]) << 48) |
601 | | (((UV)dec->cur[-6]) << 40) |
626 | | (((UV)dec->cur[-6]) << 40) |
602 | | (((UV)dec->cur[-5]) << 32) |
627 | | (((UV)dec->cur[-5]) << 32) |
|
|
628 | #endif |
603 | | (((UV)dec->cur[-4]) << 24) |
629 | | (((UV)dec->cur[-4]) << 24) |
604 | | (((UV)dec->cur[-3]) << 16) |
630 | | (((UV)dec->cur[-3]) << 16) |
605 | | (((UV)dec->cur[-2]) << 8) |
631 | | (((UV)dec->cur[-2]) << 8) |
606 | | ((UV)dec->cur[-1]); |
632 | | ((UV)dec->cur[-1]); |
607 | |
633 | |
… | |
… | |
747 | { |
773 | { |
748 | SV *sv = 0; |
774 | SV *sv = 0; |
749 | |
775 | |
750 | if ((*dec->cur & 31) == 31) |
776 | if ((*dec->cur & 31) == 31) |
751 | { |
777 | { |
|
|
778 | // indefinite length strings |
752 | ++dec->cur; |
779 | ++dec->cur; |
753 | |
780 | |
|
|
781 | unsigned char major = *dec->cur & 0xe0; |
|
|
782 | |
754 | sv = newSVpvn ("", 0); |
783 | sv = newSVpvn ("", 0); |
755 | |
784 | |
756 | // not very fast, and certainly not robust against illegal input |
|
|
757 | for (;;) |
785 | for (;;) |
758 | { |
786 | { |
759 | WANT (1); |
787 | WANT (1); |
760 | |
788 | |
|
|
789 | if ((*dec->cur ^ major) >= 31) |
761 | if (*dec->cur == (0xe0 | 31)) |
790 | if (*dec->cur == (0xe0 | 31)) |
762 | { |
791 | { |
763 | ++dec->cur; |
792 | ++dec->cur; |
764 | break; |
793 | break; |
765 | } |
794 | } |
|
|
795 | else |
|
|
796 | ERR ("corrupted CBOR data (invalid chunks in indefinite length string)"); |
766 | |
797 | |
767 | 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; |
768 | } |
803 | } |
769 | } |
804 | } |
770 | else |
805 | else |
771 | { |
806 | { |
772 | STRLEN len = decode_uint (dec); |
807 | STRLEN len = decode_uint (dec); |
… | |
… | |
1139 | void shrink (CBOR *self, int enable = 1) |
1174 | void shrink (CBOR *self, int enable = 1) |
1140 | ALIAS: |
1175 | ALIAS: |
1141 | shrink = F_SHRINK |
1176 | shrink = F_SHRINK |
1142 | allow_unknown = F_ALLOW_UNKNOWN |
1177 | allow_unknown = F_ALLOW_UNKNOWN |
1143 | allow_sharing = F_ALLOW_SHARING |
1178 | allow_sharing = F_ALLOW_SHARING |
1144 | allow_stringref = F_ALLOW_STRINGREF |
1179 | pack_strings = F_PACK_STRINGS |
1145 | PPCODE: |
1180 | PPCODE: |
1146 | { |
1181 | { |
1147 | if (enable) |
1182 | if (enable) |
1148 | self->flags |= ix; |
1183 | self->flags |= ix; |
1149 | else |
1184 | else |
… | |
… | |
1155 | void get_shrink (CBOR *self) |
1190 | void get_shrink (CBOR *self) |
1156 | ALIAS: |
1191 | ALIAS: |
1157 | get_shrink = F_SHRINK |
1192 | get_shrink = F_SHRINK |
1158 | get_allow_unknown = F_ALLOW_UNKNOWN |
1193 | get_allow_unknown = F_ALLOW_UNKNOWN |
1159 | get_allow_sharing = F_ALLOW_SHARING |
1194 | get_allow_sharing = F_ALLOW_SHARING |
1160 | get_allow_stringref = F_ALLOW_STRINGREF |
1195 | get_pack_strings = F_PACK_STRINGS |
1161 | PPCODE: |
1196 | PPCODE: |
1162 | XPUSHs (boolSV (self->flags & ix)); |
1197 | XPUSHs (boolSV (self->flags & ix)); |
1163 | |
1198 | |
1164 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1199 | void max_depth (CBOR *self, U32 max_depth = 0x80000000UL) |
1165 | PPCODE: |
1200 | PPCODE: |