OpenPGP/PacketFactory.pm

filter: Statementstmt 87.8% 137 / 156 Branchbran 67.1% 47 / 70 Conditioncond 61.9% 13 / 21 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 100% 20 / 20 Podpod 100% 2 / 2 totaltotal 78.8% 223 / 283 CC 45 SCAR 27.9
CC
45
Cov
80%
save
34.3
parse
15.2
_parse_new_len_header
11.1
CRAP 16.3
« OpenPGP/Certificate.pm ↑ Summary OpenPGP/KeyRing.pm »
1 package Crypt::OpenPGP::PacketFactory;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 our $VERSION = '1.21'; # VERSION
6  
7 13 ▶use Crypt::OpenPGP::Constants qw( :packet );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use parent qw( Crypt::OpenPGP::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 our %PACKET_TYPES = (
11 PGP_PKT_PUBKEY_ENC() => { class => 'Crypt::OpenPGP::SessionKey' },
12 PGP_PKT_SIGNATURE() => { class => 'Crypt::OpenPGP::Signature' },
13 PGP_PKT_SYMKEY_ENC() => { class => 'Crypt::OpenPGP::SKSessionKey' },
14 PGP_PKT_ONEPASS_SIG() => { class => 'Crypt::OpenPGP::OnePassSig' },
15 PGP_PKT_SECRET_KEY() => { class => 'Crypt::OpenPGP::Certificate',
16 args => [ 1, 0 ] },
17 PGP_PKT_PUBLIC_KEY() => { class => 'Crypt::OpenPGP::Certificate',
18 args => [ 0, 0 ] },
19 PGP_PKT_SECRET_SUBKEY() => { class => 'Crypt::OpenPGP::Certificate',
20 args => [ 1, 1 ] },
21 PGP_PKT_USER_ID() => { class => 'Crypt::OpenPGP::UserID' },
22 PGP_PKT_USER_ATTRIBUTE() => { class => 'Crypt::OpenPGP::UserAttribute' },
23 PGP_PKT_PUBLIC_SUBKEY() => { class => 'Crypt::OpenPGP::Certificate',
24 args => [ 0, 1 ] },
25 PGP_PKT_COMPRESSED() => { class => 'Crypt::OpenPGP::Compressed' },
26 PGP_PKT_ENCRYPTED() => { class => 'Crypt::OpenPGP::Ciphertext' },
27 PGP_PKT_MARKER() => { class => 'Crypt::OpenPGP::Marker' },
28 PGP_PKT_PLAINTEXT() => { class => 'Crypt::OpenPGP::Plaintext' },
29 PGP_PKT_RING_TRUST() => { class => 'Crypt::OpenPGP::Trust' },
30 PGP_PKT_ENCRYPTED_MDC() => { class => 'Crypt::OpenPGP::Ciphertext',
31 args => [ 1 ] },
32 PGP_PKT_MDC() => { class => 'Crypt::OpenPGP::MDC' },
33 );
34  
35 13 our %PACKET_TYPES_BY_CLASS = map { $PACKET_TYPES{$_}{class} => $_ } keys %PACKET_TYPES;
36  
37 60 ▶sub parse {
Subroutine parse: called CC=15 SCAR=27.2
Pod documented
38 60 my $class = shift;
39 60 my($buf, $find, $parse) = @_;
40 60 ▶ return unless $buf && $buf->offset < $buf->length;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf and $buf->offset < $buf->length 14 46 60
Condition66%2/3
$buf and $buf->offset < $buf->length
!ll&&!rl&&r
01446
MC/DC50%1/2
$buf and $buf->offset < $buf->length
$buf $buf->offset < $buf->length
Truth table66%2/3
A and B
A= $bufB= $buf->offset < $buf->length
ABresult
0X0
100
111
41 46 my(%find, %parse);
42 46 ▶ if ($find) {
Branch: 1 branch
BranchTrueFalseTotal
if ($find) { } 14 32 46
43 14 ▶ %find = ref($find) eq 'ARRAY' ? (map { $_ => 1 } @$find) :
Branch: 1 branch
BranchTrueFalseTotal
ref $find eq 'ARRAY' ? : 14 0 14
44 ($find => 1);
45 }
46 else {
47 32 %find = map { $_ => 1 } keys %PACKET_TYPES;
48 }
49 46 ▶ if ($parse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse) { } 10 36 46
50 10 ▶ %parse = ref($parse) eq 'ARRAY' ? (map { $_ => 1 } @$parse) :
Branch: 1 branch
BranchTrueFalseTotal
ref $parse eq 'ARRAY' ? : 10 0 10
51 ($parse => 1);
52 }
53 else {
54 36 %parse = %find;
55 }
56  
57 46 my($type, $len, $partial, $hdrlen, $b);
58 do {
59 54 ($type, $len, $partial, $hdrlen) = $class->_parse_header($buf);
60 54 ▶ $b = $buf->extract($len ? $len : $buf->length - $buf->offset);
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 52 2 54
61 54 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 53 54
62 46 ▶ } while !$find{$type}; ## Skip
Branch: 1 branch
BranchTrueFalseTotal
$find{$type} or do { do { () = $class->_parse_header($buf); $b = $buf->extract($len ? $len : $buf->length - $buf->offset); return unless $type } } 45 8 53
63  
64 45 ▶ while ($partial) {
Branch: 1 branch
BranchTrueFalseTotal
if ($partial) 0 45 45
65 0 my $off = $buf->offset;
66 0 (my($nlen), $partial) = $class->_parse_new_len_header($buf);
67 0 $len += $nlen + ($buf->offset - $off);
68 0 $b->append( $buf->get_bytes($nlen) );
69 }
70  
71 45 my $obj;
72 45 ▶ if ($parse{$type} && (my $ref = $PACKET_TYPES{$type})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse{$type} and my $ref = $Crypt::OpenPGP::PacketFactory::PACKET_TYPES{$type}) { } 43 2 45
Condition66%2/3
$parse{$type} and my $ref = $Crypt::OpenPGP::PacketFactory::PACKET_TYPES{$type}
!ll&&!rl&&r
2043
MC/DC50%1/2
$parse{$type} and my $ref = $Crypt::OpenPGP::PacketFactory::PACKET_TYPES{$type}
$parse{$type} my $ref = $Crypt::OpenPGP::PacketFactory::PACKET_TYPES{$type}
Truth table66%2/3
A and B
A= $parse{$type}B= my $ref = $Crypt::OpenPGP::PacketFactory::PACKET_TYPES{$type}
ABresult
0X0
100
111
73 43 my $pkt_class = $ref->{class};
74 43 ▶ my @args = $ref->{args} ? @{ $ref->{args} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$ref->{'args'} ? : 12 31 43
75 43 ▶ eval "use $pkt_class;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
76 43 ▶ return $class->error("Loading $pkt_class failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 43 43
77 43 $obj = $pkt_class->parse($b, @args);
78 }
79 else {
80 2 $obj = { type => $type, length => $len,
81 __pkt_len => $len + $hdrlen, __unparsed => 1 };
82 }
83 43 $obj;
84 }
85  
86 54 ▶sub _parse_header {
Subroutine _parse_header: called CC=7 SCAR=19.5
87 54 my $class = shift;
88 54 my($buf) = @_;
89 54 ▶ return unless $buf && $buf->offset < $buf->length;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf and $buf->offset < $buf->length 1 53 54
Condition66%2/3
$buf and $buf->offset < $buf->length
!ll&&!rl&&r
0153
MC/DC50%1/2
$buf and $buf->offset < $buf->length
$buf $buf->offset < $buf->length
Truth table66%2/3
A and B
A= $bufB= $buf->offset < $buf->length
ABresult
0X0
100
111
90  
91 53 my $off_start = $buf->offset;
92 53 my $tag = $buf->get_int8;
93 53 ▶ return $class->error("Parse error: bit 7 not set!")
Branch: 1 branch
BranchTrueFalseTotal
unless $tag & 128 0 53 53
94 unless $tag & 0x80;
95 53 my $is_new = $tag & 0x40;
96 53 my($type, $len, $partial);
97 53 ▶ if ($is_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new) { } 2 51 53
98 2 $type = $tag & 0x3f;
99 2 ($len, $partial) = $class->_parse_new_len_header($buf);
100 }
101 else {
102 51 $type = ($tag>>2)&0xf;
103 51 ▶ my $lenbytes = (($tag&3)==3) ? 0 : (1<<($tag & 3));
Branch: 1 branch
BranchTrueFalseTotal
($tag & 3) == 3 ? : 1 50 51
104 51 $len = 0;
105 51 for (1..$lenbytes) {
106 77 $len <<= 8;
107 77 $len += $buf->get_int8;
108 }
109 }
110 53 ($type, $len, $partial, $buf->offset - $off_start);
111 }
112  
113 2 ▶sub _parse_new_len_header {
Subroutine _parse_new_len_header: called CC=6 SCAR=24.1
114 2 my $class = shift;
115 2 my($buf) = @_;
116 2 ▶ return unless $buf && $buf->offset < $buf->length;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf and $buf->offset < $buf->length 0 2 2
Condition33%1/3
$buf and $buf->offset < $buf->length
!ll&&!rl&&r
002
MC/DC0%0/2
$buf and $buf->offset < $buf->length
$buf $buf->offset < $buf->length
Truth table33%1/3
A and B
A= $bufB= $buf->offset < $buf->length
ABresult
0X0
100
111
117 2 my $lb1 = $buf->get_int8;
118 2 my($partial, $len);
119 2 ▶ if ($lb1 <= 191) {
Branch: 3 branches
BranchTrueFalseTotal
if ($lb1 <= 191) { } 2 0 2
elsif ($lb1 <= 223) { } 0 0 0
elsif ($lb1 < 255) { } 0 0 0
120 2 $len = $lb1;
121 } elsif ($lb1 <= 223) {
122 0 $len = (($lb1-192) << 8) + $buf->get_int8 + 192;
123 } elsif ($lb1 < 255) {
124 0 $partial++;
125 0 $len = 1 << ($lb1 & 0x1f);
126 } else {
127 0 $len = $buf->get_int32;
128 }
129 2 ($len, $partial);
130 }
131  
132 13 ▶sub save {
Subroutine save: called CC=20 SCAR=35.3
Pod documented
133 13 my $class = shift;
134 13 my @objs = @_;
135 13 my $ser = '';
136 13 for my $obj (@objs) {
137 19 my $body = $obj->save;
138 19 my $len = length($body);
139 my $type = $obj->can('pkt_type') ? $obj->pkt_type :
140 19 ▶ $PACKET_TYPES_BY_CLASS{ref($obj)};
Branch: 1 branch
BranchTrueFalseTotal
$obj->can('pkt_type') ? : 4 15 19
141 19 ▶ my $hdrlen = $obj->can('pkt_hdrlen') ? $obj->pkt_hdrlen : undef;
Branch: 1 branch
BranchTrueFalseTotal
$obj->can('pkt_hdrlen') ? : 6 13 19
142 19 my $buf = Crypt::OpenPGP::Buffer->new;
143 19 ▶ if ($obj->{is_new} || $type > 15) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'is_new'} or $type > 15) { } 1 18 19
Condition66%2/3
$obj->{"is_new"} or $type > 15
l!l&&r!l&&!r
0118
MC/DC50%1/2
$obj->{"is_new"} or $type > 15
$obj->{"is_new"} $type > 15
Truth table66%2/3
A or B
A= $obj->{"is_new"}B= $type > 15
ABresult
1X1
011
000
144 1 my $tag = 0xc0 | ($type & 0x3f);
145 1 $buf->put_int8($tag);
146 1 ▶ return $class->error("Can't write partial length packets")
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 1 1
147 unless $len;
148 1 ▶ if ($len < 192) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len < 192) { } 1 0 1
elsif ($len < 8384) { } 0 0 0
149 1 $buf->put_int8($len);
150 } elsif ($len < 8384) {
151 0 $len -= 192;
152 0 $buf->put_int8(int($len / 256) + 192);
153 0 $buf->put_int8($len % 256);
154 } else {
155 0 $buf->put_int8(0xff);
156 0 $buf->put_int32($len);
157 }
158 }
159 else {
160 18 ▶ unless ($hdrlen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hdrlen) 12 6 18
161 12 ▶ if (!defined $len) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $len) { } 0 12 12
elsif ($len < 256) { } 12 0 12
elsif ($len < 65536) { } 0 0 0
162 0 $hdrlen = 0;
163 } elsif ($len < 256) {
164 12 $hdrlen = 1;
165 } elsif ($len < 65536) {
166 0 $hdrlen = 2;
167 } else {
168 0 $hdrlen = 4;
169 }
170 }
171 18 ▶ return $class->error("Packet overflow: overflow preset len")
Branch: 1 branch
BranchTrueFalseTotal
if $hdrlen == 1 and $len > 255 0 18 18
Condition66%2/3
$hdrlen == 1 and $len > 255
!ll&&!rl&&r
6120
MC/DC0%0/2
$hdrlen == 1 and $len > 255
$hdrlen == 1 $len > 255
Truth table66%2/3
A and B
A= $hdrlen == 1B= $len > 255
ABresult
0X0
100
111
172 if $hdrlen == 1 && $len > 255;
173 18 ▶ $hdrlen = 4 if $hdrlen == 2 && $len > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $hdrlen == 2 and $len > 65535 0 18 18
Condition66%2/3
$hdrlen == 2 and $len > 65535
!ll&&!rl&&r
1260
MC/DC0%0/2
$hdrlen == 2 and $len > 65535
$hdrlen == 2 $len > 65535
Truth table66%2/3
A and B
A= $hdrlen == 2B= $len > 65535
ABresult
0X0
100
111
174 18 my $tag = 0x80 | ($type << 2);
175 18 ▶ if ($hdrlen == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($hdrlen == 0) { } 0 18 18
elsif ($hdrlen == 1) { } 12 6 18
elsif ($hdrlen == 2) { } 6 0 6
176 0 $buf->put_int8($tag | 3);
177 } elsif ($hdrlen == 1) {
178 12 $buf->put_int8($tag);
179 12 $buf->put_int8($len);
180 } elsif ($hdrlen == 2) {
181 6 $buf->put_int8($tag | 1);
182 6 $buf->put_int16($len);
183 } else {
184 0 $buf->put_int8($tag | 2);
185 0 $buf->put_int32($len);
186 }
187 }
188 19 $buf->put_bytes($body);
189 19 $ser .= $buf->bytes;
190 }
191 13 $ser;
192 }
193  
194 13 1;
195 __END__
« OpenPGP/Certificate.pm ↑ Summary OpenPGP/KeyRing.pm »