lib/SMB.pm

filter: Statementstmt 30.8% 49 / 159 Branchbran 13.0% 12 / 92 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 58.8% 10 / 17 Podpod 85.7% 6 / 7 totaltotal 24.4% 77 / 315 SCAR 74.8
CC
60
Cov
22%
_dump_value
1056.0
mem
240.0
parse_share_uri
20.0
CRAP 1767.0
« blib/lib/SMB/File.pm ↑ Summary blib/lib/SMB/Server.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;
17  
18 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 our $VERSION = 0.06;
22  
23 use constant {
24 4 STATUS_SUCCESS => 0x00000000,
25 STATUS_PENDING => 0x00000103,
26 STATUS_NOTIFY_ENUM_DIR => 0x0000010c,
27 STATUS_SMB_BAD_TID => 0x00050002,
28 STATUS_OS2_INVALID_LEVEL => 0x007c0001,
29 STATUS_NO_MORE_FILES => 0x80000006,
30 STATUS_INVALID_PARAMETER => 0xc000000d,
31 STATUS_NO_SUCH_DEVICE => 0xc000000e,
32 STATUS_NO_SUCH_FILE => 0xc000000f,
33 STATUS_END_OF_FILE => 0xc0000011,
34 STATUS_MORE_PROCESSING_REQUIRED => 0xc0000016,
35 STATUS_NO_FREE_MEMORY => 0xc0000017,
36 STATUS_ACCESS_DENIED => 0xc0000022,
37 STATUS_BUFFER_TOO_SMALL => 0xc0000023,
38 STATUS_OBJECT_NAME_NOT_FOUND => 0xc0000034,
39 STATUS_OBJECT_NAME_COLLISION => 0xc0000035,
40 STATUS_OBJECT_PATH_NOT_FOUND => 0xc000003a,
41 STATUS_SHARING_VIOLATION => 0xc0000043,
42 STATUS_DELETE_PENDING => 0xc0000056,
43 STATUS_PRIVILEGE_NOT_HELD => 0xc0000061,
44 STATUS_LOGON_FAILURE => 0xc000006d,
45 STATUS_DISK_FULL => 0xc000007f,
46 STATUS_FILE_IS_A_DIRECTORY => 0xc00000ba,
47 STATUS_BAD_NETWORK_NAME => 0xc00000cc,
48 STATUS_DIRECTORY_NOT_EMPTY => 0xc0000101,
49 STATUS_NOT_A_DIRECTORY => 0xc0000103,
50 STATUS_CANCELLED => 0xc0000120,
51 STATUS_CANNOT_DELETE => 0xc0000121,
52 STATUS_FILE_CLOSED => 0xc0000128,
53 STATUS_INVALID_LEVEL => 0xc0000148,
54 STATUS_FS_DRIVER_REQUIRED => 0xc000019c,
55 STATUS_NOT_A_REPARSE_POINT => 0xc0000275,
56 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 sub new ($%) {
59 28 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
60 28 my %options = @_;
61  
62 my $self = {
63 28 ▶ disable_log => $options{quiet} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$options{'quiet'} ? : 0 28 28
64 %options,
65 };
66  
67 28 bless $self, $class;
68 }
69  
70 sub log ($$@) {
71 7 ▶ my $self = shift;
Subroutine log: called CC=3 SCAR=11.0
Pod documented
72 7 my $is_err = shift;
73 7 my $format = shift;
74 7 ▶ return if $self->disable_log;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disable_log 0 7 7
75 7 ▶ print sprintf("%s $format\n", $is_err ? '!' : '*', @_);
Branch: 1 branch
BranchTrueFalseTotal
$is_err ? : 4 3 7
76 }
77  
78 3 ▶sub msg ($@) { shift()->log(0, @_) }
Subroutine msg: called CC=1 SCAR=0.0
Pod documented
79 4 ▶sub err ($@) { shift()->log(1, @_); return }
Subroutine err: called CC=1 SCAR=0.0
Pod documented
80  
81 4 my $MAX_DUMP_BYTES = 8 * 1024;
82 4 my $dump_line_format = "%03x | 00 53 54 52 49 4E 47 aa aa aa aa aa aa aa | _STRING. ...... |\n";
83  
84 sub mem ($$;$) {
85 0 ▶ my $self = shift;
Subroutine mem: not called CC=15 SCAR=54.8
Pod documented
86 0 my $data = shift;
87 0 ▶ my $label = shift || "Data dump";
Condition0%0/2
shift() || 'Data dump'
l!l
00
MC/DC0%0/1
shift() || 'Data dump'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
88 0 ▶ return if $self->disable_log;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disable_log 0 0 0
89  
90 0 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
91 0 $self->msg("$label (undef)");
92 0 return;
93 }
94  
95 0 my $len = length($data);
96 0 ▶ $self->msg(sprintf("%s (%lu bytes%s):", $label, $len, $len > $MAX_DUMP_BYTES ? ", shorten" : ""), @_);
Branch: 1 branch
BranchTrueFalseTotal
$len > $MAX_DUMP_BYTES ? : 0 0 0
97 0 ▶ $len = $MAX_DUMP_BYTES if $len > $MAX_DUMP_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $MAX_DUMP_BYTES 0 0 0
98  
99 0 ▶ for (my $n = 0; $n < ($len + 15) / 16; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < ($len + 15) / 16) 0 0 0
100 0 ▶ for (my $i = 0; $i < 16; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 16) 0 0 0
101 0 my $valid = $n * 16 + $i < $len;
102 0 ▶ my $b = $valid ? ord(substr($data, $n * 16 + $i, 1)) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 0 0 0
103 0 ▶ substr($dump_line_format, 7 + $i * 3 + ($i >= 8), 2) = $valid ? sprintf("%02x", $b) : " ";
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 0 0 0
104 0 ▶ substr($dump_line_format, 58 + $i + ($i >= 8), 1) = $valid ? $b == 0 ? '_' : $b <= 32 || $b >= 127 || $b == 37 ? '.' : chr($b) : ' ';
Branch: 3 branches
BranchTrueFalseTotal
$valid ? : 0 0 0
$b == 0 ? : 0 0 0
$b <= 32 || $b >= 127 || $b == 37 ? : 0 0 0
Condition0%0/6
$b <= 32 or $b >= 127 or $b == 37
l!l&&r!l&&!r
000
$b <= 32 or $b >= 127
l!l&&r!l&&!r
000
MC/DC0%0/3
$b <= 32 or $b >= 127 or $b == 37
$b <= 32 $b >= 127 $b == 37
Truth table0%0/4
A or B or C
A= $b <= 32B= $b >= 127C= $b == 37
ABCresult
1XX1
01X1
0011
0000
105 }
106 0 printf $dump_line_format, $n;
107 }
108 }
109  
110 sub parse_share_uri ($$) {
111 0 ▶ my $self = shift;
Subroutine parse_share_uri: not called CC=4 SCAR=30.0
Pod undocumented
112 0 my $share_uri = shift;
113  
114 0 ▶ unless ($share_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share_uri) 0 0 0
115 0 $self->err("No share uri supplied");
116 0 return;
117 }
118 0 ▶ unless ($share_uri =~ m~^([/\\])\1([\w.]+(?::\d+)?)\1([^/\\]+)(?:$|\1)~) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($share_uri =~ m(^([/\\])\1([\w.]+(?::\d+)?)\1([^/\\]+)(?:$|\1))) 0 0 0
119 0 $self->err("Invalid share uri ($share_uri)");
120 0 return;
121 }
122  
123 0 ▶ return wantarray ? ($2, $3) : $share_uri;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
124 }
125  
126 4 our %dump_seen;
127 4 our $dump_is_newline = 1;
128 4 our $dump_level_limit = 7;
129 4 our $dump_array_limit = 20;
130 4 our $dump_string_limit = 50;
131  
132 sub _dump_prefix ($) {
133 0 ▶ my $level = shift;
Subroutine _dump_prefix: not called CC=2 SCAR=17.9
134  
135 0 ▶ return "" unless $dump_is_newline;
Branch: 1 branch
BranchTrueFalseTotal
unless $SMB::dump_is_newline 0 0 0
136 0 $dump_is_newline = 0;
137  
138 0 return " " x (4 * $level);
139 }
140  
141 sub _dump_eol () {
142 0 ▶ $dump_is_newline = 1;
Subroutine _dump_eol: not called CC=1 SCAR=6.9
143  
144 0 return "\n";
145 }
146  
147 sub _dump_string ($) {
148 0 ▶ my $value = shift;
Subroutine _dump_string: not called CC=2 SCAR=17.9
149  
150 0 my $len = length($value);
151 0 ▶ if ($len > $dump_string_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $SMB::dump_string_limit) 0 0 0
152 0 my $llen = length($len);
153 0 substr($value, $dump_string_limit - 3 - $llen) =
154 "..+" . ($len - $dump_string_limit + 3 + $llen);
155 }
156  
157 0 $value =~ s/([\\"])/\\$1/g;
158 0 $value =~ s/([^\\" -\x7e])/sprintf("\\x%02x", ord($1))/ge;
159  
160 0 return $value;
161 }
162  
163 sub _dump_value ($) {
164 0 ▶ my $value = shift;
Subroutine _dump_value: not called CC=32 SCAR=69.6
165 0 ▶ my $level = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
166 0 ▶ my $inline = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
167  
168 0 ▶ return '' if $level >= $dump_level_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $level >= $SMB::dump_level_limit 0 0 0
169  
170 0 my $type = ref($value);
171 0 my $dump = _dump_prefix($level);
172 0 ▶ my $is_seen = $type && $dump_seen{$value};
Condition0%0/3
$type && $SMB::dump_seen{$value}
!ll&&!rl&&r
000
MC/DC0%0/2
$type && $SMB::dump_seen{$value}
$type $SMB::dump_seen{$value}
Truth table0%0/3
A && B
A= $typeB= $SMB::dump_seen{$value}
ABresult
0X0
100
111
173 0 ▶ $dump_seen{$value} = 1 if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 0 0 0
174  
175 0 ▶ if (! $type) {
Branch: 7 branches
BranchTrueFalseTotal
if (not $type) { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'REF') { } 0 0 0
elsif ($type eq 'CODE') { } 0 0 0
elsif ($type eq 'GLOB') { } 0 0 0
elsif ($type eq 'SCALAR') { } 0 0 0
176 0 ▶ $dump .= defined $value
Branch: 2 branches
BranchTrueFalseTotal
defined $value ? : 0 0 0
$value =~ /^-?\d+$/ || $inline == 2 && $value =~ /^-?\w+$/ ? : 0 0 0
Condition0%0/6
$value =~ /^-?\d+$/ or $inline == 2 and $value =~ /^-?\w+$/
l!l&&r!l&&!r
000
$inline == 2 and $value =~ /^-?\w+$/
!ll&&!rl&&r
000
MC/DC0%0/3
$value =~ /^-?\d+$/ or $inline == 2 and $value =~ /^-?\w+$/
$value =~ /^-?\d+$/ $inline == 2 $value =~ /^-?\w+$/
Truth table0%0/4
A or B and C
A= $value =~ /^-?\d+$/B= $inline == 2C= $value =~ /^-?\w+$/
ABCresult
1XX1
0111
00X0
0100
177 ? $value =~ /^-?\d+$/ ||$inline == 2 && $value =~ /^-?\w+$/
178 ? $value : '"' . _dump_string($value) . '"'
179 : 'undef';
180 } elsif ($type eq 'ARRAY') {
181 0 ▶ if ($is_seen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_seen) { } 0 0 0
182 0 $dump .= "ARRAY (seen)";
183 } else {
184 0 $dump .= "[" . _dump_eol();
185 0 ▶ my @array = @$value > $dump_array_limit ? (@$value)[0 .. $dump_array_limit - 2] : @$value;
Branch: 1 branch
BranchTrueFalseTotal
@$value > $SMB::dump_array_limit ? : 0 0 0
186 0 my $prev_elem = '';
187 0 foreach (@array) {
188 # compress equal consecutive elements
189 0 my $elem = &_dump_value($_, $level + 1, 1);
190 0 ▶ if ($elem eq $prev_elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem eq $prev_elem) 0 0 0
191 0 ▶ $dump =~ s/^(\s+)(?:\()?(.*?)(?:\) x (\d+))?,$(\n)\z/my $c = ($3 || 1) + 1; "$1($2) x $c," . _dump_eol()/me;
Condition0%0/2
$3 || 1
l!l
00
MC/DC0%0/1
$3 || 1
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
192 0 next;
193 }
194 0 $dump .= _dump_prefix($level + 1);
195 0 $dump .= $prev_elem = $elem;
196 0 $dump .= "," . _dump_eol();
197 }
198 0 ▶ if (@$value > $dump_array_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$value > $SMB::dump_array_limit) 0 0 0
199 0 $dump .= _dump_prefix($level + 1);
200 0 $dump .= "...[+" . (@$value - $dump_array_limit + 1) . "]," . _dump_eol();
201 }
202 0 $dump .= _dump_prefix($level) . "]";
203 }
204 } elsif ($type eq 'HASH') {
205 0 ▶ if ($is_seen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_seen) { } 0 0 0
206 0 $dump .= "HASH (seen)";
207 } else {
208 0 $dump .= "{" . _dump_eol();
209 0 my $idx = 0;
210 0 my @keys = sort keys %$value;
211 0 my $size = @keys;
212 0 foreach my $key (@keys) {
213 0 my $val = $value->{$key};
214 0 ▶ last if ++$idx == $dump_array_limit && $size > $dump_array_limit;
Branch: 1 branch
BranchTrueFalseTotal
if ++$idx == $SMB::dump_array_limit and $size > $SMB::dump_array_limit 0 0 0
Condition0%0/3
++$idx == $SMB::dump_array_limit and $size > $SMB::dump_array_limit
!ll&&!rl&&r
000
MC/DC0%0/2
++$idx == $SMB::dump_array_limit and $size > $SMB::dump_array_limit
++$idx == $SMB::dump_array_limit $size > $SMB::dump_array_limit
Truth table0%0/3
A and B
A= ++$idx == $SMB::dump_array_limitB= $size > $SMB::dump_array_limit
ABresult
0X0
100
111
215 0 $dump .= _dump_prefix($level + 1);
216 0 $dump .= &_dump_value($key, $level + 1, 2);
217 0 $dump .= " => ";
218 0 $dump .= &_dump_value($val, $level + 1, 1);
219 0 $dump .= "," . _dump_eol();
220 }
221 0 ▶ if ($size > $dump_array_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $SMB::dump_array_limit) 0 0 0
222 0 $dump .= _dump_prefix($level + 1);
223 0 $dump .= "...[+" . ($size - $dump_array_limit + 1) . "]," . _dump_eol();
224 }
225 0 $dump .= _dump_prefix($level) . "}";
226 }
227 } elsif ($type eq 'REF') {
228 0 $dump .= "REF";
229 } elsif ($type eq 'CODE') {
230 0 $dump .= "CODE";
231 } elsif ($type eq 'GLOB') {
232 0 $dump .= "GLOB";
233 } elsif ($type eq 'SCALAR') {
234 0 $dump .= "\\";
235 0 $dump .= &_dump_value($$value, $level + 1, 1);
236 } else {
237 0 $dump .= "$type ";
238 0 my $native_type;
239 0 foreach ('SCALAR', 'ARRAY', 'HASH', 'CODE', 'GLOB') {
240 0 ▶ $native_type = $_ if $value->isa($_);
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa($_) 0 0 0
241 }
242 0 ▶ die "Non-standard perl ref type to dump in $value\n" unless $native_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $native_type 0 0 0
243  
244 0 $dump_seen{$value} = 0;
245 0 bless($value, $native_type);
246 0 $dump .= &_dump_value($value, $level, 1);
247 0 bless($value, $type);
248 }
249  
250 0 ▶ $dump .= _dump_eol() unless $inline;
Branch: 1 branch
BranchTrueFalseTotal
unless $inline 0 0 0
251  
252 0 return $dump;
253  
254 }
255  
256 sub dump ($;$) {
257 0 ▶ my $self = shift;
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
258 0 my $level = 0;
259  
260 0 my $dump = _dump_value($self);
261  
262 0 %dump_seen = ();
263  
264 0 return $dump;
265 }
266  
267 4 our $AUTOLOAD;
268  
269 sub AUTOLOAD ($;@) {
270 62 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=5 SCAR=16.3
271 62 my @params = @_;
272  
273 62 my $method = $AUTOLOAD;
274 62 $method =~ s/.*://g;
275  
276 62 ▶ return if $method eq 'DESTROY'; # ignore DESTROY messages
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 27 35 62
277  
278 35 ▶ die "Calling method $method for non-object '$self'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 35 35
279 unless ref($self);
280  
281 35 ▶ if (exists $self->{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$method}) 35 0 35
282 # define this accessor method explicitely if not yet
283 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
284 35 *{$AUTOLOAD} = sub {
285 331 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
286 331 ▶ warn "Skipping extraneous params (@_) on access of field '$method' in $self\n"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 331 331
287 if @_ > 1;
288 331 ▶ $self->{$method} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 59 272 331
289 331 return $self->{$method};
290 35 ▶ } unless $self->can($AUTOLOAD);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($SMB::AUTOLOAD) 35 0 35
291  
292 35 return *{$AUTOLOAD}->($self, @params);
293 }
294  
295 0 die "Unknown method or field '$method' in $self\n";
296 }
297  
298 4 1;
299  
300 __END__
« blib/lib/SMB/File.pm ↑ Summary blib/lib/SMB/Server.pm »