Hotline/Shared.pm

filter: Statementstmt 26.2% 21 / 80 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 45.4% 5 / 11 Podpod n/a totaltotal 17.3% 26 / 150 CC 26 SCAR 37.3
CC
26
Cov
16%
_hexdump
72.0
_read
56.0
_write
42.0
CRAP 41.7
« Hotline/Protocol/Packet.pm ↑ Summary Hotline/User.pm »
1 package Net::Hotline::Shared;
2  
3 ## Copyright(c) 1998-2002 by John C. Siracusa. All rights reserved. This
4 ## program is free software; you can redistribute it and/or modify it under
5 ## the same terms as Perl itself.
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POSIX qw(F_GETFL F_SETFL O_NONBLOCK EINTR EWOULDBLOCK EAGAIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 require Exporter;
16 1 @ISA = qw(Exporter);
17 1 @EXPORT_OK = qw(_encode _write _read _hexdump _debug _set_blocking);
18 1 %EXPORT_TAGS = (all => \@EXPORT_OK);
19  
20 1 $VERSION = '0.80';
21  
22 sub _debug
23 0 ▶{
Subroutine _debug: not called CC=2 SCAR=17.9
24 0 ▶ if($Net::Hotline::Client::DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Hotline::Client::DEBUG) 0 0 0
25 {
26 0 print STDERR join('', @_);
27 }
28 }
29  
30 sub _encode
31 0 ▶{
Subroutine _encode: not called CC=2 SCAR=17.9
32 0 my($data) = join('', @_);
33  
34 0 my($i, $len, $enc);
35  
36 0 $len = length($data);
37 0 $enc = '';
38  
39 0 ▶ for($i = 0; $i < $len; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
40 {
41 0 $enc .= pack("C", (255 - unpack("C", substr($data, $i, 1))));
42 }
43  
44 0 return $enc;
45 }
46  
47 sub _write
48 0 ▶{
Subroutine _write: not called CC=6 SCAR=37.4
49 0 my($fh, $data_ref, $length) = @_;
50  
51 0 my($written, $offset);
52  
53 0 $offset = 0;
54  
55 0 ▶ while($length > 0) # Handle partial writes
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
56 {
57 0 $written = syswrite($fh, $$data_ref, $length, $offset);
58 0 ▶ next if($! == EINTR);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
59 0 ▶ unless(defined($written))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $written) 0 0 0
60 {
61 0 ▶ next if($! == EWOULDBLOCK || $! == EAGAIN);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 11 0 0 0
Condition0%0/3
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
62 0 croak("System write error(", $! + 0, "): $!\n");
63 }
64 0 $length -= $written;
65 0 $offset += $written;
66 }
67  
68 0 return $offset;
69 }
70  
71 sub _read
72 0 ▶{
Subroutine _read: not called CC=7 SCAR=40.3
73 0 my($fh, $data_ref, $length, $blocking) = @_;
74  
75 0 my($offset) = 0;
76 0 my($read) = 0;
77  
78 0 ▶ $blocking = 1 unless(defined($blocking));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $blocking 0 0 0
79  
80 #_debug("Reading $length...");
81  
82 0 ▶ while($length > 0) # Handle partial reads
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
83 {
84 0 $read = sysread($fh, $$data_ref, $length, $offset);
85  
86 0 ▶ unless(defined($read))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $read) 0 0 0
87 {
88 0 ▶ next if($! == EINTR);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
89  
90 # Once we read a little bit, we keep readinuntil we get it all
91 # Otherwise, we can return undef and treat it as a WOULDBLOCK
92 0 ▶ if($blocking || $offset > 0) { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($blocking or $offset > 0) { } 0 0 0
Condition0%0/3
$blocking or $offset > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$blocking or $offset > 0
$blocking $offset > 0
Truth table0%0/3
A or B
A= $blockingB= $offset > 0
ABresult
1X1
011
000
93 0 else { return }
94 }
95  
96 0 $offset += $read;
97 0 $length -= $read;
98 }
99  
100 #_debug("read $offset ($length)\n");
101 0 return($offset);
102 }
103  
104 sub _set_blocking
105 0 ▶{
Subroutine _set_blocking: not called CC=6 SCAR=37.4
106 0 my($fh, $blocking) = @_;
107  
108 0 ▶ if($IO::VERSION >= 1.19) # The easy way, with the IO module
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::VERSION >= 1.19) { } 0 0 0
109 {
110 0 $fh->blocking($blocking);
111 }
112 else # The hard way...not 100% successful :-/
113 {
114 0 my($flags) = fcntl($fh, F_GETFL, 0);
115  
116 0 ▶ defined($flags) || croak "Can't get flags for socket: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $flags or croak("Can't get flags for socket: $!\n") 0 0 0
117  
118 0 ▶ if($blocking)
Branch: 1 branch
BranchTrueFalseTotal
if ($blocking) { } 0 0 0
119 {
120 0 ▶ fcntl($fh, F_SETFL, $flags & ~O_NONBLOCK) ||
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags & '18446744073709549567' or croak("Can't make socket blocking: $!\n") 0 0 0
121 croak "Can't make socket blocking: $!\n";
122 }
123 else
124 {
125 0 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK) ||
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or croak("Can't make socket nonblocking: $!\n") 0 0 0
126 croak "Can't make socket nonblocking: $!\n";
127 }
128 }
129 }
130  
131 sub _hexdump
132 0 ▶{
Subroutine _hexdump: not called CC=8 SCAR=42.8
133 0 my($data) = join('', @_);
134  
135 0 my($ret, $hex, $ascii, $len, $i);
136  
137 0 $len = length($data);
138  
139 0 ▶ for($i = 0; $i < $len; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
140 {
141 0 ▶ if($i > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
142 {
143 0 ▶ if($i % 4 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 4 == 0) 0 0 0
144 {
145 0 $hex .= ' ';
146 }
147  
148 0 ▶ if($i % 16 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 16 == 0) 0 0 0
149 {
150 0 $ret .= "$hex$ascii\n";
151 0 $ascii = $hex = '';
152 }
153 }
154  
155 0 $hex .= sprintf("%02x ", ord(substr($data, $i, 1)));
156  
157 0 ▶ $ascii .= sprintf("%c", (ord(substr($data, $i, 1)) > 31 and
Branch: 1 branch
BranchTrueFalseTotal
ord substr($data, $i, 1) > 31 && ord substr($data, $i, 1) < 127 ? : 0 0 0
Condition0%0/3
ord substr($data, $i, 1) > 31 and ord substr($data, $i, 1) < 127
!ll&&!rl&&r
000
MC/DC0%0/2
ord substr($data, $i, 1) > 31 and ord substr($data, $i, 1) < 127
ord substr($data, $i, 1) > 31 ord substr($data, $i, 1) < 127
Truth table0%0/3
A and B
A= ord substr($data, $i, 1) > 31B= ord substr($data, $i, 1) < 127
ABresult
0X0
100
111
158 ord(substr($data, $i, 1)) < 127) ?
159 ord(substr($data, $i, 1)) : 46);
160 }
161  
162 0 ▶ if(length($hex) < 50)
Branch: 1 branch
BranchTrueFalseTotal
if (length $hex < 50) 0 0 0
163 {
164 0 $hex .= ' ' x (50 - length($hex));
165 }
166  
167 0 $ret .= "$hex $ascii\n";
168  
169 0 return $ret;
170 }
171  
172 1 1;
« Hotline/Protocol/Packet.pm ↑ Summary Hotline/User.pm »