Country/Fast.pm

filter: Statementstmt 94.5% 70 / 74 Branchbran 80.0% 24 / 30 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 4 totaltotal 81.7% 103 / 126 CC 12 SCAR 16.0
CC
12
Cov
87%
inet_ntocc
8.1
inet_atocc
4.9
new
3.1
CRAP 5.0
« Authority.pm ↑ Summary Country.pm »
1 package IP::Country::Fast;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Socket qw ( inet_aton );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use vars qw ( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 $VERSION = '604.001'; # APR 2006, version 001
8  
9 4 my $singleton = undef;
10 4 my $ip_db;
11 4 my $null = substr(pack('N',0),0,1);
12 4 my $nullnullnull = $null . $null . $null;
13 4 my %cc;
14 4 my $tld_match = qr/\.([a-zA-Z][a-zA-Z])$/o;
15 4 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;
16  
17 4 my $bit0;
18 my $bit1;
19 4 my @mask;
20 4 my @dtoc;
21 {
22 4 $bit0 = substr(pack('N',2 ** 31),0,1);
23 4 $bit1 = substr(pack('N',2 ** 30),0,1);
24  
25 4 ▶ for (my $i = 0; $i <= 31; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 31) 128 4 132
26 128 $mask[$i] = pack('N',2 ** (31 - $i));
27 }
28  
29 4 ▶ for (my $i = 0; $i <= 255; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 255) 1024 4 1028
30 1024 $dtoc[$i] = substr(pack('N',$i),3,1);
31 }
32 4 (my $module_dir = __FILE__) =~ s/\.pm$//;
33  
34 4 local $/; # set it so <> reads all the file at once
35  
36 4 ▶ open (CC, "< $module_dir/cc.gif")
Branch: 1 branch
BranchTrueFalseTotal
open CC, "< $module_dir/cc.gif" or die "couldn't read country database: $!" 4 0 4
37 or die ("couldn't read country database: $!");
38 4 binmode CC;
39 4 my $cc_ultra = <CC>; # read in the file
40 4 close CC;
41 4 my $cc_num = (length $cc_ultra) / 3;
42 4 ▶ for (my $i = 0; $i < $cc_num; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cc_num) 1020 4 1024
43 1020 my $cc = substr($cc_ultra,3 * $i + 1,2);
44 1020 ▶ $cc = undef if ($cc eq '--');
Branch: 1 branch
BranchTrueFalseTotal
if $cc eq "--" 4 1016 1020
45 1020 $cc{substr($cc_ultra,3 * $i,1)} = $cc;
46 }
47  
48 4 ▶ open (IP, "< $module_dir/ip.gif")
Branch: 1 branch
BranchTrueFalseTotal
open IP, "< $module_dir/ip.gif" or die "couldn't read IP database: $!" 4 0 4
49 or die ("couldn't read IP database: $!");
50 4 binmode IP;
51 4 $ip_db = <IP>;
52 4 close IP;
53 }
54  
55 sub new ()
56 4 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
57 4 my $caller = shift;
58 4 ▶ unless (defined $singleton){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $singleton) 3 1 4
59 3 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
60 3 $singleton = bless {}, $class;
61 }
62 4 return $singleton;
63 }
64  
65 sub db_time
66 0 ▶{
Subroutine db_time: not called CC=1 SCAR=6.9
Pod undocumented
67 0 return unpack("N",substr($ip_db,0,4));
68 }
69  
70 sub inet_atocc
71 65563 ▶{
Subroutine inet_atocc: called CC=3 SCAR=16.0
Pod undocumented
72 65563 my $inet_a = $_[1];
73 65563 ▶ if ($inet_a =~ $ip_match){
Branch: 1 branch
BranchTrueFalseTotal
if ($inet_a =~ /$ip_match/) { } 65563 0 65563
74 65563 return inet_ntocc($dtoc[$1].$dtoc[$2].$dtoc[$3].$dtoc[$4]);
75 } else {
76 0 ▶ if (defined (my $n = inet_aton($inet_a))){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = inet_aton($inet_a))) { } 0 0 0
77 0 return inet_ntocc($n);
78 } else {
79 0 return undef;
80 }
81 }
82 }
83  
84 sub inet_ntocc
85 65592 ▶{
Subroutine inet_ntocc: called CC=8 SCAR=20.9
Pod undocumented
86 # FORMATTING OF EACH NODE IN $ip_db
87 # bit0 - true if this is a country code, false if this
88 # is a jump to the next node
89 #
90 # country codes:
91 # bit1 - true if the country code is stored in bits 2-7
92 # of this byte, false if the country code is
93 # stored in bits 0-7 of the next byte
94 # bits 2-7 or bits 0-7 of next byte contain country code
95 #
96 # jumps:
97 # bytes 0-3 jump distance (only first byte used if
98 # distance < 64)
99  
100 65592 ▶ my $inet_n = $_[1] || $_[0];
Condition33%1/3
$_[1] || $_[0]
l!l&&r!l&&!r
0655920
MC/DC0%0/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table33%1/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
101  
102 65592 my $pos = 4;
103 65592 my $byte_zero = substr($ip_db,$pos,1);
104 # loop through bits of IP address
105 65592 ▶ for (my $i = 0; $i <= 31; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 31) 737878 0 737878
106  
107 737878 ▶ if (($inet_n & $mask[$i]) eq $mask[$i]){
Branch: 1 branch
BranchTrueFalseTotal
if (($inet_n & $mask[$i]) eq $mask[$i]) { } 368290 369588 737878
108 # bit[$i] is set [binary one]
109 # - jump to next node
110 # (start of child[1] node)
111 368290 ▶ if (($byte_zero & $bit1) eq $bit1){
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit1) eq $bit1) { } 85498 282792 368290
112 85498 $pos = $pos + 1 + unpack('N', $nullnullnull . ($byte_zero ^ $bit1));
113 } else {
114 282792 $pos = $pos + 3 + unpack('N', $null . substr($ip_db,$pos,3));
115 }
116 } else {
117 # bit[$i] is unset [binary zero]
118 # jump to end of this node
119 # (start of child[0] node)
120 369588 ▶ if (($byte_zero & $bit1) eq $bit1){
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit1) eq $bit1) { } 85636 283952 369588
121 85636 $pos = $pos + 1;
122 } else {
123 283952 $pos = $pos + 3;
124 }
125 }
126
127 # all terminal nodes of the tree start with zeroth bit
128 # set to zero. the first bit can then be used to indicate
129 # whether we're using the first or second byte to store the
130 # country code
131 737878 $byte_zero = substr($ip_db,$pos,1);
132 737878 ▶ if (($byte_zero & $bit0) eq $bit0){ # country code
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit0) eq $bit0) 65592 672286 737878
133 65592 ▶ if (($byte_zero & $bit1) eq $bit1){
Branch: 1 branch
BranchTrueFalseTotal
if (($byte_zero & $bit1) eq $bit1) { } 2426 63166 65592
134 # unpopular country code - stored in second byte
135 2426 return $cc{substr($ip_db,$pos+1,1)};
136 } else {
137 # popular country code - stored in bits 2-7
138 # (we already know that bit 1 is not set, so
139 # just need to unset bit 1)
140 63166 return $cc{$byte_zero ^ $bit0};
141 }
142 }
143 }
144 }
145  
146 4 1;
147 __END__
« Authority.pm ↑ Summary Country.pm »