… | |
… | |
64 | |
64 | |
65 | package CBOR::XS; |
65 | package CBOR::XS; |
66 | |
66 | |
67 | use common::sense; |
67 | use common::sense; |
68 | |
68 | |
69 | our $VERSION = 1.25; |
69 | our $VERSION = 1.41; |
70 | our @ISA = qw(Exporter); |
70 | our @ISA = qw(Exporter); |
71 | |
71 | |
72 | our @EXPORT = qw(encode_cbor decode_cbor); |
72 | our @EXPORT = qw(encode_cbor decode_cbor); |
73 | |
73 | |
74 | use Exporter; |
74 | use Exporter; |
… | |
… | |
224 | cycles - future versions of this module might chose to decode cyclic data |
224 | cycles - future versions of this module might chose to decode cyclic data |
225 | structures using weak references when this option is off, instead of |
225 | structures using weak references when this option is off, instead of |
226 | throwing an error. |
226 | throwing an error. |
227 | |
227 | |
228 | This option does not affect C<encode> in any way - shared values and |
228 | This option does not affect C<encode> in any way - shared values and |
229 | references will always be decoded properly if present. |
229 | references will always be encoded properly if present. |
230 | |
230 | |
231 | =item $cbor = $cbor->pack_strings ([$enable]) |
231 | =item $cbor = $cbor->pack_strings ([$enable]) |
232 | |
232 | |
233 | =item $enabled = $cbor->get_pack_strings |
233 | =item $enabled = $cbor->get_pack_strings |
234 | |
234 | |
… | |
… | |
247 | the standard CBOR way. |
247 | the standard CBOR way. |
248 | |
248 | |
249 | This option does not affect C<decode> in any way - string references will |
249 | This option does not affect C<decode> in any way - string references will |
250 | always be decoded properly if present. |
250 | always be decoded properly if present. |
251 | |
251 | |
|
|
252 | =item $cbor = $cbor->text_keys ([$enable]) |
|
|
253 | |
|
|
254 | =item $enabled = $cbor->get_text_keys |
|
|
255 | |
|
|
256 | If C<$enabled> is true (or missing), then C<encode> will encode all |
|
|
257 | perl hash keys as CBOR text strings/UTF-8 string, upgrading them as needed. |
|
|
258 | |
|
|
259 | If C<$enable> is false (the default), then C<encode> will encode hash keys |
|
|
260 | normally - upgraded perl strings (strings internally encoded as UTF-8) as |
|
|
261 | CBOR text strings, and downgraded perl strings as CBOR byte strings. |
|
|
262 | |
|
|
263 | This option does not affect C<decode> in any way. |
|
|
264 | |
|
|
265 | This option is useful for interoperability with CBOR decoders that don't |
|
|
266 | treat byte strings as a form of text. It is especially useful as Perl |
|
|
267 | gives very little control over hash keys. |
|
|
268 | |
|
|
269 | Enabling this option can be slow, as all downgraded hash keys that are |
|
|
270 | encoded need to be scanned and converted to UTF-8. |
|
|
271 | |
|
|
272 | =item $cbor = $cbor->text_strings ([$enable]) |
|
|
273 | |
|
|
274 | =item $enabled = $cbor->get_text_strings |
|
|
275 | |
|
|
276 | This option works similar to C<text_keys>, above, but works on all strings |
|
|
277 | (including hash keys), so C<text_keys> has no further effect after |
|
|
278 | enabling C<text_strings>. |
|
|
279 | |
|
|
280 | If C<$enabled> is true (or missing), then C<encode> will encode all perl |
|
|
281 | strings as CBOR text strings/UTF-8 strings, upgrading them as needed. |
|
|
282 | |
|
|
283 | If C<$enable> is false (the default), then C<encode> will encode strings |
|
|
284 | normally (but see C<text_keys>) - upgraded perl strings (strings |
|
|
285 | internally encoded as UTF-8) as CBOR text strings, and downgraded perl |
|
|
286 | strings as CBOR byte strings. |
|
|
287 | |
|
|
288 | This option does not affect C<decode> in any way. |
|
|
289 | |
|
|
290 | This option has similar advantages and disadvantages as C<text_keys>. In |
|
|
291 | addition, this option effectively removes the ability to encode byte |
|
|
292 | strings, which might break some C<FREEZE> and C<TO_CBOR> methods that rely |
|
|
293 | on this, such as bignum encoding, so this option is mainly useful for very |
|
|
294 | simple data. |
|
|
295 | |
252 | =item $cbor = $cbor->validate_utf8 ([$enable]) |
296 | =item $cbor = $cbor->validate_utf8 ([$enable]) |
253 | |
297 | |
254 | =item $enabled = $cbor->get_validate_utf8 |
298 | =item $enabled = $cbor->get_validate_utf8 |
255 | |
299 | |
256 | If C<$enable> is true (or missing), then C<decode> will validate that |
300 | If C<$enable> is true (or missing), then C<decode> will validate that |
… | |
… | |
261 | The concept of "valid UTF-8" used is perl's concept, which is a superset |
305 | The concept of "valid UTF-8" used is perl's concept, which is a superset |
262 | of the official UTF-8. |
306 | of the official UTF-8. |
263 | |
307 | |
264 | If C<$enable> is false (the default), then C<decode> will blindly accept |
308 | If C<$enable> is false (the default), then C<decode> will blindly accept |
265 | UTF-8 data, marking them as valid UTF-8 in the resulting data structure |
309 | UTF-8 data, marking them as valid UTF-8 in the resulting data structure |
266 | regardless of whether thats true or not. |
310 | regardless of whether that's true or not. |
267 | |
311 | |
268 | Perl isn't too happy about corrupted UTF-8 in strings, but should |
312 | Perl isn't too happy about corrupted UTF-8 in strings, but should |
269 | generally not crash or do similarly evil things. Extensions might be not |
313 | generally not crash or do similarly evil things. Extensions might be not |
270 | so forgiving, so it's recommended to turn on this setting if you receive |
314 | so forgiving, so it's recommended to turn on this setting if you receive |
271 | untrusted CBOR. |
315 | untrusted CBOR. |
… | |
… | |
481 | |
525 | |
482 | =item hash references |
526 | =item hash references |
483 | |
527 | |
484 | Perl hash references become CBOR maps. As there is no inherent ordering in |
528 | Perl hash references become CBOR maps. As there is no inherent ordering in |
485 | hash keys (or CBOR maps), they will usually be encoded in a pseudo-random |
529 | hash keys (or CBOR maps), they will usually be encoded in a pseudo-random |
486 | order. This order can be different each time a hahs is encoded. |
530 | order. This order can be different each time a hash is encoded. |
487 | |
531 | |
488 | Currently, tied hashes will use the indefinite-length format, while normal |
532 | Currently, tied hashes will use the indefinite-length format, while normal |
489 | hashes will use the fixed-length format. |
533 | hashes will use the fixed-length format. |
490 | |
534 | |
491 | =item array references |
535 | =item array references |
… | |
… | |
663 | "$self" # encode url string |
707 | "$self" # encode url string |
664 | } |
708 | } |
665 | |
709 | |
666 | sub URI::THAW { |
710 | sub URI::THAW { |
667 | my ($class, $serialiser, $uri) = @_; |
711 | my ($class, $serialiser, $uri) = @_; |
668 | |
|
|
669 | $class->new ($uri) |
712 | $class->new ($uri) |
670 | } |
713 | } |
671 | |
714 | |
672 | Unlike C<TO_CBOR>, multiple values can be returned by C<FREEZE>. For |
715 | Unlike C<TO_CBOR>, multiple values can be returned by C<FREEZE>. For |
673 | example, a C<FREEZE> method that returns "type", "id" and "variant" values |
716 | example, a C<FREEZE> method that returns "type", "id" and "variant" values |
… | |
… | |
804 | additional tags (such as base64url). |
847 | additional tags (such as base64url). |
805 | |
848 | |
806 | =head2 ENFORCED TAGS |
849 | =head2 ENFORCED TAGS |
807 | |
850 | |
808 | These tags are always handled when decoding, and their handling cannot be |
851 | These tags are always handled when decoding, and their handling cannot be |
809 | overriden by the user. |
852 | overridden by the user. |
810 | |
853 | |
811 | =over 4 |
854 | =over 4 |
812 | |
855 | |
813 | =item 26 (perl-object, L<http://cbor.schmorp.de/perl-object>) |
856 | =item 26 (perl-object, L<http://cbor.schmorp.de/perl-object>) |
814 | |
857 | |
815 | These tags are automatically created (and decoded) for serialisable |
858 | These tags are automatically created (and decoded) for serialisable |
816 | objects using the C<FREEZE/THAW> methods (the L<Types::Serialier> object |
859 | objects using the C<FREEZE/THAW> methods (the L<Types::Serialier> object |
817 | serialisation protocol). See L<OBJECT SERIALISATION> for details. |
860 | serialisation protocol). See L<OBJECT SERIALISATION> for details. |
818 | |
861 | |
819 | =item 28, 29 (shareable, sharedref, L <http://cbor.schmorp.de/value-sharing>) |
862 | =item 28, 29 (shareable, sharedref, L<http://cbor.schmorp.de/value-sharing>) |
820 | |
863 | |
821 | These tags are automatically decoded when encountered (and they do not |
864 | These tags are automatically decoded when encountered (and they do not |
822 | result in a cyclic data structure, see C<allow_cycles>), resulting in |
865 | result in a cyclic data structure, see C<allow_cycles>), resulting in |
823 | shared values in the decoded object. They are only encoded, however, when |
866 | shared values in the decoded object. They are only encoded, however, when |
824 | C<allow_sharing> is enabled. |
867 | C<allow_sharing> is enabled. |
… | |
… | |
834 | will be shared, others will not. While non-reference shared values can be |
877 | will be shared, others will not. While non-reference shared values can be |
835 | generated in Perl with some effort, they were considered too unimportant |
878 | generated in Perl with some effort, they were considered too unimportant |
836 | to be supported in the encoder. The decoder, however, will decode these |
879 | to be supported in the encoder. The decoder, however, will decode these |
837 | values as shared values. |
880 | values as shared values. |
838 | |
881 | |
839 | =item 256, 25 (stringref-namespace, stringref, L <http://cbor.schmorp.de/stringref>) |
882 | =item 256, 25 (stringref-namespace, stringref, L<http://cbor.schmorp.de/stringref>) |
840 | |
883 | |
841 | These tags are automatically decoded when encountered. They are only |
884 | These tags are automatically decoded when encountered. They are only |
842 | encoded, however, when C<pack_strings> is enabled. |
885 | encoded, however, when C<pack_strings> is enabled. |
843 | |
886 | |
844 | =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>) |
887 | =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>) |
845 | |
888 | |
846 | This tag is automatically generated when a reference are encountered (with |
889 | This tag is automatically generated when a reference are encountered (with |
847 | the exception of hash and array refernces). It is converted to a reference |
890 | the exception of hash and array references). It is converted to a reference |
848 | when decoding. |
891 | when decoding. |
849 | |
892 | |
850 | =item 55799 (self-describe CBOR, RFC 7049) |
893 | =item 55799 (self-describe CBOR, RFC 7049) |
851 | |
894 | |
852 | This value is not generated on encoding (unless explicitly requested by |
895 | This value is not generated on encoding (unless explicitly requested by |
… | |
… | |
855 | =back |
898 | =back |
856 | |
899 | |
857 | =head2 NON-ENFORCED TAGS |
900 | =head2 NON-ENFORCED TAGS |
858 | |
901 | |
859 | These tags have default filters provided when decoding. Their handling can |
902 | These tags have default filters provided when decoding. Their handling can |
860 | be overriden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by |
903 | be overridden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by |
861 | providing a custom C<filter> callback when decoding. |
904 | providing a custom C<filter> callback when decoding. |
862 | |
905 | |
863 | When they result in decoding into a specific Perl class, the module |
906 | When they result in decoding into a specific Perl class, the module |
864 | usually provides a corresponding C<TO_CBOR> method as well. |
907 | usually provides a corresponding C<TO_CBOR> method as well. |
865 | |
908 | |
… | |
… | |
907 | C<URI::TO_CBOR> method again results in a CBOR URI value. |
950 | C<URI::TO_CBOR> method again results in a CBOR URI value. |
908 | |
951 | |
909 | =back |
952 | =back |
910 | |
953 | |
911 | =cut |
954 | =cut |
912 | |
|
|
913 | our %FILTER = ( |
|
|
914 | # 0 # rfc4287 datetime, utf-8 |
|
|
915 | # 1 # unix timestamp, any |
|
|
916 | |
|
|
917 | 2 => sub { # pos bigint |
|
|
918 | require Math::BigInt; |
|
|
919 | Math::BigInt->new ("0x" . unpack "H*", pop) |
|
|
920 | }, |
|
|
921 | |
|
|
922 | 3 => sub { # neg bigint |
|
|
923 | require Math::BigInt; |
|
|
924 | -Math::BigInt->new ("0x" . unpack "H*", pop) |
|
|
925 | }, |
|
|
926 | |
|
|
927 | 4 => sub { # decimal fraction, array |
|
|
928 | require Math::BigFloat; |
|
|
929 | Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) |
|
|
930 | }, |
|
|
931 | |
|
|
932 | 5 => sub { # bigfloat, array |
|
|
933 | require Math::BigFloat; |
|
|
934 | scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2) |
|
|
935 | }, |
|
|
936 | |
|
|
937 | 21 => sub { pop }, # expected conversion to base64url encoding |
|
|
938 | 22 => sub { pop }, # expected conversion to base64 encoding |
|
|
939 | 23 => sub { pop }, # expected conversion to base16 encoding |
|
|
940 | |
|
|
941 | # 24 # embedded cbor, byte string |
|
|
942 | |
|
|
943 | 32 => sub { |
|
|
944 | require URI; |
|
|
945 | URI->new (pop) |
|
|
946 | }, |
|
|
947 | |
|
|
948 | # 33 # base64url rfc4648, utf-8 |
|
|
949 | # 34 # base64 rfc46484, utf-8 |
|
|
950 | # 35 # regex pcre/ecma262, utf-8 |
|
|
951 | # 36 # mime message rfc2045, utf-8 |
|
|
952 | ); |
|
|
953 | |
|
|
954 | |
955 | |
955 | =head1 CBOR and JSON |
956 | =head1 CBOR and JSON |
956 | |
957 | |
957 | CBOR is supposed to implement a superset of the JSON data model, and is, |
958 | CBOR is supposed to implement a superset of the JSON data model, and is, |
958 | with some coercion, able to represent all JSON texts (something that other |
959 | with some coercion, able to represent all JSON texts (something that other |
… | |
… | |
1022 | |
1023 | |
1023 | |
1024 | |
1024 | =head1 LIMITATIONS ON PERLS WITHOUT 64-BIT INTEGER SUPPORT |
1025 | =head1 LIMITATIONS ON PERLS WITHOUT 64-BIT INTEGER SUPPORT |
1025 | |
1026 | |
1026 | On perls that were built without 64 bit integer support (these are rare |
1027 | On perls that were built without 64 bit integer support (these are rare |
1027 | nowadays, even on 32 bit architectures), support for any kind of 64 bit |
1028 | nowadays, even on 32 bit architectures, as all major Perl distributions |
|
|
1029 | are built with 64 bit integer support), support for any kind of 64 bit |
1028 | integer in CBOR is very limited - most likely, these 64 bit values will |
1030 | integer in CBOR is very limited - most likely, these 64 bit values will |
1029 | be truncated, corrupted, or otherwise not decoded correctly. This also |
1031 | be truncated, corrupted, or otherwise not decoded correctly. This also |
1030 | includes string, array and map sizes that are stored as 64 bit integers. |
1032 | includes string, array and map sizes that are stored as 64 bit integers. |
1031 | |
1033 | |
1032 | |
1034 | |
… | |
… | |
1057 | # Time::Piece::Strptime uses the "incredibly flexible date parsing routine" |
1059 | # Time::Piece::Strptime uses the "incredibly flexible date parsing routine" |
1058 | # from FreeBSD, which can't parse ISO 8601, RFC3339, RFC4287 or much of anything |
1060 | # from FreeBSD, which can't parse ISO 8601, RFC3339, RFC4287 or much of anything |
1059 | # else either. Whats incredibe over standard strptime totally escapes me. |
1061 | # else either. Whats incredibe over standard strptime totally escapes me. |
1060 | # doesn't do fractional times, either. sigh. |
1062 | # doesn't do fractional times, either. sigh. |
1061 | # In fact, it's all a lie, it uses whatever strptime it wants, and of course, |
1063 | # In fact, it's all a lie, it uses whatever strptime it wants, and of course, |
1062 | # they are all incomptible. The openbsd one simply ignores %z (but according to the |
1064 | # they are all incompatible. The openbsd one simply ignores %z (but according to the |
1063 | # docs, it would be much more incredibly flexible indeed. If it worked, that is.). |
1065 | # docs, it would be much more incredibly flexible indeed. If it worked, that is.). |
1064 | scalar eval { |
1066 | scalar eval { |
1065 | my $s = $_[1]; |
1067 | my $s = $_[1]; |
1066 | |
1068 | |
1067 | $s =~ s/Z$/+00:00/; |
1069 | $s =~ s/Z$/+00:00/; |
… | |
… | |
1095 | Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) |
1097 | Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) |
1096 | }, |
1098 | }, |
1097 | |
1099 | |
1098 | 5 => sub { # bigfloat, array |
1100 | 5 => sub { # bigfloat, array |
1099 | require Math::BigFloat; |
1101 | require Math::BigFloat; |
1100 | scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2) |
1102 | scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0]) |
1101 | }, |
1103 | }, |
1102 | |
1104 | |
1103 | 21 => sub { pop }, # expected conversion to base64url encoding |
1105 | 21 => sub { pop }, # expected conversion to base64url encoding |
1104 | 22 => sub { pop }, # expected conversion to base64 encoding |
1106 | 22 => sub { pop }, # expected conversion to base64 encoding |
1105 | 23 => sub { pop }, # expected conversion to base16 encoding |
1107 | 23 => sub { pop }, # expected conversion to base16 encoding |