lib/SMB/Packer.pm

filter: Statementstmt 92.3% 84 / 91 Branchbran 87.5% 14 / 16 Conditioncond 48.1% 13 / 27 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 86.2% 25 / 29 Podpod 96.0% 24 / 25 totaltotal 79.7% 161 / 202 SCAR 32.0
CC
22
Cov
83%
diff
12.0
stub
6.4
fill
5.1
CRAP 24.4
« lib/SMB/OpenFile.pm ↑ Summary lib/SMB/Header.pm »
1 # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 package SMB::Packer;
17  
18 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Encode 'encode';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub new ($$) {
25 3 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
26  
27 3 my $self = bless {}, $class;
28  
29 3 return $self->reset;
30 }
31  
32 sub reset ($) {
33 14 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
34  
35 14 $self->{data} = '';
36 14 $self->{offset} = 0;
37 14 $self->{marks} = {};
38 14 $self->{stubs} = {};
39  
40 14 return $self;
41 }
42  
43 10 ▶sub data { $_[0]->{data} }
Subroutine data: called CC=1 SCAR=0.0
Pod documented
44 23 ▶sub size { length($_[0]->{data}) }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
45 19 ▶sub offset { $_[0]->{offset} }
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
46  
47 sub zero ($$) {
48 1 ▶ my $self = shift;
Subroutine zero: called CC=2 SCAR=7.0
Pod documented
49 1 ▶ my $n_bytes = shift // die;
Condition50%1/2
shift() // die()
l!l
10
MC/DC0%0/1
shift() // die()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
50  
51 1 substr($self->{data}, $self->{offset}, $n_bytes) = "\0" x $n_bytes;
52  
53 1 return $self->skip($n_bytes);
54 }
55  
56 sub skip ($$) {
57 2 ▶ my $self = shift;
Subroutine skip: called CC=4 SCAR=13.9
Pod documented
58 2 ▶ my $n_bytes = shift // die;
Condition50%1/2
shift() // die()
l!l
20
MC/DC0%0/1
shift() // die()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
59  
60 my $n_avail = $self->{offset} + $n_bytes > $self->size
61 2 ▶ ? $self->size - $self->{offset} : $n_bytes;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'offset'} + $n_bytes > $self->size ? : 1 1 2
62  
63 2 ▶ $self->zero($n_bytes - $n_avail) if $n_avail < $n_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $n_avail < $n_bytes 1 1 2
64  
65 2 $self->{offset} += $n_avail;
66  
67 2 return $self;
68 }
69  
70 sub stub ($$$) {
71 3 ▶ my $self = shift;
Subroutine stub: called CC=6 SCAR=18.6
Pod documented
72 3 ▶ my $name = shift // '';
Condition50%1/2
shift() // ""
l!l
30
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
73 3 ▶ my $type = shift // '';
Condition50%1/2
shift() // ""
l!l
30
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
74  
75 3 ▶ die "type must be either size of bytes or uint{8,16{,_be},32{,_be},64}"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^uint(8|16(_be)?|32(_be)?|64)$/ or $type =~ /^\d+$/ 0 3 3
Condition66%2/3
$type =~ /^uint(8|16(_be)?|32(_be)?|64)$/ or $type =~ /^\d+$/
l!l&&r!l&&!r
210
MC/DC0%0/2
$type =~ /^uint(8|16(_be)?|32(_be)?|64)$/ or $type =~ /^\d+$/
$type =~ /^uint(8|16(_be)?|32(_be)?|64)$/ $type =~ /^\d+$/
Truth table66%2/3
A or B
A= $type =~ /^uint(8|16(_be)?|32(_be)?|64)$/B= $type =~ /^\d+$/
ABresult
1X1
011
000
76 unless $type =~ /^uint(8|16(_be)?|32(_be)?|64)$/ || $type =~ /^\d+$/;
77 3 $self->{stubs}{$name} = [ $self->{offset}, $type ];
78  
79 3 ▶ $type =~ /^\d+$/
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^\d+$/ ? : 1 2 3
80 ? $self->bytes("\0" x $type)
81 : $self->$type(0);
82  
83 3 return $self;
84 }
85  
86 sub fill ($$) {
87 3 ▶ my $self = shift;
Subroutine fill: called CC=5 SCAR=16.3
Pod documented
88 3 ▶ my $name = shift // '';
Condition50%1/2
shift() // ""
l!l
30
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
89 3 ▶ my $data = shift // die;
Condition50%1/2
shift() // die()
l!l
30
MC/DC0%0/1
shift() // die()
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
90  
91 3 my $curr_offset = $self->{offset};
92 3 ▶ my ($offset, $type) = @{$self->{stubs}{$name} || die "No previously set stub '$name'"};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"stubs"}{$name} or die "No previously set stub '${name}'" 0 3 3
93 3 $self->{offset} = $offset;
94 3 ▶ $type =~ /^\d+$/
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^\d+$/ ? : 1 2 3
95 ? $self->bytes($data)
96 : $self->$type($data);
97 3 $self->{offset} = $curr_offset;
98  
99 3 return $self;
100 }
101  
102 sub mark ($$) {
103 1 ▶ my $self = shift;
Subroutine mark: called CC=2 SCAR=7.0
Pod documented
104 1 ▶ my $name = shift // '';
Condition50%1/2
shift() // ""
l!l
10
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
105  
106 1 $self->{marks}{$name} = $self->{offset};
107  
108 1 return $self;
109 }
110  
111 sub jump ($$) {
112 1 ▶ my $self = shift;
Subroutine jump: called CC=3 SCAR=11.4
Pod documented
113 1 ▶ my $name = shift // '';
Condition50%1/2
shift() // ""
l!l
10
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
114  
115 1 ▶ $self->{offset} = $self->{marks}{$name} || 0;
Condition50%1/2
$self->{'marks'}{$name} || 0
l!l
10
MC/DC0%0/1
$self->{'marks'}{$name} || 0
$self->{'marks'}{$name}
Truth table50%1/2
A
A= $self->{'marks'}{$name}
Aresult
11
00
116  
117 1 return $self;
118 }
119  
120 sub diff ($$) {
121 0 ▶ my $self = shift;
Subroutine diff: not called CC=3 SCAR=24.8
Pod documented
122 0 ▶ my $name = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
123  
124 0 ▶ return $self->{offset} - ($self->{marks}{$name} || 0);
Condition0%0/2
$self->{'marks'}{$name} || 0
l!l
00
MC/DC0%0/1
$self->{'marks'}{$name} || 0
$self->{'marks'}{$name}
Truth table0%0/2
A
A= $self->{'marks'}{$name}
Aresult
11
00
125 }
126  
127 3 my %UINT_MODS = (
128 +1 => 'C',
129 +2 => 'v',
130 +4 => 'V',
131 -1 => 'C',
132 -2 => 'n',
133 -4 => 'N',
134 );
135  
136 sub uint ($$$$) {
137 103 ▶ my $self = shift;
Subroutine uint: called CC=2 SCAR=6.9
Pod undocumented
138 103 my $n_bytes = shift;
139 103 ▶ my $be_factor = shift() ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 9 94 103
140 103 my $i = shift;
141  
142 103 return $self->bytes(pack($UINT_MODS{$be_factor * $n_bytes}, $i));
143 }
144  
145 sub str ($$;$) {
146 15 ▶ my $self = shift;
Subroutine str: called CC=2 SCAR=6.9
Pod documented
147 15 my $str = shift;
148 15 ▶ my $enc = shift || 'UTF-16LE';
Condition100%2/2
shift() || 'UTF-16LE'
l!l
114
MC/DC100%1/1
shift() || 'UTF-16LE'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
149  
150 15 return $self->bytes(encode($enc, $str));
151 }
152  
153 sub bytes ($$) {
154 136 ▶ my $self = shift;
Subroutine bytes: called CC=2 SCAR=6.9
Pod documented
155 136 my $data = shift;
156  
157 136 ▶ $data = join('', @$data) if ref($data) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "ARRAY" 1 135 136
158  
159 136 substr($self->{data}, $self->{offset}, length($data)) = $data;
160 136 $self->{offset} += length($data);
161  
162 136 return $self;
163 }
164  
165 14 ▶sub uint8 { uint($_[0], 1, 0, $_[1]); }
Subroutine uint8: called CC=1 SCAR=0.0
Pod documented
166 52 ▶sub uint16 { uint($_[0], 2, 0, $_[1]); }
Subroutine uint16: called CC=1 SCAR=0.0
Pod documented
167 28 ▶sub uint32 { uint($_[0], 4, 0, $_[1]); }
Subroutine uint32: called CC=1 SCAR=0.0
Pod documented
168 6 ▶sub uint16_be { uint($_[0], 2, 1, $_[1]); }
Subroutine uint16_be: called CC=1 SCAR=0.0
Pod documented
169 3 ▶sub uint32_be { uint($_[0], 4, 1, $_[1]); }
Subroutine uint32_be: called CC=1 SCAR=0.0
Pod documented
170 3 ▶sub uint64 { uint32($_[0], $_[1] & 0xffffffff); uint32($_[0], $_[1] >> 32); }
Subroutine uint64: called CC=1 SCAR=0.0
Pod documented
171 1 ▶sub utf16 { str($_[0], $_[1]); }
Subroutine utf16: called CC=1 SCAR=0.0
Pod documented
172 0 ▶sub utf16_be { str($_[0], $_[1], 'UTF-16BE'); }
Subroutine utf16_be: not called CC=1 SCAR=6.9
Pod documented
173 0 ▶sub fid1 { uint16($_[0], $_[1]); }
Subroutine fid1: not called CC=1 SCAR=6.9
Pod documented
174 0 ▶sub fid2 { uint64($_[0], $_[1][0]); uint64($_[0], $_[1][1]); }
Subroutine fid2: not called CC=1 SCAR=6.9
Pod documented
175  
176 3 1;
177  
178 __END__
« lib/SMB/OpenFile.pm ↑ Summary lib/SMB/Header.pm »