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.57 by root, Mon Apr 25 21:57:57 2016 UTC vs.
Revision 1.58 by root, Tue Apr 26 16:07:04 2016 UTC

935objects. The corresponding C<Math::BigFloat::TO_CBOR> method I<always> 935objects. The corresponding C<Math::BigFloat::TO_CBOR> method I<always>
936encodes into a decimal fraction (either tag 4 or 264). 936encodes into a decimal fraction (either tag 4 or 264).
937 937
938NaN and infinities are not encoded properly, as they cannot be represented 938NaN and infinities are not encoded properly, as they cannot be represented
939in CBOR. 939in CBOR.
940
941See L<BIGNUM SECURITY CONSIDERATIONS> for more info.
942
943=item 30 (rational numbers)
944
945These tags are decoded into L<Math::BigRat> objects. The corresponding
946C<Math::BigRat::TO_CBOR> method encodes rational numbers with denominator
947C<1> via their numerator only, i.e., they become normal integers or
948C<bignums>.
940 949
941See L<BIGNUM SECURITY CONSIDERATIONS> for more info. 950See L<BIGNUM SECURITY CONSIDERATIONS> for more info.
942 951
943=item 21, 22, 23 (expected later JSON conversion) 952=item 21, 22, 23 (expected later JSON conversion)
944 953
1008=head1 BIGNUM SECURITY CONSIDERATIONS 1017=head1 BIGNUM SECURITY CONSIDERATIONS
1009 1018
1010CBOR::XS provides a C<TO_CBOR> method for both L<Math::BigInt> and 1019CBOR::XS provides a C<TO_CBOR> method for both L<Math::BigInt> and
1011L<Math::BigFloat> that tries to encode the number in the simplest possible 1020L<Math::BigFloat> that tries to encode the number in the simplest possible
1012way, that is, either a CBOR integer, a CBOR bigint/decimal fraction (tag 1021way, that is, either a CBOR integer, a CBOR bigint/decimal fraction (tag
10134) or an arbitrary-exponent decimal fraction (tag 264). 10224) or an arbitrary-exponent decimal fraction (tag 264). Rational numbers
1023(L<Math::BigRat>, tag 30) can also contain bignums as members.
1014 1024
1015It will also understand base-2 bigfloat or arbitrary-exponent bigfloats 1025CBOR::XS will also understand base-2 bigfloat or arbitrary-exponent
1016(tags 5 and 265), but it will never generate these on its own. 1026bigfloats (tags 5 and 265), but it will never generate these on its own.
1017 1027
1018Using the built-in L<Math::BigInt::Calc> support, encoding and decoding 1028Using the built-in L<Math::BigInt::Calc> support, encoding and decoding
1019decimal fractions is generally fast. Decoding bigints can be slow for very 1029decimal fractions is generally fast. Decoding bigints can be slow for very
1020big numbers, and decoding bigfloats or arbitrary-exponent bigfloats can be 1030big numbers (tens of thousands of digits, something that could potentially
1021extremely slow (minutes, decades) for large exponents. 1031be caught by limiting the size of CBOR texts), and decoding bigfloats or
1032arbitrary-exponent bigfloats can be I<extremely> slow (minutes, decades)
1033for large exponents (roughly 40 bit and longer).
1022 1034
1023Additionally, L<Math::BigInt> can take advantage of other bignum 1035Additionally, L<Math::BigInt> can take advantage of other bignum
1024libraries, such as L<Math::GMP>, which cannot handle big 1036libraries, such as L<Math::GMP>, which cannot handle big floats with large
1025floats with large exponents, and might simply abort or crash your program, 1037exponents, and might simply abort or crash your program, due to their code
1026due to their code quality. 1038quality.
1027 1039
1028This can be a concern if you want to parse untrusted CBOR. If it is, you 1040This can be a concern if you want to parse untrusted CBOR. If it is, you
1029need to disable decoding of tag 2 (bigint) and 3 (negative bigint) types, 1041might want to disable decoding of tag 2 (bigint) and 3 (negative bigint)
1030which will also disable bigfloat support (to be sure, you can also disable 1042types. You should also disable types 5 and 265, as these can be slow even
1031types 4, 5, 264 and 265). 1043without bigints.
1044
1045Disabling bigints will also partially or fully disable types that rely on
1046them, e.g. rational numbers that use bignums.
1032 1047
1033 1048
1034=head1 CBOR IMPLEMENTATION NOTES 1049=head1 CBOR IMPLEMENTATION NOTES
1035 1050
1036This section contains some random implementation notes. They do not 1051This section contains some random implementation notes. They do not
1123 4 => sub { # decimal fraction, array 1138 4 => sub { # decimal fraction, array
1124 require Math::BigFloat; 1139 require Math::BigFloat;
1125 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) 1140 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0])
1126 }, 1141 },
1127 1142
1143 264 => sub { # decimal fraction with arbitrary exponent
1144 require Math::BigFloat;
1145 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0])
1146 },
1147
1128 5 => sub { # bigfloat, array 1148 5 => sub { # bigfloat, array
1129 require Math::BigFloat; 1149 require Math::BigFloat;
1130 scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0]) 1150 scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0])
1131 }, 1151 },
1132 1152
1153 265 => sub { # bigfloat with arbitrary exponent
1154 require Math::BigFloat;
1155 scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0])
1156 },
1157
1158 30 => sub { # rational number
1159 require Math::BigRat;
1160 Math::BigRat->new ("$_[1][0]/$_[1][1]") # separate parameters only work in recent versons
1161 },
1162
1133 21 => sub { pop }, # expected conversion to base64url encoding 1163 21 => sub { pop }, # expected conversion to base64url encoding
1134 22 => sub { pop }, # expected conversion to base64 encoding 1164 22 => sub { pop }, # expected conversion to base64 encoding
1135 23 => sub { pop }, # expected conversion to base16 encoding 1165 23 => sub { pop }, # expected conversion to base16 encoding
1136 1166
1137 # 24 # embedded cbor, byte string 1167 # 24 # embedded cbor, byte string
1143 1173
1144 # 33 # base64url rfc4648, utf-8 1174 # 33 # base64url rfc4648, utf-8
1145 # 34 # base64 rfc46484, utf-8 1175 # 34 # base64 rfc46484, utf-8
1146 # 35 # regex pcre/ecma262, utf-8 1176 # 35 # regex pcre/ecma262, utf-8
1147 # 36 # mime message rfc2045, utf-8 1177 # 36 # mime message rfc2045, utf-8
1148
1149 264 => sub { # decimal fraction with arbitrary exponent
1150 require Math::BigFloat;
1151 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0])
1152 },
1153
1154 265 => sub { # bigfloat with arbitrary exponent
1155 require Math::BigFloat;
1156 scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0])
1157 },
1158); 1178);
1159 1179
1160sub CBOR::XS::default_filter { 1180sub CBOR::XS::default_filter {
1161 &{ $FILTER{$_[0]} or return } 1181 &{ $FILTER{$_[0]} or return }
1162} 1182}
1183 -9223372036854775808 <= $e && $e <= 18446744073709551615 1203 -9223372036854775808 <= $e && $e <= 18446744073709551615
1184 ? tag 4, [$e->numify, $m] 1204 ? tag 4, [$e->numify, $m]
1185 : tag 264, [$e, $m] 1205 : tag 264, [$e, $m]
1186} 1206}
1187 1207
1208sub Math::BigRat::TO_CBOR {
1209 my ($n, $d) = $_[0]->parts;
1210
1211 $d == 1
1212 ? $n
1213 : tag 30, [$n, $d]
1214}
1215
1188sub Time::Piece::TO_CBOR { 1216sub Time::Piece::TO_CBOR {
1189 tag 1, 0 + $_[0]->epoch 1217 tag 1, 0 + $_[0]->epoch
1190} 1218}
1191 1219
1192XSLoader::load "CBOR::XS", $VERSION; 1220XSLoader::load "CBOR::XS", $VERSION;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines