Authority.pm

filter: Statementstmt 93.1% 68 / 73 Branchbran 73.3% 22 / 30 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 7 / 8 Podpod 75.0% 3 / 4 totaltotal 81.6% 102 / 125 CC 12 SCAR 16.3
CC
12
Cov
84%
inet_ntoauth
8.3
inet_atoauth
4.9
new
3.2
CRAP 5.1
« Country/MaxMind.pm ↑ Summary Country/Fast.pm »
1 package IP::Authority;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Socket qw ( inet_aton );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use vars qw ( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 $VERSION = '1305.001'; # MAY 2013, version 0.01
8  
9 2 my $singleton = undef;
10 2 my $ip_db;
11 2 my $null = substr(pack('N',0),0,1);
12 2 my $nullnullnull = $null . $null . $null;
13 2 my %auth;
14 2 my $ip_match = qr/^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])$/o;
15  
16 2 my $bit0;
17 my $bit1;
18 2 my @mask;
19 2 my @dtoc;
20 {
21 2 $bit0 = substr(pack('N',2 ** 31),0,1);
22 2 $bit1 = substr(pack('N',2 ** 30),0,1);
23  
24 2 ▶ for (my $i = 0; $i <= 31; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 31) 64 2 66
25 64 $mask[$i] = pack('N',2 ** (31 - $i));
26 }
27  
28 2 ▶ for (my $i = 0; $i <= 255; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 255) 512 2 514
29 512 $dtoc[$i] = substr(pack('N',$i),3,1);
30 }
31 2 (my $module_dir = __FILE__) =~ s/\.pm$//;
32  
33 2 local $/; # set it so <> reads all the file at once
34  
35 2 ▶ open (AUTH, "< $module_dir/auth.gif")
Branch: 1 branch
BranchTrueFalseTotal
open AUTH, "< $module_dir/auth.gif" or die "couldn't read authority database: $!" 2 0 2
36 or die ("couldn't read authority database: $!");
37 2 binmode AUTH;
38 2 my $auth_ultra = <AUTH>; # read in the file
39 2 close AUTH;
40 2 my $auth_num = (length $auth_ultra) / 3;
41 2 ▶ for (my $i = 0; $i < $auth_num; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $auth_num) 14 2 16
42 14 my $auth = substr($auth_ultra,3 * $i + 1,2);
43 14 ▶ $auth = undef if ($auth eq '--');
Branch: 1 branch
BranchTrueFalseTotal
if $auth eq "--" 2 12 14
44 14 $auth{substr($auth_ultra,3 * $i,1)} = $auth;
45 }
46  
47 2 ▶ open (IP, "< $module_dir/ipauth.gif")
Branch: 1 branch
BranchTrueFalseTotal
open IP, "< $module_dir/ipauth.gif" or die "couldn't read IP database: $!" 2 0 2
48 or die ("couldn't read IP database: $!");
49 2 binmode IP;
50 2 $ip_db = <IP>;
51 2 close IP;
52 }
53  
54 sub new ()
55 2 ▶{
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
56 2 my $caller = shift;
57 2 ▶ unless (defined $singleton){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $singleton) 2 0 2
58 2 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
59 2 $singleton = bless {}, $class;
60 }
61 2 return $singleton;
62 }
63  
64 sub inet_atoauth
65 29 ▶{
Subroutine inet_atoauth: called CC=3 SCAR=16.0
Pod documented
66 29 my $inet_a = $_[1];
67 29 ▶ if ($inet_a =~ $ip_match){
Branch: 1 branch
BranchTrueFalseTotal
if ($inet_a =~ /$ip_match/) { } 29 0 29
68 29 return inet_ntoauth($dtoc[$1].$dtoc[$2].$dtoc[$3].$dtoc[$4]);
69 } else {
70 0 ▶ if (defined (my $n = inet_aton($inet_a))){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = inet_aton($inet_a))) { } 0 0 0
71 0 return inet_ntoauth($n);
72 } else {
73 0 return undef;
74 }
75 }
76 }
77  
78 sub db_time
79 0 ▶{
Subroutine db_time: not called CC=1 SCAR=6.9
Pod documented
80 0 return unpack("N",substr($ip_db,0,4));
81 }
82  
83 sub inet_ntoauth
84 29 ▶{
Subroutine inet_ntoauth: called CC=8 SCAR=21.1
Pod documented
85 29 ▶ my $inet_n = $_[1] || $_[0];
Condition33%1/3
$_[1] || $_[0]
l!l&&r!l&&!r
0290
MC/DC0%0/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table33%1/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
86  
87 29 my $pos = 4;
88 29 my $byte_zero = substr($ip_db,$pos,1);
89 # loop through bits of IP address
90 29 ▶ for (my $i = 0; $i <= 31; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 31) 411 0 411
91  
92 411 ▶ if (($inet_n & $mask[$i]) eq $mask[$i]){
Branch: 1 branch
BranchTrueFalseTotal
if (($inet_n & $mask[$i]) eq $mask[$i]) { } 198 213 411
93 # bit[$i] is set [binary one]
94 # - jump to next node
95 # (start of child[1] node)
96 198 ▶ if (($byte_zero & $bit1) eq $bit1){
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit1) eq $bit1) { } 61 137 198
97 61 $pos = $pos + 1 + unpack('N', $nullnullnull . ($byte_zero ^ $bit1));
98 } else {
99 137 $pos = $pos + 3 + unpack('N', $null . substr($ip_db,$pos,3));
100 }
101 } else {
102 # bit[$i] is unset [binary zero]
103 # jump to end of this node
104 # (start of child[0] node)
105 213 ▶ if (($byte_zero & $bit1) eq $bit1){
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit1) eq $bit1) { } 31 182 213
106 31 $pos = $pos + 1;
107 } else {
108 182 $pos = $pos + 3;
109 }
110 }
111
112 # all terminal nodes of the tree start with zeroth bit
113 # set to zero. the first bit can then be used to indicate
114 # whether we're using the first or second byte to store the
115 # country code
116 411 $byte_zero = substr($ip_db,$pos,1);
117 411 ▶ if (($byte_zero & $bit0) eq $bit0){ # country code
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit0) eq $bit0) 29 382 411
118 29 ▶ if (($byte_zero & $bit1) eq $bit1){
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit1) eq $bit1) { } 0 29 29
119 # unpopular country code - stored in second byte
120 0 return $auth{substr($ip_db,$pos+1,1)};
121 } else {
122 # popular country code - stored in bits 2-7
123 # (we already know that bit 1 is not set, so
124 # just need to unset bit 1)
125 29 return $auth{$byte_zero ^ $bit0};
126 }
127 }
128 }
129 }
130  
131 2 1;
132 __END__
« Country/MaxMind.pm ↑ Summary Country/Fast.pm »