blib/lib/POE/Component/Server/NSCA.pm

filter: Statementstmt 85.4% 287 / 336 Branchbran 46.1% 36 / 78 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.9% 62 / 66 Podpod 100% 6 / 6 totaltotal 79.2% 394 / 497 SCAR 44.7
CC
54
Cov
77%
_decrypt_mcrypt
42.0
unregister
30.0
spawn
10.8
CRAP 87.5
↑ Summary
1 package POE::Component::Server::NSCA;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Math::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Stream);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 $VERSION='0.08';
13  
14 5 ▶use constant MAX_INPUT_BUFFER => 2048 ; # /* max size of most buffers we use */
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use constant MAX_HOST_ADDRESS_LENGTH => 256 ; # /* max size of a host address */
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use constant MAX_HOSTNAME_LENGTH => 64 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use constant MAX_DESCRIPTION_LENGTH => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use constant MAX_PLUGINOUTPUT_LENGTH => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use constant MAX_PASSWORD_LENGTH => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use constant TRANSMITTED_IV_SIZE => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use constant SIZEOF_U_INT32_T => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use constant SIZEOF_INT16_T => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use constant SIZEOF_INIT_PACKET => TRANSMITTED_IV_SIZE + SIZEOF_U_INT32_T;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use constant PROBABLY_ALIGNMENT_ISSUE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 ▶use constant SIZEOF_DATA_PACKET => SIZEOF_INT16_T + SIZEOF_U_INT32_T + SIZEOF_U_INT32_T + SIZEOF_INT16_T + MAX_HOSTNAME_LENGTH + MAX_DESCRIPTION_LENGTH + MAX_PLUGINOUTPUT_LENGTH + PROBABLY_ALIGNMENT_ISSUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 ▶use constant ENCRYPT_NONE => 0 ; # /* no encryption */
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use constant ENCRYPT_XOR => 1 ; # /* not really encrypted, just obfuscated */
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use constant ENCRYPT_DES => 2 ; # /* DES */
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use constant ENCRYPT_3DES => 3 ; # /* 3DES or Triple DES */
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use constant ENCRYPT_CAST128 => 4 ; # /* CAST-128 */
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use constant ENCRYPT_CAST256 => 5 ; # /* CAST-256 */
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use constant ENCRYPT_XTEA => 6 ; # /* xTEA */
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use constant ENCRYPT_3WAY => 7 ; # /* 3-WAY */
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use constant ENCRYPT_BLOWFISH => 8 ; # /* SKIPJACK */
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use constant ENCRYPT_TWOFISH => 9 ; # /* TWOFISH */
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use constant ENCRYPT_LOKI97 => 10 ; # /* LOKI97 */
Subroutine BEGIN: called CC=1 SCAR=0.0
40 5 ▶use constant ENCRYPT_RC2 => 11 ; # /* RC2 */
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use constant ENCRYPT_ARCFOUR => 12 ; # /* RC4 */
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use constant ENCRYPT_RC6 => 13 ; # /* RC6 */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use constant ENCRYPT_RIJNDAEL128 => 14 ; # /* RIJNDAEL-128 */
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶use constant ENCRYPT_RIJNDAEL192 => 15 ; # /* RIJNDAEL-192 */
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶use constant ENCRYPT_RIJNDAEL256 => 16 ; # /* RIJNDAEL-256 */
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 ▶use constant ENCRYPT_MARS => 17 ; # /* MARS */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use constant ENCRYPT_PANAMA => 18 ; # /* PANAMA */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use constant ENCRYPT_WAKE => 19 ; # /* WAKE */
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 ▶use constant ENCRYPT_SERPENT => 20 ; # /* SERPENT */
Subroutine BEGIN: called CC=1 SCAR=0.0
50 5 ▶use constant ENCRYPT_IDEA => 21 ; # /* IDEA */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
51 5 ▶use constant ENCRYPT_ENIGMA => 22 ; # /* ENIGMA (Unix crypt) */
Subroutine BEGIN: called CC=1 SCAR=0.0
52 5 ▶use constant ENCRYPT_GOST => 23 ; # /* GOST */
Subroutine BEGIN: called CC=1 SCAR=0.0
53 5 ▶use constant ENCRYPT_SAFER64 => 24 ; # /* SAFER-sk64 */
Subroutine BEGIN: called CC=1 SCAR=0.0
54 5 ▶use constant ENCRYPT_SAFER128 => 25 ; # /* SAFER-sk128 */
Subroutine BEGIN: called CC=1 SCAR=0.0
55 5 ▶use constant ENCRYPT_SAFERPLUS => 26 ; # /* SAFER+ */
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 5 my $HAVE_MCRYPT = 0;
58 5 eval {
59 5 require Mcrypt;
60 0 $HAVE_MCRYPT++;
61 };
62  
63 # Lookups for loading.
64 5 my %mcrypts = ( ENCRYPT_NONE, "none",
65 ENCRYPT_XOR, "xor",
66 ENCRYPT_DES, "des",
67 ENCRYPT_3DES, "3des",
68 ENCRYPT_CAST128, "cast-128",
69 ENCRYPT_CAST256, "cast-256",
70 ENCRYPT_XTEA, "xtea",
71 ENCRYPT_3WAY, "threeway",
72 ENCRYPT_BLOWFISH, "blowfish",
73 ENCRYPT_TWOFISH, "twofish",
74 ENCRYPT_LOKI97, "loki97",
75 ENCRYPT_RC2, "rc2",
76 ENCRYPT_ARCFOUR, "arcfour",
77 ENCRYPT_RC6, "rc6",
78 ENCRYPT_RIJNDAEL128, "rijndael-128",
79 ENCRYPT_RIJNDAEL192, "rijndael-192",
80 ENCRYPT_RIJNDAEL256, "rijndael-256",
81 ENCRYPT_MARS, "mars",
82 ENCRYPT_PANAMA, "panama",
83 ENCRYPT_WAKE, "wake",
84 ENCRYPT_SERPENT, "serpent",
85 ENCRYPT_IDEA, "idea",
86 ENCRYPT_ENIGMA, "engima",
87 ENCRYPT_GOST, "gost",
88 ENCRYPT_SAFER64, "safer-sk64",
89 ENCRYPT_SAFER128, "safer-sk128",
90 ENCRYPT_SAFERPLUS, "saferplus",
91 );
92  
93 sub spawn {
94 3 ▶ my $package = shift;
Subroutine spawn: called CC=10 SCAR=23.8
Pod documented
95 3 my %opts = @_;
96 3 $opts{lc $_} = delete $opts{$_} for keys %opts;
97 croak "$package requires a 'password' argument\n"
98 3 ▶ unless $opts{password};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"password"} 0 3 3
99 croak "$package requires an 'encryption' argument\n"
100 3 ▶ unless defined $opts{encryption};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"encryption"} 0 3 3
101 croak "'encryption' argument must be a valid numeric\n"
102 3 ▶ unless defined $mcrypts{ $opts{encryption} };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mcrypts{$opts{"encryption"}} 0 3 3
103 3 my $options = delete $opts{options};
104 3 ▶ my $access = delete $opts{access} || [ Net::Netmask->new('any') ];
Condition50%1/2
delete $opts{'access'} || ['Net::Netmask'->new('any')]
l!l
03
MC/DC0%0/1
delete $opts{'access'} || ['Net::Netmask'->new('any')]
delete $opts{'access'}
Truth table50%1/2
A
A= delete $opts{'access'}
Aresult
11
00
105 3 ▶ $access = [ ] unless ref $access eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $access eq "ARRAY" 0 3 3
106 3 foreach my $acl ( @$access ) {
107 3 ▶ next unless $acl->isa('Net::Netmask');
Branch: 1 branch
BranchTrueFalseTotal
unless $acl->isa("Net::Netmask") 0 3 3
108 3 push @{ $opts{access} }, $acl;
109 }
110 3 my $self = bless \%opts, $package;
111 3 ▶ $self->{session_id} = POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 1 2 3
112 object_states => [
113 $self => { shutdown => '_shutdown',
114 },
115 $self => [qw(
116 _start
117 _accept_client
118 _accept_failed
119 _conn_input
120 _conn_error
121 _conn_alarm
122 register
123 unregister
124 )],
125 ],
126 heap => $self,
127 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
128 )->ID();
129 3 return $self;
130 }
131  
132 sub shutdown {
133 3 ▶ my $self = shift;
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
134 3 $poe_kernel->post( $self->{session_id}, 'shutdown' );
135 }
136  
137 sub session_id {
138 2 ▶ return $_[0]->{session_id};
Subroutine session_id: called CC=1 SCAR=0.0
Pod documented
139 }
140  
141 sub getsockname {
142 3 ▶ return unless $_[0]->{listener};
Subroutine getsockname: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"listener"} 0 3 3
143 3 return $_[0]->{listener}->getsockname();
144 }
145  
146 sub _start {
147 3 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine _start: called CC=4 SCAR=14.8
148 3 $self->{session_id} = $_[SESSION]->ID();
149 3 ▶ if ( $self->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alias'}) { } 0 3 3
150 0 $kernel->alias_set( $self->{alias} );
151 }
152 else {
153 3 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
154 }
155 3 $self->{filter} = POE::Filter::Stream->new();
156 $self->{listener} = POE::Wheel::SocketFactory->new(
157 ( defined $self->{address} ? ( BindAddress => $self->{address} ) : () ),
158 3 ▶ ( defined $self->{port} ? ( BindPort => $self->{port} ) : ( BindPort => 5667 ) ),
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'address'} ? : 3 0 3
defined $self->{'port'} ? : 3 0 3
159 SuccessEvent => '_accept_client',
160 FailureEvent => '_accept_failed',
161 SocketDomain => AF_INET, # Sets the socket() domain
162 SocketType => SOCK_STREAM, # Sets the socket() type
163 SocketProtocol => 'tcp', # Sets the socket() protocol
164 Reuse => 'on', # Lets the port be reused
165 );
166 3 return;
167 }
168  
169 sub register {
170 2 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine register: called CC=6 SCAR=21.7
Pod documented
171 2 my $sender_id = $sender->ID();
172 2 my %args;
173 2 ▶ if ( ref $_[ARG0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[10] eq 'HASH') { } 2 0 2
elsif (ref $_[10] eq 'ARRAY') { } 0 0 0
174 2 %args = %{ $_[ARG0] };
175 }
176 elsif ( ref $_[ARG0] eq 'ARRAY' ) {
177 0 %args = @{ $_[ARG0] };
178 }
179 else {
180 0 %args = @_[ARG0..$#_];
181 }
182 2 $args{lc $_} = delete $args{$_} for keys %args;
183 2 ▶ unless ( $args{event} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"event"}) 0 2 2
184 0 warn "No 'event' argument supplied\n";
185 0 return;
186 }
187 2 ▶ if ( defined $self->{sessions}->{ $sender_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'sessions'}{$sender_id}) { } 0 2 2
188 0 $self->{sessions}->{ $sender_id } = \%args;
189 }
190 else {
191 2 $self->{sessions}->{ $sender_id } = \%args;
192 2 $kernel->refcount_increment( $sender_id, __PACKAGE__ );
193 }
194 2 return;
195 }
196  
197 sub unregister {
198 0 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine unregister: not called CC=5 SCAR=34.0
Pod documented
199 0 my $sender_id = $sender->ID();
200 0 my %args;
201 0 ▶ if ( ref $_[ARG0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[10] eq 'HASH') { } 0 0 0
elsif (ref $_[10] eq 'ARRAY') { } 0 0 0
202 0 %args = %{ $_[ARG0] };
203 }
204 elsif ( ref $_[ARG0] eq 'ARRAY' ) {
205 0 %args = @{ $_[ARG0] };
206 }
207 else {
208 0 %args = @_[ARG0..$#_];
209 }
210 0 $args{lc $_} = delete $args{$_} for keys %args;
211 0 my $data = delete $self->{sessions}->{ $sender_id };
212 0 ▶ $kernel->refcount_decrement( $sender_id, __PACKAGE__ ) if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 0 0 0
213 0 return;
214 }
215  
216 sub _shutdown {
217 3 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _shutdown: called CC=4 SCAR=13.9
218 3 delete $self->{listener};
219 3 delete $self->{clients};
220 3 $kernel->refcount_decrement( $_, __PACKAGE__ ) for keys %{ $self->{sessions} };
221 3 $kernel->alarm_remove_all();
222 3 $kernel->alias_remove( $_ ) for $kernel->alias_list();
223 3 ▶ $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 3 0 3
224 3 return;
225 }
226  
227 sub _accept_failed {
228 0 ▶ my ($kernel,$self,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,OBJECT,ARG0..ARG3];
Subroutine _accept_failed: not called CC=1 SCAR=6.9
229 0 warn "Listener: $wheel_id generated $operation error $errnum: $errstr\n";
230 0 delete $self->{listener};
231 0 $kernel->yield( '_shutdown' );
232 0 return;
233 }
234  
235 sub _accept_client {
236 3 ▶ my ($kernel,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,OBJECT,ARG0..ARG2];
Subroutine _accept_client: called CC=4 SCAR=14.0
237 3 my $sockaddr = inet_ntoa( ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[1] );
238 3 my $sockport = ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[0];
239 3 $peeraddr = inet_ntoa( $peeraddr );
240  
241 3 ▶ return unless grep { $_->match( $peeraddr ) } @{ $self->{access} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->match($peeraddr);} @{$$self{"access"};} 0 3 3
242  
243 my $wheel = POE::Wheel::ReadWrite->new(
244 Handle => $socket,
245 Filter => $self->{filter},
246 3 InputEvent => '_conn_input',
247 ErrorEvent => '_conn_error',
248 FlushedEvent => '_conn_flushed',
249 );
250  
251 3 ▶ return unless $wheel;
Branch: 1 branch
BranchTrueFalseTotal
unless $wheel 0 3 3
252
253 3 my $id = $wheel->ID();
254 3 my $time = time();
255 3 my $iv = join '', random_uniform_integer(128,0,9);
256 3 my $init_packet = $iv . pack 'N', $time;
257 3 $self->{clients}->{ $id } =
258 {
259 wheel => $wheel,
260 peeraddr => $peeraddr,
261 peerport => $peerport,
262 sockaddr => $sockaddr,
263 sockport => $sockport,
264 ts => $time,
265 iv => $iv,
266 };
267 3 ▶ $self->{clients}->{ $id }->{alarm} = $kernel->delay_set( '_conn_alarm', $self->{time_out} || 60, $id );
Condition50%1/2
$self->{'time_out'} || 60
l!l
30
MC/DC0%0/1
$self->{'time_out'} || 60
$self->{'time_out'}
Truth table50%1/2
A
A= $self->{'time_out'}
Aresult
11
00
268 3 $wheel->put( $init_packet );
269 3 return;
270 }
271  
272 sub _conn_exists {
273 3 ▶ my ($self,$wheel_id) = @_;
Subroutine _conn_exists: called CC=3 SCAR=12.5
274 3 ▶ return 0 unless $wheel_id and defined $self->{clients}->{ $wheel_id };
Branch: 1 branch
BranchTrueFalseTotal
unless $wheel_id and defined $self->{"clients"}{$wheel_id} 0 3 3
Condition33%1/3
$wheel_id and defined $self->{"clients"}{$wheel_id}
!ll&&!rl&&r
003
MC/DC0%0/2
$wheel_id and defined $self->{"clients"}{$wheel_id}
$wheel_id defined $self->{"clients"}{$wheel_id}
Truth table33%1/3
A and B
A= $wheel_idB= defined $self->{"clients"}{$wheel_id}
ABresult
0X0
100
111
275 3 return 1;
276 }
277  
278 sub _conn_error {
279 0 ▶ my ($self,$errstr,$id) = @_[OBJECT,ARG2,ARG3];
Subroutine _conn_error: not called CC=2 SCAR=17.9
280 0 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 0 0
281 0 delete $self->{clients}->{ $id };
282 0 return;
283 }
284  
285 sub _conn_alarm {
286 1 ▶ my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0];
Subroutine _conn_alarm: called CC=2 SCAR=7.0
287 1 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 1 1
288 1 delete $self->{clients}->{ $id };
289 1 return;
290 }
291  
292 sub _conn_input {
293 2 ▶ my ($kernel,$self,$packet,$id) = @_[KERNEL,OBJECT,ARG0,ARG1];
Subroutine _conn_input: called CC=9 SCAR=22.1
294 2 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 2 2
295 2 my $client = delete $self->{clients}->{ $id };
296 2 delete $client->{wheel};
297 2 $kernel->alarm_remove( delete $client->{alarm} );
298 2 ▶ return unless length( $packet ) == SIZEOF_DATA_PACKET; # wrong packet size received
Branch: 1 branch
BranchTrueFalseTotal
unless length $packet == 720 0 2 2
299 2 my $input = _decrypt( $packet, $self->{encryption}, $client->{iv}, $self->{password} );
300 2 ▶ return unless $input; # something wrong with the decryption
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 2 2
301 2 my $version = unpack 'n', substr $input, 0, 4;
302 2 ▶ return unless $version == 3; # Wrong version received
Branch: 1 branch
BranchTrueFalseTotal
unless $version == 3 0 2 2
303 2 my $crc32 = unpack 'N', substr $input, 4, 4;
304 2 my $ts = unpack 'N', substr $input, 8, 4;
305 2 my $rc = unpack 'n', substr $input, 12, 2;
306 2 my $firstbit = substr $input, 0, 4;
307 2 my $secondbit = substr $input, 8;
308 2 my $checksum = _calculate_crc32( $firstbit . pack('N', 0) . $secondbit );
309 2 my @data = unpack 'a[64]a[128]a[512]', substr $input, 14;
310 2 s/\000.*$// for @data;
311 2 $client->{version} = $version;
312 2 $client->{crc32} = $crc32;
313 2 $client->{checksum} = $checksum;
314 2 $client->{return_code} = $rc;
315 2 $client->{$_} = shift @data for qw(host_name svc_description plugin_output);
316 2 delete $client->{$_} for qw(peerport sockaddr sockport ts iv);
317 $kernel->post( $_, $self->{sessions}->{$_}->{event}, $client, $self->{sessions}->{$_}->{context} )
318 2 for keys %{ $self->{sessions} };
319 2 return;
320 }
321  
322 #/* calculates the CRC 32 value for a buffer */
323 sub _calculate_crc32 {
324 2 ▶ my $string = shift;
Subroutine _calculate_crc32: called CC=2 SCAR=6.9
325  
326 2 my $crc32_table = _generate_crc32_table();
327 2 my $crc = 0xFFFFFFFF;
328  
329 2 foreach my $tchar (split(//, $string)) {
330 1440 my $char = ord($tchar);
331 1440 $crc = (($crc >> 8) & 0x00FFFFFF) ^ $crc32_table->[($crc ^ $char) & 0xFF];
332 }
333  
334 2 return ($crc ^ 0xFFFFFFFF);
335 }
336  
337 #/* build the crc table - must be called before calculating the crc value */
338 sub _generate_crc32_table {
339 2 ▶ my $crc32_table = [];
Subroutine _generate_crc32_table: called CC=4 SCAR=13.9
340 2 my $poly = 0xEDB88320;
341  
342 2 ▶ for (my $i = 0; $i < 256; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 256) 512 2 514
343 512 my $crc = $i;
344 512 ▶ for (my $j = 8; $j > 0; $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > 0) 4096 512 4608
345 4096 ▶ if ($crc & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crc & 1) { } 2048 2048 4096
346 2048 $crc = ($crc >> 1) ^ $poly;
347 }
348 else {
349 2048 $crc = ($crc >> 1);
350 }
351 }
352 512 $crc32_table->[$i] = $crc;
353 }
354 2 return $crc32_table;
355 }
356  
357 # central switchboard for encryption methods.
358 sub _decrypt {
359 2 ▶ my ($data_packet_string, $encryption_method, $iv_salt, $password) = @_;
Subroutine _decrypt: called CC=3 SCAR=11.2
360  
361 2 my $crypted;
362 2 ▶ if ($encryption_method == ENCRYPT_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($encryption_method == 0) { } 1 1 2
elsif ($encryption_method == 1) { } 1 0 1
363 1 $crypted = $data_packet_string;
364 }
365 elsif ($encryption_method == ENCRYPT_XOR) {
366 1 $crypted = _decrypt_xor($data_packet_string, $iv_salt, $password);
367 }
368 else {
369 0 $crypted = _decrypt_mcrypt( $data_packet_string, $encryption_method, $iv_salt, $password );
370 }
371 2 return $crypted;
372 }
373  
374 sub _decrypt_xor {
375 1 ▶ my ($data_packet_string, $iv_salt, $password) = @_;
Subroutine _decrypt_xor: called CC=3 SCAR=11.0
376  
377 1 my @out = split(//, $data_packet_string);
378 1 my @salt_iv = split(//, $iv_salt);
379 1 my @salt_pw = split(//, $password);
380  
381 1 my $y = 0;
382 1 my $x = 0;
383  
384 #/* rotate over IV we received from the server... */
385 1 ▶ while ($y < SIZEOF_DATA_PACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 720) 720 1 721
386 #/* keep rotating over IV */
387 720 $out[$y] = $out[$y] ^ $salt_iv[$x % scalar(@salt_iv)];
388  
389 720 $y++;
390 720 $x++;
391 }
392  
393 #/* rotate over password... */
394 1 $y=0;
395 1 $x=0;
396 1 ▶ while ($y < SIZEOF_DATA_PACKET){
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 720) 720 1 721
397 #/* keep rotating over password */
398 720 $out[$y] = $out[$y] ^ $salt_pw[$x % scalar(@salt_pw)];
399  
400 720 $y++;
401 720 $x++;
402 }
403 1 return join '', @out;
404 }
405  
406 sub _decrypt_mcrypt {
407 0 ▶ my ( $data_packet_string, $encryption_method, $iv_salt, $password ) = @_;
Subroutine _decrypt_mcrypt: not called CC=6 SCAR=37.4
408 0 my $crypted;
409 0 my $evalok = 0;
410 0 ▶ if( $HAVE_MCRYPT ){
Branch: 1 branch
BranchTrueFalseTotal
if ($HAVE_MCRYPT) 0 0 0
411 # Initialise the routine
412 0 ▶ if( defined( $mcrypts{$encryption_method} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mcrypts{$encryption_method}) 0 0 0
413 # Load the routine.
414 0 my $routine = $mcrypts{$encryption_method};
415 0 eval {
416 # This sometimes dies with 'mcrypt is not of type MCRYPT'.
417 0 my $td = Mcrypt->new( algorithm => $routine, mode => 'cfb', verbose => 0 );
418 0 my $key = $password;
419 0 my $iv = substr $iv_salt, 0, $td->{IV_SIZE};
420 0 ▶ if( defined( $td ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $td) 0 0 0
421 0 $td->init($key, $iv);
422 0 ▶ for (my $i = 0; $i < length( $data_packet_string ); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $data_packet_string) 0 0 0
423 0 $crypted .= $td->decrypt( substr $data_packet_string, 0+$i, 1 );
424 }
425 0 $td->end();
426 }
427 0 $evalok++;
428 };
429 0 ▶ warn "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
430 }
431 }
432 0 return $crypted;
433 }
434  
435 5 'Hon beiriant goes at hun ar ddeg';
436  
437 __END__
↑ Summary