ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.pm
(Generate patch)

Comparing CBOR-XS/XS.pm (file contents):
Revision 1.76 by root, Tue Dec 1 01:49:47 2020 UTC vs.
Revision 1.89 by root, Fri Sep 8 20:03:06 2023 UTC

64 64
65package CBOR::XS; 65package CBOR::XS;
66 66
67use common::sense; 67use common::sense;
68 68
69our $VERSION = 1.82; 69our $VERSION = 1.87;
70our @ISA = qw(Exporter); 70our @ISA = qw(Exporter);
71 71
72our @EXPORT = qw(encode_cbor decode_cbor); 72our @EXPORT = qw(encode_cbor decode_cbor);
73 73
74use Exporter; 74use Exporter;
121but configures the coder object to be safe to use with untrusted 121but configures the coder object to be safe to use with untrusted
122data. Currently, this is equivalent to: 122data. Currently, this is equivalent to:
123 123
124 my $cbor = CBOR::XS 124 my $cbor = CBOR::XS
125 ->new 125 ->new
126 ->validate_utf8
126 ->forbid_objects 127 ->forbid_objects
127 ->filter (\&CBOR::XS::safe_filter) 128 ->filter (\&CBOR::XS::safe_filter)
128 ->max_size (1e8); 129 ->max_size (1e8);
129 130
130But is more future proof (it is better to crash because of a change than 131But is more future proof (it is better to crash because of a change than
133=cut 134=cut
134 135
135sub new_safe { 136sub new_safe {
136 CBOR::XS 137 CBOR::XS
137 ->new 138 ->new
139 ->validate_utf8
138 ->forbid_objects 140 ->forbid_objects
139 ->filter (\&CBOR::XS::safe_filter) 141 ->filter (\&CBOR::XS::safe_filter)
140 ->max_size (1e8) 142 ->max_size (1e8)
141} 143}
142 144
214communication partner supports the value sharing extensions to CBOR 216communication partner supports the value sharing extensions to CBOR
215(L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the 217(L<http://cbor.schmorp.de/value-sharing>), as without decoder support, the
216resulting data structure might be unusable. 218resulting data structure might be unusable.
217 219
218Detecting shared values incurs a runtime overhead when values are encoded 220Detecting shared values incurs a runtime overhead when values are encoded
219that have a reference counter large than one, and might unnecessarily 221that have a reference counter larger than one, and might unnecessarily
220increase the encoded size, as potentially shared values are encoded as 222increase the encoded size, as potentially shared values are encoded as
221shareable whether or not they are actually shared. 223shareable whether or not they are actually shared.
222 224
223At the moment, only targets of references can be shared (e.g. scalars, 225At the moment, only targets of references can be shared (e.g. scalars,
224arrays or hashes pointed to by a reference). Weirder constructs, such as 226arrays or hashes pointed to by a reference). Weirder constructs, such as
243isn't prepared for this will not leak memory. 245isn't prepared for this will not leak memory.
244 246
245If C<$enable> is false (the default), then C<decode> will throw an error 247If C<$enable> is false (the default), then C<decode> will throw an error
246when it encounters a self-referential/cyclic data structure. 248when it encounters a self-referential/cyclic data structure.
247 249
248FUTURE DIRECTION: the motivation behind this option is to avoid I<real> 250This option does not affect C<encode> in any way - shared values and
249cycles - future versions of this module might chose to decode cyclic data 251references will always be encoded properly if present.
250structures using weak references when this option is off, instead of 252
251throwing an error. 253=item $cbor = $cbor->allow_weak_cycles ([$enable])
254
255=item $enabled = $cbor->get_allow_weak_cycles
256
257This works like C<allow_cycles> in that it allows the resulting data
258structures to contain cycles, but unlike C<allow_cycles>, those cyclic
259rreferences will be weak. That means that code that recurrsively walks
260the data structure must be prepared with cycles, but at least not special
261precautions must be implemented to free these data structures.
262
263Only those references leading to actual cycles will be weakened - other
264references, e.g. when the same hash or arrray is referenced multiple times
265in an arrray, will be normal references.
252 266
253This option does not affect C<encode> in any way - shared values and 267This option does not affect C<encode> in any way - shared values and
254references will always be encoded properly if present. 268references will always be encoded properly if present.
255 269
256=item $cbor = $cbor->forbid_objects ([$enable]) 270=item $cbor = $cbor->forbid_objects ([$enable])
471Perl data structure in memory at one time, it does allow you to parse a 485Perl data structure in memory at one time, it does allow you to parse a
472CBOR stream incrementally, using a similar to using "decode_prefix" to see 486CBOR stream incrementally, using a similar to using "decode_prefix" to see
473if a full CBOR object is available, but is much more efficient. 487if a full CBOR object is available, but is much more efficient.
474 488
475It basically works by parsing as much of a CBOR string as possible - if 489It basically works by parsing as much of a CBOR string as possible - if
476the CBOR data is not complete yet, the pasrer will remember where it was, 490the CBOR data is not complete yet, the parser will remember where it was,
477to be able to restart when more data has been accumulated. Once enough 491to be able to restart when more data has been accumulated. Once enough
478data is available to either decode a complete CBOR value or raise an 492data is available to either decode a complete CBOR value or raise an
479error, a real decode will be attempted. 493error, a real decode will be attempted.
480 494
481A typical use case would be a network protocol that consists of sending 495A typical use case would be a network protocol that consists of sending
633create such objects. 647create such objects.
634 648
635=item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error 649=item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error
636 650
637These special values become CBOR true, CBOR false and CBOR undefined 651These special values become CBOR true, CBOR false and CBOR undefined
638values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly 652values, respectively.
639if you want.
640 653
641=item other blessed objects 654=item other blessed objects
642 655
643Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See 656Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See
644L<TAG HANDLING AND EXTENSIONS> for specific classes handled by this 657L<TAG HANDLING AND EXTENSIONS> for specific classes handled by this
704=back 717=back
705 718
706=head2 TYPE CASTS 719=head2 TYPE CASTS
707 720
708B<EXPERIMENTAL>: As an experimental extension, C<CBOR::XS> allows you to 721B<EXPERIMENTAL>: As an experimental extension, C<CBOR::XS> allows you to
709force specific cbor types to be used when encoding. That allows you to 722force specific CBOR types to be used when encoding. That allows you to
710encode types not normally accessible (e.g. half floats) as well as force 723encode types not normally accessible (e.g. half floats) as well as force
711string types even when C<text_strings> is in effect. 724string types even when C<text_strings> is in effect.
712 725
713Type forcing is done by calling a special "cast" function which keeps a 726Type forcing is done by calling a special "cast" function which keeps a
714copy of the value and returns a new value that can be handed over to any 727copy of the value and returns a new value that can be handed over to any
715CBOR encoder function. 728CBOR encoder function.
716 729
717The following casts are currently available (all of which are unary operators): 730The following casts are currently available (all of which are unary
731operators, that is, have a prototype of C<$>):
718 732
719=over 733=over
720 734
721=item CBOR::XS::as_int $value 735=item CBOR::XS::as_int $value
722 736
729 743
730=item CBOR::XS::as_bytes $value 744=item CBOR::XS::as_bytes $value
731 745
732Forces the value to be encoded as a (binary) string value. 746Forces the value to be encoded as a (binary) string value.
733 747
748Example: encode a perl string as binary even though C<text_strings> is in
749effect.
750
751 CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]);
752
734=item CBOR::XS::as_bool $value 753=item CBOR::XS::as_bool $value
735 754
736Converts a Perl boolean (which can be any kind of scalar) into a CBOR 755Converts a Perl boolean (which can be any kind of scalar) into a CBOR
737boolean. Strictly the same, but shorter to write, than: 756boolean. Strictly the same, but shorter to write, than:
738 757
748 767
749=item CBOR::XS::as_float64 $value 768=item CBOR::XS::as_float64 $value
750 769
751Forces double-float (IEEE 754 binary64) encoding of the given value. 770Forces double-float (IEEE 754 binary64) encoding of the given value.
752 771
753=item, CBOR::XS::as_cbor $cbor_text 772=item CBOR::XS::as_cbor $cbor_text
754 773
755Bot a type cast per-se, this type cast forces the argument to eb encoded 774Not a type cast per-se, this type cast forces the argument to be encoded
756as-is. This can be used to embed pre-encoded CBOR data. 775as-is. This can be used to embed pre-encoded CBOR data.
757 776
758Note that no checking on the validity of the C<$cbor_text> is done - it's 777Note that no checking on the validity of the C<$cbor_text> is done - it's
759the callers responsibility to correctly encode values. 778the callers responsibility to correctly encode values.
760 779
780=item CBOR::XS::as_map [key => value...]
781
782Treat the array reference as key value pairs and output a CBOR map. This
783allows you to generate CBOR maps with arbitrary key types (or, if you
784don't care about semantics, duplicate keys or pairs in a custom order),
785which is otherwise hard to do with Perl.
786
787The single argument must be an array reference with an even number of
788elements.
789
790Note that only the reference to the array is copied, the array itself is
791not. Modifications done to the array before calling an encoding function
792will be reflected in the encoded output.
793
794Example: encode a CBOR map with a string and an integer as keys.
795
796 encode_cbor CBOR::XS::as_map [string => "value", 5 => "value"]
797
761=back 798=back
762
763Example: encode a perl string as binary even though C<text_strings> is in
764effect.
765
766 CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]);
767 799
768=cut 800=cut
769 801
770sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: } 802sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: }
771sub CBOR::XS::as_int ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: } 803sub CBOR::XS::as_int ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: }
774sub CBOR::XS::as_float16 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: } 806sub CBOR::XS::as_float16 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: }
775sub CBOR::XS::as_float32 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: } 807sub CBOR::XS::as_float32 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: }
776sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: } 808sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: }
777 809
778sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false } 810sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false }
811
812sub CBOR::XS::as_map ($) {
813 ARRAY:: eq ref $_[0]
814 and $#{ $_[0] } & 1
815 or do { require Carp; Carp::croak ("CBOR::XS::as_map only acepts array references with an even number of elements, caught") };
816
817 bless [$_[0], 7, undef], CBOR::XS::Tagged::
818}
779 819
780=head2 OBJECT SERIALISATION 820=head2 OBJECT SERIALISATION
781 821
782This module implements both a CBOR-specific and the generic 822This module implements both a CBOR-specific and the generic
783L<Types::Serialier> object serialisation protocol. The following 823L<Types::Serialier> object serialisation protocol. The following

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines