IPPUtil.pm

filter: Statementstmt 31.0% 45 / 145 Branchbran 15.3% 8 / 52 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 30.4% 7 / 23 Podpod 0.0% 0 / 18 totaltotal 22.3% 60 / 268 CC 36 SCAR 37.6
CC
36
Cov
25%
statusToString
132.0
ippToString
72.0
groupStructureToString
42.0
CRAP 42.8
« IPPAttribute.pm ↑ Summary IPPRequest.pm »
1 ###
2 # Copyright (c) 2004 Matthias Hilbig <bighil@cpan.org>
3 # All rights reserved.
4 #
5 # This program is free software; you may redistribute it and/or modify it
6 # under the same terms as Perl itself.
7 #
8  
9 package Net::IPP::IPPUtil;
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Net::IPP::IPP qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 require Exporter;
17 2 our @ISA = ("Exporter");
18 2 our @EXPORT_OK = qw(ippToString printIPP bytesToString printBytes searchGroup findAttribute findNextAttribute
19 findGroup findNextGroup isSuccessful printerStateToString jobStateToString operationToString groupToString
20 statusToString statusToDetailedString);
21 2 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
22  
23 ###
24 # convert group to string
25 #
26 # Parameter: $group - group to convert
27 #
28 # Return: $string with converted group
29 #
30 0 ▶sub groupStructureToString($) {
Subroutine groupStructureToString: not called CC=6 SCAR=37.4
Pod undocumented
31 0 my $group = shift;
32 0 my $string = "";
33 0 $string .= "GROUP " . groupToString($group->{&TYPE}) . "\n";
34 0 foreach my $key (sort keys %{$group}) {
35 0 ▶ if ($key ne &TYPE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne &TYPE) 0 0 0
36 0 my $value = $group->{$key};
37
38 0 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 0 0 0
39 0 $value = $value->{&VALUE};
40 }
41
42 0 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
43 0 $string .= " " . $key . " = [";
44 0 foreach my $val (@{$value}) {
45 0 $string .= "$val, ";
46 }
47 0 $string .= "]\n";
48 } else {
49 0 $string .= " " . $key . " = " . $value . "\n";
50 }
51 }
52 }
53 0 return $string;
54 }
55  
56 ###
57 # Convert perl structure to string
58 #
59 # Parameter: $ipp - IPP request to convert
60 #
61 # Return: $string with converted perl structure
62 #
63 0 ▶sub ippToString($) {
Subroutine ippToString: not called CC=8 SCAR=42.8
Pod undocumented
64 0 my $ipp = shift;
65 0 my $string = "";
66 0 ▶ $string .= "URL: " . $ipp->{&URL} . "\n" if exists($ipp->{&URL});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$ipp{&URL} 0 0 0
67 0 ▶ $string .= "REQUEST-ID: " . $ipp->{&REQUEST_ID} . "\n" if exists($ipp->{&REQUEST_ID});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$ipp{&REQUEST_ID} 0 0 0
68 0 ▶ $string .= "STATUS: " . $ipp->{&STATUS} ." (" .statusToDetailedString($ipp->{&STATUS}) . ")\n" if exists($ipp->{&STATUS});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$ipp{&STATUS} 0 0 0
69 0 ▶ $string .= "OPERATION: " . operationToString($ipp->{&OPERATION}) . "\n" if exists($ipp->{&OPERATION});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$ipp{&OPERATION} 0 0 0
70 0 ▶ $string .= "VERSION: " . $ipp->{&VERSION} . "\n" if exists($ipp->{&VERSION});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$ipp{&VERSION} 0 0 0
71 0 ▶ if (exists($ipp->{&GROUPS})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$ipp{&GROUPS}) 0 0 0
72 0 foreach my $group (@{$ipp->{&GROUPS}}) {
73 0 $string .= groupStructureToString($group);
74 }
75 }
76 0 return $string;
77 }
78  
79 ###
80 # print perl structure of IPP request/response
81 #
82 0 ▶sub printIPP($) {
Subroutine printIPP: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $ipp = shift;
84 0 my $string = ippToString($ipp);
85 0 print $string;
86 }
87  
88 ###
89 # helper function to dump hexview of bytes
90 #
91 18 ▶sub bytesToString($) {
Subroutine bytesToString: called CC=5 SCAR=16.1
Pod undocumented
92 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
93
94 18 my $bytes = shift;
95 18 my @bytes = unpack("c*", $bytes);
96  
97 18 my $width = 16; #how many bytes to print per line
98 18 my $hexWidth = 3*$width;
99  
100 18 my $string = "";
101  
102 18 my $offset = 0;
103  
104 18 ▶ while ($offset *$width < length($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset * $width < length $bytes) 18 18 36
105 18 my $hexString = "";
106 18 my $charString = "";
107 18 ▶ for (my $i = 0; $i < $width; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $width) 288 18 306
108 288 ▶ if ($offset*$width + $i < length($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset * $width + $i < length $bytes) 113 175 288
109 113 my $char;
110 2 ▶ {use bytes;$char = substr($bytes, $offset*$width + $i, 1);}
Subroutine BEGIN: called CC=1 SCAR=0.0
111
112 113 $hexString .= sprintf("%02X ", ord($char));
113 113 ▶ if ($char =~ /[\w\-\:]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /[\w\-\:]/) { } 31 82 113
114 31 $charString .= $char;
115 } else {
116 82 $charString .= ".";
117 }
118 }
119 }
120
121 18 $string .= sprintf("%-${hexWidth}s%s\n",$hexString,$charString);
122 18 $offset++;
123 }
124 18 return $string;
125 }
126  
127 18 ▶sub printBytes($) {
Subroutine printBytes: called CC=1 SCAR=0.0
Pod undocumented
128 18 my $bytes = shift;
129 18 print bytesToString($bytes);
130 }
131  
132 ###
133 # Searches for attribute in group
134 #
135 # Parameter: $group - IPP group
136 # $name - name of IPP attribute
137 #
138 # Return: value of attribute if found, undef otherwise
139 #
140 0 ▶sub searchGroup($$) {
Subroutine searchGroup: not called CC=3 SCAR=24.8
Pod undocumented
141 0 my $group = shift;
142 0 my $name = shift;
143
144 0 ▶ while (my ($key, $value) = each %{$group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$group;}) 0 0 0
145 0 ▶ if ($key eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $name) 0 0 0
146
147 # reset hash iterator
148 0 keys %{$group};
149
150 0 return $value;
151 }
152 }
153
154 0 return undef;
155 }
156  
157  
158 ###
159 # Searches for next attribute in IPP structure
160 #
161 # Parameter: $ipp - IPP structure
162 # $name - name of IPP attribute
163 #
164 # Return: value of attribute if found, undef otherwise
165 #
166 # Each attribute must be unique in a group [RFC 2911 3.1.3],
167 # so it is only necessary to remember the last group that was searched.
168 #
169 2 my $lastAttributeIndex = -1;
170 0 ▶sub findNextAttribute($$) {
Subroutine findNextAttribute: not called CC=3 SCAR=24.8
Pod undocumented
171 0 my $ipp = shift;
172 0 my $name = shift;
173  
174 0 my @groups = @{$ipp->{&GROUPS}};
175 0 my $length = scalar(@groups);
176  
177 # search restarts automagically, because $lastAttributeIndex is
178 # initialized and resetted to -1
179 0 ▶ for (my $i = $lastAttributeIndex + 1; $i < $length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
180 0 my $value = searchGroup($groups[$i], $name);
181 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
182 0 $lastAttributeIndex = $i;
183 0 return $value;
184 }
185 }
186  
187 0 $lastAttributeIndex = -1;
188 0 return undef;
189 }
190  
191 ###
192 # Search for first attribute in IPP structure
193 # Internally call findNextAttribute after resetting $lastIndex
194 #
195 # Parameter: $ipp - IPP structure
196 # $name - name of IPP attribute
197 #
198 # Return: value of attribute if found, undef otherwise
199 #
200 0 ▶sub findAttribute($$) {
Subroutine findAttribute: not called CC=1 SCAR=6.9
Pod undocumented
201 0 my $ipp = shift;
202 0 my $name = shift;
203
204 0 $lastAttributeIndex = -1;
205  
206 0 return findNextAttribute($ipp, $name);
207 }
208  
209 ###
210 # Search for next group in IPP structure
211 #
212 # Parameter: $ipp - IPP structure
213 # $type - type of IPP group
214 #
215 # Return: group if a group with the specified type was found, undef otherwise
216 #
217 2 my $lastGroupIndex = -1;
218 0 ▶sub findNextGroup($$) {
Subroutine findNextGroup: not called CC=3 SCAR=24.8
Pod undocumented
219 0 my $ipp = shift;
220 0 my $type = shift;
221 0 my @groups = @{$ipp->{&GROUPS}};
222 0 my $length = scalar(@groups);
223  
224 # search restarts automagically, because $lastGroupIndex is
225 # initialized and resetted to -1
226 0 ▶ for (my $i = $lastGroupIndex + 1; $i < $length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
227 0 my $groupType = $groups[$i]->{&TYPE};
228 0 ▶ if ($groupType == $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupType == $type) 0 0 0
229 0 $lastGroupIndex = $i;
230 0 return $groups[$i];
231 }
232 }
233  
234 0 $lastGroupIndex = -1;
235 0 return undef;
236 }
237  
238 ###
239 # Search for first group in IPP structure
240 #
241 # Parameter: $ipp - IPP structure
242 # $type - type of IPP group
243 #
244 # Return: group if a group with the specified type was found, undef otherwise
245 #
246 0 ▶sub findGroup($$) {
Subroutine findGroup: not called CC=1 SCAR=6.9
Pod undocumented
247 0 my $ipp = shift;
248 0 my $type = shift;
249
250 0 $lastGroupIndex = -1;
251  
252 0 return findNextGroup($ipp, $type);
253 }
254  
255 ###
256 # returns 1 if IPP request was successful, 0 otherwise
257 #
258 # Parameter: $response - IPP response
259 #
260 # Return: 1 if successful request, 0 otherwise
261 #
262 0 ▶sub isSuccessful($) {
Subroutine isSuccessful: not called CC=3 SCAR=24.8
Pod undocumented
263 0 my $response = shift;
264 0 ▶ if (exists($response->{&STATUS})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$response{&STATUS}) 0 0 0
265 0 my $status = $response->{&STATUS};
266 0 ▶ return ($status >= 0x0000 and $status <= 0x00ff);
Condition0%0/3
$status >= 0 && $status <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 0 && $status <= 255
$status >= 0 $status <= 255
Truth table0%0/3
A && B
A= $status >= 0B= $status <= 255
ABresult
0X0
100
111
267 }
268 0 return 0;
269 }
270  
271 ###
272 # look for key in the specified hash and return value if the key exists.
273 #
274 # Parameter: $key - key for hash
275 # $hashref - reference to hash
276 #
277 # Return: value in hash if key exists, "unknown" otherwise
278 #
279 0 ▶sub hashResolve($$) {
Subroutine hashResolve: not called CC=2 SCAR=17.9
Pod undocumented
280 0 my $key = shift;
281 0 my $hashref = shift;
282  
283 0 ▶ if (exists($hashref -> {$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hashref->{$key}) { } 0 0 0
284 0 return $hashref->{$key};
285 } else {
286 0 return "unknown";
287 }
288 }
289  
290 ###
291 # The following functions are all build similar and could be made more complicated with AUTOLOADER :-)
292 # All functions use hashResolve to transform value to string.
293 #
294 # Parameter: $value - value to transform to string
295 #
296 # Return: $value transformed to string
297 #
298  
299 0 ▶sub printerStateToString($) {
Subroutine printerStateToString: not called CC=1 SCAR=6.9
Pod undocumented
300 0 my $state = shift;
301 0 return hashResolve($state, \%Net::IPP::IPP::printerState);
302 }
303  
304 0 ▶sub jobStateToString($) {
Subroutine jobStateToString: not called CC=1 SCAR=6.9
Pod undocumented
305 0 my $state = shift;
306 0 return hashResolve($state, \%Net::IPP::IPP::jobState);
307 }
308  
309 0 ▶sub operationToString($) {
Subroutine operationToString: not called CC=1 SCAR=6.9
Pod undocumented
310 0 my $operation = shift;
311 0 return hashResolve($operation, \%Net::IPP::IPP::operation);
312 }
313  
314 0 ▶sub groupToString($) {
Subroutine groupToString: not called CC=1 SCAR=6.9
Pod undocumented
315 0 my $group = shift;
316 0 return hashResolve($group, \%Net::IPP::IPP::group);
317 }
318  
319 0 ▶sub statusToDetailedString($) {
Subroutine statusToDetailedString: not called CC=1 SCAR=6.9
Pod undocumented
320 0 my $status = shift;
321 0 return hashResolve($status, \%Net::IPP::IPP::statusCodes);
322 }
323  
324 ###
325 # returns type of IPP status.
326 #
327 # Parameter: $status - IPP status
328 #
329 # Return:
330 # "informational" - Request received, continuing process
331 # "successful" - The action was successfully received, understood, and accepted
332 # "redirection" - Further action must be taken in order to complete the request
333 # "client-error" - The request contains bad syntax or cannot be fulfilled
334 # "server-error" - The IPP object failed to fulfill an apparently valid request
335 #
336 0 ▶sub statusToString($) {
Subroutine statusToString: not called CC=11 SCAR=48.8
Pod undocumented
337 0 my $status = shift;
338 0 ▶ if ($status >= 0x0000 and $status <= 0x00ff) {
Branch: 5 branches
BranchTrueFalseTotal
if ($status >= 0 and $status <= 255) { } 0 0 0
elsif ($status >= 256 and $status <= 511) { } 0 0 0
elsif ($status >= 512 and $status <= 767) { } 0 0 0
elsif ($status >= 1024 and $status <= 1279) { } 0 0 0
elsif ($status >= 1280 and $status <= 1535) { } 0 0 0
Condition0%0/3
$status >= 0 and $status <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 0 and $status <= 255
$status >= 0 $status <= 255
Truth table0%0/3
A and B
A= $status >= 0B= $status <= 255
ABresult
0X0
100
111
339 0 return "successful";
340 ▶ } elsif ($status >= 0x0100 and $status <= 0x01ff) {
Condition0%0/3
$status >= 256 and $status <= 511
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 256 and $status <= 511
$status >= 256 $status <= 511
Truth table0%0/3
A and B
A= $status >= 256B= $status <= 511
ABresult
0X0
100
111
341 0 return "informational";
342 ▶ } elsif ($status >= 0x0200 and $status <= 0x02ff) {
Condition0%0/3
$status >= 512 and $status <= 767
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 512 and $status <= 767
$status >= 512 $status <= 767
Truth table0%0/3
A and B
A= $status >= 512B= $status <= 767
ABresult
0X0
100
111
343 0 return "redirection";
344 ▶ } elsif ($status >= 0x0400 and $status <= 0x04ff) {
Condition0%0/3
$status >= 1024 and $status <= 1279
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 1024 and $status <= 1279
$status >= 1024 $status <= 1279
Truth table0%0/3
A and B
A= $status >= 1024B= $status <= 1279
ABresult
0X0
100
111
345 0 return "client-error";
346 ▶ } elsif ($status >= 0x0500 and $status <= 0x05ff) {
Condition0%0/3
$status >= 1280 and $status <= 1535
!ll&&!rl&&r
000
MC/DC0%0/2
$status >= 1280 and $status <= 1535
$status >= 1280 $status <= 1535
Truth table0%0/3
A and B
A= $status >= 1280B= $status <= 1535
ABresult
0X0
100
111
347 0 return "server-error";
348 }
349 }
350  
351  
352 2 1;
353 __END__
« IPPAttribute.pm ↑ Summary IPPRequest.pm »