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.0';
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 use constant {
26 HAS_INT64 => $Config{use64bitint},
27 71 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
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 use constant {
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 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 sub _printable {
70 1 ▶ my $value = shift;
Subroutine _printable: called CC=1 SCAR=0.0
71 1 $value =~ s/([^[:print:]])/sprintf("\\x%02x",ord($1))/ge;
72 1 return $value;
73 }
74  
75 sub _split_re {
76 5 ▶ my $value = shift;
Subroutine _split_re: called CC=2 SCAR=10.5
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 sub _ixhash_iterator {
90 21 ▶ my $ixhash = shift;
Subroutine _ixhash_iterator: called CC=1 SCAR=0.0
91 21 my $started = 0;
92 return sub {
93 67 ▶ my $k = $started ? $ixhash->NEXTKEY : do { $started++; $ixhash->FIRSTKEY };
Subroutine __ANON__: called CC=3 SCAR=11.0
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 sub _hashlike_iterator {
102 28692 ▶ my $hashlike = shift;
Subroutine _hashlike_iterator: called CC=2 SCAR=6.9
103 28692 my @keys = keys %$hashlike;
104 @keys = sort @keys
105 28692 ▶ if $ENV{BSON_TEST_SORT_HASH};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"BSON_TEST_SORT_HASH"} 0 28692 28692
106 return sub {
107 167610 ▶ my $k = shift @keys;
Subroutine __ANON__: called CC=2 SCAR=6.9
108 167610 ▶ return unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 28483 139127 167610
109 139127 return ($k, $hashlike->{$k});
110 }
111 28692 }
112  
113 # XXX could be optimized down to only one substr to trim/pad
114 sub _bigint_to_int64 {
115 10 ▶ my $bigint = shift;
Subroutine _bigint_to_int64: called CC=6 SCAR=18.2
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 sub _int64_to_bigint {
137 0 ▶ my $bytes = reverse(shift);
Subroutine _int64_to_bigint: not called CC=4 SCAR=30.0
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 sub _pack_int64 {
153 17148 ▶ my $value = shift;
Subroutine _pack_int64: called CC=4 SCAR=16.8
154 17148 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 17148 ▶ if ( ! $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 17138 10 17148
159 17138 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 sub _reftype_check {
176 1 ▶ my $doc = shift;
Subroutine _reftype_check: called CC=2 SCAR=7.4
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 sub _encode_bson {
184 28843 ▶ my ($doc, $opt) = @_;
Subroutine _encode_bson: called CC=101 SCAR=48.6
185  
186 28843 my $refaddr = refaddr($doc);
187 28843 ▶ die "circular reference detected" if $opt->{_circular}{$refaddr}++;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"_circular"}{$refaddr}++ 4 28839 28843
188  
189 28839 ▶ $opt->{_depth} = 0 unless defined $opt->{_depth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"_depth"} 2482 26357 28839
190 28839 $opt->{_depth}++;
191 28839 ▶ if ($opt->{_depth} > BSON_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"_depth"} > 100) 2 28837 28839
192 2 croak "Exceeded max object depth of ". BSON_MAX_DEPTH;
193 }
194  
195 28837 my $doc_type = ref($doc);
196  
197 28837 ▶ 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 28734 28837
Condition66%2/3
$doc_type eq "BSON::Raw" or $doc_type eq "MongoDB::BSON::_EncodedDoc"
l!l&&r!l&&!r
103028734
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 28734 ▶ if ( $doc_type eq 'MongoDB::BSON::Raw' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc_type eq "MongoDB::BSON::Raw") 3 28731 28734
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 28731 ▶ : do { _reftype_check($doc); undef };
Branch: 5 branches
BranchTrueFalseTotal
$doc_type eq 'HASH' ? : 28692 39 28731
$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 28730 ▶ $iter //= _hashlike_iterator($doc);
Condition66%2/3
$iter //= _hashlike_iterator($doc)
l!l&&r!l&&!r
38286920
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 28730 ▶ my $op_char = defined($opt->{op_char}) ? $opt->{op_char} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'op_char'} ? : 2 28728 28730
220 my $invalid =
221 28730 ▶ length( $opt->{invalid_chars} ) ? qr/[\Q$opt->{invalid_chars}\E]/ : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $opt->{'invalid_chars'} ? : 2 28728 28730
222  
223 # Set up first key bookkeeping
224 28730 my $first_key_pending = !! defined($opt->{first_key});
225 28730 my $first_key;
226 28730 my $bson = '';
227  
228 28730 my ($key, $value);
229 28730 ▶ while ( $first_key_pending or ( $key, $value ) = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_key_pending or () = &$iter()) 139199 28520 167719
Condition100%3/3
$first_key_pending or () = &$iter()
l!l&&r!l&&!r
213919728520
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 139199 ▶ next if defined $first_key && $key eq $first_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_key and $key eq $first_key 1 139198 139199
Condition100%3/3
defined $first_key and $key eq $first_key
!ll&&!rl&&r
13919621
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 139198 ▶ if ( $first_key_pending ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_key_pending) 2 139196 139198
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 139198 ▶ last unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 139198 139198
239  
240 139198 ▶ croak "Key '" . _printable($key) . "' contains null character"
Branch: 1 branch
BranchTrueFalseTotal
unless index($key, "\0") == -1 1 139197 139198
241 unless -1 == index($key, "\0");
242  
243 139197 ▶ substr( $key, 0, 1 ) = '$'
Branch: 1 branch
BranchTrueFalseTotal
if length $op_char and substr($key, 0, 1) eq $op_char 1 139196 139197
Condition100%3/3
length $op_char and substr($key, 0, 1) eq $op_char
!ll&&!rl&&r
13919511
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 139197 ▶ if ( $invalid && $key =~ $invalid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invalid and $key =~ /$invalid/) 2 139195 139197
Condition66%2/3
$invalid and $key =~ /$invalid/
!ll&&!rl&&r
13919502
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 139195 my $utf8_key = $key;
256 139195 utf8::encode($utf8_key);
257 139195 my $type = ref $value;
258  
259 # If the type is a subtype of BSON::*, use that instead
260 139195 ▶ if ( blessed $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 86749 52446 139195
261 86749 ▶ if ($type !~ /\ABSON::\w+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /\ABSON::\w+\z/) 39 86710 86749
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 139195 ▶ if ( !defined $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 8551 130644 139195
elsif (length $type) { } 104742 25902 130644
269 8551 $bson .= pack( BSON_TYPE_NAME, 0x0A, $utf8_key );
270 }
271  
272 # REFERENCES/OBJECTS
273 elsif ( length $type ) {
274  
275 # Array
276 104742 ▶ if ( $type eq 'ARRAY' || $type eq 'BSON::Array' ) {
Branch: 25 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY' or $type eq 'BSON::Array') { } 8499 96243 104742
elsif ($type eq 'BSON::DBPointer') { } 4 96239 96243
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') { } 9618 86621 96239
elsif ($type eq 'Regexp') { } 4 86617 86621
elsif ($type eq 'BSON::Regex' or $type eq 'MongoDB::BSON::Regexp') { } 8394 78223 86617
elsif ($type eq 'BSON::OID' or $type eq 'BSON::ObjectId') { } 8494 69729 78223
elsif ($type eq 'MongoDB::OID') { } 0 69729 69729
elsif ($type eq 'BSON::Time') { } 8568 61161 69729
elsif ($type eq 'Time::Moment') { } 0 61161 61161
elsif ($type eq 'DateTime') { } 0 61161 61161
elsif ($type eq 'DateTime::Tiny') { } 0 61161 61161
elsif ($type eq 'Mango::BSON::Time') { } 0 61161 61161
elsif ($type eq 'BSON::Timestamp') { } 8559 52602 61161
elsif ($type eq 'MongoDB::Timestamp') { } 0 52602 52602
elsif ($type eq 'BSON::MinKey' or $type eq 'MongoDB::MinKey') { } 8423 44179 52602
elsif ($type eq 'BSON::MaxKey' or $type eq 'MongoDB::MaxKey') { } 8492 35687 44179
elsif ($type eq 'SCALAR' or $type eq 'BSON::Bytes' or $type eq 'BSON::Binary' or $type eq 'MongoDB::BSON::Binary') { } 8603 27084 35687
elsif ($type eq 'BSON::Code' or $type eq 'MongoDB::Code') { } 8505 18579 27084
elsif ($type eq 'boolean' or $type =~ /$bools_re/) { } 8567 10012 18579
elsif ($type eq 'BSON::String' or $type eq 'BSON::Symbol') { } 8406 1606 10012
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
8498196243
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 8499 my $i = 0;
278 8499 tie( my %h, 'Tie::IxHash' );
279 8499 %h = map { $i++ => $_ } @$value;
280 8499 $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
9618086621
$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
96031586621
$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
9601286636
$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
9598386638
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw" or $type eq "MongoDB::BSON::_EncodedDoc"
l!l&&r!l&&!r
9598086641
$type eq "HASH" or $type eq "BSON::Doc" or $type eq "BSON::Raw"
l!l&&r!l&&!r
949610286641
$type eq "HASH" or $type eq "BSON::Doc"
l!l&&r!l&&!r
9493386743
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 9618 $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
8394078223
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 8394 my ( $re, $flags ) = @{$value}{qw/pattern flags/};
313 8394 $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
26846869729
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 8494 $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 8568 $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 8559 $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
8422144179
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 8423 $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
8491135687
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 8492 $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
8603027084
$type eq "SCALAR" or $type eq "BSON::Bytes" or $type eq "BSON::Binary"
l!l&&r!l&&!r
8599427084
$type eq "SCALAR" or $type eq "BSON::Bytes"
l!l&&r!l&&!r
2859727088
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 8603 ▶ : pack( "C*", @{ $value->data } );
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'SCALAR' ? : 2 8601 8603
$type eq 'BSON::Bytes' ? : 8597 4 8601
$type eq 'MongoDB::BSON::Binary' ? : 0 4 4
378 8603 ▶ my $subtype = $type eq 'SCALAR' ? 0 : $value->subtype;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'SCALAR' ? : 2 8601 8603
379 8603 my $len = length($data);
380 8603 ▶ if ( $subtype == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtype == 2) { } 2 8601 8603
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 8601 $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
8505018579
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 8505 my $code = $value->code;
394 8505 utf8::encode($code);
395 8505 $code = pack(BSON_STRING,$code);
396 8505 ▶ if ( ref( $value->scope ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value->scope eq 'HASH') { } 8240 265 8505
397 8240 my $scope = _encode_bson( $value->scope, $opt );
398 8240 $bson .=
399 pack( BSON_TYPE_NAME.BSON_CODE_W_SCOPE, 0x0F, $utf8_key, (4 + length($scope) + length($code)) ) . $code . $scope;
400 }
401 else {
402 265 $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
7856010012
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 8567 ▶ $bson .= pack( BSON_TYPE_NAME.BSON_BOOLEAN, 0x08, $utf8_key, ( $value ? 1 : 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 4305 4262 8567
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
840061606
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 8406 $value = $value->value;
414 8406 utf8::encode($value);
415 8406 $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 25902 my $flags = B::svref_2object(\$value)->FLAGS;
466  
467 25902 ▶ if ( $flags & B::SVf_NOK() ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($flags & 512) { } 8649 17253 25902
elsif ($flags & 256) { } 17129 124 17253
elsif ($opt->{'prefer_numeric'} and &looks_like_number($value)) { } 3 121 124
468 8649 $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 17129 $value = 0+$value;
473 17129 ▶ if ( $value > $max_int64 || $value < $min_int64 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value > $max_int64 or $value < $min_int64) { } 0 17129 17129
elsif ($value > $max_int32 or $value < $min_int32) { } 8556 8573 17129
Condition33%1/3
$value > $max_int64 or $value < $min_int64
l!l&&r!l&&!r
0017129
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
428842688573
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 8556 $bson .= pack( BSON_TYPE_NAME, 0x12, $utf8_key ) . _pack_int64($value);
478 }
479 else {
480 8573 $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 28520 delete $opt->{_circular}{$refaddr};
518 28520 $opt->{_depth}--;
519  
520 28520 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 sub __dump_bson {
556 0 ▶ my $bson = unpack("H*", shift);
Subroutine __dump_bson: not called CC=1 SCAR=6.9
557 0 my @pairs = $bson=~ m/(..)/g;
558 0 return join(" ", @pairs);
559 }
560  
561 sub _decode_bson {
562 27712 ▶ my ($bson, $opt) = @_;
Subroutine _decode_bson: called CC=72 SCAR=43.1
563 27712 ▶ if ( !defined $bson ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bson) 0 27712 27712
564 0 croak("Decode argument must not be undef");
565 }
566 27712 ▶ $opt->{_depth} = 0 unless defined $opt->{_depth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"_depth"} 2149 25563 27712
567 27712 $opt->{_depth}++;
568 27712 ▶ if ($opt->{_depth} > BSON_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"_depth"} > 100) 1 27711 27712
569 1 croak "Exceeded max object depth of ". BSON_MAX_DEPTH;
570 }
571 27711 my $blen= length($bson);
572 27711 my $len = unpack( BSON_INT32, $bson );
573 27711 ▶ if ( length($bson) != $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bson != $len) 15 27696 27711
574 15 croak("Incorrect length of the bson string (got $blen, wanted $len)");
575 }
576 27696 ▶ if ( chop($bson) ne "\x00" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (chop $bson ne "\0") 5 27691 27696
577 5 croak("BSON document not null terminated");
578 }
579 27691 $bson = substr $bson, 4;
580 27691 my @array = ();
581 27691 my %hash = ();
582 27691 ▶ tie( %hash, 'Tie::IxHash' ) if $opt->{ordered};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"ordered"} 1485 26206 27691
583 27691 my ($type, $key, $value);
584 27691 ▶ while ($bson) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bson) 138053 27526 165579
585 138053 ( $type, $key, $bson ) = unpack( BSON_TYPE_NAME.BSON_REMAINING, $bson );
586 138053 utf8::decode($key);
587  
588 # Check type and truncation
589 138053 my $min_size = $FIELD_SIZES{$type};
590 138053 ▶ if ( !defined $min_size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min_size) 8 138045 138053
591 8 croak( sprintf( $ERR_UNSUPPORTED, $type, $key ) );
592 }
593 138045 ▶ if ( length($bson) < $min_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bson < $min_size) 12 138033 138045
594 12 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
595 }
596  
597 # Double
598 138033 ▶ if ( $type == 0x01 ) {
Branch: 19 branches
BranchTrueFalseTotal
if ($type == 1) { } 8695 129338 138033
elsif ($type == 2 or $type == 14) { } 8538 120800 129338
elsif ($type == 3 or $type == 4) { } 17322 103478 120800
elsif ($type == 5) { } 8605 94873 103478
elsif ($type == 6) { } 2 94871 94873
elsif ($type == 7) { } 8487 86384 94871
elsif ($type == 8) { } 8569 77815 86384
elsif ($type == 9) { } 8571 69244 77815
elsif ($type == 10) { } 8543 60701 69244
elsif ($type == 11) { } 8398 52303 60701
elsif ($type == 12) { } 11 52292 52303
elsif ($type == 13) { } 265 52027 52292
elsif ($type == 15) { } 8251 43776 52027
elsif ($type == 16) { } 8512 35264 43776
elsif ($type == 17) { } 8558 26706 35264
elsif ($type == 18) { } 8585 18121 26706
elsif ($type == 19) { } 1206 16915 18121
elsif ($type == 255) { } 8423 8492 16915
elsif ($type == 127) { } 8492 0 8492
599 8695 ( $value, $bson ) = unpack( BSON_DOUBLE.BSON_REMAINING, $bson );
600 8695 ▶ $value = BSON::Double->new( value => $value ) if $opt->{wrap_numbers};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_numbers"} 51 8644 8695
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
852612120800
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 8538 ( $len, $bson ) = unpack( BSON_INT32 . BSON_REMAINING, $bson );
606 8538 ▶ 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 8527 8538
Condition100%3/3
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
l!l&&r!l&&!r
838527
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 8527 ( $value, $bson ) = unpack( "a$len" . BSON_REMAINING, $bson );
610 8527 chop($value); # remove trailing \x00
611 8527 ▶ if ( !utf8::decode($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($value)) 3 8524 8527
612 3 croak( sprintf( $ERR_BAD_UTF8, $key, $type ) );
613 }
614 8524 ▶ $value = BSON::String->new( value => $value ) if $opt->{wrap_strings};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_strings"} 6 8518 8524
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
88238499103478
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 17322 my $len = unpack( BSON_INT32, $bson );
620 17322 $value = _decode_bson( substr( $bson, 0, $len ), { %$opt, _decode_array => $type == 0x04} );
621 17216 ▶ 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 17205 17216
Condition88%8/9
$opt->{"wrap_dbrefs"} and $type == 3 and exists $value->{"\$id"} and exists $value->{"\$ref"}
!ll&&!rl&&r
17205011
$opt->{"wrap_dbrefs"} and $type == 3 and exists $value->{"\$id"}
!ll&&!rl&&r
8497870811
$opt->{"wrap_dbrefs"} and $type == 3
!ll&&!rl&&r
184968719
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 17216 $bson = substr( $bson, $len, length($bson) - $len );
625 }
626  
627 # Binary
628 elsif ( $type == 0x05 ) {
629 8605 my ( $len, $btype ) = unpack( BSON_INT32 . BSON_BINARY_TYPE, $bson );
630 8605 substr( $bson, 0, 5, '' );
631  
632 8605 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 1 8604 8605
633 1 croak( sprintf( $ERR_NEG_LENGTH, $key, $type ) );
634 }
635 8604 ▶ if ( $len > length($bson) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $bson) 1 8603 8604
636 1 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
637 }
638  
639 8603 my $binary = substr( $bson, 0, $len, '' );
640  
641 8603 ▶ if ( $btype == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($btype == 2) 5 8598 8603
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 8600 $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 8487 ( my $oid, $bson ) = unpack( BSON_OBJECTID.BSON_REMAINING, $bson );
665 8487 $value = BSON::OID->new(oid => $oid);
666 }
667  
668 # Boolean
669 elsif ( $type == 0x08 ) {
670 8569 ( my $bool, $bson ) = unpack( BSON_BOOLEAN.BSON_REMAINING, $bson );
671 8569 ▶ croak("BSON boolean must be 0 or 1. Key '$key' is $bool")
Branch: 1 branch
BranchTrueFalseTotal
unless $bool == 0 or $bool == 1 2 8567 8569
Condition100%3/3
$bool == 0 or $bool == 1
l!l&&r!l&&!r
426243052
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 8567 $value = boolean( $bool );
674 }
675  
676 # Datetime
677 elsif ( $type == 0x09 ) {
678 8571 if ( HAS_INT64 ) {
679 8571 ($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 8571 $value = BSON::Time->new( value => $value );
686 8571 my $dt_type = $opt->{dt_type};
687 8571 ▶ if ( defined $dt_type && $dt_type ne 'BSON::Time' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dt_type and $dt_type ne "BSON::Time") 1 8570 8571
Condition100%3/3
defined $dt_type and $dt_type ne "BSON::Time"
!ll&&!rl&&r
856911
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 8543 $value = undef;
700 }
701  
702 # Regex
703 elsif ( $type == 0x0B ) {
704 8398 ( my $re, my $op, $bson ) = unpack( BSON_CSTRING.BSON_CSTRING.BSON_REMAINING, $bson );
705 8398 $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 265 ( $len, $bson ) = unpack( BSON_INT32 . BSON_REMAINING, $bson );
727 265 ▶ 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 265 265
Condition33%1/3
length $bson < $len or substr($bson, $len - 1, 1) ne "\0"
l!l&&r!l&&!r
00265
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 265 ( $value, $bson ) = unpack( "a$len" . BSON_REMAINING, $bson );
731 265 chop($value); # remove trailing \x00
732 265 ▶ if ( !utf8::decode($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($value)) 0 265 265
733 0 croak( sprintf( $ERR_BAD_UTF8, $key, $type ) );
734 }
735 265 $value = BSON::Code->new( code => $value );
736 }
737  
738 # Code with scope
739 elsif ( $type == 0x0F ) {
740 8251 my $len = unpack( BSON_INT32, $bson );
741  
742 # validate length
743 8251 ▶ if ( $len < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 1 8250 8251
744 1 croak( sprintf( $ERR_NEG_LENGTH, $key, $type ) );
745 }
746 8250 ▶ if ( $len > length($bson) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $bson) 2 8248 8250
747 2 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
748 }
749 8248 ▶ if ( $len < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 5) 1 8247 8248
750 1 croak( sprintf( $ERR_LENGTH, $key, $type, 5, $len ) );
751 }
752  
753 # extract code and scope and chop off leading length
754 8247 my $codewscope = substr( $bson, 0, $len, '' );
755 8247 substr( $codewscope, 0, 4, '' );
756  
757 # extract code ( i.e. string )
758 8247 my $strlen = unpack( BSON_INT32, $codewscope );
759 8247 substr( $codewscope, 0, 4, '' );
760  
761 8247 ▶ 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 8246 8247
Condition66%2/3
length $codewscope < $strlen or substr($codewscope, -1, 1) ne "\0"
l!l&&r!l&&!r
108246
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 8246 my $code = substr($codewscope, 0, $strlen, '' );
766 8246 chop($code); # remove trailing \x00
767 8246 ▶ if ( !utf8::decode($code) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($code)) 0 8246 8246
768 0 croak( sprintf( $ERR_BAD_UTF8, $key, $type ) );
769 }
770  
771 8246 ▶ if ( length($codewscope) < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $codewscope < 5) 2 8244 8246
772 2 croak( sprintf( $ERR_TRUNCATED, $key, $type ) );
773 }
774  
775 # extract scope
776 8244 my $scopelen = unpack( BSON_INT32, $codewscope );
777 8244 ▶ 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 8241 8244
Condition66%2/3
length $codewscope < $scopelen or substr($codewscope, $scopelen - 1, 1) ne "\0"
l!l&&r!l&&!r
308241
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 8241 my $scope = _decode_bson( $codewscope, { %$opt, _decode_array => 0} );
782  
783 8240 $value = BSON::Code->new( code => $code, scope => $scope );
784 }
785  
786 # Int32
787 elsif ( $type == 0x10 ) {
788 8512 ( $value, $bson ) = unpack( BSON_INT32.BSON_REMAINING, $bson );
789 8512 ▶ $value = BSON::Int32->new( value => $value ) if $opt->{wrap_numbers};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_numbers"} 38 8474 8512
790 }
791  
792 # Timestamp
793 elsif ( $type == 0x11 ) {
794 8558 ( my $sec, my $inc, $bson ) = unpack( BSON_UINT32.BSON_UINT32.BSON_REMAINING, $bson );
795 8558 $value = BSON::Timestamp->new( $inc, $sec );
796 }
797  
798 # Int64
799 elsif ( $type == 0x12 ) {
800 8585 if ( HAS_INT64 ) {
801 8585 ($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 8585 ▶ $value = BSON::Int64->new( value => $value ) if $opt->{wrap_numbers};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"wrap_numbers"} 21 8564 8585
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 8423 $value = BSON::MinKey->new;
819 }
820  
821 # MaxKey
822 elsif ( $type == 0x7F ) {
823 8492 $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 137888 ▶ if ( $opt->{_decode_array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'_decode_array'}) { } 27677 110211 137888
834 27677 push @array, $value;
835 }
836 else {
837 110211 $hash{$key} = $value;
838 }
839 }
840 27526 $opt->{_depth}--;
841 27526 ▶ return $opt->{_decode_array} ? \@array : \%hash;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'_decode_array'} ? : 8496 19030 27526
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.0
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) 2019 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 »