ASN1/IO.pm

filter: Statementstmt 85.0% 119 / 140 Branchbran 68.1% 90 / 132 Conditioncond 69.5% 16 / 23 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 76.4% 247 / 323 CC 76 SCAR 36.8
CC
76
Cov
76%
asn_recv
67.9
asn_read
38.4
asn_get
12.0
CRAP 39.8
« ASN1/parser.pm ↑ Summary ASN1/_encode.pm »
1 # Copyright (c) 2000-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
2 # This program is free software; you can redistribute it and/or
3 # modify it under the same terms as Perl itself.
4  
5 package Convert::ASN1;
6 32 $Convert::ASN1::VERSION = '0.37';
7 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
11 32 local $SIG{__DIE__};
12 32 ▶ eval { require bytes } and 'bytes'->import
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require bytes } } and "bytes"->import 32 0 32
13 }
14  
15 # Largest single read, so the buffer perl allocates tracks the data
16 # received and not the length an element declares
17 32 my $MAX_READ = 65536;
18  
19 15 ▶sub asn_recv { # $socket, $buffer, $flags
Subroutine asn_recv: called CC=30 SCAR=42.2
Pod documented
20  
21 15 my $peer;
22 my $buf;
23 15 my $n = 128;
24 15 my $pos = 0;
25 15 my $depth = 0;
26 15 my $len = 0;
27 15 my($tmp,$tb,$lb);
28  
29 MORE:
30 15 ▶ for(
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) 15 0 15
31 $peer = recv($_[0],$buf,$n,MSG_PEEK);
32 defined $peer;
33 $peer = recv($_[0],$buf,$n<<=1,MSG_PEEK)
34 ) {
35  
36 15 ▶ if ($depth) { # Are we searching of "\0\0"
Branch: 1 branch
BranchTrueFalseTotal
if ($depth) 0 15 15
37  
38 0 ▶ unless (2+$pos <= length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
unless (2 + $pos <= length $buf) 0 0 0
39 0 ▶ next MORE if $n == length $buf;
Branch: 1 branch
BranchTrueFalseTotal
if $n == length $buf 0 0 0
40 0 last MORE;
41 }
42  
43 0 ▶ if(substr($buf,$pos,2) eq "\0\0") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($buf, $pos, 2) eq "\0\0") 0 0 0
44 0 ▶ unless (--$depth) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$depth) 0 0 0
45 0 $len = $pos + 2;
46 0 last MORE;
47 }
48 }
49 }
50  
51 # If we can decode a tag and length we can determine the length
52 15 ($tb,$tmp) = asn_decode_tag(substr($buf,$pos));
53 15 ▶ unless ($tb && ($pos+$tb < length $buf)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tb and $pos + $tb < length $buf) 4 11 15
Condition66%2/3
$tb and $pos + $tb < length $buf
!ll&&!rl&&r
0411
MC/DC50%1/2
$tb and $pos + $tb < length $buf
$tb $pos + $tb < length $buf
Truth table66%2/3
A and B
A= $tbB= $pos + $tb < length $buf
ABresult
0X0
100
111
54 4 ▶ next MORE if $n == length $buf;
Branch: 1 branch
BranchTrueFalseTotal
if $n == length $buf 0 4 4
55 4 last MORE;
56 }
57  
58 11 ▶ if (unpack("C",substr($buf,$pos+$tb,1)) == 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (unpack("C", substr($buf, $pos + $tb, 1)) == 128) 0 11 11
59 # indefinite length, grrr!
60 0 $depth++;
61 0 $pos += $tb + 1;
62 0 redo MORE;
63 }
64  
65 11 ($lb,$len) = asn_decode_length(substr($buf,$pos+$tb));
66  
67 11 ▶ if ($lb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lb) { } 9 2 11
68 9 ▶ if ($depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth) { } 0 9 9
69 0 $pos += $tb + $lb + $len;
70 0 redo MORE;
71 }
72 else {
73 9 $len += $tb + $lb + $pos;
74 9 last MORE;
75 }
76 }
77 else {
78 2 ▶ next MORE if $n == length $buf;
Branch: 1 branch
BranchTrueFalseTotal
if $n == length $buf 0 2 2
79 2 last MORE;
80 }
81 }
82  
83 15 ▶ if (defined $peer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) 15 0 15
84 15 my $type = getsockopt($_[0], SOL_SOCKET, SO_TYPE);
85 15 ▶ my $discard = defined $type && unpack('i', $type) == SOCK_DGRAM
Condition50%3/6
defined $type && unpack('i', $type) == 2 && !($_[2] & 2)
!ll&&!rl&&r
0114
defined $type and unpack("i", $type) == 2
!ll&&!rl&&r
0015
MC/DC33%1/3
defined $type && unpack('i', $type) == 2 && !($_[2] & 2)
defined $type unpack("i", $type) == 2 !($_[2] & 2)
Truth table50%2/4
A and B && not(C)
A= defined $typeB= unpack("i", $type) == 2C= !($_[2] & 2)
ABCresult
0XX0
10X0
1100
1111
86 && !($_[2] & MSG_PEEK);
87  
88 15 ▶ if ($len > length $buf) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len > length $buf) { } 3 12 15
elsif ($len == 0) { } 6 6 12
89 # Check we can read the whole element. Peek in doubling steps so the
90 # buffer perl allocates is bounded by the data available, not by the
91 # declared length
92 3 ▶ my $ask = length($buf) || 64;
Condition50%1/2
length $buf || 64
l!l
30
MC/DC0%0/1
length $buf || 64
length $buf
Truth table50%1/2
A
A= length $buf
Aresult
11
00
93 3 ▶ while ($len > length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $buf) 9 1 10
94 9 $ask *= 2;
95 9 ▶ $ask = $len if $ask > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $ask > $len 1 8 9
96 goto error
97 9 ▶ unless defined($peer = recv($_[0],$buf,$ask,MSG_PEEK));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($peer = recv($_[0], $buf, $ask, 2)) 0 9 9
98 9 ▶ last if length($buf) < $ask;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < $ask 2 7 9
99 }
100  
101 3 ▶ if ($len > length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $buf) 2 1 3
102 # Cannot get whole element; discard the datagram so it doesn't wedge the queue
103 2 ▶ recv($_[0], my $junk, length $buf, 0) if $discard;
Branch: 1 branch
BranchTrueFalseTotal
if $discard 2 0 2
104 2 $_[1]='';
105 2 return $peer;
106 }
107 }
108 elsif ($len == 0) {
109 # Discard the datagram so it doesn't wedge the queue
110 6 ▶ recv($_[0], my $junk, length($buf) || 1, 0) if $discard;
Branch: 1 branch
BranchTrueFalseTotal
if $discard 5 1 6
Condition50%1/2
length $buf || 1
l!l
50
MC/DC0%0/1
length $buf || 1
length $buf
Truth table50%1/2
A
A= length $buf
Aresult
11
00
111 6 $_[1] = '';
112 6 return $peer;
113 }
114  
115 7 ▶ if ($_[2] & MSG_PEEK) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[2] & 2) { } 0 7 7
elsif (not defined($peer = recv($_[0], $_[1], $len, 0))) { } 0 7 7
116 0 $_[1] = substr($buf,0,$len);
117 }
118 elsif (!defined($peer = recv($_[0],$_[1],$len,0))) {
119 0 goto error;
120 }
121  
122 7 return $peer;
123 }
124  
125 error:
126 0 $_[1] = undef;
127 }
128  
129 5 ▶sub asn_read { # $fh, $buffer, $offset
Subroutine asn_read: called CC=29 SCAR=36.5
Pod documented
130  
131 # We need to read one packet, and exactly only one packet.
132 # So we have to read the first few bytes one at a time, until
133 # we have enough to decode a tag and a length. We then know
134 # how many more bytes to read
135  
136 5 ▶ if ($_[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 5 5
137 0 ▶ if ($_[2] > length $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] > length $_[1]) 0 0 0
138 0 require Carp;
139 0 Carp::carp("Offset beyond end of buffer");
140 0 return;
141 }
142 0 substr($_[1],$_[2]) = '';
143 }
144 else {
145 5 $_[1] = '';
146 }
147  
148 5 my $pos = 0;
149 5 my $need = 0;
150 5 my $depth = 0;
151 5 my $ch;
152 my $n;
153 5 my $e;
154
155  
156 5 while(1) {
157 269 ▶ $need = ($pos + ($depth * 2)) || 2;
Condition100%2/2
$pos + $depth * 2 || 2
l!l
2645
MC/DC100%1/1
$pos + $depth * 2 || 2
$pos + $depth * 2
Truth table100%2/2
A
A= $pos + $depth * 2
Aresult
11
00
158  
159 269 ▶ while(($n = $need - length $_[1]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($n = $need - length($_[1])) > 0) 199 269 468
160 199 ▶ $e = sysread($_[0],$_[1],($n > $MAX_READ ? $MAX_READ : $n),length $_[1]) or
Branch: 2 branches
BranchTrueFalseTotal
$e = sysread($_[0], $_[1], $n > $MAX_READ ? $MAX_READ : $n, length $_[1]) or goto READ_ERR 199 0 199
$n > $MAX_READ ? : 0 199 199
161 goto READ_ERR;
162 }
163  
164 269 my $tch = unpack("C",substr($_[1],$pos++,1));
165 # Tag may be multi-byte
166 269 ▶ if(($tch & 0x1f) == 0x1f) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tch & 31) == 31) 175 94 269
167 175 my $ch;
168 175 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$ch & 128 and do { do { ++$need; while (($n = $need - length($_[1])) > 0) { $e = sysread($_[0], $_[1], $n > $MAX_READ ? $MAX_READ : $n, length $_[1]) or goto READ_ERR; } ; $ch = unpack("C", substr($_[1], $pos++, 1)) } } 98 175 273
169 273 $need++;
170 273 ▶ while(($n = $need - length $_[1]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($n = $need - length($_[1])) > 0) 273 273 546
171 273 ▶ $e = sysread($_[0],$_[1],($n > $MAX_READ ? $MAX_READ : $n),length $_[1]) or
Branch: 2 branches
BranchTrueFalseTotal
$e = sysread($_[0], $_[1], $n > $MAX_READ ? $MAX_READ : $n, length $_[1]) or goto READ_ERR 273 0 273
$n > $MAX_READ ? : 0 273 273
172 goto READ_ERR;
173 }
174 273 $ch = unpack("C",substr($_[1],$pos++,1));
175 } while($ch & 0x80);
176 }
177  
178 269 $need = $pos + 1;
179  
180 269 ▶ while(($n = $need - length $_[1]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($n = $need - length($_[1])) > 0) 0 269 269
181 0 ▶ $e = sysread($_[0],$_[1],($n > $MAX_READ ? $MAX_READ : $n),length $_[1]) or
Branch: 2 branches
BranchTrueFalseTotal
$e = sysread($_[0], $_[1], $n > $MAX_READ ? $MAX_READ : $n, length $_[1]) or goto READ_ERR 0 0 0
$n > $MAX_READ ? : 0 0 0
182 goto READ_ERR;
183 }
184  
185 269 my $len = unpack("C",substr($_[1],$pos++,1));
186  
187 269 ▶ if($len & 0x80) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len & 128) { } 73 196 269
elsif (not $len || $tch) { } 71 125 196
188 73 ▶ unless ($len &= 0x7f) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len &= 127) 71 2 73
189 71 $depth++;
190 71 next;
191 }
192 2 $need = $pos + $len;
193  
194 2 ▶ while(($n = $need - length $_[1]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($n = $need - length($_[1])) > 0) 2 2 4
195 2 ▶ $e = sysread($_[0],$_[1],($n > $MAX_READ ? $MAX_READ : $n),length $_[1]) or
Branch: 2 branches
BranchTrueFalseTotal
$e = sysread($_[0], $_[1], $n > $MAX_READ ? $MAX_READ : $n, length $_[1]) or goto READ_ERR 2 0 2
$n > $MAX_READ ? : 0 2 2
196 goto READ_ERR;
197 }
198  
199 2 $pos += $len + unpack("N", "\0" x (4 - $len) . substr($_[1],$pos,$len));
200 }
201 ▶ elsif (!$len && !$tch) {
Condition66%2/3
$len || $tch
l!l&&r!l&&!r
125071
MC/DC50%1/2
$len || $tch
$len $tch
Truth table66%2/3
A || B
A= $lenB= $tch
ABresult
1X1
011
000
202 71 ▶ die "Bad ASN PDU" unless $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless $depth 0 71 71
203 71 ▶ unless (--$depth) {
Branch: 1 branch
BranchTrueFalseTotal
unless (--$depth) 1 70 71
204 1 last;
205 }
206 }
207 else {
208 125 $pos += $len;
209 }
210  
211 197 ▶ last unless $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless $depth 4 193 197
212 }
213  
214 5 ▶ while(($n = $pos - length $_[1]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($n = $pos - length($_[1])) > 0) 8 4 12
215 8 ▶ $e = sysread($_[0],$_[1],($n > $MAX_READ ? $MAX_READ : $n),length $_[1]) or
Branch: 2 branches
BranchTrueFalseTotal
$e = sysread($_[0], $_[1], $n > $MAX_READ ? $MAX_READ : $n, length $_[1]) or goto READ_ERR 7 1 8
$n > $MAX_READ ? : 5 3 8
216 goto READ_ERR;
217 }
218  
219 4 return length $_[1];
220  
221 1 ▶READ_ERR:
Branch: 1 branch
BranchTrueFalseTotal
defined $e ? : 1 0 1
222 $@ = defined($e) ? "Unexpected EOF" : "I/O Error $!"; # . CORE::unpack("H*",$_[1]);
223 1 return undef;
224 }
225  
226 1 ▶sub asn_send { # $sock, $buffer, $flags, $to
Subroutine asn_send: called CC=2 SCAR=7.6
Pod documented
227  
228 1 ▶ @_ == 4
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 ? : 1 0 1
229 ? send($_[0],$_[1],$_[2],$_[3])
230 : send($_[0],$_[1],$_[2]);
231 }
232  
233 2 ▶sub asn_write { # $sock, $buffer
Subroutine asn_write: called CC=1 SCAR=0.0
Pod documented
234  
235 2 syswrite($_[0],$_[1], length $_[1]);
236 }
237  
238 6 ▶sub asn_get { # $fh
Subroutine asn_get: called CC=12 SCAR=24.9
Pod documented
239  
240 6 ▶ my $fh = ref($_[0]) ? $_[0] : \($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 2 6
241 6 my $href = \%{*$fh};
242  
243 6 ▶ $href->{'asn_buffer'} = '' unless exists $href->{'asn_buffer'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $href->{"asn_buffer"} 3 3 6
244  
245 6 ▶ my $need = delete $href->{'asn_need'} || 0;
Condition100%2/2
delete $href->{'asn_need'} || 0
l!l
15
MC/DC100%1/1
delete $href->{'asn_need'} || 0
delete $href->{'asn_need'}
Truth table100%2/2
A
A= delete $href->{'asn_need'}
Aresult
11
00
246 6 while(1) {
247 13 ▶ next if $need;
Branch: 1 branch
BranchTrueFalseTotal
if $need 5 8 13
248 8 ▶ my($tb,$tag) = asn_decode_tag($href->{'asn_buffer'}) or next;
Branch: 1 branch
BranchTrueFalseTotal
() = asn_decode_tag($href->{"asn_buffer"}) or next 4 4 8
249 4 ▶ my($lb,$len) = asn_decode_length(substr($href->{'asn_buffer'},$tb,8)) or next;
Branch: 1 branch
BranchTrueFalseTotal
() = asn_decode_length(substr($href->{"asn_buffer"}, $tb, 8)) or next 4 0 4
250 4 $need = $tb + $lb + $len;
251 }
252 continue {
253 13 ▶ if ($need && $need <= length $href->{'asn_buffer'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need and $need <= length $href->{"asn_buffer"}) 4 9 13
Condition100%3/3
$need and $need <= length $href->{"asn_buffer"}
!ll&&!rl&&r
454
MC/DC100%2/2
$need and $need <= length $href->{"asn_buffer"}
$need $need <= length $href->{"asn_buffer"}
Truth table100%3/3
A and B
A= $needB= $need <= length $href->{"asn_buffer"}
ABresult
0X0
100
111
254 4 my $ret = substr($href->{'asn_buffer'},0,$need);
255 4 substr($href->{'asn_buffer'},0,$need) = '';
256 4 return $ret;
257 }
258  
259 9 ▶ my $get = $need > 1024 ? $need : 1024;
Branch: 1 branch
BranchTrueFalseTotal
$need > 1024 ? : 5 4 9
260 9 ▶ $get = $MAX_READ if $get > $MAX_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $get > $MAX_READ 5 4 9
261  
262 9 ▶ sysread($_[0], $href->{'asn_buffer'}, $get, length $href->{'asn_buffer'})
Branch: 1 branch
BranchTrueFalseTotal
sysread $_[0], $href->{"asn_buffer"}, $get, length $href->{"asn_buffer"} or return undef 7 2 9
263 or return undef;
264 }
265 }
266  
267 2 ▶sub asn_ready { # $fh
Subroutine asn_ready: called CC=6 SCAR=18.5
Pod documented
268  
269 2 ▶ my $fh = ref($_[0]) ? $_[0] : \($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
270 2 my $href = \%{*$fh};
271  
272 2 ▶ return 0 unless exists $href->{'asn_buffer'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $href->{"asn_buffer"} 0 2 2
273
274 return $href->{'asn_need'} <= length $href->{'asn_buffer'}
275 2 ▶ if exists $href->{'asn_need'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $href->{"asn_need"} 0 2 2
276  
277 2 ▶ my($tb,$tag) = asn_decode_tag($href->{'asn_buffer'}) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = asn_decode_tag($href->{"asn_buffer"}) or return 0 1 1 2
278 1 ▶ my($lb,$len) = asn_decode_length(substr($href->{'asn_buffer'},$tb,8)) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = asn_decode_length(substr($href->{"asn_buffer"}, $tb, 8)) or return 0 1 0 1
279  
280 1 $href->{'asn_need'} = $tb + $lb + $len;
281  
282 1 $href->{'asn_need'} <= length $href->{'asn_buffer'};
283 }
284  
285 32 1;
« ASN1/parser.pm ↑ Summary ASN1/_encode.pm »