BSON/PP.pm

filter: Statementstmt 88.6% 352 / 397 Branchbran 85.7% 247 / 288 Conditioncond 82.5% 109 / 132 MC/DCmcdc 68.8% 53 / 77 Subroutinesub 93.7% 30 / 32 Podpod n/a totaltotal 85.4% 791 / 926 CC 189 SCAR 44.4
CC
189
Cov
87%
_encode_bson
129.3
_decode_bson
74.4
_int64_to_bigint
20.0
CRAP 84.6
« BSON.pm ↑ Summary BSON/Decimal128.pm »
1 71 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 71 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package BSON::PP;
7 # ABSTRACT: Pure Perl BSON implementation
8  
9 71 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 71 our $VERSION = 'v1.12.2';
11  
12 71 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 71 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 71 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 71 ▶use Scalar::Util qw/blessed looks_like_number refaddr reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 71 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 71 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 71 ▶use BSON::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 71 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 71 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 71 ▶use re 'regexp_pattern';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 71 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 HAS_INT64 => $Config{use64bitint},
27 71 };
28  
29 71 ▶use if !HAS_INT64, "Math::BigInt";
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Max integer sizes
32 71 my $max_int32 = 2147483647;
33 71 my $min_int32 = -2147483648;
34 71 my $max_int64 =
35 HAS_INT64 ? 9223372036854775807 : Math::BigInt->new("9223372036854775807");
36 71 my $min_int64 =
37 HAS_INT64 ? -9223372036854775808 : Math::BigInt->new("-9223372036854775808");
38  
39 #<<<
40 71 my $int_re = qr/^(?:(?:[+-]?)(?:[0123456789]+))$/;
41 71 my $doub_re = qr/^(?:(?i)(?:NaN|-?Inf(?:inity)?)|(?:[+-]?)(?:(?=[0123456789]|[.])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))$/;
42 #>>>
43  
44 71 my $bools_re = qr/::(?:Boolean|_Bool|Bool)\z/;
45  
46 71 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 71 BSON_TYPE_NAME => "CZ*",
49 BSON_DOUBLE => "d<",
50 BSON_STRING => "V/Z*",
51 BSON_BOOLEAN => "C",
52 BSON_REGEX => "Z*Z*",
53 BSON_JSCODE => "",
54 BSON_INT32 => "l<",
55 BSON_UINT32 => "L<",
56 BSON_INT64 => "q<",
57 BSON_8BYTES => "a8",
58 BSON_16BYTES => "a16",
59 BSON_TIMESTAMP => "L<L<",
60 BSON_CODE_W_SCOPE => "l<",
61 BSON_REMAINING => 'a*',
62 BSON_SKIP_4_BYTES => 'x4',
63 BSON_OBJECTID => 'a12',
64 BSON_BINARY_TYPE => 'C',
65 BSON_CSTRING => 'Z*',
66 BSON_MAX_DEPTH => 100,
67 71 };
68  
69 1 ▶sub _printable {
Subroutine _printable: called CC=1 SCAR=0.0
70 1 my $value = shift;
71 1 $value =~ s/([^[:print:]])/sprintf("\\x%02x",ord($1))/ge;
72 1 return $value;
73 }
74  
75 5 ▶sub _split_re {
Subroutine _split_re: called CC=2 SCAR=10.5
76 5 my $value = shift;
77 5 ▶ if ( $] ge 5.010 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] ge 5.01) { } 5 0 5
78 5 return re::regexp_pattern($value);
79 }
80 else {
81 0 $value =~ s/^\(\?\^?//;
82 0 $value =~ s/\)$//;
83 0 my ( $opt, $re ) = split( /:/, $value, 2 );
84 0 $opt =~ s/\-\w+$//;
85 0 return ( $re, $opt );
86 }
87 }
88  
89 21 ▶sub _ixhash_iterator {
Subroutine _ixhash_iterator: called CC=1 SCAR=0.0
90 21 my $ixhash = shift;
91 21 my $started = 0;
92 67 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
93 67 ▶ my $k = $started ? $ixhash->NEXTKEY : do { $started++; $ixhash->FIRSTKEY };
Branch: 1 branch
BranchTrueFalseTotal
$started ? : 46 21 67
94 67 ▶ return unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 20 47 67
95 47 return ($k, $ixhash->FETCH($k));
96 }
97 21 }
98  
99 # relying on Perl's each() is prone to action-at-a-distance effects we
100 # want to avoid, so we construct our own iterator for hashes
101 29304 ▶sub _hashlike_iterator {
Subroutine _hashlike_iterator: called CC=2 SCAR=6.9
102 29304 my $hashlike = shift;
103 29304 my @keys = keys %$hashlike;
104 @keys = sort @keys
105 29304 ▶ if $ENV{BSON_TEST_SORT_HASH};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"BSON_TEST_SORT_HASH"} 0 29304 29304
106 172007 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
107 172007 my $k = shift @keys;
108 172007 ▶ return unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 29095 142912 172007
109 142912 return ($k, $hashlike->{$k});
110 }
111 29304 }
112  
113 # XXX could be optimized down to only one substr to trim/pad
114 10 ▶sub _bigint_to_int64 {
Subroutine _bigint_to_int64: called CC=6 SCAR=18.2
115 10 my $bigint = shift;
116 10 my $neg = $bigint < 0;
117 10 ▶ if ( $neg ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($neg) { } 1 9 10
elsif ($bigint > $max_int64) { } 0 9 9
118 1 ▶ if ( $bigint < $min_int64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bigint < $min_int64) 0 1 1
119 0 return "\x80\x00\x00\x00\x00\x00\x00\x00";
120 }
121 1 $bigint = abs($bigint) - ($max_int64 + 1);
122 }
123 elsif ( $bigint > $max_int64 ) {
124 0 return "\x7f\xff\xff\xff\xff\xff\xff\xff";
125 }
126  
127 10 my $as_hex = $bigint->as_hex; # big-endian hex
128 10 $as_hex =~ s{-?0x}{};
129 10 my $len = length($as_hex);
130 10 ▶ substr( $as_hex, 0, 0, "0" x ( 16 - $len ) ) if $len < 16; # pad to quad length
Branch: 1 branch
BranchTrueFalseTotal
if $len < 16 9 1 10
131 10 my $pack = pack( "H*", $as_hex );
132 10 ▶ $pack |= "\x80\x00\x00\x00\x00\x00\x00\x00" if $neg;
Branch: 1 branch
BranchTrueFalseTotal
if $neg 1 9 10
133 10 return scalar reverse $pack;
134 }
135  
136 0 ▶sub _int64_to_bigint {
Subroutine _int64_to_bigint: not called CC=4 SCAR=30.0
137 0 my $bytes = reverse(shift);
138 0 ▶ return Math::BigInt->new() if $bytes eq "\x00\x00\x00\x00\x00\x00\x00\x00";
Branch: 1 branch
BranchTrueFalseTotal
if $bytes eq "\0\0\0\0\0\0\0\0" 0 0 0
139 0 ▶ if ( unpack("c", $bytes) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (unpack('c', $bytes) < 0) { } 0 0 0
140 0 ▶ if ( $bytes eq "\x80\x00\x00\x00\x00\x00\x00\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes eq "\200\000\000\000\000\000\000\000") { } 0 0 0
141 0 return -1 * Math::BigInt->new( "0x" . unpack("H*",$bytes) );
142 }
143 else {
144 0 return -1 * Math::BigInt->new( "0x" . unpack( "H*", ~$bytes ) ) - 1;
145 }
146 }
147 else {
148 0 return Math::BigInt->new( "0x" . unpack( "H*", $bytes ) );
149 }
150 }
151  
152 17678 ▶sub _pack_int64 {
Subroutine _pack_int64: called CC=4 SCAR=16.8
153 17678 my $value = shift;
154 17678 my $type = ref($value);
155  
156 # if no type, then on 64-big perl we can pack with 'q'; otherwise
157 # we need to convert scalars to Math::BigInt and pack them that way.
158 17678 ▶ if ( ! $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 17668 10 17678
159 17668 return pack(BSON_INT64,$value ) if HAS_INT64;
160 0 $value = Math::BigInt->new($value);
161 0 $type = 'Math::BigInt';
162 }
163  
164 10 ▶ if ( $type eq 'Math::BigInt' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Math::BigInt') { } 10 0 10
elsif ($type eq 'Math::Int64') { } 0 0 0
165 10 return _bigint_to_int64($value);
166 }
167 elsif ( $type eq 'Math::Int64' ) {
168 0 return Math::Int64::int64_to_native($value);
169 }
170 else {
171 0 croak "Don't know how to encode $type '$value' as an Int64.";
172 }
173 }
174  
175 1 ▶sub _reftype_check {
Subroutine _reftype_check: called CC=2 SCAR=7.4
176 1 my $doc = shift;
177 1 my $type = ref($doc);
178 1 my $reftype = reftype($doc);
179 1 ▶ die "Can't encode non-container of type '$type'" unless $reftype eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $reftype eq "HASH" 1 0 1
180 0 return;
181 }
182  
183 29455 ▶sub _encode_bson {
Subroutine _encode_bson: called CC=101 SCAR=48.6
184 29455 my ($doc, $opt) = @_;
185  
186 29455 my $refaddr = refaddr($doc);
187 29455 ▶ die "circular reference detected" if $opt->{_circular}{$refaddr}++;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"_circular"}{$refaddr}++ 4 29451 29455
188  
189 29451 ▶ $opt->{_depth} = 0 unless defined $opt->{_depth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"_depth"} 2482 26969 29451
190 29451 $opt->{_depth}++;
191 29451 ▶ if ($opt->{_depth} > BSON_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"_depth"} > 100) 2 29449 29451
192 2 croak "Exceeded max object depth of ". BSON_MAX_DEPTH;
193 }
194  
195 29449 my $doc_type = ref($doc);
196  
197 29449 ▶ if ( $doc_type eq 'BSON::Raw' || $doc_type eq 'MongoDB::BSON::_EncodedDoc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc_type eq "BSON::Raw" or $doc_type eq "MongoDB::BSON::_EncodedDoc") 103 29346 29449
Condition66%2/3
$doc_type eq "BSON::Raw" or $doc_type eq "MongoDB::BSON::_EncodedDoc"
l!l&&r!l&&!r
103029346
MC/DC50%1/2
$doc_type eq "BSON::Raw" or $doc_type eq "MongoDB::BSON::_EncodedDoc"
$doc_type eq "BSON::Raw" $doc_type eq "MongoDB::BSON::_EncodedDoc"
Truth table66%2/3
A or B
A= $doc_type eq "BSON::Raw"B= $doc_type eq "MongoDB::BSON::_EncodedDoc"
ABresult
1X1
011
000
198 103 delete $opt->{_circular}{$refaddr};
199 103 $opt->{_depth}--;
200 103 return $doc->bson;
201 }
202  
203 29346 ▶ if ( $doc_type eq 'MongoDB::BSON::Raw' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc_type eq "MongoDB::BSON::Raw") 3 29343 29346
204 3 delete $opt->{_circular}{$refaddr};
205 3 $opt->{_depth}--;
206 3 return $$doc;
207 }
208  
209 my $iter =
210 $doc_type eq 'HASH' ? undef
211 : $doc_type eq 'BSON::Doc' ? $doc->_iterator
212 : $doc_type eq 'Tie::IxHash' ? _ixhash_iterator($doc)
213 : $doc_type eq 'BSON::DBRef' ? _ixhash_iterator( $doc->_ordered )
214 : $doc_type eq 'MongoDB::DBRef' ? _ixhash_iterator( $doc->_ordered )
215 0 ▶ : do { _reftype_check($doc); undef };
Branch: 5 branches
BranchTrueFalseTotal
$doc_type eq 'HASH' ? : 29304 39 29343
$doc_type eq 'BSON::Doc' ? : 17 22 39
$doc_type eq 'Tie::IxHash' ? : 6 16 22
$doc_type eq 'BSON::DBRef' ? : 15 1 16
$doc_type eq 'MongoDB::DBRef' ? : 0 1 1
216  
217 29342 ▶ $iter //= _hashlike_iterator($doc);
Condition66%2/3
$iter //= _hashlike_iterator($doc)
l!l&&r!l&&!r
38293040
MC/DC0%0/2
$iter //= _hashlike_iterator($doc)
$iter _hashlike_iterator($doc)
Truth table66%2/3
A //= B
A= $iterB= _hashlike_iterator($doc)
ABresult
1X1
011
000
218  
219 29342 ▶ my $op_char = defined($opt->{op_char}) ? $opt->{op_char} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'op_char'} ? : 2 29340 29342
220 my $invalid =
221 29342 ▶ length( $opt->{invalid_chars} ) ? qr/[\Q$opt->{invalid_chars}\E]/ : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $opt->{'invalid_chars'} ? : 2 29340 29342
222  
223 # Set up first key bookkeeping
224 29342 my $first_key_pending = !! defined($opt->{first_key});
225 29342 my $first_key;
226 29342 my $bson = '';
227  
228 29342 my ($key, $value);
229 29342 ▶ while ( $first_key_pending or ( $key, $value ) = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_key_pending or () = &$iter()) 142984 29132 172116
Condition100%3/3
$first_key_pending or () = &$iter()
l!l&&r!l&&!r
214298229132
MC/DC100%2/2
$first_key_pending or () = &$iter()
$first_key_pending () = &$iter()
Truth table100%3/3
A or B
A= $first_key_pendingB= () = &$iter()
ABresult
1X1
011
000
230 142984 ▶ next if defined $first_key && $key eq $first_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_key and $key eq $first_key 1 142983 142984
Condition100%3/3
defined $first_key and $key eq $first_key
!ll&&!rl&&r
14298121
MC/DC100%2/2
defined $first_key and $key eq $first_key
defined $first_key $key eq $first_key
Truth table100%3/3
A and B
A= defined $first_keyB= $key eq $first_key
ABresult
0X0
100
111
231  
232 142983 ▶ if ( $first_key_pending ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_key_pending) 2 142981 142983
233 2 $first_key = $key = delete $opt->{first_key};
234 2 $value = delete $opt->{first_value};
235 2 undef $first_key_pending;
236 }
237  
238 142983 ▶ last unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 142983 142983
239  
240 142983 ▶ croak "Key '" . _printable($key) . "' contains null character"
Branch: 1 branch
BranchTrueFalseTotal
unless index($key, "\0") == -1 1 142982 142983
241 unless -1 == index($key, "\0");
242  
243 142982 ▶ substr( $key, 0, 1 ) = '$'
Branch: 1 branch
BranchTrueFalseTotal
if length $op_char and substr($key, 0, 1) eq $op_char 1 142981 142982
Condition100%3/3
length $op_char and substr($key, 0, 1) eq $op_char
!ll&&!rl&&r
14298011
MC/DC100%2/2
length $op_char and substr($key, 0, 1) eq $op_char
length $op_char substr($key, 0, 1) eq $op_char
Truth table100%3/3
A and B
A= length $op_charB= substr($key, 0, 1) eq $op_char
ABresult
0X0
100
111
244 if length($op_char) && substr( $key, 0, 1 ) eq $op_char;
245  
246 142982 ▶ if ( $invalid && $key =~ $invalid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invalid and $key =~ /$invalid/) 2 142980 142982
Condition66%2/3
$invalid and $key =~ /$invalid/
!ll&&!rl&&r
14298002
MC/DC50%1/2
$invalid and $key =~ /$invalid/
$invalid $key =~ /$invalid/
Truth table66%2/3
A and B
A= $invalidB= $key =~ /$invalid/
ABresult
0X0
100
111
247 croak(
248 sprintf(
249 "key '%s' has invalid character(s) '%s'",
250 $key, $opt->{invalid_chars}
251 )
252 2 );
253 }
254  
255 142980 my $utf8_key = $key;
256 142980 utf8::encode($utf8_key);
257 142980 my $type = ref $value;
258  
259 # If the type is a subtype of BSON::*, use that instead
260 142980 ▶ if ( blessed $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 89112 53868 142980
261 89112 ▶ if ($type !~ /\ABSON::\w+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /\ABSON::\w+\z/) 39 89073 89112
262 39 ▶ my $parent = first { /\ABSON::\w+\z/ } reverse @{mro::get_linear_isa($type)};
Subroutine __ANON__: called CC=1 SCAR=0.0
263 39 ▶ $type = $parent if defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parent 0 39 39
264 }
265 }
266  
267 # Null
268 142980 ▶ if ( !defined $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 8806 134174 142980
elsif (length $type) { } 107373 26801 134174
269 8806 $bson .= pack( BSON_TYPE_NAME, 0x0A, $utf8_key );
270 }
271  
272 # REFERENCES/OBJECTS
273 elsif ( length $type ) {
274  
275 # Array
276 107373 ▶ if ( $type eq 'ARRAY' || $type eq 'BSON::Array' ) {
Branch: 25 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY' or $type eq 'BSON::Array') { } 8576 98797 107373
elsif ($type eq 'BSON::DBPointer') { } 4 98793 98797
elsif ($type eq 'HASH' or $type eq 'BSON::Doc' or $type eq 'BSON::Raw' or $type eq 'MongoDB::BSON::_EncodedDoc' or $type eq 'Tie::IxHash' or $type eq 'MongoDB::BSON::Raw' or $type eq 'BSON::DBRef' or $type eq 'MongoDB::DBRef') { } 9809 88984 98793
elsif ($type eq 'Regexp') { } 4 88980 88984
elsif ($type eq 'BSON::Regex' or $type eq 'MongoDB::BSON::Regexp') { } 8619 80361 88980
elsif ($type eq 'BSON::OID' or $type eq 'BSON::ObjectId') { } 8879 71482 80361
elsif ($type eq 'MongoDB::OID') { } 0 71482 71482
elsif ($type eq 'BSON::Time') { } 8709 62773 71482
elsif ($type eq 'Time::Moment') { } 0 62773 62773
elsif ($type eq 'DateTime') { } 0 62773 62773
elsif ($type eq 'DateTime::Tiny') { } 0 62773 62773
elsif ($type eq 'Mango::BSON::Time') { } 0 62773 62773
elsif ($type eq 'BSON::Timestamp') { } 8677 54096 62773
elsif ($type eq 'MongoDB::Timestamp') { } 0 54096 54096
elsif ($type eq 'BSON::MinKey' or $type eq 'MongoDB::MinKey') { } 8807 45289 54096
elsif ($type eq 'BSON::MaxKey' or $type eq 'MongoDB::MaxKey') { } 8744 36545 45289
elsif ($type eq 'SCALAR' or $type eq 'BSON::Bytes' or $type eq 'BSON::Binary' or $type eq 'MongoDB::BSON::Binary') { } 8702 27843 36545
elsif ($type eq 'BSON::Code' or $type eq 'MongoDB::Code') { } 8857 18986 27843
elsif ($type eq 'boolean' or $type =~ /$bools_re/) { } 8621 10365 18986
elsif ($type eq 'BSON::String' or $type eq 'BSON::Symbol') { } 8759 1606 10365
elsif ($type eq 'MongoDB::BSON::String') { } 2 1604 1606
elsif ($type eq 'BSON::Int64' or $type eq 'Math::BigInt' or $type eq 'Math::Int64') { } 24 1580 1604
elsif ($type eq 'BSON::Int32') { } 35 1545 1580
elsif ($type eq 'BSON::Double') { } 32 1513 1545
elsif ($type eq 'BSON::Decimal128') { } 1512 1 1513
Condition100%3/3
$type eq "ARRAY" or $type eq "BSON::Array"
l!l&&r!l&&!r
8575198797
MC/DC100%2/2
$type eq "ARRAY" or $type eq "BSON::Array"
$type eq "ARRAY" $type eq "BSON::Array"
Truth table100%3/3
A or B
A= $type eq "ARRAY"B= $type eq "BSON::Array"
ABresult
1X1
011
000
277 8576 my $i = 0;
278 8576 tie( my %h, 'Tie::IxHash' );
279 8576 %h = map { $i++ => $_ } @$value;
280 8576 $bson .= pack( BSON_TYPE_NAME, 0x04, $utf8_key ) . _encode_bson( \%h, $opt );
281 }
282  
283 # special-cased deprecated DBPointer
284 elsif ($type eq 'BSON::DBPointer') {
285 4 my %data;
286 4 tie %data, 'Tie::IxHash';
287 4 $data{'$ref'} = $value->{'ref'};
288 4 $data{'$id'} = $value->{id};
289 4 $bson .= pack( BSON_TYPE_NAME, 0x03, $utf8_key )
290 . _encode_bson(\%data, $opt);
291 }
292  
293 # Document
294 ▶ elsif ($type eq 'HASH'
Condition90%19/21
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw" or $type eq "MongoDB::BSON::_EncodedDoc" or $type eq "Tie::IxHash" or $type eq "MongoDB::BSON::Raw" or $type eq "BSON::DBRef" or $type eq "MongoDB::DBRef"
l!l&&r!l&&!r
9809088984
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw" or $type eq "MongoDB::BSON::_EncodedDoc" or $type eq "Tie::IxHash" or $type eq "MongoDB::BSON::Raw" or $type eq "BSON::DBRef"
l!l&&r!l&&!r
97941588984
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw" or $type eq "MongoDB::BSON::_EncodedDoc" or $type eq "Tie::IxHash" or $type eq "MongoDB::BSON::Raw"
l!l&&r!l&&!r
9792288999
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw" or $type eq "MongoDB::BSON::_EncodedDoc" or $type eq "Tie::IxHash"
l!l&&r!l&&!r
9789389001
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw" or $type eq "MongoDB::BSON::_EncodedDoc"
l!l&&r!l&&!r
9789089004
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw"
l!l&&r!l&&!r
968710289004
$type eq "HASH" or $type eq "BSON::Doc"
l!l&&r!l&&!r
9684389106
MC/DC75%6/8
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw" or $type eq "MongoDB::BSON::_EncodedDoc" or $type eq "Tie::IxHash" or $type eq "MongoDB::BSON::Raw" or $type eq "BSON::DBRef" or $type eq "MongoDB::DBRef"
$type eq "HASH" $type eq "BSON::Doc" $type eq "BSON::Raw" $type eq "MongoDB::BSON::_EncodedDoc" $type eq "Tie::IxHash" $type eq "MongoDB::BSON::Raw" $type eq "BSON::DBRef" $type eq "MongoDB::DBRef"
Truth table77%7/9
A or B or C or D or E or F or G or H
A= $type eq "HASH"B= $type eq "BSON::Doc"C= $type eq "BSON::Raw"D= $type eq "MongoDB::BSON::_EncodedDoc"E= $type eq "Tie::IxHash"F= $type eq "MongoDB::BSON::Raw"G= $type eq "BSON::DBRef"H= $type eq "MongoDB::DBRef"
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
295 || $type eq 'BSON::Doc'
296 || $type eq 'BSON::Raw'
297 || $type eq 'MongoDB::BSON::_EncodedDoc'
298 || $type eq 'Tie::IxHash'
299 || $type eq 'MongoDB::BSON::Raw'
300 || $type eq 'BSON::DBRef'
301 || $type eq 'MongoDB::DBRef')
302 {
303 9809 $bson .= pack( BSON_TYPE_NAME, 0x03, $utf8_key ) . _encode_bson($value, $opt);
304 }
305  
306 # Regex
307 elsif ( $type eq 'Regexp' ) {
308 4 my ( $re, $flags ) = _split_re($value);
309 4 $bson .= pack( BSON_TYPE_NAME.BSON_REGEX, 0x0B, $utf8_key, $re, join( "", sort grep /^(i|m|x|l|s|u)$/, split( //, $flags ) ));
310 }
311 ▶ elsif ( $type eq 'BSON::Regex' || $type eq 'MongoDB::BSON::Regexp' ) {
Condition66%2/3
$type eq "BSON::Regex" or $type eq "MongoDB::BSON::Regexp"
l!l&&r!l&&!r
8619080361
MC/DC50%1/2
$type eq "BSON::Regex" or $type eq "MongoDB::BSON::Regexp"
$type eq "BSON::Regex" $type eq "MongoDB::BSON::Regexp"
Truth table66%2/3
A or B
A= $type eq "BSON::Regex"B= $type eq "MongoDB::BSON::Regexp"
ABresult
1X1
011
000
312 8619 my ( $re, $flags ) = @{$value}{qw/pattern flags/};
313 8619 $bson .= pack( BSON_TYPE_NAME.BSON_REGEX, 0x0B, $utf8_key, $re, $flags) ;
314 }
315  
316 # ObjectId
317 ▶ elsif ( $type eq 'BSON::OID' || $type eq 'BSON::ObjectId' ) {
Condition100%3/3
$type eq "BSON::OID" or $type eq "BSON::ObjectId"
l!l&&r!l&&!r
26885371482
MC/DC100%2/2
$type eq "BSON::OID" or $type eq "BSON::ObjectId"
$type eq "BSON::OID" $type eq "BSON::ObjectId"
Truth table100%3/3
A or B
A= $type eq "BSON::OID"B= $type eq "BSON::ObjectId"
ABresult
1X1
011
000
318 8879 $bson .= pack( BSON_TYPE_NAME.BSON_OBJECTID, 0x07, $utf8_key, $value->oid );
319 }
320 elsif ( $type eq 'MongoDB::OID' ) {
321 0 $bson .= pack( BSON_TYPE_NAME."H*", 0x07, $utf8_key, $value->value );
322 }
323  
324 # Datetime
325 elsif ( $type eq 'BSON::Time' ) {
326 8709 $bson .= pack( BSON_TYPE_NAME, 0x09, $utf8_key ) . _pack_int64( $value->value );
327 }
328 elsif ( $type eq 'Time::Moment' ) {
329 0 $bson .= pack( BSON_TYPE_NAME, 0x09, $utf8_key ) . _pack_int64( int( $value->epoch * 1000 + $value->millisecond ) );
330 }
331 elsif ( $type eq 'DateTime' ) {
332 0 ▶ if ( $value->time_zone->name eq 'floating' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->time_zone->name eq "floating") 0 0 0
333 0 warn("saving floating timezone as UTC");
334 }
335 0 $bson .= pack( BSON_TYPE_NAME, 0x09, $utf8_key ) . _pack_int64( int( $value->hires_epoch * 1000 ) );
336 }
337 elsif ( $type eq 'DateTime::Tiny' ) {
338 0 require Time::Local;
339 0 my $epoch = Time::Local::timegm(
340 $value->second, $value->minute, $value->hour,
341 $value->day, $value->month - 1, $value->year,
342 );
343 0 $bson .= pack( BSON_TYPE_NAME, 0x09, $utf8_key ) . _pack_int64( $epoch * 1000 );
344 }
345 elsif ( $type eq 'Mango::BSON::Time' ) {
346 0 $bson .= pack( BSON_TYPE_NAME, 0x09, $utf8_key ) . _pack_int64( $value->{time} );
347 }
348  
349 # Timestamp
350 elsif ( $type eq 'BSON::Timestamp' ) {
351 8677 $bson .= pack( BSON_TYPE_NAME.BSON_TIMESTAMP, 0x11, $utf8_key, $value->increment, $value->seconds );
352 }
353 elsif ( $type eq 'MongoDB::Timestamp' ){
354 0 $bson .= pack( BSON_TYPE_NAME.BSON_TIMESTAMP, 0x11, $utf8_key, $value->inc, $value->sec );
355 }
356  
357 # MinKey
358 ▶ elsif ( $type eq 'BSON::MinKey' || $type eq 'MongoDB::MinKey' ) {
Condition100%3/3
$type eq "BSON::MinKey" or $type eq "MongoDB::MinKey"
l!l&&r!l&&!r
8806145289
MC/DC100%2/2
$type eq "BSON::MinKey" or $type eq "MongoDB::MinKey"
$type eq "BSON::MinKey" $type eq "MongoDB::MinKey"
Truth table100%3/3
A or B
A= $type eq "BSON::MinKey"B= $type eq "MongoDB::MinKey"
ABresult
1X1
011
000
359 8807 $bson .= pack( BSON_TYPE_NAME, 0xFF, $utf8_key );
360 }
361  
362 # MaxKey
363 ▶ elsif ( $type eq 'BSON::MaxKey' || $type eq 'MongoDB::MaxKey' ) {
Condition100%3/3
$type eq "BSON::MaxKey" or $type eq "MongoDB::MaxKey"
l!l&&r!l&&!r
8743136545
MC/DC100%2/2
$type eq "BSON::MaxKey" or $type eq "MongoDB::MaxKey"
$type eq "BSON::MaxKey" $type eq "MongoDB::MaxKey"
Truth table100%3/3
A or B
A= $type eq "BSON::MaxKey"B= $type eq "MongoDB::MaxKey"
ABresult
1X1
011
000
364 8744 $bson .= pack( BSON_TYPE_NAME, 0x7F, $utf8_key );
365 }
366  
367 # Binary (XXX need to add string ref support)
368 ▶ elsif ($type eq 'SCALAR'
Condition88%8/9
$type eq "SCALAR" or $type eq "BSON::Bytes" or $type eq "BSON::Binary" or $type eq "MongoDB::BSON::Binary"
l!l&&r!l&&!r
8702027843
$type eq "SCALAR" or $type eq "BSON::Bytes" or $type eq "BSON::Binary"
l!l&&r!l&&!r
8698427843
$type eq "SCALAR" or $type eq "BSON::Bytes"
l!l&&r!l&&!r
2869627847
MC/DC75%3/4
$type eq "SCALAR" or $type eq "BSON::Bytes" or $type eq "BSON::Binary" or $type eq "MongoDB::BSON::Binary"
$type eq "SCALAR" $type eq "BSON::Bytes" $type eq "BSON::Binary" $type eq "MongoDB::BSON::Binary"
Truth table80%4/5
A or B or C or D
A= $type eq "SCALAR"B= $type eq "BSON::Bytes"C= $type eq "BSON::Binary"D= $type eq "MongoDB::BSON::Binary"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
369 || $type eq 'BSON::Bytes'
370 || $type eq 'BSON::Binary'
371 || $type eq 'MongoDB::BSON::Binary' )
372 {
373 my $data =
374 $type eq 'SCALAR' ? $$value
375 : $type eq 'BSON::Bytes' ? $value->data
376 : $type eq 'MongoDB::BSON::Binary' ? $value->data
377 8702 ▶ : pack( "C*", @{ $value->data } );
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'SCALAR' ? : 2 8700 8702
$type eq 'BSON::Bytes' ? : 8696 4 8700
$type eq 'MongoDB::BSON::Binary' ? : 0 4 4
378 8702 ▶ my $subtype = $type eq 'SCALAR' ? 0 : $value->subtype;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'SCALAR' ? : 2 8700 8702
379 8702 my $len = length($data);
380 8702 ▶ if ( $subtype == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtype == 2) { } 2 8700 8702
381 2 $bson .=
382 pack( BSON_TYPE_NAME . BSON_INT32 . BSON_BINARY_TYPE . BSON_INT32 . BSON_REMAINING,
383 0x05, $utf8_key, $len + 4, $subtype, $len, $data );
384 }
385 else {
386 8700 $bson .= pack( BSON_TYPE_NAME . BSON_INT32 . BSON_BINARY_TYPE . BSON_REMAINING,
387 0x05, $utf8_key, $len, $subtype, $data );
388 }
389 }
390  
391 # Code
392 ▶ elsif ( $type eq 'BSON::Code' || $type eq 'MongoDB::Code' ) {
Condition66%2/3
$type eq "BSON::Code" or $type eq "MongoDB::Code"
l!l&&r!l&&!r
8857018986
MC/DC50%1/2
$type eq "BSON::Code" or $type eq "MongoDB::Code"
$type eq "BSON::Code" $type eq "MongoDB::Code"
Truth table66%2/3
A or B
A= $type eq "BSON::Code"B= $type eq "MongoDB::Code"
ABresult
1X1
011
000
393 8857 my $code = $value->code;
394 8857 utf8::encode($code);
395 8857 $code = pack(BSON_STRING,$code);
396 8857 ▶ if ( ref( $value->scope ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value->scope eq 'HASH') { } 8584 273 8857
397 8584 my $scope = _encode_bson( $value->scope, $opt );
398 8584 $bson .=
399 pack( BSON_TYPE_NAME.BSON_CODE_W_SCOPE, 0x0F, $utf8_key, (4 + length($scope) + length($code)) ) . $code . $scope;
400 }
401 else {
402 273 $bson .= pack( BSON_TYPE_NAME, 0x0D, $utf8_key) . $code;
403 }
404 }
405  
406 # Boolean
407 ▶ elsif ( $type eq 'boolean' || $type =~ $bools_re ) {
Condition100%3/3
$type eq "boolean" or $type =~ /$bools_re/
l!l&&r!l&&!r
7861410365
MC/DC100%2/2
$type eq "boolean" or $type =~ /$bools_re/
$type eq "boolean" $type =~ /$bools_re/
Truth table100%3/3
A or B
A= $type eq "boolean"B= $type =~ /$bools_re/
ABresult
1X1
011
000
408 8621 ▶ $bson .= pack( BSON_TYPE_NAME.BSON_BOOLEAN, 0x08, $utf8_key, ( $value ? 1 : 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 4340 4281 8621
409 }
410  
411 # String (explicit)
412 ▶ elsif ( $type eq 'BSON::String' || $type eq 'BSON::Symbol') {
Condition100%3/3
$type eq "BSON::String" or $type eq "BSON::Symbol"
l!l&&r!l&&!r
875361606
MC/DC100%2/2
$type eq "BSON::String" or $type eq "BSON::Symbol"
$type eq "BSON::String" $type eq "BSON::Symbol"
Truth table100%3/3
A or B
A= $type eq "BSON::String"B= $type eq "BSON::Symbol"
ABresult
1X1
011
000
413 8759 $value = $value->value;
414 8759 utf8::encode($value);
415 8759 $bson .= pack( BSON_TYPE_NAME.BSON_STRING, 0x02, $utf8_key, $value );
416 }
417 elsif ( $type eq 'MongoDB::BSON::String' ) {
418 2 $value = $$value;
419 2 utf8::encode($value);
420 2 $bson .= pack( BSON_TYPE_NAME.BSON_STRING, 0x02, $utf8_key, $value );
421 }
422  
423 # Int64 (XXX and eventually BigInt)
424 ▶ elsif ( $type eq 'BSON::Int64' || $type eq 'Math::BigInt' || $type eq 'Math::Int64' )
Condition83%5/6
$type eq "BSON::Int64" or $type eq "Math::BigInt" or $type eq "Math::Int64"
l!l&&r!l&&!r
2401580
$type eq "BSON::Int64" or $type eq "Math::BigInt"
l!l&&r!l&&!r
1681580
MC/DC66%2/3
$type eq "BSON::Int64" or $type eq "Math::BigInt" or $type eq "Math::Int64"
$type eq "BSON::Int64" $type eq "Math::BigInt" $type eq "Math::Int64"
Truth table75%3/4
A or B or C
A= $type eq "BSON::Int64"B= $type eq "Math::BigInt"C= $type eq "Math::Int64"
ABCresult
1XX1
01X1
0011
0000
425 {
426 24 ▶ if ( $value > $max_int64 || $value < $min_int64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $max_int64 or $value < $min_int64) 0 24 24
Condition33%1/3
$value > $max_int64 or $value < $min_int64
l!l&&r!l&&!r
0024
MC/DC0%0/2
$value > $max_int64 or $value < $min_int64
$value > $max_int64 $value < $min_int64
Truth table33%1/3
A or B
A= $value > $max_int64B= $value < $min_int64
ABresult
1X1
011
000
427 0 croak("BSON can only handle 8-byte integers. Key '$key' is '$value'");
428 }
429  
430 # unwrap BSON::Int64; it could be Math::BigInt, etc.
431 24 ▶ if ( $type eq 'BSON::Int64' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "BSON::Int64") 16 8 24
432 16 $value = $value->value;
433 }
434  
435 24 $bson .= pack( BSON_TYPE_NAME, 0x12, $utf8_key ) . _pack_int64($value);
436 }
437  
438 elsif ( $type eq 'BSON::Int32' ) {
439 35 $bson .= pack( BSON_TYPE_NAME . BSON_INT32, 0x10, $utf8_key, $value->value );
440 }
441  
442 # Double (explicit)
443 elsif ( $type eq 'BSON::Double' ) {
444 32 $bson .= pack( BSON_TYPE_NAME.BSON_DOUBLE, 0x01, $utf8_key, $value/1.0 );
445 }
446  
447 # Decimal128
448 elsif ( $type eq 'BSON::Decimal128' ) {
449 1512 $bson .= pack( BSON_TYPE_NAME.BSON_16BYTES, 0x13, $utf8_key, $value->bytes );
450 }
451  
452 # Unsupported type
453 else {
454 1 croak("For key '$key', can't encode value of type '$type'");
455 }
456 }
457  
458 # SCALAR
459 else {
460 # If a numeric value exists based on internal flags, use it;
461 # otherwise, if prefer_numeric is true and it looks like a
462 # number, then coerce to a number of the right type;
463 # otherwise, leave it as a string
464  
465 26801 my $flags = B::svref_2object(\$value)->FLAGS;
466  
467 26801 ▶ if ( $flags & B::SVf_NOK() ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($flags & 512) { } 8874 17927 26801
elsif ($flags & 256) { } 17803 124 17927
elsif ($opt->{'prefer_numeric'} and &looks_like_number($value)) { } 3 121 124
468 8874 $bson .= pack( BSON_TYPE_NAME.BSON_DOUBLE, 0x01, $utf8_key, $value );
469 }
470 elsif ( $flags & B::SVf_IOK() ) {
471 # Force numeric; fixes dual-vars comparison bug on old Win32s
472 17803 $value = 0+$value;
473 17803 ▶ if ( $value > $max_int64 || $value < $min_int64 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value > $max_int64 or $value < $min_int64) { } 0 17803 17803
elsif ($value > $max_int32 or $value < $min_int32) { } 8945 8858 17803
Condition33%1/3
$value > $max_int64 or $value < $min_int64
l!l&&r!l&&!r
0017803
MC/DC0%0/2
$value > $max_int64 or $value < $min_int64
$value > $max_int64 $value < $min_int64
Truth table33%1/3
A or B
A= $value > $max_int64B= $value < $min_int64
ABresult
1X1
011
000
474 0 croak("BSON can only handle 8-byte integers. Key '$key' is '$value'");
475 }
476 ▶ elsif ( $value > $max_int32 || $value < $min_int32 ) {
Condition100%3/3
$value > $max_int32 or $value < $min_int32
l!l&&r!l&&!r
441545308858
MC/DC100%2/2
$value > $max_int32 or $value < $min_int32
$value > $max_int32 $value < $min_int32
Truth table100%3/3
A or B
A= $value > $max_int32B= $value < $min_int32
ABresult
1X1
011
000
477 8945 $bson .= pack( BSON_TYPE_NAME, 0x12, $utf8_key ) . _pack_int64($value);
478 }
479 else {
480 8858 $bson .= pack( BSON_TYPE_NAME . BSON_INT32, 0x10, $utf8_key, $value );
481 }
482 }
483 ▶ elsif ( $opt->{prefer_numeric} && looks_like_number($value) ) {
Condition100%3/3
$opt->{"prefer_numeric"} and &looks_like_number($value)
!ll&&!rl&&r
52693
MC/DC100%2/2
$opt->{"prefer_numeric"} and &looks_like_number($value)
$opt->{"prefer_numeric"} &looks_like_number($value)
Truth table100%3/3
A and B
A= $opt->{"prefer_numeric"}B= &looks_like_number($value)
ABresult
0X0
100
111
484 # Looks like int: type heuristic based on size
485 3 ▶ if ( $value =~ $int_re ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /$int_re/) { } 2 1 3
elsif ($value =~ /$doub_re/) { } 1 0 1
486 2 ▶ if ( $value > $max_int64 || $value < $min_int64 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value > $max_int64 or $value < $min_int64) { } 0 2 2
elsif ($value > $max_int32 or $value < $min_int32) { } 0 2 2
Condition33%1/3
$value > $max_int64 or $value < $min_int64
l!l&&r!l&&!r
002
MC/DC0%0/2
$value > $max_int64 or $value < $min_int64
$value > $max_int64 $value < $min_int64
Truth table33%1/3
A or B
A= $value > $max_int64B= $value < $min_int64
ABresult
1X1
011
000
487 0 croak("BSON can only handle 8-byte integers. Key '$key' is '$value'");
488 }
489 ▶ elsif ( $value > $max_int32 || $value < $min_int32 ) {
Condition33%1/3
$value > $max_int32 or $value < $min_int32
l!l&&r!l&&!r
002
MC/DC0%0/2
$value > $max_int32 or $value < $min_int32
$value > $max_int32 $value < $min_int32
Truth table33%1/3
A or B
A= $value > $max_int32B= $value < $min_int32
ABresult
1X1
011
000
490 0 $bson .= pack( BSON_TYPE_NAME, 0x12, $utf8_key ) . _pack_int64($value);
491 }
492 else {
493 2 $bson .= pack( BSON_TYPE_NAME . BSON_INT32, 0x10, $utf8_key, $value );
494 }
495 }
496  
497 # Looks like double
498 elsif ( $value =~ $doub_re ) {
499 1 $bson .= pack( BSON_TYPE_NAME.BSON_DOUBLE, 0x01, $utf8_key, $value );
500 }
501  
502 # looks_like_number true, but doesn't match int/double
503 # regexes, so as a last resort we leave as string
504 else {
505 0 utf8::encode($value);
506 0 $bson .= pack( BSON_TYPE_NAME.BSON_STRING, 0x02, $utf8_key, $value );
507 }
508 }
509 else {
510 # Not coercing or didn't look like a number
511 121 utf8::encode($value);
512 121 $bson .= pack( BSON_TYPE_NAME.BSON_STRING, 0x02, $utf8_key, $value );
513 }
514 }
515 }
516  
517 29132 delete $opt->{_circular}{$refaddr};
518 29132 $opt->{_depth}--;
519  
520 29132 return pack( BSON_INT32, length($bson) + 5 ) . $bson . "\0";
521 }
522  
523 71 my %FIELD_SIZES = (
524 0x01 => 8,
525 0x02 => 5,
526 0x03 => 5,
527 0x04 => 5,
528 0x05 => 5,
529 0x06 => 0,
530 0x07 => 12,
531 0x08 => 1,
532 0x09 => 8,
533 0x0A => 0,
534 0x0B => 2,
535 0x0C => 17,
536 0x0D => 5,
537 0x0E => 5,
538 0x0F => 11,
539 0x10 => 4,
540 0x11 => 8,
541 0x12 => 8,
542 0x13 => 16,
543 0x7F => 0,
544 0xFF => 0,
545 );
546  
547 71 my $ERR_UNSUPPORTED = "unsupported BSON type \\x%X for key '%s'. Are you using the latest version of BSON.pm?";
548 71 my $ERR_TRUNCATED = "premature end of BSON field '%s' (type 0x%x)";
549 71 my $ERR_LENGTH = "BSON field '%s' (type 0x%x) has invalid length: wanted %d, got %d";
550 71 my $ERR_MISSING_NULL = "BSON field '%s' (type 0x%x) missing null terminator";
551 71 my $ERR_BAD_UTF8 = "BSON field '%s' (type 0x%x) contains invalid UTF-8";
552 71 my $ERR_NEG_LENGTH = "BSON field '%s' (type 0x%x) contains negative length";
553 71 my $ERR_BAD_OLDBINARY = "BSON field '%s' (type 0x%x, subtype 0x02) is invalid";
554  
555 0 ▶sub __dump_bson {
Subroutine __dump_bson: not called CC=1 SCAR=6.9
556 0 my $bson = unpack("H*", shift);
557 0 my @pairs = $bson=~ m/(..)/g;
558 0 return join(" ", @pairs);
559 }
560  
561 28325 ▶sub _decode_bson {
Subroutine _decode_bson: called CC=72 SCAR=43.1
562 28325 my ($bson, $opt) = @_;
563 28325 ▶ if ( !defined $bson ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bson) 0 28325 28325
564 0 croak("Decode argument must not be undef");
565 }
566 28325 ▶ $opt->{_depth} = 0 unless defined $opt->{_depth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"_depth"} 2149 26176 28325
567 28325 $opt->{_depth}++;
568 28325 ▶ if ($opt->{_depth} > BSON_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"_depth"} > 100) 1 28324 28325
569 1 croak "Exceeded max object depth of ". BSON_MAX_DEPTH;
570 }
571 28324 my $blen= length($bson);
572 28324 my $len = unpack( BSON_INT32, $bson );
573 28324 ▶ if ( length($bson) != $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bson != $len) 15 28309 28324
574 15 croak("Incorrect length of the bson string (got $blen, wanted $len)");
575 }
576 28309 ▶ if ( chop($bson) ne "\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (chop $bson ne "\0") 5 28304 28309
577 5 croak("BSON document not null terminated");
578 }
579 28304 $bson = substr $bson, 4;
580 28304 my @array = ();
581 28304 my %hash = ();
582 28304 ▶ tie( %hash, 'Tie::IxHash' ) if $opt->{ordered};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"ordered"} 1485 26819 28304
583 28304 my ($type, $key, $value);
584 28304 ▶ while ($bson) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bson) 141840 28139 169979
585 141840 ( $type, $key, $bson ) = unpack( BSON_TYPE_NAME.BSON_REMAINING, $bson );
586 141840 utf8::decode($key);
587  
588 # Check type and truncation
589 141840 my $min_size = $FIELD_SIZES{$type};
590 141840 ▶ if ( !defined $min_size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min_size) 8 141832 141840
591 8 croak( sprintf( $ERR_UNSUPPORTED, $type, $key ) );
592 }
593 141832 ▶ if ( length($bson) < $min_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bson < $min_size) 12 141820 141832
594 12 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
595 }
596  
597 # Double
598 141820 ▶ if ( $type == 0x01 ) {
Branch: 19 branches
BranchTrueFalseTotal
if ($type == 1) { } 8920 132900 141820
elsif ($type == 2 or $type == 14) { } 8891 124009 132900
elsif ($type == 3 or $type == 4) { } 17591 106418 124009
elsif ($type == 5) { } 8704 97714 106418
elsif ($type == 6) { } 2 97712 97714
elsif ($type == 7) { } 8872 88840 97712
elsif ($type == 8) { } 8623 80217 88840
elsif ($type == 9) { } 8712 71505 80217
elsif ($type == 10) { } 8798 62707 71505
elsif ($type == 11) { } 8623 54084 62707
elsif ($type == 12) { } 11 54073 54084
elsif ($type == 13) { } 273 53800 54073
elsif ($type == 15) { } 8595 45205 53800
elsif ($type == 16) { } 8798 36407 45205
elsif ($type == 17) { } 8676 27731 36407
elsif ($type == 18) { } 8974 18757 27731
elsif ($type == 19) { } 1206 17551 18757
elsif ($type == 255) { } 8807 8744 17551
elsif ($type == 127) { } 8744 0 8744
599 8920 ( $value, $bson ) = unpack( BSON_DOUBLE.BSON_REMAINING, $bson );
600 8920 ▶ $value = BSON::Double->new( value => $value ) if $opt->{wrap_numbers};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_numbers"} 51 8869 8920
601 }
602  
603 # String and Symbol (deprecated); Symbol will be convert to String
604 ▶ elsif ( $type == 0x02 || $type == 0x0E ) {
Condition100%3/3
$type == 2 or $type == 14
l!l&&r!l&&!r
887912124009
MC/DC100%2/2
$type == 2 or $type == 14
$type == 2 $type == 14
Truth table100%3/3
A or B
A= $type == 2B= $type == 14
ABresult
1X1
011
000
605 8891 ( $len, $bson ) = unpack( BSON_INT32 . BSON_REMAINING, $bson );
606 8891 ▶ if ( length($bson) < $len || substr( $bson, $len - 1, 1 ) ne "\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bson < $len or substr($bson, $len - 1, 1) ne "\0") 11 8880 8891
Condition100%3/3
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
l!l&&r!l&&!r
838880
MC/DC100%2/2
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
length $bson < $len substr($bson, $len - 1, 1) ne "\0"
Truth table100%3/3
A or B
A= length $bson < $lenB= substr($bson, $len - 1, 1) ne "\0"
ABresult
1X1
011
000
607 11 croak( sprintf( $ERR_MISSING_NULL, $key, $type ) );
608 }
609 8880 ( $value, $bson ) = unpack( "a$len" . BSON_REMAINING, $bson );
610 8880 chop($value); # remove trailing \x00
611 8880 ▶ if ( !utf8::decode($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($value)) 3 8877 8880
612 3 croak( sprintf( $ERR_BAD_UTF8, $key, $type ) );
613 }
614 8877 ▶ $value = BSON::String->new( value => $value ) if $opt->{wrap_strings};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_strings"} 6 8871 8877
615 }
616  
617 # Document and Array
618 ▶ elsif ( $type == 0x03 || $type == 0x04 ) {
Condition100%3/3
$type == 3 or $type == 4
l!l&&r!l&&!r
90158576106418
MC/DC100%2/2
$type == 3 or $type == 4
$type == 3 $type == 4
Truth table100%3/3
A or B
A= $type == 3B= $type == 4
ABresult
1X1
011
000
619 17591 my $len = unpack( BSON_INT32, $bson );
620 17591 $value = _decode_bson( substr( $bson, 0, $len ), { %$opt, _decode_array => $type == 0x04} );
621 17485 ▶ if ( $opt->{wrap_dbrefs} && $type == 0x03 && exists $value->{'$id'} && exists $value->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"wrap_dbrefs"} and $type == 3 and exists $value->{"\$id"} and exists $value->{"\$ref"}) 11 17474 17485
Condition88%8/9
$opt->{"wrap_dbrefs"} and $type == 3 and exists $value->{"\$id"} and exists $value->{"\$ref"}
!ll&&!rl&&r
17474011
$opt->{"wrap_dbrefs"} and $type == 3 and exists $value->{"\$id"}
!ll&&!rl&&r
8574890011
$opt->{"wrap_dbrefs"} and $type == 3
!ll&&!rl&&r
185738911
MC/DC75%3/4
$opt->{"wrap_dbrefs"} and $type == 3 and exists $value->{"\$id"} and exists $value->{"\$ref"}
$opt->{"wrap_dbrefs"} $type == 3 exists $value->{"\$id"} exists $value->{"\$ref"}
Truth table80%4/5
A and B and C and D
A= $opt->{"wrap_dbrefs"}B= $type == 3C= exists $value->{"\$id"}D= exists $value->{"\$ref"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
622 11 $value = BSON::DBRef->new( %$value );
623 }
624 17485 $bson = substr( $bson, $len, length($bson) - $len );
625 }
626  
627 # Binary
628 elsif ( $type == 0x05 ) {
629 8704 my ( $len, $btype ) = unpack( BSON_INT32 . BSON_BINARY_TYPE, $bson );
630 8704 substr( $bson, 0, 5, '' );
631  
632 8704 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 1 8703 8704
633 1 croak( sprintf( $ERR_NEG_LENGTH, $key, $type ) );
634 }
635 8703 ▶ if ( $len > length($bson) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $bson) 1 8702 8703
636 1 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
637 }
638  
639 8702 my $binary = substr( $bson, 0, $len, '' );
640  
641 8702 ▶ if ( $btype == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($btype == 2) 5 8697 8702
642 5 ▶ if ( $len < 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 4) 0 5 5
643 0 croak( sprintf( $ERR_BAD_OLDBINARY, $key, $type ) );
644 }
645  
646 5 my $sublen = unpack( BSON_INT32, $binary );
647 5 ▶ if ( $sublen != length($binary) - 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sublen != length($binary) - 4) 3 2 5
648 3 croak( sprintf( $ERR_BAD_OLDBINARY, $key, $type ) );
649 }
650  
651 2 substr( $binary, 0, 4, '' );
652 }
653  
654 8699 $value = BSON::Bytes->new( subtype => $btype, data => $binary );
655 }
656  
657 # Undef (deprecated)
658 elsif ( $type == 0x06 ) {
659 2 $value = undef;
660 }
661  
662 # ObjectId
663 elsif ( $type == 0x07 ) {
664 8872 ( my $oid, $bson ) = unpack( BSON_OBJECTID.BSON_REMAINING, $bson );
665 8872 $value = BSON::OID->new(oid => $oid);
666 }
667  
668 # Boolean
669 elsif ( $type == 0x08 ) {
670 8623 ( my $bool, $bson ) = unpack( BSON_BOOLEAN.BSON_REMAINING, $bson );
671 8623 ▶ croak("BSON boolean must be 0 or 1. Key '$key' is $bool")
Branch: 1 branch
BranchTrueFalseTotal
unless $bool == 0 or $bool == 1 2 8621 8623
Condition100%3/3
$bool == 0 or $bool == 1
l!l&&r!l&&!r
428143402
MC/DC100%2/2
$bool == 0 or $bool == 1
$bool == 0 $bool == 1
Truth table100%3/3
A or B
A= $bool == 0B= $bool == 1
ABresult
1X1
011
000
672 unless $bool == 0 || $bool == 1;
673 8621 $value = boolean( $bool );
674 }
675  
676 # Datetime
677 elsif ( $type == 0x09 ) {
678 8712 if ( HAS_INT64 ) {
679 8712 ($value, $bson) = unpack(BSON_INT64.BSON_REMAINING,$bson);
680 }
681 else {
682 ($value, $bson) = unpack(BSON_8BYTES.BSON_REMAINING,$bson);
683 $value = _int64_to_bigint($value);
684 }
685 8712 $value = BSON::Time->new( value => $value );
686 8712 my $dt_type = $opt->{dt_type};
687 8712 ▶ if ( defined $dt_type && $dt_type ne 'BSON::Time' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dt_type and $dt_type ne "BSON::Time") 1 8711 8712
Condition100%3/3
defined $dt_type and $dt_type ne "BSON::Time"
!ll&&!rl&&r
871011
MC/DC100%2/2
defined $dt_type and $dt_type ne "BSON::Time"
defined $dt_type $dt_type ne "BSON::Time"
Truth table100%3/3
A and B
A= defined $dt_typeB= $dt_type ne "BSON::Time"
ABresult
0X0
100
111
688 1 ▶ $value =
Branch: 4 branches
BranchTrueFalseTotal
$dt_type eq 'Time::Moment' ? : 0 1 1
$dt_type eq 'DateTime' ? : 0 1 1
$dt_type eq 'DateTime::Tiny' ? : 0 1 1
$dt_type eq 'Mango::BSON::Time' ? : 0 1 1
689 $dt_type eq 'Time::Moment' ? $value->as_time_moment
690 : $dt_type eq 'DateTime' ? $value->as_datetime
691 : $dt_type eq 'DateTime::Tiny' ? $value->as_datetime_tiny
692 : $dt_type eq 'Mango::BSON::Time' ? $value->as_mango_time
693 : croak("Unsupported dt_type '$dt_type'");
694 }
695 }
696  
697 # Null
698 elsif ( $type == 0x0A ) {
699 8798 $value = undef;
700 }
701  
702 # Regex
703 elsif ( $type == 0x0B ) {
704 8623 ( my $re, my $op, $bson ) = unpack( BSON_CSTRING.BSON_CSTRING.BSON_REMAINING, $bson );
705 8623 $value = BSON::Regex->new( pattern => $re, flags => $op );
706 }
707  
708 # DBPointer (deprecated)
709 elsif ( $type == 0x0C ) {
710 11 ( $len, $bson ) = unpack( BSON_INT32 . BSON_REMAINING, $bson );
711 11 ▶ if ( length($bson) < $len || substr( $bson, $len - 1, 1 ) ne "\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bson < $len or substr($bson, $len - 1, 1) ne "\0") 3 8 11
Condition66%2/3
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
l!l&&r!l&&!r
038
MC/DC50%1/2
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
length $bson < $len substr($bson, $len - 1, 1) ne "\0"
Truth table66%2/3
A or B
A= length $bson < $lenB= substr($bson, $len - 1, 1) ne "\0"
ABresult
1X1
011
000
712 3 croak( sprintf( $ERR_MISSING_NULL, $key, $type ) );
713 }
714 8 ( my ($ref), $bson ) = unpack( "a$len" . BSON_REMAINING, $bson );
715 8 chop($ref); # remove trailing \x00
716 8 ▶ if ( !utf8::decode($ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($ref)) 1 7 8
717 1 croak( sprintf( $ERR_BAD_UTF8, $key, $type ) );
718 }
719  
720 7 ( my ($oid), $bson ) = unpack( BSON_OBJECTID . BSON_REMAINING, $bson );
721 7 $value = BSON::DBRef->new( '$ref' => $ref, '$id' => BSON::OID->new( oid => $oid ) );
722 }
723  
724 # Code
725 elsif ( $type == 0x0D ) {
726 273 ( $len, $bson ) = unpack( BSON_INT32 . BSON_REMAINING, $bson );
727 273 ▶ if ( length($bson) < $len || substr( $bson, $len - 1, 1 ) ne "\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bson < $len or substr($bson, $len - 1, 1) ne "\0") 0 273 273
Condition33%1/3
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
l!l&&r!l&&!r
00273
MC/DC0%0/2
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
length $bson < $len substr($bson, $len - 1, 1) ne "\0"
Truth table33%1/3
A or B
A= length $bson < $lenB= substr($bson, $len - 1, 1) ne "\0"
ABresult
1X1
011
000
728 0 croak( sprintf( $ERR_MISSING_NULL, $key, $type ) );
729 }
730 273 ( $value, $bson ) = unpack( "a$len" . BSON_REMAINING, $bson );
731 273 chop($value); # remove trailing \x00
732 273 ▶ if ( !utf8::decode($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($value)) 0 273 273
733 0 croak( sprintf( $ERR_BAD_UTF8, $key, $type ) );
734 }
735 273 $value = BSON::Code->new( code => $value );
736 }
737  
738 # Code with scope
739 elsif ( $type == 0x0F ) {
740 8595 my $len = unpack( BSON_INT32, $bson );
741  
742 # validate length
743 8595 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 1 8594 8595
744 1 croak( sprintf( $ERR_NEG_LENGTH, $key, $type ) );
745 }
746 8594 ▶ if ( $len > length($bson) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $bson) 2 8592 8594
747 2 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
748 }
749 8592 ▶ if ( $len < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 5) 1 8591 8592
750 1 croak( sprintf( $ERR_LENGTH, $key, $type, 5, $len ) );
751 }
752  
753 # extract code and scope and chop off leading length
754 8591 my $codewscope = substr( $bson, 0, $len, '' );
755 8591 substr( $codewscope, 0, 4, '' );
756  
757 # extract code ( i.e. string )
758 8591 my $strlen = unpack( BSON_INT32, $codewscope );
759 8591 substr( $codewscope, 0, 4, '' );
760  
761 8591 ▶ if ( length($codewscope) < $strlen || substr( $codewscope, -1, 1 ) ne "\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $codewscope < $strlen or substr($codewscope, -1, 1) ne "\0") 1 8590 8591
Condition66%2/3
length $codewscope < $strlen or substr($codewscope, -1, 1) ne "\0"
l!l&&r!l&&!r
108590
MC/DC50%1/2
length $codewscope < $strlen or substr($codewscope, -1, 1) ne "\0"
length $codewscope < $strlen substr($codewscope, -1, 1) ne "\0"
Truth table66%2/3
A or B
A= length $codewscope < $strlenB= substr($codewscope, -1, 1) ne "\0"
ABresult
1X1
011
000
762 1 croak( sprintf( $ERR_MISSING_NULL, $key, $type ) );
763 }
764  
765 8590 my $code = substr($codewscope, 0, $strlen, '' );
766 8590 chop($code); # remove trailing \x00
767 8590 ▶ if ( !utf8::decode($code) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($code)) 0 8590 8590
768 0 croak( sprintf( $ERR_BAD_UTF8, $key, $type ) );
769 }
770  
771 8590 ▶ if ( length($codewscope) < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $codewscope < 5) 2 8588 8590
772 2 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
773 }
774  
775 # extract scope
776 8588 my $scopelen = unpack( BSON_INT32, $codewscope );
777 8588 ▶ if ( length($codewscope) < $scopelen || substr( $codewscope, $scopelen - 1, 1 ) ne "\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $codewscope < $scopelen or substr($codewscope, $scopelen - 1, 1) ne "\0") 3 8585 8588
Condition66%2/3
length $codewscope < $scopelen or substr($codewscope, $scopelen - 1, 1) ne "\0"
l!l&&r!l&&!r
308585
MC/DC50%1/2
length $codewscope < $scopelen or substr($codewscope, $scopelen - 1, 1) ne "\0"
length $codewscope < $scopelen substr($codewscope, $scopelen - 1, 1) ne "\0"
Truth table66%2/3
A or B
A= length $codewscope < $scopelenB= substr($codewscope, $scopelen - 1, 1) ne "\0"
ABresult
1X1
011
000
778 3 croak( sprintf( $ERR_MISSING_NULL, $key, $type ) );
779 }
780  
781 8585 my $scope = _decode_bson( $codewscope, { %$opt, _decode_array => 0} );
782  
783 8584 $value = BSON::Code->new( code => $code, scope => $scope );
784 }
785  
786 # Int32
787 elsif ( $type == 0x10 ) {
788 8798 ( $value, $bson ) = unpack( BSON_INT32.BSON_REMAINING, $bson );
789 8798 ▶ $value = BSON::Int32->new( value => $value ) if $opt->{wrap_numbers};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_numbers"} 38 8760 8798
790 }
791  
792 # Timestamp
793 elsif ( $type == 0x11 ) {
794 8676 ( my $sec, my $inc, $bson ) = unpack( BSON_UINT32.BSON_UINT32.BSON_REMAINING, $bson );
795 8676 $value = BSON::Timestamp->new( $inc, $sec );
796 }
797  
798 # Int64
799 elsif ( $type == 0x12 ) {
800 8974 if ( HAS_INT64 ) {
801 8974 ($value, $bson) = unpack(BSON_INT64.BSON_REMAINING,$bson);
802 }
803 else {
804 ($value, $bson) = unpack(BSON_8BYTES.BSON_REMAINING,$bson);
805 $value = _int64_to_bigint($value);
806 }
807 8974 ▶ $value = BSON::Int64->new( value => $value ) if $opt->{wrap_numbers};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_numbers"} 21 8953 8974
808 }
809  
810 # Decimal128
811 elsif ( $type == 0x13 ) {
812 1206 ( my $bytes, $bson ) = unpack( BSON_16BYTES.BSON_REMAINING, $bson );
813 1206 $value = BSON::Decimal128->new( bytes => $bytes );
814 }
815  
816 # MinKey
817 elsif ( $type == 0xFF ) {
818 8807 $value = BSON::MinKey->new;
819 }
820  
821 # MaxKey
822 elsif ( $type == 0x7F ) {
823 8744 $value = BSON::MaxKey->new;
824 }
825  
826 # ???
827 else {
828 # Should have already been caught in the minimum length check,
829 # but just in case not:
830 0 croak( sprintf( $ERR_UNSUPPORTED, $type, $key ) );
831 }
832  
833 141675 ▶ if ( $opt->{_decode_array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'_decode_array'}) { } 26904 114771 141675
834 26904 push @array, $value;
835 }
836 else {
837 114771 $hash{$key} = $value;
838 }
839 }
840 28139 $opt->{_depth}--;
841 28139 ▶ return $opt->{_decode_array} ? \@array : \%hash;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'_decode_array'} ? : 8573 19566 28139
842 }
843  
844 71 1;
845  
846 =pod
847  
848 =encoding UTF-8
849  
850 =head1 NAME
851  
852 BSON::PP - Pure Perl BSON implementation
853  
854 =head1 VERSION
855  
856 version v1.12.2
857  
858 =head1 DESCRIPTION
859  
860 This module contains the pure-Perl implementation for BSON encoding and
861 decoding. There is no public API. Use the L<BSON> module and it will
862 choose the best implementation for you.
863  
864 =head1 AUTHORS
865  
866 =over 4
867  
868 =item *
869  
870 David Golden <david@mongodb.com>
871  
872 =item *
873  
874 Stefan G. <minimalist@lavabit.com>
875  
876 =back
877  
878 =head1 COPYRIGHT AND LICENSE
879  
880 This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
881  
882 This is free software, licensed under:
883  
884 The Apache License, Version 2.0, January 2004
885  
886 =cut
887  
888 __END__
« BSON.pm ↑ Summary BSON/Decimal128.pm »