author_tools/hobodecoder.pl

filter: Statementstmt 51.9% 189 / 364 Branchbran 32.5% 71 / 218 Conditioncond 22.7% 15 / 66 MC/DCmcdc 4.8% 2 / 41 Subroutinesub 69.2% 18 / 26 Podpod n/a totaltotal 41.2% 295 / 715 CC 128 SCAR 62.0
CC
128
Cov
42%
parse_sv
1419.3
parse_declarations
162.5
parse_header
73.7
CRAP 494.9
↑ Summary blib/lib/Sereal/Encoder.pm »
1 #!perl
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Encode qw(encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 our @constants;
10 5 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶BEGIN {
Subroutine BEGIN: called CC=9 SCAR=23.5
13 # Binary128 conversion and native-layout validation use Decoder's
14 # implementation. In a repository build, the matching Decoder may exist
15 # only in blib, so make it available before loading the tool's constants.
16 5 ▶ if ( -f '../this_is_the_Sereal_repo.txt' && -d '../Decoder/blib' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "../this_is_the_Sereal_repo.txt" and -d "../Decoder/blib") 0 5 5
Condition33%1/3
-f "../this_is_the_Sereal_repo.txt" and -d "../Decoder/blib"
!ll&&!rl&&r
500
MC/DC0%0/2
-f "../this_is_the_Sereal_repo.txt" and -d "../Decoder/blib"
-f "../this_is_the_Sereal_repo.txt" -d "../Decoder/blib"
Truth table33%1/3
A and B
A= -f "../this_is_the_Sereal_repo.txt"B= -d "../Decoder/blib"
ABresult
0X0
100
111
17 0 require blib;
18 0 blib->import('../Decoder');
19 }
20 5 my $add_use_blib = "";
21 5 my $use = "";
22 5 my @check;
23 5 for my $type ( "Decoder", "Encoder" ) {
24 10 ▶ if ( -e "blib/lib/Sereal/$type/Constants.pm" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "blib/lib/Sereal/$type/Constants.pm") 5 5 10
25 5 $add_use_blib = "use blib;";
26 5 @check = ($type);
27 5 last;
28 }
29 5 push @check, $type;
30 }
31  
32 5 my @err;
33 5 foreach my $check (@check) {
34 5 ▶ if (
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (eval(my $code = sprintf(qq[\n %s\n use Sereal::%s::Constants qw(:all);\n \@constants= \@Sereal::%s::Constants::EXPORT_OK;\n print "Loaded constants from \$INC{q(Sereal/%s/Constants.pm)}\\n";\n 1;\n ], $add_use_blib, ($check) x 3))) { } 5 0 5
35 eval(
36 my $code = sprintf '
37 %s
38 use Sereal::%s::Constants qw(:all);
39 @constants= @Sereal::%s::Constants::EXPORT_OK;
40 print "Loaded constants from $INC{q(Sereal/%s/Constants.pm)}\n";
41 1;
42 ', $add_use_blib, ($check) x 3
43 ) )
44 {
45 5 @err = ();
46 5 last;
47 }
48 else {
49 0 ▶ push @err, "Error:", $@ || "Zombie Error", "\nCode:\n$code";
Condition0%0/2
$@ || 'Zombie Error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
50 }
51 }
52 5 ▶ die @err if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 0 5 5
53 }
54  
55 5 my $done;
56 my $data;
57 5 my $packet_remainder;
58 5 my $hlen = -1;
59 5 my $indent = "";
60 5 my $opt;
61 my %siphash_function;
62  
63 5 ▶sub _load_siphash_function {
Subroutine _load_siphash_function: called CC=7 SCAR=19.9
64 5 my ($version) = @_;
65 5 ▶ return $siphash_function{$version} if $siphash_function{$version};
Branch: 1 branch
BranchTrueFalseTotal
if $siphash_function{$version} 0 5 5
66 5 for my $module (qw(Sereal::Decoder Sereal::Encoder)) {
67 5 ( my $file = "$module.pm" ) =~ s{::}{/}g;
68 5 ▶ next unless eval { require $file; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require $file; 1 } } 0 5 5
69 5 ▶ my $function =
Branch: 2 branches
BranchTrueFalseTotal
$version == 0 ? : 1 4 5
$version == 1 ? : 3 1 4
70 $version == 0 ? 'siphash24' : $version == 1 ? 'siphash24_128' : 'siphash36_128';
71 5 ▶ my $code = $module->can($function) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $module->can($function) or next 5 0 5
72 5 return $siphash_function{$version} = $code;
73 }
74 0 die "Could not load a Sereal module with SipHash support\n";
75 }
76  
77 5 ▶sub _constant_time_equal {
Subroutine _constant_time_equal: called CC=3 SCAR=11.0
78 5 my ( $left, $right ) = @_;
79 5 ▶ return 0 unless length($left) == length($right);
Branch: 1 branch
BranchTrueFalseTotal
unless length $left == length $right 0 5 5
80 5 my $different = 0;
81 5 for my $i ( 0 .. length($left) - 1 ) {
82 72 $different |= ord( substr( $left, $i, 1 ) ) ^ ord( substr( $right, $i, 1 ) );
83 }
84 5 return $different == 0;
85 }
86  
87 44 ▶sub _chop_data_prefix {
Subroutine _chop_data_prefix: called CC=2 SCAR=7.1
88 44 my ($len) = @_;
89 44 ▶ die "Unexpected end of packet" unless length($data) >= $len;
Branch: 1 branch
BranchTrueFalseTotal
unless length $data >= $len 0 44 44
90 44 return substr( $data, 0, $len, '' );
91 }
92  
93 5 ▶sub parse_header {
Subroutine parse_header: called CC=27 SCAR=43.0
94 5 ▶ $data =~ s/^(=[s\xF3]rl)(.)// or die "invalid header: $data";
Branch: 1 branch
BranchTrueFalseTotal
$data =~ s/^(=[s\xF3]rl)(.)// or die "invalid header: $data" 5 0 5
95 5 $done .= $1 . $2;
96 5 my $flags = $2;
97 5 my $len = varint();
98 5 my $hdr = _chop_data_prefix($len);
99  
100 5 my $proto_version = ord($flags) & SRL_PROTOCOL_VERSION_MASK;
101 5 $opt->{protocol_version} = $proto_version;
102 5 delete $opt->{long_double_declaration};
103 5 print "Sereal protocol version: $proto_version\n";
104 5 ▶ if ( length($hdr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $hdr) { } 0 5 5
105 0 print "Header($len): " . join( " ", map ord, split //, $hdr ) . "\n";
106 # The low suffix-flag bit identifies a serialized user-header value.
107 0 ▶ if ( $proto_version >= 2 && ( ord( substr( $hdr, 0, 1 ) ) & 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($proto_version >= 2 and ord substr($hdr, 0, 1) & 1) { } 0 0 0
Condition0%0/3
$proto_version >= 2 and ord substr($hdr, 0, 1) & 1
!ll&&!rl&&r
000
MC/DC0%0/2
$proto_version >= 2 and ord substr($hdr, 0, 1) & 1
$proto_version >= 2 ord substr($hdr, 0, 1) & 1
Truth table0%0/3
A and B
A= $proto_version >= 2B= ord substr($hdr, 0, 1) & 1
ABresult
0X0
100
111
108 {
109 0 $done .= substr( $hdr, 0, 1 );
110 0 print "Found user data in header:\n";
111 # parse_sv consumes the global input buffer. Temporarily replace
112 # it with the header's value, then restore the untouched body so
113 # reading metadata does not consume compressed document bytes.
114 0 my $tmp_data =
115 $data;
116 0 $data = substr( $hdr, 1 );
117 0 parse_sv(" ");
118 0 $data = $tmp_data;
119 0 print "End of user data in header. Body:\n";
120 }
121 else {
122 0 $done .= $hdr;
123 }
124 }
125 else {
126 5 print "Empty Header.\n";
127 }
128  
129 5 my $encoding = ord($flags) & SRL_PROTOCOL_ENCODING_MASK;
130 5 my $checksummed = ( $encoding & SRL_PROTOCOL_ENCODING_CHECKSUM ) != 0;
131 5 $encoding &= SRL_PROTOCOL_ENCODING_BASE_MASK;
132  
133 5 printf "%i %i %i\n", $encoding, ord(SRL_PROTOCOL_ENCODING_MASK), ord($flags);
134 5 $packet_remainder = undef;
135 5 ▶ if ($checksummed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksummed) 5 0 5
136 5 ▶ die "Checksummed packets require protocol version 6 or later\n"
Branch: 1 branch
BranchTrueFalseTotal
if $proto_version < 6 0 5 5
137 if $proto_version < 6;
138  
139 5 my $checksum_type = ord( _chop_data_prefix(1) );
140 5 $done .= chr($checksum_type);
141 5 my $tag_len = varint();
142 5 my $checksum_prefix = $done;
143 5 my $tag = _chop_data_prefix($tag_len);
144 5 $done .= $tag;
145 5 my $payload_len_start = length($done);
146 5 my $payload_len = varint();
147 5 my $payload_len_bytes = substr( $done, $payload_len_start );
148 5 ▶ die "Checksummed payload length exceeds available input\n"
Branch: 1 branch
BranchTrueFalseTotal
if $payload_len > length $data 0 5 5
149 if $payload_len > length($data);
150 5 $packet_remainder = substr( $data, $payload_len, length($data) - $payload_len, '' );
151 5 my $wire_payload = $data;
152 5 printf "Checksum type %u, tag length %u bytes, encoded payload length %u bytes.\n",
153 $checksum_type, $tag_len, $payload_len;
154 5 ▶ my $expected_len =
Branch: 2 branches
BranchTrueFalseTotal
$checksum_type == 0 ? : 1 4 5
$checksum_type == 1 || $checksum_type == 2 ? : 4 0 4
Condition66%2/3
$checksum_type == 1 or $checksum_type == 2
l!l&&r!l&&!r
310
MC/DC0%0/2
$checksum_type == 1 or $checksum_type == 2
$checksum_type == 1 $checksum_type == 2
Truth table66%2/3
A or B
A= $checksum_type == 1B= $checksum_type == 2
ABresult
1X1
011
000
155 $checksum_type == 0 ? 8 : ( $checksum_type == 1 || $checksum_type == 2 ) ? 16 : undef;
156 5 ▶ die "Invalid SipHash checksum length $tag_len for type $checksum_type\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $expected_len and $tag_len != $expected_len 0 5 5
Condition33%1/3
defined $expected_len and $tag_len != $expected_len
!ll&&!rl&&r
050
MC/DC0%0/2
defined $expected_len and $tag_len != $expected_len
defined $expected_len $tag_len != $expected_len
Truth table33%1/3
A and B
A= defined $expected_lenB= $tag_len != $expected_len
ABresult
0X0
100
111
157 if defined($expected_len) && $tag_len != $expected_len;
158 my $key =
159 $checksum_type == 0 ? $opt->{checksum_key_v1}
160 : $checksum_type == 1 ? $opt->{checksum_key_v2}
161 : $checksum_type == 2 ? $opt->{checksum_key_v3}
162 5 ▶ : undef;
Branch: 3 branches
BranchTrueFalseTotal
$checksum_type == 0 ? : 1 4 5
$checksum_type == 1 ? : 3 1 4
$checksum_type == 2 ? : 1 0 1
163  
164 5 ▶ if ( defined($expected_len) && defined($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected_len and defined $key) { } 5 0 5
Condition33%1/3
defined $expected_len and defined $key
!ll&&!rl&&r
005
MC/DC0%0/2
defined $expected_len and defined $key
defined $expected_len defined $key
Truth table33%1/3
A and B
A= defined $expected_lenB= defined $key
ABresult
0X0
100
111
165 5 my $siphash = _load_siphash_function($checksum_type);
166 5 my $actual = $siphash->( $checksum_prefix . $payload_len_bytes . $wire_payload, $key );
167 5 ▶ die "SipHash checksum verification failed\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _constant_time_equal($tag, $actual) 1 4 5
168 unless _constant_time_equal( $tag, $actual );
169 4 ▶ printf "SipHash-%s checksum verified: %s\n",
Branch: 2 branches
BranchTrueFalseTotal
$checksum_type == 0 ? : 1 3 4
$checksum_type == 1 ? : 2 1 3
170 $checksum_type == 0 ? '2-4' : $checksum_type == 1 ? '2-4-128' : '3-6-128',
171 unpack( 'H*', $tag );
172 }
173 else {
174 0 ▶ printf "SipHash tag (%u bytes, %s): %s\n",
Branch: 1 branch
BranchTrueFalseTotal
defined $expected_len ? : 0 0 0
175 $tag_len,
176 defined($expected_len) ? 'not verified (no matching key)' : 'unknown type, skipped',
177 unpack( 'H*', $tag );
178 }
179 }
180 4 ▶ if ( $encoding == SRL_PROTOCOL_ENCODING_RAW ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($encoding == 0) { } 3 1 4
elsif ($encoding == 16) { } 0 1 1
elsif ($encoding == 32) { } 0 1 1
elsif ($encoding == 48) { } 1 0 1
elsif ($encoding == 64) { } 0 0 0
181 3 print "Header says: Document body is uncompressed.\n";
182 }
183 elsif ( $encoding == SRL_PROTOCOL_ENCODING_SNAPPY ) {
184 0 print "Header says: Document body is Snappy-compressed.\n";
185 0 require Compress::Snappy;
186 0 my $out = Compress::Snappy::decompress($data);
187 0 $data = $out;
188 }
189 elsif ( $encoding == SRL_PROTOCOL_ENCODING_SNAPPY_INCREMENTAL ) {
190 0 print "Header says: Document body is Snappy-compressed (incremental).\n";
191 0 my $compressed_len = varint();
192 0 require Compress::Snappy;
193 0 my $out = Compress::Snappy::decompress($data);
194 0 $data = $out;
195 }
196 elsif ( $encoding == SRL_PROTOCOL_ENCODING_ZLIB ) {
197 1 print "Header says: Document body is ZLIB-compressed.\n";
198 1 my $uncompressed_len = varint();
199 1 my $compressed_len = varint();
200 1 require Compress::Zlib;
201 1 my $out = Compress::Zlib::uncompress($data);
202 1 $data = $out;
203 }
204 elsif ( $encoding == SRL_PROTOCOL_ENCODING_ZSTD ) {
205 0 print "Header says: Document body is ZSTD-compressed.\n";
206 0 my $compressed_len = varint();
207 0 require Compress::Zstd;
208 0 my $out = Compress::Zstd::decompress($data);
209 0 $data = $out;
210 }
211 else {
212 0 die "Invalid encoding '" . ( $encoding >> SRL_PROTOCOL_VERSION_BITS ) . "'";
213 }
214 4 $hlen = length($done);
215 }
216  
217 5 my ( $len_f, $len_d, $len_D, $len_F );
218  
219 0 ▶sub parse_float {
Subroutine parse_float: not called CC=3 SCAR=24.8
220 0 ▶ $len_f ||= length( pack( "f", 0 ) );
Condition0%0/2
$len_f ||= 4
l!l
00
MC/DC0%0/1
$len_f ||= 4
$len_f
Truth table0%0/2
A
A= $len_f
Aresult
11
00
221 0 my $v = _chop_data_prefix($len_f);
222 0 $done .= $v;
223 0 ▶ return unpack( $opt->{protocol_version} >= 7 ? 'f<' : 'f', $v );
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'protocol_version'} >= 7 ? : 0 0 0
224 }
225  
226 0 ▶sub parse_float_128 {
Subroutine parse_float_128: not called CC=2 SCAR=17.9
227 0 my $v = _chop_data_prefix(16);
228 0 $done .= $v;
229 0 require Sereal::Decoder;
230  
231 # Perl's native unpack templates cannot convert binary128 on every NV
232 # build. A small synthetic packet delegates that conversion to Decoder,
233 # keeping the debugger consistent with the library's rounding behavior.
234 # This packet has no checksum, independently of the input packet's policy.
235 0 my $packet = "=\xF3rl" . chr( $opt->{protocol_version} ) . "\0" . chr(SRL_HDR_FLOAT_128) . $v;
236 return Sereal::Decoder->new( { checksum_policy => 'ignore',
237 0 ▶ allow_unknown_long_double => $opt->{'allow-unknown-long-double'} || 0 } )->decode($packet);
Condition0%0/2
$opt->{'allow-unknown-long-double'} || 0
l!l
00
MC/DC0%0/1
$opt->{'allow-unknown-long-double'} || 0
$opt->{'allow-unknown-long-double'}
Truth table0%0/2
A
A= $opt->{'allow-unknown-long-double'}
Aresult
11
00
238 }
239  
240 0 ▶sub parse_double {
Subroutine parse_double: not called CC=3 SCAR=24.8
241 0 ▶ $len_d ||= length( pack( "d", 0 ) );
Condition0%0/2
$len_d ||= 8
l!l
00
MC/DC0%0/1
$len_d ||= 8
$len_d
Truth table0%0/2
A
A= $len_d
Aresult
11
00
242 0 my $v = _chop_data_prefix($len_d);
243 0 $done .= $v;
244 0 ▶ return unpack( $opt->{protocol_version} >= 7 ? 'd<' : 'd', $v );
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'protocol_version'} >= 7 ? : 0 0 0
245 }
246  
247 0 ▶sub parse_long_double {
Subroutine parse_long_double: not called CC=6 SCAR=37.4
248 0 ▶ if ( $opt->{protocol_version} >= 7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"protocol_version"} >= 7) 0 0 0
249 my $declaration = $opt->{long_double_declaration}
250 0 ▶ or die "LONG_DOUBLE requires a declaration\n";
Branch: 1 branch
BranchTrueFalseTotal
my $declaration = $opt->{"long_double_declaration"} or die "LONG_DOUBLE requires a declaration\n" 0 0 0
251 0 my $v = _chop_data_prefix( $opt->{long_double_size} );
252 0 $done .= $v;
253 0 require Sereal::Decoder;
254 return Sereal::Decoder->new( { checksum_policy => 'ignore',
255 0 ▶ allow_unknown_long_double => $opt->{'allow-unknown-long-double'} || 0 } )
Condition0%0/2
$opt->{'allow-unknown-long-double'} || 0
l!l
00
MC/DC0%0/1
$opt->{'allow-unknown-long-double'} || 0
$opt->{'allow-unknown-long-double'}
Truth table0%0/2
A
A= $opt->{'allow-unknown-long-double'}
Aresult
11
00
256 ->decode( "=\xF3rl\7\0" . $declaration . chr(SRL_HDR_LONG_DOUBLE) . $v );
257 }
258 0 ▶ $len_D ||= eval { length( pack( "D", 0.0 ) ) };
Condition0%0/3
$len_D ||= eval { do { 16 } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$len_D ||= eval { do { 16 } }
$len_D eval { do { 16 } }
Truth table0%0/3
A ||= B
A= $len_DB= eval { do { 16 } }
ABresult
1X1
011
000
259 0 ▶ die "Long double not supported" unless $len_D;
Branch: 1 branch
BranchTrueFalseTotal
unless $len_D 0 0 0
260 0 my $v = _chop_data_prefix($len_D);
261 0 $done .= $v;
262 0 return unpack( 'D', $v );
263 }
264  
265 4 ▶sub parse_declarations {
Subroutine parse_declarations: called CC=13 SCAR=50.9
266 4 ▶ return if $opt->{protocol_version} < 7;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"protocol_version"} < 7 4 0 4
267 0 my %seen;
268 0 ▶ while (length($data) >= 2
Branch: 1 branch
BranchTrueFalseTotal
if (length $data >= 2 and ord substr($data, 0, 1) == 62 and ord substr($data, 1, 1) == 1) 0 0 0
Condition0%0/6
length $data >= 2 and ord substr($data, 0, 1) == 62 and ord substr($data, 1, 1) == 1
!ll&&!rl&&r
000
length $data >= 2 and ord substr($data, 0, 1) == 62
!ll&&!rl&&r
000
MC/DC0%0/3
length $data >= 2 and ord substr($data, 0, 1) == 62 and ord substr($data, 1, 1) == 1
length $data >= 2 ord substr($data, 0, 1) == 62 ord substr($data, 1, 1) == 1
Truth table0%0/4
A and B and C
A= length $data >= 2B= ord substr($data, 0, 1) == 62C= ord substr($data, 1, 1) == 1
ABCresult
0XX0
10X0
1100
1111
269 && ord( substr( $data, 0, 1 ) ) == SRL_HDR_EXTEND
270 && ord( substr( $data, 1, 1 ) ) == SRL_EXT_DECLARE )
271 {
272 0 my $start = length($done);
273 0 $done .= _chop_data_prefix(2);
274 0 my $len = varint();
275 0 ▶ die "Invalid declaration payload length\n" unless $len <= length($data);
Branch: 1 branch
BranchTrueFalseTotal
unless $len <= length $data 0 0 0
276 0 my $declaration_end = length($done) + $len;
277 0 my $kind = varint();
278 0 my $name = $DECLARATION_NAME{$kind};
279 0 ▶ die "Unknown declaration kind $kind\n" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
280 0 ▶ die "Duplicate declaration $name (kind $kind)\n" if $seen{$kind}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$kind}++ 0 0 0
281 0 ▶ die "Unsupported declaration $name (kind $kind)\n" unless $kind == SRL_DECL_LONG_DOUBLE;
Branch: 1 branch
BranchTrueFalseTotal
unless $kind == 1 0 0 0
282 0 ▶ die "Invalid $name declaration length\n" unless $declaration_end - length($done) == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $declaration_end - length($done) == 2 0 0 0
283 0 my $payload = _chop_data_prefix(2);
284 0 $done .= $payload;
285 0 my ( $format, $size ) = unpack( 'CC', $payload );
286 0 $opt->{long_double_declaration} = substr( $done, $start );
287 0 $opt->{long_double_size} = $size;
288 0 require Sereal::Decoder;
289  
290 # A declaration is a promise about this document's native layout,
291 # even when no LONG_DOUBLE follows. Decoder validates that promise
292 # using a synthetic packet with an integer root, so the debugger
293 # reports the same incompatibility as the library.
294 Sereal::Decoder->new( { checksum_policy => 'ignore',
295 allow_unknown_long_double => $opt->{'allow-unknown-long-double'} || 0 } )
296 0 ▶ ->decode( "=\xF3rl\7\0" . $opt->{long_double_declaration} . "\0" );
Condition0%0/2
$opt->{'allow-unknown-long-double'} || 0
l!l
00
MC/DC0%0/1
$opt->{'allow-unknown-long-double'} || 0
$opt->{'allow-unknown-long-double'}
Truth table0%0/2
A
A= $opt->{'allow-unknown-long-double'}
Aresult
11
00
297 0 ▶ my $order = $format & SRL_LD_BIG_ENDIAN ? 'big' : 'little';
Branch: 1 branch
BranchTrueFalseTotal
$format & 128 ? : 0 0 0
298 0 ▶ my $layout = ( $format & 127 ) == SRL_LD_OPAQUE
Branch: 1 branch
BranchTrueFalseTotal
($format & 127) == 127 ? : 0 0 0
299 ? "opaque native storage, $order-endian host"
300 : sprintf( 'format %u, %s endian', $format & 127, $order );
301 printf "%s %s (kind %u): %s, %u bytes\n",
302 0 $EXT_TAG_NAME{SRL_EXT_DECLARE()}, $name, $kind, $layout, $size;
303 }
304 }
305  
306 5 my $fmt1 = "%06d/%06d: %02x%1s %03s %s";
307 5 my $fmt2 = "%-6s %-6s %-2s%1s %-3s %s";
308 5 my $lead_items = 5; # 1 less than the fmt2
309  
310 4 ▶sub parse_sv {
Subroutine parse_sv: called CC=50 SCAR=72.6
311 4 my ($ind) = @_;
312  
313 4 my $p = length($done);
314 4 my $t = _chop_data_prefix(1);
315 4 $done .= $t;
316 4 my $o = ord($t);
317 4 my $bv = $o;
318 4 my $high = $o >= 128;
319 4 ▶ $o -= 128 if $high;
Branch: 1 branch
BranchTrueFalseTotal
if $high 0 4 4
320 4 ▶ printf $fmt1, $p, $p - $hlen + 1, $o, $high ? '*' : ' ', $bv, $ind;
Branch: 1 branch
BranchTrueFalseTotal
$high ? : 0 4 4
321  
322 4 ▶ if ( $o == SRL_HDR_VARINT ) {
Branch: 33 branches
BranchTrueFalseTotal
if ($o == 32) { } 0 4 4
elsif ($o == 33) { } 0 4 4
elsif ($o == 60) { } 0 4 4
elsif (0 <= $o and $o <= 15) { } 0 4 4
elsif (16 <= $o and $o <= 31) { } 0 4 4
elsif ($o >= 96) { } 3 1 4
elsif ($o == 38 or $o == 39) { } 1 0 1
elsif ($o == 62) { } 0 0 0
elsif ($o == 34) { } 0 0 0
elsif ($o == 35) { } 0 0 0
elsif ($o == 36) { } 0 0 0
elsif ($o == 56) { } 0 0 0
elsif ($o == 40) { } 0 0 0
elsif ($o == 41) { } 0 0 0
elsif ($o == 47) { } 0 0 0
elsif (64 <= $o and $o <= 79) { } 0 0 0
elsif ($o == 43) { } 0 0 0
elsif (80 <= $o and $o <= 95) { } 0 0 0
elsif ($o == 42) { } 0 0 0
elsif ($o == 57) { } 0 0 0
elsif ($o == 37) { } 0 0 0
elsif ($o == 48) { } 0 0 0
elsif ($o == 63) { } 0 0 0
elsif ($o == 46) { } 0 0 0
elsif ($o == 45) { } 0 0 0
elsif ($o == 51) { } 0 0 0
elsif ($o == 44) { } 0 0 0
elsif ($o == 50) { } 0 0 0
elsif ($o == 49) { } 0 0 0
elsif ($o == 58) { } 0 0 0
elsif ($o == 59) { } 0 0 0
elsif ($o == 52) { } 0 0 0
elsif ($o == 53) { } 0 0 0
323 0 printf "VARINT: %u\n", varint();
324 }
325 elsif ( $o == SRL_HDR_ZIGZAG ) {
326 0 printf "ZIGZAG: %d\n", zigzag();
327 }
328 elsif ( $o == SRL_HDR_NEG_VARINT ) {
329 0 my $magnitude = varint();
330 0 ▶ die "NEG_VARINT magnitude must be nonzero" unless $magnitude;
Branch: 1 branch
BranchTrueFalseTotal
unless $magnitude 0 0 0
331 0 printf "NEG_VARINT: -%u\n", $magnitude;
332 }
333 ▶ elsif ( SRL_HDR_POS_LOW <= $o && $o <= SRL_HDR_POS_HIGH ) {
Condition33%1/3
0 <= $o and $o <= 15
!ll&&!rl&&r
040
MC/DC0%0/2
0 <= $o and $o <= 15
0 <= $o $o <= 15
Truth table33%1/3
A and B
A= 0 <= $oB= $o <= 15
ABresult
0X0
100
111
334 0 printf "POS: %u\n", $o;
335 }
336 ▶ elsif ( SRL_HDR_NEG_LOW <= $o && $o <= SRL_HDR_NEG_HIGH ) {
Condition33%1/3
16 <= $o and $o <= 31
!ll&&!rl&&r
040
MC/DC0%0/2
16 <= $o and $o <= 31
16 <= $o $o <= 31
Truth table33%1/3
A and B
A= 16 <= $oB= $o <= 31
ABresult
0X0
100
111
337 0 $o = $o - 32;
338 0 printf "NEG: %i\n", $o;
339 }
340 elsif ( $o >= SRL_HDR_SHORT_BINARY_LOW ) {
341 3 $o -= SRL_HDR_SHORT_BINARY_LOW;
342 3 my $len = $o;
343 3 my $str = _chop_data_prefix($len);
344 3 $done .= $str;
345 3 printf "SHORT_BINARY(%u): '%s' (%s)\n", $len, encode_utf8($str), unpack( "H*", $str );
346 }
347 ▶ elsif ( $o == SRL_HDR_BINARY || $o == SRL_HDR_STR_UTF8 ) {
Condition33%1/3
$o == 38 or $o == 39
l!l&&r!l&&!r
100
MC/DC0%0/2
$o == 38 or $o == 39
$o == 38 $o == 39
Truth table33%1/3
A or B
A= $o == 38B= $o == 39
ABresult
1X1
011
000
348 1 my $l = varint();
349 1 my $str = _chop_data_prefix($l); # fixme UTF8
350 1 $done .= $str;
351 1 ▶ $str = decode_utf8($str) if $o == SRL_HDR_STR_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $o == 39 0 1 1
352 1 ▶ printf( ( $o == SRL_HDR_STR_UTF8 ? "STR_UTF8" : "BINARY" ) . "(%u): '%s' (%s)\n",
Branch: 1 branch
BranchTrueFalseTotal
$o == 39 ? : 0 1 1
353 $l, encode_utf8($str), unpack( "H*", encode_utf8($str) ) );
354 }
355 elsif ( $o == SRL_HDR_EXTEND ) {
356 0 my $ext = ord( _chop_data_prefix(1) );
357 0 $done .= chr($ext);
358 0 ▶ if ( $ext == SRL_EXT_DECLARE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext == 1) 0 0 0
359 0 die "Declaration outside document prefix\n";
360 }
361 0 ▶ if ( $ext == SRL_EXT_UNDEF_DEADLY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext == 255) { } 0 0 0
362 0 print "UNDEF_DEADLY (undef; must not be emitted by an encoder)\n";
363 }
364 else {
365 0 ▶ die "Unknown extended tag ID $ext\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ext == 38 or $ext == 39 0 0 0
Condition0%0/3
$ext == 38 or $ext == 39
l!l&&r!l&&!r
000
MC/DC0%0/2
$ext == 38 or $ext == 39
$ext == 38 $ext == 39
Truth table0%0/3
A or B
A= $ext == 38B= $ext == 39
ABresult
1X1
011
000
366 unless $ext == SRL_EXT_VSTRING_BINARY || $ext == SRL_EXT_VSTRING_UTF8;
367  
368 # The VSTRING payload is its original spelling. The extension ID
369 # describes the encoding of the PV reconstructed from that spelling.
370 0 my $l = varint();
371 0 my $str = _chop_data_prefix($l);
372 0 $done .= $str;
373 0 ▶ $str = decode_utf8($str) if $ext == SRL_EXT_VSTRING_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $ext == 39 0 0 0
374 0 ▶ printf "VSTRING-%s spelling(%u): '%s' (%s)\n",
Branch: 1 branch
BranchTrueFalseTotal
$ext == 39 ? : 0 0 0
375 $ext == SRL_EXT_VSTRING_UTF8 ? "UTF8" : "BINARY",
376 $l, encode_utf8($str), unpack( "H*", encode_utf8($str) );
377 }
378 }
379 elsif ( $o == SRL_HDR_FLOAT ) {
380 0 printf "FLOAT(%f)\n", parse_float();
381 }
382 elsif ( $o == SRL_HDR_DOUBLE ) {
383 0 printf "DOUBLE(%f)\n", parse_double();
384 }
385 elsif ( $o == SRL_HDR_LONG_DOUBLE ) {
386 0 printf "LONG_DOUBLE(%f)\n", parse_long_double();
387 }
388 elsif ( $o == SRL_HDR_FLOAT_128 ) {
389 0 printf "FLOAT_128(%f)\n", parse_float_128();
390 }
391 elsif ( $o == SRL_HDR_REFN ) {
392 0 printf "REFN\n";
393 0 parse_sv( $ind . " " );
394 }
395 elsif ( $o == SRL_HDR_REFP ) {
396 0 my $len = varint();
397 0 printf "REFP(%u)\n", $len;
398 }
399 elsif ( $o == SRL_HDR_COPY ) {
400 0 my $len = varint();
401 0 printf "COPY(%u)\n", $len;
402 }
403 ▶ elsif ( SRL_HDR_ARRAYREF_LOW <= $o && $o <= SRL_HDR_ARRAYREF_HIGH ) {
Condition0%0/3
64 <= $o and $o <= 79
!ll&&!rl&&r
000
MC/DC0%0/2
64 <= $o and $o <= 79
64 <= $o $o <= 79
Truth table0%0/3
A and B
A= 64 <= $oB= $o <= 79
ABresult
0X0
100
111
404 0 printf "ARRAYREF";
405 0 parse_av( $ind, $o );
406 }
407 elsif ( $o == SRL_HDR_ARRAY ) {
408 0 printf "ARRAY";
409 0 parse_av($ind);
410 }
411 ▶ elsif ( SRL_HDR_HASHREF_LOW <= $o && $o <= SRL_HDR_HASHREF_HIGH ) {
Condition0%0/3
80 <= $o and $o <= 95
!ll&&!rl&&r
000
MC/DC0%0/2
80 <= $o and $o <= 95
80 <= $o $o <= 95
Truth table0%0/3
A and B
A= 80 <= $oB= $o <= 95
ABresult
0X0
100
111
412 0 printf "HASHREF";
413 0 parse_hv( $ind, $o );
414 }
415 elsif ( $o == SRL_HDR_HASH ) {
416 0 printf "HASH";
417 0 parse_hv($ind);
418 }
419 elsif ( $o == SRL_HDR_CANONICAL_UNDEF ) {
420 0 printf "CANONICAL_UNDEF\n";
421 }
422 elsif ( $o == SRL_HDR_UNDEF ) {
423 0 printf "UNDEF\n";
424 }
425 elsif ( $o == SRL_HDR_WEAKEN ) {
426 0 printf "WEAKEN\n";
427 0 parse_sv($ind);
428 }
429 elsif ( $o == SRL_HDR_PAD ) {
430 0 printf "[PAD]\n";
431 0 parse_sv($ind);
432 }
433 elsif ( $o == SRL_HDR_ALIAS ) {
434 0 my $ofs = varint();
435 0 printf "ALIAS(%u)\n", $ofs;
436 }
437 elsif ( $o == SRL_HDR_OBJECTV ) {
438 0 my $ofs = varint();
439 0 printf "OBJECTV(%d)\n", $ofs;
440 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
441 0 parse_sv( $ind . " " );
442 }
443 elsif ( $o == SRL_HDR_OBJECTV_FREEZE ) {
444 0 my $ofs = varint();
445 0 printf "OBJECTV_FREEZE(%d)\n", $ofs;
446 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
447 0 parse_sv( $ind . " " );
448 }
449 elsif ( $o == SRL_HDR_OBJECT ) {
450 0 printf "OBJECT\n";
451 0 printf "$fmt2 Class:\n", ("") x $lead_items, $ind;
452 0 parse_sv( $ind . " " );
453 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
454 0 parse_sv( $ind . " " );
455 }
456 elsif ( $o == SRL_HDR_OBJECT_FREEZE ) {
457 0 printf "OBJECT_FREEZE\n";
458 0 printf "$fmt2 Class:\n", ("") x $lead_items, $ind;
459 0 parse_sv( $ind . " " );
460 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
461 0 parse_sv( $ind . " " );
462 }
463 elsif ( $o == SRL_HDR_REGEXP ) {
464 0 printf "REGEXP\n";
465 0 parse_sv( $ind . " " );
466 0 parse_sv( $ind . " " );
467 }
468 elsif ( $o == SRL_HDR_FALSE ) {
469 0 printf "FALSE\n";
470 }
471 elsif ( $o == SRL_HDR_TRUE ) {
472 0 printf "TRUE\n";
473 }
474 elsif ( $o == SRL_HDR_NO ) {
475 0 printf "NO (Perl boolean false)\n";
476 }
477 elsif ( $o == SRL_HDR_YES ) {
478 0 printf "YES (Perl boolean true)\n";
479  
480 }
481 else {
482 0 printf "<UNKNOWN>\n";
483 0 die sprintf "unsupported type: 0x%02x (%d) %s: %s", $o, $o,
484 Data::Dumper::qquote($t),
485 Data::Dumper->new( [ $TAG_INFO_ARRAY[$o] ] )->Terse(1)->Dump();
486 }
487 4 return 0;
488 }
489  
490 0 ▶sub parse_av {
Subroutine parse_av: not called CC=3 SCAR=24.8
491 0 my ( $ind, $o ) = @_;
492 0 ▶ my $len = defined $o ? $o & 15 : varint();
Branch: 1 branch
BranchTrueFalseTotal
defined $o ? : 0 0 0
493 0 printf "(%u)\n", $len;
494 0 $ind .= " ";
495 0 ▶ while ( $len-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len--) 0 0 0
496 0 parse_sv( $ind, \$len );
497 }
498 }
499  
500 0 ▶sub parse_hv {
Subroutine parse_hv: not called CC=3 SCAR=24.8
501 0 my ( $ind, $o ) = @_;
502 0 ▶ my $len = ( defined $o ? $o & 15 : varint() );
Branch: 1 branch
BranchTrueFalseTotal
defined $o ? : 0 0 0
503 0 printf "(%u)\n", $len;
504 0 $ind .= " ";
505 0 ▶ while ( $len-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len--) 0 0 0
506 0 printf "$fmt2%s:\n", ("") x $lead_items, $ind, "KEY";
507 0 parse_sv( $ind . " " );
508 0 printf "$fmt2%s:\n", ("") x $lead_items, $ind, "VALUE";
509 0 parse_sv( $ind . " " );
510 }
511 }
512  
513 # super inefficient
514 18 ▶sub varint {
Subroutine varint: called CC=6 SCAR=19.9
515 18 my $uv_bits = 8 * $Config{uvsize};
516 18 my $max_bytes = int( ( $uv_bits + 6 ) / 7 );
517 18 my ( $x, $lshift ) = ( 0, 0 );
518 18 for my $index ( 0 .. $max_bytes - 1 ) {
519 21 ▶ die "premature end of varint" unless length $data;
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 21 21
520 21 my $c = ord( _chop_data_prefix(1) );
521 21 $done .= chr($c);
522 21 my $part = $c & 0x7f;
523 21 ▶ if ( $index == $max_bytes - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $max_bytes - 1) 0 21 21
524 0 my $bits_left = $uv_bits - $lshift;
525 0 my $max_part = ( 1 << $bits_left ) - 1;
526 0 ▶ die "varint too big" if $part > $max_part;
Branch: 1 branch
BranchTrueFalseTotal
if $part > $max_part 0 0 0
527 }
528 21 $x |= $part << $lshift;
529 21 ▶ return $x unless $c & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless $c & 128 18 3 21
530 3 $lshift += 7;
531 }
532 0 die "varint too long";
533 }
534  
535 0 ▶sub _zigzag {
Subroutine _zigzag: not called CC=2 SCAR=17.9
536 0 my $n = $_[0];
537 0 ▶ return $n & 1 ? -( ( $n >> 1 ) + 1 ) : ( $n >> 1 );
Branch: 1 branch
BranchTrueFalseTotal
$n & 1 ? : 0 0 0
538 }
539  
540 0 ▶sub zigzag {
Subroutine zigzag: not called CC=1 SCAR=6.9
541 0 return _zigzag( varint() );
542 }
543  
544 5 GetOptions( $opt = {}, 'e|stderr', 'checksum-v0-key=s', 'checksum-v1-key=s', 'checksum-v2-key=s',
545 'checksum-v0-key-hex=s', 'checksum-v1-key-hex=s', 'checksum-v2-key-hex=s',
546 'allow-unknown-long-double', );
547  
548 5 for my $version ( 0, 1, 2 ) {
549 15 my $plain_name = "checksum_key_v" . ( $version + 1 );
550 15 my $hex_name = "checksum_key_v" . ( $version + 1 ) . "_hex";
551 15 my $plain_option = "checksum-v${version}-key";
552 15 my $hex_option = "checksum-v${version}-key-hex";
553 die "Specify only one of --$plain_option and --$hex_option\n"
554 15 ▶ if defined( $opt->{$plain_option} ) && defined( $opt->{$hex_option} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{$plain_option} and defined $opt->{$hex_option} 0 15 15
Condition33%1/3
defined $opt->{$plain_option} and defined $opt->{$hex_option}
!ll&&!rl&&r
1500
MC/DC0%0/2
defined $opt->{$plain_option} and defined $opt->{$hex_option}
defined $opt->{$plain_option} defined $opt->{$hex_option}
Truth table33%1/3
A and B
A= defined $opt->{$plain_option}B= defined $opt->{$hex_option}
ABresult
0X0
100
111
555 15 ▶ if ( defined $opt->{$hex_option} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opt->{$hex_option}) { } 5 10 15
elsif (defined $opt->{$plain_option}) { } 0 10 10
556 die "--$hex_option expects an even number of hexadecimal digits\n"
557 5 ▶ if $opt->{$hex_option} =~ /[^0-9a-fA-F]/ || length( $opt->{$hex_option} ) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{$hex_option} =~ /[^0-9a-fA-F]/ or length($opt->{$hex_option}) % 2 0 5 5
Condition33%1/3
$opt->{$hex_option} =~ /[^0-9a-fA-F]/ or length($opt->{$hex_option}) % 2
l!l&&r!l&&!r
005
MC/DC0%0/2
$opt->{$hex_option} =~ /[^0-9a-fA-F]/ or length($opt->{$hex_option}) % 2
$opt->{$hex_option} =~ /[^0-9a-fA-F]/ length($opt->{$hex_option}) % 2
Truth table33%1/3
A or B
A= $opt->{$hex_option} =~ /[^0-9a-fA-F]/B= length($opt->{$hex_option}) % 2
ABresult
1X1
011
000
558 5 $opt->{$plain_name} = pack( 'H*', $opt->{$hex_option} );
559 }
560 elsif ( defined $opt->{$plain_option} ) {
561 0 $opt->{$plain_name} = $opt->{$plain_option};
562 }
563 die "--checksum-v${version}-key must be exactly 16 bytes\n"
564 15 ▶ if defined( $opt->{$plain_name} ) && length( $opt->{$plain_name} ) != 16;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{$plain_name} and length $opt->{$plain_name} != 16 0 15 15
Condition66%2/3
defined $opt->{$plain_name} and length $opt->{$plain_name} != 16
!ll&&!rl&&r
1050
MC/DC0%0/2
defined $opt->{$plain_name} and length $opt->{$plain_name} != 16
defined $opt->{$plain_name} length $opt->{$plain_name} != 16
Truth table66%2/3
A and B
A= defined $opt->{$plain_name}B= length $opt->{$plain_name} != 16
ABresult
0X0
100
111
565 }
566  
567 5 $| = 1;
568 5 ▶if ( $opt->{e} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"e"}) 0 5 5
569 0 select(STDERR);
570 }
571  
572 5 ▶sub dump_bytes {
Subroutine dump_bytes: called CC=4 SCAR=13.9
573 5 my ($bytes) = @_;
574 5 print "Hex dump:\n";
575 5 ▶ for ( my $offset = 0; $offset < length($bytes); $offset += 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $bytes) 23 5 28
576 23 my @octets = unpack( 'C*', substr( $bytes, $offset, 16 ) );
577 23 my $hex = join ' ', map { sprintf '%02x', $_ } @octets;
578 23 ▶ my $ascii = join '', map { $_ >= 32 && $_ <= 126 ? chr($_) : '.' } @octets;
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 32 && $_ <= 126 ? : 160 166 326
Condition100%3/3
$_ >= 32 and $_ <= 126
!ll&&!rl&&r
44122160
MC/DC100%2/2
$_ >= 32 and $_ <= 126
$_ >= 32 $_ <= 126
Truth table100%3/3
A and B
A= $_ >= 32B= $_ <= 126
ABresult
0X0
100
111
579 23 printf "%08x %-47s |%s|\n", $offset, $hex, $ascii;
580 }
581 }
582  
583 5 binmode STDIN;
584 5 local $/ = undef;
585 5 $data = <STDIN>;
586  
587 5 dump_bytes($data);
588 5 print "\n\nTotal length: " . length($data) . "\n\n";
589  
590 5 ▶while ( length $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 5 4 9
591 5 parse_header();
592 4 print "--- End header\n";
593 4 parse_declarations();
594 4 $done = parse_sv("");
595 4 print "--- End Document\n";
596 4 ▶ $data = $packet_remainder if defined $packet_remainder;
Branch: 1 branch
BranchTrueFalseTotal
if defined $packet_remainder 4 0 4
597 4 $packet_remainder = undef;
598 }
↑ Summary blib/lib/Sereal/Encoder.pm »