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  
107 # The low suffix-flag bit identifies a serialized user-header value.
108 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
109 0 $done .= substr( $hdr, 0, 1 );
110 0 print "Found user data in header:\n";
111  
112 # parse_sv consumes the global input buffer. Temporarily replace
113 # it with the header's value, then restore the untouched body so
114 # reading metadata does not consume compressed document bytes.
115 0 my $tmp_data = $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( {
237 checksum_policy => 'ignore',
238 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
239 } )->decode($packet);
240 }
241  
242 0 ▶sub parse_double {
Subroutine parse_double: not called CC=3 SCAR=24.8
243 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
244 0 my $v = _chop_data_prefix($len_d);
245 0 $done .= $v;
246 0 ▶ return unpack( $opt->{protocol_version} >= 7 ? 'd<' : 'd', $v );
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'protocol_version'} >= 7 ? : 0 0 0
247 }
248  
249 0 ▶sub parse_long_double {
Subroutine parse_long_double: not called CC=6 SCAR=37.4
250 0 ▶ if ( $opt->{protocol_version} >= 7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"protocol_version"} >= 7) 0 0 0
251 my $declaration = $opt->{long_double_declaration}
252 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
253 0 my $v = _chop_data_prefix( $opt->{long_double_size} );
254 0 $done .= $v;
255 0 require Sereal::Decoder;
256 return Sereal::Decoder->new( {
257 checksum_policy => 'ignore',
258 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
259 } )->decode( "=\xF3rl\7\0" . $declaration . chr(SRL_HDR_LONG_DOUBLE) . $v );
260 }
261 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
262 0 ▶ die "Long double not supported" unless $len_D;
Branch: 1 branch
BranchTrueFalseTotal
unless $len_D 0 0 0
263 0 my $v = _chop_data_prefix($len_D);
264 0 $done .= $v;
265 0 return unpack( 'D', $v );
266 }
267  
268 4 ▶sub parse_declarations {
Subroutine parse_declarations: called CC=13 SCAR=50.9
269 4 ▶ return if $opt->{protocol_version} < 7;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"protocol_version"} < 7 4 0 4
270 0 my %seen;
271 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
272 && ord( substr( $data, 0, 1 ) ) == SRL_HDR_EXTEND
273 && ord( substr( $data, 1, 1 ) ) == SRL_EXT_DECLARE )
274 {
275 0 my $start = length($done);
276 0 $done .= _chop_data_prefix(2);
277 0 my $len = varint();
278 0 ▶ die "Invalid declaration payload length\n" unless $len <= length($data);
Branch: 1 branch
BranchTrueFalseTotal
unless $len <= length $data 0 0 0
279 0 my $declaration_end = length($done) + $len;
280 0 my $kind = varint();
281 0 my $name = $DECLARATION_NAME{$kind};
282 0 ▶ die "Unknown declaration kind $kind\n" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
283 0 ▶ die "Duplicate declaration $name (kind $kind)\n" if $seen{$kind}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$kind}++ 0 0 0
284 0 ▶ die "Unsupported declaration $name (kind $kind)\n" unless $kind == SRL_DECL_LONG_DOUBLE;
Branch: 1 branch
BranchTrueFalseTotal
unless $kind == 1 0 0 0
285 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
286 0 my $payload = _chop_data_prefix(2);
287 0 $done .= $payload;
288 0 my ( $format, $size ) = unpack( 'CC', $payload );
289 0 $opt->{long_double_declaration} = substr( $done, $start );
290 0 $opt->{long_double_size} = $size;
291 0 require Sereal::Decoder;
292  
293 # A declaration is a promise about this document's native layout,
294 # even when no LONG_DOUBLE follows. Decoder validates that promise
295 # using a synthetic packet with an integer root, so the debugger
296 # reports the same incompatibility as the library.
297 Sereal::Decoder->new( {
298 checksum_policy => 'ignore',
299 allow_unknown_long_double => $opt->{'allow-unknown-long-double'} || 0
300 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
301 0 ▶ my $order = $format & SRL_LD_BIG_ENDIAN ? 'big' : 'little';
Branch: 1 branch
BranchTrueFalseTotal
$format & 128 ? : 0 0 0
302 0 ▶ my $layout =
Branch: 1 branch
BranchTrueFalseTotal
($format & 127) == 127 ? : 0 0 0
303 ( $format & 127 ) == SRL_LD_OPAQUE
304 ? "opaque native storage, $order-endian host"
305 : sprintf( 'format %u, %s endian', $format & 127, $order );
306 printf "%s %s (kind %u): %s, %u bytes\n",
307 0 $EXT_TAG_NAME{ SRL_EXT_DECLARE() }, $name, $kind, $layout, $size;
308 }
309 }
310  
311 5 my $fmt1 = "%06d/%06d: %02x%1s %03s %s";
312 5 my $fmt2 = "%-6s %-6s %-2s%1s %-3s %s";
313 5 my $lead_items = 5; # 1 less than the fmt2
314  
315 4 ▶sub parse_sv {
Subroutine parse_sv: called CC=50 SCAR=72.6
316 4 my ($ind) = @_;
317  
318 4 my $p = length($done);
319 4 my $t = _chop_data_prefix(1);
320 4 $done .= $t;
321 4 my $o = ord($t);
322 4 my $bv = $o;
323 4 my $high = $o >= 128;
324 4 ▶ $o -= 128 if $high;
Branch: 1 branch
BranchTrueFalseTotal
if $high 0 4 4
325 4 ▶ printf $fmt1, $p, $p - $hlen + 1, $o, $high ? '*' : ' ', $bv, $ind;
Branch: 1 branch
BranchTrueFalseTotal
$high ? : 0 4 4
326  
327 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
328 0 printf "VARINT: %u\n", varint();
329 }
330 elsif ( $o == SRL_HDR_ZIGZAG ) {
331 0 printf "ZIGZAG: %d\n", zigzag();
332 }
333 elsif ( $o == SRL_HDR_NEG_VARINT ) {
334 0 my $magnitude = varint();
335 0 ▶ die "NEG_VARINT magnitude must be nonzero" unless $magnitude;
Branch: 1 branch
BranchTrueFalseTotal
unless $magnitude 0 0 0
336 0 printf "NEG_VARINT: -%u\n", $magnitude;
337 }
338 ▶ 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
339 0 printf "POS: %u\n", $o;
340 }
341 ▶ 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
342 0 $o = $o - 32;
343 0 printf "NEG: %i\n", $o;
344 }
345 elsif ( $o >= SRL_HDR_SHORT_BINARY_LOW ) {
346 3 $o -= SRL_HDR_SHORT_BINARY_LOW;
347 3 my $len = $o;
348 3 my $str = _chop_data_prefix($len);
349 3 $done .= $str;
350 3 printf "SHORT_BINARY(%u): '%s' (%s)\n", $len, encode_utf8($str), unpack( "H*", $str );
351 }
352 ▶ 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
353 1 my $l = varint();
354 1 my $str = _chop_data_prefix($l); # fixme UTF8
355 1 $done .= $str;
356 1 ▶ $str = decode_utf8($str) if $o == SRL_HDR_STR_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $o == 39 0 1 1
357 1 ▶ printf(
Branch: 1 branch
BranchTrueFalseTotal
$o == 39 ? : 0 1 1
358 ( $o == SRL_HDR_STR_UTF8 ? "STR_UTF8" : "BINARY" ) . "(%u): '%s' (%s)\n",
359 $l, encode_utf8($str), unpack( "H*", encode_utf8($str) ) );
360 }
361 elsif ( $o == SRL_HDR_EXTEND ) {
362 0 my $ext = ord( _chop_data_prefix(1) );
363 0 $done .= chr($ext);
364 0 ▶ if ( $ext == SRL_EXT_DECLARE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext == 1) 0 0 0
365 0 die "Declaration outside document prefix\n";
366 }
367 0 ▶ if ( $ext == SRL_EXT_UNDEF_DEADLY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext == 255) { } 0 0 0
368 0 print "UNDEF_DEADLY (undef; must not be emitted by an encoder)\n";
369 }
370 else {
371 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
372 unless $ext == SRL_EXT_VSTRING_BINARY || $ext == SRL_EXT_VSTRING_UTF8;
373  
374 # The VSTRING payload is its original spelling. The extension ID
375 # describes the encoding of the PV reconstructed from that spelling.
376 0 my $l = varint();
377 0 my $str = _chop_data_prefix($l);
378 0 $done .= $str;
379 0 ▶ $str = decode_utf8($str) if $ext == SRL_EXT_VSTRING_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $ext == 39 0 0 0
380 0 ▶ printf "VSTRING-%s spelling(%u): '%s' (%s)\n",
Branch: 1 branch
BranchTrueFalseTotal
$ext == 39 ? : 0 0 0
381 $ext == SRL_EXT_VSTRING_UTF8 ? "UTF8" : "BINARY",
382 $l, encode_utf8($str), unpack( "H*", encode_utf8($str) );
383 }
384 }
385 elsif ( $o == SRL_HDR_FLOAT ) {
386 0 printf "FLOAT(%f)\n", parse_float();
387 }
388 elsif ( $o == SRL_HDR_DOUBLE ) {
389 0 printf "DOUBLE(%f)\n", parse_double();
390 }
391 elsif ( $o == SRL_HDR_LONG_DOUBLE ) {
392 0 printf "LONG_DOUBLE(%f)\n", parse_long_double();
393 }
394 elsif ( $o == SRL_HDR_FLOAT_128 ) {
395 0 printf "FLOAT_128(%f)\n", parse_float_128();
396 }
397 elsif ( $o == SRL_HDR_REFN ) {
398 0 printf "REFN\n";
399 0 parse_sv( $ind . " " );
400 }
401 elsif ( $o == SRL_HDR_REFP ) {
402 0 my $len = varint();
403 0 printf "REFP(%u)\n", $len;
404 }
405 elsif ( $o == SRL_HDR_COPY ) {
406 0 my $len = varint();
407 0 printf "COPY(%u)\n", $len;
408 }
409 ▶ 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
410 0 printf "ARRAYREF";
411 0 parse_av( $ind, $o );
412 }
413 elsif ( $o == SRL_HDR_ARRAY ) {
414 0 printf "ARRAY";
415 0 parse_av($ind);
416 }
417 ▶ 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
418 0 printf "HASHREF";
419 0 parse_hv( $ind, $o );
420 }
421 elsif ( $o == SRL_HDR_HASH ) {
422 0 printf "HASH";
423 0 parse_hv($ind);
424 }
425 elsif ( $o == SRL_HDR_CANONICAL_UNDEF ) {
426 0 printf "CANONICAL_UNDEF\n";
427 }
428 elsif ( $o == SRL_HDR_UNDEF ) {
429 0 printf "UNDEF\n";
430 }
431 elsif ( $o == SRL_HDR_WEAKEN ) {
432 0 printf "WEAKEN\n";
433 0 parse_sv($ind);
434 }
435 elsif ( $o == SRL_HDR_PAD ) {
436 0 printf "[PAD]\n";
437 0 parse_sv($ind);
438 }
439 elsif ( $o == SRL_HDR_ALIAS ) {
440 0 my $ofs = varint();
441 0 printf "ALIAS(%u)\n", $ofs;
442 }
443 elsif ( $o == SRL_HDR_OBJECTV ) {
444 0 my $ofs = varint();
445 0 printf "OBJECTV(%d)\n", $ofs;
446 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
447 0 parse_sv( $ind . " " );
448 }
449 elsif ( $o == SRL_HDR_OBJECTV_FREEZE ) {
450 0 my $ofs = varint();
451 0 printf "OBJECTV_FREEZE(%d)\n", $ofs;
452 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
453 0 parse_sv( $ind . " " );
454 }
455 elsif ( $o == SRL_HDR_OBJECT ) {
456 0 printf "OBJECT\n";
457 0 printf "$fmt2 Class:\n", ("") x $lead_items, $ind;
458 0 parse_sv( $ind . " " );
459 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
460 0 parse_sv( $ind . " " );
461 }
462 elsif ( $o == SRL_HDR_OBJECT_FREEZE ) {
463 0 printf "OBJECT_FREEZE\n";
464 0 printf "$fmt2 Class:\n", ("") x $lead_items, $ind;
465 0 parse_sv( $ind . " " );
466 0 printf "$fmt2 Value:\n", ("") x $lead_items, $ind;
467 0 parse_sv( $ind . " " );
468 }
469 elsif ( $o == SRL_HDR_REGEXP ) {
470 0 printf "REGEXP\n";
471 0 parse_sv( $ind . " " );
472 0 parse_sv( $ind . " " );
473 }
474 elsif ( $o == SRL_HDR_FALSE ) {
475 0 printf "FALSE\n";
476 }
477 elsif ( $o == SRL_HDR_TRUE ) {
478 0 printf "TRUE\n";
479 }
480 elsif ( $o == SRL_HDR_NO ) {
481 0 printf "NO (Perl boolean false)\n";
482 }
483 elsif ( $o == SRL_HDR_YES ) {
484 0 printf "YES (Perl boolean true)\n";
485  
486 }
487 else {
488 0 printf "<UNKNOWN>\n";
489 0 die sprintf "unsupported type: 0x%02x (%d) %s: %s", $o, $o,
490 Data::Dumper::qquote($t),
491 Data::Dumper->new( [ $TAG_INFO_ARRAY[$o] ] )->Terse(1)->Dump();
492 }
493 4 return 0;
494 }
495  
496 0 ▶sub parse_av {
Subroutine parse_av: not called CC=3 SCAR=24.8
497 0 my ( $ind, $o ) = @_;
498 0 ▶ my $len = defined $o ? $o & 15 : varint();
Branch: 1 branch
BranchTrueFalseTotal
defined $o ? : 0 0 0
499 0 printf "(%u)\n", $len;
500 0 $ind .= " ";
501 0 ▶ while ( $len-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len--) 0 0 0
502 0 parse_sv( $ind, \$len );
503 }
504 }
505  
506 0 ▶sub parse_hv {
Subroutine parse_hv: not called CC=3 SCAR=24.8
507 0 my ( $ind, $o ) = @_;
508 0 ▶ my $len = ( defined $o ? $o & 15 : varint() );
Branch: 1 branch
BranchTrueFalseTotal
defined $o ? : 0 0 0
509 0 printf "(%u)\n", $len;
510 0 $ind .= " ";
511 0 ▶ while ( $len-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len--) 0 0 0
512 0 printf "$fmt2%s:\n", ("") x $lead_items, $ind, "KEY";
513 0 parse_sv( $ind . " " );
514 0 printf "$fmt2%s:\n", ("") x $lead_items, $ind, "VALUE";
515 0 parse_sv( $ind . " " );
516 }
517 }
518  
519 # super inefficient
520 18 ▶sub varint {
Subroutine varint: called CC=6 SCAR=19.9
521 18 my $uv_bits = 8 * $Config{uvsize};
522 18 my $max_bytes = int( ( $uv_bits + 6 ) / 7 );
523 18 my ( $x, $lshift ) = ( 0, 0 );
524 18 for my $index ( 0 .. $max_bytes - 1 ) {
525 21 ▶ die "premature end of varint" unless length $data;
Branch: 1 branch
BranchTrueFalseTotal
unless length $data 0 21 21
526 21 my $c = ord( _chop_data_prefix(1) );
527 21 $done .= chr($c);
528 21 my $part = $c & 0x7f;
529 21 ▶ if ( $index == $max_bytes - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $max_bytes - 1) 0 21 21
530 0 my $bits_left = $uv_bits - $lshift;
531 0 my $max_part = ( 1 << $bits_left ) - 1;
532 0 ▶ die "varint too big" if $part > $max_part;
Branch: 1 branch
BranchTrueFalseTotal
if $part > $max_part 0 0 0
533 }
534 21 $x |= $part << $lshift;
535 21 ▶ return $x unless $c & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless $c & 128 18 3 21
536 3 $lshift += 7;
537 }
538 0 die "varint too long";
539 }
540  
541 0 ▶sub _zigzag {
Subroutine _zigzag: not called CC=2 SCAR=17.9
542 0 my $n = $_[0];
543 0 ▶ return $n & 1 ? -( ( $n >> 1 ) + 1 ) : ( $n >> 1 );
Branch: 1 branch
BranchTrueFalseTotal
$n & 1 ? : 0 0 0
544 }
545  
546 0 ▶sub zigzag {
Subroutine zigzag: not called CC=1 SCAR=6.9
547 0 return _zigzag( varint() );
548 }
549  
550 5 GetOptions(
551 $opt = {}, 'e|stderr', 'checksum-v0-key=s', 'checksum-v1-key=s', 'checksum-v2-key=s',
552 'checksum-v0-key-hex=s', 'checksum-v1-key-hex=s', 'checksum-v2-key-hex=s',
553 'allow-unknown-long-double',
554 );
555  
556 5 for my $version ( 0, 1, 2 ) {
557 15 my $plain_name = "checksum_key_v" . ( $version + 1 );
558 15 my $hex_name = "checksum_key_v" . ( $version + 1 ) . "_hex";
559 15 my $plain_option = "checksum-v${version}-key";
560 15 my $hex_option = "checksum-v${version}-key-hex";
561 die "Specify only one of --$plain_option and --$hex_option\n"
562 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
563 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
564 die "--$hex_option expects an even number of hexadecimal digits\n"
565 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
566 5 $opt->{$plain_name} = pack( 'H*', $opt->{$hex_option} );
567 }
568 elsif ( defined $opt->{$plain_option} ) {
569 0 $opt->{$plain_name} = $opt->{$plain_option};
570 }
571 die "--checksum-v${version}-key must be exactly 16 bytes\n"
572 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
573 }
574  
575 5 $| = 1;
576 5 ▶if ( $opt->{e} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"e"}) 0 5 5
577 0 select(STDERR);
578 }
579  
580 5 ▶sub dump_bytes {
Subroutine dump_bytes: called CC=4 SCAR=13.9
581 5 my ($bytes) = @_;
582 5 print "Hex dump:\n";
583 5 ▶ for ( my $offset = 0 ; $offset < length($bytes) ; $offset += 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $bytes) 23 5 28
584 23 my @octets = unpack( 'C*', substr( $bytes, $offset, 16 ) );
585 23 my $hex = join ' ', map { sprintf '%02x', $_ } @octets;
586 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
587 23 printf "%08x %-47s |%s|\n", $offset, $hex, $ascii;
588 }
589 }
590  
591 5 binmode STDIN;
592 5 local $/ = undef;
593 5 $data = <STDIN>;
594  
595 5 dump_bytes($data);
596 5 print "\n\nTotal length: " . length($data) . "\n\n";
597  
598 5 ▶while ( length $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 5 4 9
599 5 parse_header();
600 4 print "--- End header\n";
601 4 parse_declarations();
602 4 $done = parse_sv("");
603 4 print "--- End Document\n";
604 4 ▶ $data = $packet_remainder if defined $packet_remainder;
Branch: 1 branch
BranchTrueFalseTotal
if defined $packet_remainder 4 0 4
605 4 $packet_remainder = undef;
606 }
↑ Summary blib/lib/Sereal/Encoder.pm »