blib/lib/Geo/IP/PurePerl.pm

filter: Statementstmt 29.1% 125 / 429 Branchbran 1.3% 2 / 146 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 55.2% 37 / 67 Podpod 70.8% 17 / 24 totaltotal 24.7% 181 / 730 SCAR 83.1
CC
89
Cov
21%
_setup_segments
342.0
get_city_record
272.0
open
90.0
CRAP 4056.5
↑ Summary
1 package Geo::IP::PurePerl;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN {
8 1 ▶ if ( $] >= 5.008 ) {
Subroutine BEGIN: called CC=2 SCAR=10.3
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.008) { } 1 0 1
9 1 require Encode;
10 1 Encode->import(qw/ decode /);
11 }
12 else {
13 *decode = sub {
14 0 ▶ local $_ = $_[1];
Subroutine __ANON__: not called CC=2 SCAR=12.6
15 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 s/([\x80-\xff])/my $c = ord($1);
17 0 ▶ my $p = $c >= 192 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c >= 192 ? : 0 0 0
18 0 pack ( 'CC' => 0xc2 + $p , $c & ~0x40 ); /ge;
19 0 return $_;
20 0 };
21 }
22 };
23  
24  
25 1 ▶use vars qw( @ISA $VERSION @EXPORT $OPEN_TYPE_PATH );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use constant GEOIP_CHARSET_ISO_8859_1 => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant GEOIP_CHARSET_UTF8 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use constant FULL_RECORD_LENGTH => 50;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant GEOIP_COUNTRY_BEGIN => 16776960;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant RECORD_LENGTH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant GEOIP_STATE_BEGIN_REV0 => 16700000;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant GEOIP_STATE_BEGIN_REV1 => 16000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant STRUCTURE_INFO_MAX_SIZE => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant DATABASE_INFO_MAX_SIZE => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant GEOIP_COUNTRY_EDITION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use constant GEOIP_REGION_EDITION_REV0 => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant GEOIP_REGION_EDITION_REV1 => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant GEOIP_CITY_EDITION_REV0 => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant GEOIP_CITY_EDITION_REV1 => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use constant GEOIP_ORG_EDITION => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use constant GEOIP_ISP_EDITION => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant GEOIP_PROXY_EDITION => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant GEOIP_ASNUM_EDITION => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant GEOIP_NETSPEED_EDITION => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant GEOIP_DOMAIN_EDITION => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant SEGMENT_RECORD_LENGTH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant STANDARD_RECORD_LENGTH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant ORG_RECORD_LENGTH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant MAX_RECORD_LENGTH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use constant MAX_ORG_RECORD_LENGTH => 300;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use constant US_OFFSET => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant CANADA_OFFSET => 677;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant WORLD_OFFSET => 1353;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant FIPS_RANGE => 360;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 $VERSION = '1.25';
59  
60 1 require Exporter;
61 1 @ISA = qw(Exporter);
62  
63 # cheat --- try to load Sys::Mmap
64 BEGIN {
65 1 ▶ eval {
Subroutine BEGIN: called CC=3 SCAR=11.1
66 # wrap into eval again, as workaround for centos / mod_perl issue
67 # seems they use $@ without eval somewhere
68
69 eval "require Sys::Mmap"
70 ? Sys::Mmap->import
71 1 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
eval 'require Sys::Mmap' ? : 0 1 1
72 1 for (qw/ PROT_READ MAP_PRIVATE MAP_SHARED /) {
73 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 3 my $unused_stub = $_; # we must use a copy
75 3 ▶ *$unused_stub = sub { die 'Sys::Mmap required for mmap support' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
76 } # for
77 }; # do
78 1 1;
79 }; # eval
80 } # begin
81  
82  
83 sub GEOIP_STANDARD(){0;}
84 sub GEOIP_MEMORY_CACHE(){1;}
85  
86 #sub GEOIP_CHECK_CACHE(){2;}
87 #sub GEOIP_INDEX_CACHE(){4;}
88 sub GEOIP_MMAP_CACHE(){8;}
89  
90 sub GEOIP_UNKNOWN_SPEED(){0;}
91 sub GEOIP_DIALUP_SPEED(){1;}
92 sub GEOIP_CABLEDSL_SPEED(){2;}
93 sub GEOIP_CORPORATE_SPEED(){3;}
94  
95 1 @EXPORT = qw( GEOIP_STANDARD GEOIP_MEMORY_CACHE GEOIP_MMAP_CACHE
96 GEOIP_UNKNOWN_SPEED GEOIP_DIALUP_SPEED GEOIP_CABLEDSL_SPEED GEOIP_CORPORATE_SPEED );
97 1 my @countries =
98 (undef,"AP","EU","AD","AE","AF","AG","AI","AL","AM","AN","AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO","BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD","CF","CG","CH","CI","CK","CL","CM","CN","CO","CR","CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO","DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ","FK","FM","FO","FR","FX","GA","GB","GD","GE","GF","GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HM","HN","HR","HT","HU","ID","IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO","JP","KE","KG","KH","KI","KM","KN","KP","KR","KW","KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT","LU","LV","LY","MA","MC","MD","MG","MH","MK","ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI","NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PW","PY","QA","RE","RO","RU","RW","SA","SB","SC","SD","SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO","SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH","TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW","TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA","ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE","BL","MF");
99 1 my @code3s = ( undef,"AP","EU","AND","ARE","AFG","ATG","AIA","ALB","ARM","ANT","AGO","AQ","ARG","ASM","AUT","AUS","ABW","AZE","BIH","BRB","BGD","BEL","BFA","BGR","BHR","BDI","BEN","BMU","BRN","BOL","BRA","BHS","BTN","BV","BWA","BLR","BLZ","CAN","CC","COD","CAF","COG","CHE","CIV","COK","CHL","CMR","CHN","COL","CRI","CUB","CPV","CX","CYP","CZE","DEU","DJI","DNK","DMA","DOM","DZA","ECU","EST","EGY","ESH","ERI","ESP","ETH","FIN","FJI","FLK","FSM","FRO","FRA","FX","GAB","GBR","GRD","GEO","GUF","GHA","GIB","GRL","GMB","GIN","GLP","GNQ","GRC","GS","GTM","GUM","GNB","GUY","HKG","HM","HND","HRV","HTI","HUN","IDN","IRL","ISR","IND","IO","IRQ","IRN","ISL","ITA","JAM","JOR","JPN","KEN","KGZ","KHM","KIR","COM","KNA","PRK","KOR","KWT","CYM","KAZ","LAO","LBN","LCA","LIE","LKA","LBR","LSO","LTU","LUX","LVA","LBY","MAR","MCO","MDA","MDG","MHL","MKD","MLI","MMR","MNG","MAC","MNP","MTQ","MRT","MSR","MLT","MUS","MDV","MWI","MEX","MYS","MOZ","NAM","NCL","NER","NFK","NGA","NIC","NLD","NOR","NPL","NRU","NIU","NZL","OMN","PAN","PER","PYF","PNG","PHL","PAK","POL","SPM","PCN","PRI","PSE","PRT","PLW","PRY","QAT","REU","ROU","RUS","RWA","SAU","SLB","SYC","SDN","SWE","SGP","SHN","SVN","SJM","SVK","SLE","SMR","SEN","SOM","SUR","STP","SLV","SYR","SWZ","TCA","TCD","TF","TGO","THA","TJK","TKL","TKM","TUN","TON","TLS","TUR","TTO","TUV","TWN","TZA","UKR","UGA","UM","USA","URY","UZB","VAT","VCT","VEN","VGB","VIR","VNM","VUT","WLF","WSM","YEM","YT","SRB","ZAF","ZMB","MNE","ZWE","A1","A2","O1","ALA","GGY","IMN","JEY","BLM","MAF");
100 1 my @names = (undef,"Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda",
101 "Anguilla","Albania","Armenia","Netherlands Antilles","Angola","Antarctica","Argentina","American Samoa",
102 "Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados","Bangladesh","Belgium","Burkina Faso",
103 "Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia","Brazil","Bahamas","Bhutan","Bouvet Island",
104 "Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the","Central African Republic",
105 "Congo","Switzerland","Cote D'Ivoire","Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica","Cuba","Cape Verde",
106 "Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic","Algeria","Ecuador",
107 "Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji","Falkland Islands (Malvinas)",
108 "Micronesia, Federated States of","Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom","Grenada","Georgia",
109 "French Guiana","Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece",
110 "South Georgia and the South Sandwich Islands","Guatemala","Guam","Guinea-Bissau","Guyana","Hong Kong",
111 "Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia","Ireland","Israel","India",
112 "British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan","Japan","Kenya",
113 "Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis","Korea, Democratic People's Republic of","Korea, Republic of",
114 "Kuwait","Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka",
115 "Liberia","Lesotho","Lithuania","Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco","Moldova, Republic of",
116 "Madagascar","Marshall Islands","Macedonia","Mali","Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique",
117 "Mauritania","Montserrat","Malta","Mauritius","Maldives","Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia",
118 "Niger","Norfolk Island","Nigeria","Nicaragua","Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru",
119 "French Polynesia","Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico",
120 "Palestinian Territory","Portugal","Palau","Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia",
121 "Solomon Islands","Seychelles","Sudan","Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone",
122 "San Marino","Senegal","Somalia","Suriname","Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland",
123 "Turks and Caicos Islands","Chad","French Southern Territories","Togo","Thailand","Tajikistan","Tokelau","Turkmenistan","Tunisia",
124 "Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan","Tanzania, United Republic of","Ukraine","Uganda",
125 "United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)",
126 "Saint Vincent and the Grenadines","Venezuela","Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu",
127 "Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa","Zambia","Montenegro","Zimbabwe","Anonymous Proxy",
128 "Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy","Saint Martin");
129  
130  
131 # --- unfortunately we do not know the path so we assume the
132 # default path /usr/local/share/GeoIP
133 # if thats not true, you can set $Geo::IP::PurePerl::OPEN_TYPE_PATH
134 #
135 sub open_type {
136 0 ▶ my ( $class, $type, $flags ) = @_;
Subroutine open_type: not called CC=6 SCAR=37.4
Pod undocumented
137 0 my %type_dat_name_mapper = (
138 GEOIP_COUNTRY_EDITION() => 'GeoIP',
139 GEOIP_REGION_EDITION_REV0() => 'GeoIPRegion',
140 GEOIP_REGION_EDITION_REV1() => 'GeoIPRegion',
141 GEOIP_CITY_EDITION_REV0() => 'GeoIPCity',
142 GEOIP_CITY_EDITION_REV1() => 'GeoIPCity',
143 GEOIP_ISP_EDITION() => 'GeoIPISP',
144 GEOIP_ORG_EDITION() => 'GeoIPOrg',
145 GEOIP_PROXY_EDITION() => 'GeoIPProxy',
146 GEOIP_ASNUM_EDITION() => 'GeoIPASNum',
147 GEOIP_NETSPEED_EDITION() => 'GeoIPNetSpeed',
148 GEOIP_DOMAIN_EDITION() => 'GeoIPDomain',
149 );
150  
151 # backward compatibility for 2003 databases.
152 0 ▶ $type -= 105 if $type >= 106;
Branch: 1 branch
BranchTrueFalseTotal
if $type >= 106 0 0 0
153  
154 0 my $name = $type_dat_name_mapper{$type};
155 0 ▶ die("Invalid database type $type\n") unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
156  
157 0 ▶ my $mkpath = sub { File::Spec->catfile( File::Spec->rootdir, @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
158  
159 my $path =
160 defined $Geo::IP::PurePerl::OPEN_TYPE_PATH
161 ? $Geo::IP::PurePerl::OPEN_TYPE_PATH
162 0 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $Geo::IP::PurePerl::OPEN_TYPE_PATH ? : 0 0 0
163 $^O eq 'NetWare'
164 ? $mkpath->(qw/ etc GeoIP /)
165 0 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'NetWare' ? : 0 0 0
166 0 ▶ $^O eq 'MSWin32'
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
167 ? $mkpath->(qw/ GeoIP /)
168 : $mkpath->(qw/ usr local share GeoIP /);
169 }
170 };
171  
172 0 my $filename = File::Spec->catfile( $path, $name . '.dat' );
173 0 return $class->open( $filename, $flags );
174 }
175  
176  
177 sub open {
178 0 ▶ die "Geo::IP::PurePerl::open() requires a path name"
Subroutine open: not called CC=9 SCAR=45.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 and $_[1] 0 0 0
Condition0%0/3
@_ > 1 and $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and $_[1]
@_ > 1 $_[1]
Truth table0%0/3
A and B
A= @_ > 1B= $_[1]
ABresult
0X0
100
111
179 unless( @_ > 1 and $_[1] );
180 0 my ($class, $db_file, $flags) = @_;
181 0 my $fh = FileHandle->new;
182 0 my $gi;
183 0 ▶ CORE::open $fh, $db_file or die "Error opening $db_file";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, $db_file or die "Error opening $db_file" 0 0 0
184 0 binmode($fh);
185 0 ▶ if ( $flags && ( $flags & ( GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags and $flags & 9) { } 0 0 0
Condition0%0/3
$flags and $flags & 9
!ll&&!rl&&r
000
MC/DC0%0/2
$flags and $flags & 9
$flags $flags & 9
Truth table0%0/3
A and B
A= $flagsB= $flags & 9
ABresult
0X0
100
111
186 0 my %self;
187  
188 0 ▶ if ( $flags & GEOIP_MMAP_CACHE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 8) { } 0 0 0
189 0 ▶ die "Sys::Mmap required for MMAP support"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Sys::Mmap::VERSION 0 0 0
190 unless defined $Sys::Mmap::VERSION;
191 0 ▶ mmap( $self{buf} = undef, 0, PROT_READ, MAP_PRIVATE, $fh )
Branch: 1 branch
BranchTrueFalseTotal
mmap($self{"buf"} = undef, 0, PROT_READ(), MAP_PRIVATE(), $fh) or die "mmap: $!" 0 0 0
192 or die "mmap: $!";
193 }
194 else {
195 0 local $/ = undef;
196 0 $self{buf} = <$fh>;
197 }
198 0 $self{fh} = $fh;
199 0 $gi = bless \%self, $class;
200 }
201 else {
202 0 $gi = bless { fh => $fh }, $class;
203 }
204 0 $gi->_setup_segments();
205 0 return $gi;
206 }
207  
208 sub new {
209 0 ▶ my ($class, $db_file, $flags) = @_;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
210 # this will be less messy once deprecated new( $path, [$flags] )
211 # is no longer supported (that's what open() is for)
212  
213 0 my $def_db_file = '/usr/local/share/GeoIP/GeoIP.dat';
214 0 ▶ if ($^O eq 'NetWare') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'NetWare') { } 0 0 0
elsif ($^O eq 'MSWin32') { } 0 0 0
215 0 $def_db_file = 'sys:/etc/GeoIP/GeoIP.dat';
216 } elsif ($^O eq 'MSWin32') {
217 0 $def_db_file = 'c:/GeoIP/GeoIP.dat';
218 }
219 0 ▶ if ( !defined $db_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $db_file) { } 0 0 0
elsif ($db_file =~ /^\d+$/) { } 0 0 0
220 # called as new()
221 0 $db_file = $def_db_file;
222 } elsif ( $db_file =~ /^\d+$/ ) {
223 # db_file is GEOIP_MEMORY_CACHE or GEOIP_STANDARD
224 # called as new( $flags )
225 0 $flags = $db_file;
226 0 $db_file = $def_db_file;
227 } # else called as new( $database_filename, [$flags] );
228  
229 0 $class->open( $db_file, $flags );
230 }
231  
232 #this function setups the database segments
233 sub _setup_segments {
234 0 ▶ my ($gi) = @_;
Subroutine _setup_segments: not called CC=18 SCAR=58.3
235 0 my $a = 0;
236 0 my $i = 0;
237 0 my $j = 0;
238 0 my $delim;
239 my $buf;
240 0 $gi->{_charset} = GEOIP_CHARSET_ISO_8859_1;
241 0 $gi->{"databaseType"} = GEOIP_COUNTRY_EDITION;
242 0 $gi->{"record_length"} = STANDARD_RECORD_LENGTH;
243  
244 0 my $filepos = tell($gi->{fh});
245 0 seek($gi->{fh}, -3, 2);
246 0 ▶ for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 20) 0 0 0
247 0 read($gi->{fh},$delim,3);
248
249 #find the delim
250 0 ▶ if ($delim eq (chr(255).chr(255).chr(255))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delim eq "\377\377\377") { } 0 0 0
251 0 read($gi->{fh},$a,1);
252
253 #read the databasetype
254 0 $gi->{"databaseType"} = ord($a);
255  
256 # backward compatibility for 2003 databases.
257 0 ▶ $gi->{databaseType} -= 105 if $gi->{databaseType} >= 106;
Branch: 1 branch
BranchTrueFalseTotal
if $gi->{"databaseType"} >= 106 0 0 0
258  
259 #chose the database segment for the database type
260 #if database Type is GEOIP_REGION_EDITION then use database segment GEOIP_STATE_BEGIN
261 0 ▶ if ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($gi->{'databaseType'} == 7) { } 0 0 0
elsif ($gi->{'databaseType'} == 3) { } 0 0 0
elsif ($gi->{'databaseType'} == 6 or $gi->{'databaseType'} == 2 or $gi->{'databaseType'} == 5 or $gi->{'databaseType'} == 9 or $gi->{'databaseType'} == 11 or $gi->{'databaseType'} == 4) { } 0 0 0
262 0 $gi->{"databaseSegments"} = GEOIP_STATE_BEGIN_REV0;
263 } elsif ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV1) {
264 0 $gi->{"databaseSegments"} = GEOIP_STATE_BEGIN_REV1;
265 }
266  
267 #if database Type is GEOIP_CITY_EDITION, GEOIP_ISP_EDITION or GEOIP_ORG_EDITION then
268 #read in the database segment
269 ▶ elsif (($gi->{"databaseType"} == GEOIP_CITY_EDITION_REV0) ||
Condition0%0/15
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9 or $gi->{"databaseType"} == 11 or $gi->{"databaseType"} == 4
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9 or $gi->{"databaseType"} == 11
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2
l!l&&r!l&&!r
000
MC/DC0%0/6
$gi->{"databaseType"} == 6 or $gi->{"databaseType"} == 2 or $gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 9 or $gi->{"databaseType"} == 11 or $gi->{"databaseType"} == 4
$gi->{"databaseType"} == 6 $gi->{"databaseType"} == 2 $gi->{"databaseType"} == 5 $gi->{"databaseType"} == 9 $gi->{"databaseType"} == 11 $gi->{"databaseType"} == 4
Truth table0%0/7
A or B or C or D or E or F
A= $gi->{"databaseType"} == 6B= $gi->{"databaseType"} == 2C= $gi->{"databaseType"} == 5D= $gi->{"databaseType"} == 9E= $gi->{"databaseType"} == 11F= $gi->{"databaseType"} == 4
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
270 ($gi->{"databaseType"} == GEOIP_CITY_EDITION_REV1) ||
271 ($gi->{"databaseType"} == GEOIP_ORG_EDITION) ||
272 ($gi->{"databaseType"} == GEOIP_ASNUM_EDITION) ||
273 ($gi->{"databaseType"} == GEOIP_DOMAIN_EDITION) ||
274 ($gi->{"databaseType"} == GEOIP_ISP_EDITION)) {
275 0 $gi->{"databaseSegments"} = 0;
276  
277 #read in the database segment for the database type
278 0 read($gi->{fh},$buf,SEGMENT_RECORD_LENGTH);
279 0 ▶ for ($j = 0;$j < SEGMENT_RECORD_LENGTH;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
280 0 $gi->{"databaseSegments"} += (ord(substr($buf,$j,1)) << ($j * 8));
281 }
282  
283 #record length is four for ISP databases and ORG databases
284 #record length is three for country databases, region database and city databases
285 0 ▶ if ($gi->{"databaseType"} == GEOIP_ORG_EDITION ||
Branch: 1 branch
BranchTrueFalseTotal
if ($gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4 or $gi->{"databaseType"} == 11) 0 0 0
Condition0%0/6
$gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4 or $gi->{"databaseType"} == 11
l!l&&r!l&&!r
000
$gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4
l!l&&r!l&&!r
000
MC/DC0%0/3
$gi->{"databaseType"} == 5 or $gi->{"databaseType"} == 4 or $gi->{"databaseType"} == 11
$gi->{"databaseType"} == 5 $gi->{"databaseType"} == 4 $gi->{"databaseType"} == 11
Truth table0%0/4
A or B or C
A= $gi->{"databaseType"} == 5B= $gi->{"databaseType"} == 4C= $gi->{"databaseType"} == 11
ABCresult
1XX1
01X1
0011
0000
286 $gi->{"databaseType"} == GEOIP_ISP_EDITION ||
287 $gi->{"databaseType"} == GEOIP_DOMAIN_EDITION) {
288 0 $gi->{"record_length"} = ORG_RECORD_LENGTH;
289 }
290 }
291 0 last;
292 } else {
293 0 seek($gi->{fh}, -4 , 1);
294 }
295 }
296 #if database Type is GEOIP_COUNTY_EDITION then use database segment GEOIP_COUNTRY_BEGIN
297 0 ▶ if ($gi->{"databaseType"} == GEOIP_COUNTRY_EDITION ||
Branch: 1 branch
BranchTrueFalseTotal
if ($gi->{"databaseType"} == 1 or $gi->{"databaseType"} == 10) 0 0 0
Condition0%0/3
$gi->{"databaseType"} == 1 or $gi->{"databaseType"} == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
$gi->{"databaseType"} == 1 or $gi->{"databaseType"} == 10
$gi->{"databaseType"} == 1 $gi->{"databaseType"} == 10
Truth table0%0/3
A or B
A= $gi->{"databaseType"} == 1B= $gi->{"databaseType"} == 10
ABresult
1X1
011
000
298 $gi->{"databaseType"} == GEOIP_NETSPEED_EDITION) {
299 0 $gi->{"databaseSegments"} = GEOIP_COUNTRY_BEGIN;
300 }
301 0 seek($gi->{fh},$filepos,0);
302 0 return $gi;
303 }
304  
305 sub _seek_country {
306 0 ▶ my ($gi, $ipnum) = @_;
Subroutine _seek_country: not called CC=6 SCAR=37.4
307  
308 0 my $fh = $gi->{fh};
309 0 my $offset = 0;
310  
311 0 my ($x0, $x1);
312  
313 0 my $reclen = $gi->{"record_length"};
314  
315 0 ▶ for (my $depth = 31; $depth >= 0; $depth--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= 0) 0 0 0
316 0 ▶ unless ( exists $gi->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{'buf'}) { } 0 0 0
317 0 seek $fh, $offset * 2 * $reclen, 0;
318 0 read $fh, $x0, $reclen;
319 0 read $fh, $x1, $reclen;
320 } else {
321
322 0 $x0 = substr($gi->{buf}, $offset * 2 * $reclen, $reclen);
323 0 $x1 = substr($gi->{buf}, $offset * 2 * $reclen + $reclen, $reclen);
324 }
325  
326 0 $x0 = unpack("V1", $x0."\0");
327 0 $x1 = unpack("V1", $x1."\0");
328  
329 0 ▶ if ($ipnum & (1 << $depth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipnum & 1 << $depth) { } 0 0 0
330 0 ▶ if ($x1 >= $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 >= $gi->{"databaseSegments"}) 0 0 0
331 0 $gi->{last_netmask} = 32 - $depth;
332 0 return $x1;
333 }
334 0 $offset = $x1;
335 } else {
336 0 ▶ if ($x0 >= $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x0 >= $gi->{"databaseSegments"}) 0 0 0
337 0 $gi->{last_netmask} = 32 - $depth;
338 0 return $x0;
339 }
340 0 $offset = $x0;
341 }
342 }
343  
344 0 print STDERR "Error Traversing Database for ipnum = $ipnum - Perhaps database is corrupt?";
345 }
346 sub charset {
347 0 ▶ return $_[0]->{_charset};
Subroutine charset: not called CC=1 SCAR=6.9
Pod documented
348 }
349  
350 sub set_charset{
351 0 ▶ my ( $gi, $charset ) = @_;
Subroutine set_charset: not called CC=1 SCAR=6.9
Pod documented
352 0 my $old_charset = $gi->{_charset};
353 0 $gi->{_charset} = $charset;
354  
355 0 return $old_charset;
356 }
357  
358 #this function returns the country code of ip address
359 sub country_code_by_addr {
360 0 ▶ my ($gi, $ip_address) = @_;
Subroutine country_code_by_addr: not called CC=2 SCAR=17.9
Pod documented
361 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
362 0 return $countries[$gi->id_by_addr($ip_address)];
363 }
364  
365 #this function returns the country code3 of ip address
366 sub country_code3_by_addr {
367 0 ▶ my ($gi, $ip_address) = @_;
Subroutine country_code3_by_addr: not called CC=2 SCAR=17.9
Pod documented
368 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
369 0 return $code3s[$gi->id_by_addr($ip_address)];
370 }
371  
372 #this function returns the name of ip address
373 sub country_name_by_addr {
374 0 ▶ my ($gi, $ip_address) = @_;
Subroutine country_name_by_addr: not called CC=2 SCAR=17.9
Pod documented
375 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
376 0 return $names[$gi->id_by_addr($ip_address)];
377 }
378  
379 sub id_by_addr {
380 0 ▶ my ($gi, $ip_address) = @_;
Subroutine id_by_addr: not called CC=2 SCAR=17.9
Pod undocumented
381 0 ▶ return unless $ip_address =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 0 0 0
382 0 return $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_COUNTRY_BEGIN;
383 }
384  
385 #this function returns the country code of domain name
386 sub country_code_by_name {
387 0 ▶ my ($gi, $host) = @_;
Subroutine country_code_by_name: not called CC=1 SCAR=6.9
Pod documented
388 0 my $country_id = $gi->id_by_name($host);
389 0 return $countries[$country_id];
390 }
391  
392 #this function returns the country code3 of domain name
393 sub country_code3_by_name {
394 0 ▶ my ($gi, $host) = @_;
Subroutine country_code3_by_name: not called CC=1 SCAR=6.9
Pod documented
395 0 my $country_id = $gi->id_by_name($host);
396 0 return $code3s[$country_id];
397 }
398  
399 #this function returns the country name of domain name
400 sub country_name_by_name {
401 0 ▶ my ($gi, $host) = @_;
Subroutine country_name_by_name: not called CC=1 SCAR=6.9
Pod documented
402 0 my $country_id = $gi->id_by_name($host);
403 0 return $names[$country_id];
404 }
405  
406 sub id_by_name {
407 0 ▶ my ($gi, $host) = @_;
Subroutine id_by_name: not called CC=3 SCAR=24.8
Pod undocumented
408 0 my $ip_address;
409 0 ▶ if ($host =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 0 0
410 0 $ip_address = $host;
411 } else {
412 0 $ip_address = join('.',unpack('C4',(gethostbyname($host))[4]));
413 }
414 0 ▶ return unless $ip_address;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address 0 0 0
415 0 return $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_COUNTRY_BEGIN;
416 }
417  
418 #this function returns the city record as a array
419 sub get_city_record {
420 0 ▶ my ($gi, $host) = @_;
Subroutine get_city_record: not called CC=16 SCAR=56.1
Pod documented
421 0 my $ip_address = $gi->get_ip_address($host);
422 0 ▶ return unless $ip_address;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address 0 0 0
423 0 my $record_buf;
424 my $record_buf_pos;
425 0 my $char;
426 0 my $metroarea_combo;
427 0 my $record_country_code = "";
428 0 my $record_country_code3 = "";
429 0 my $record_country_name = "";
430 0 my $record_region = "";
431 0 my $record_city = "";
432 0 my $record_postal_code = "";
433 0 my $record_latitude = "";
434 0 my $record_longitude = "";
435 0 my $record_metro_code = "";
436 0 my $record_area_code = "";
437 0 my $str_length = 0;
438 0 my $i;
439 my $j;
440  
441 #lookup the city
442 0 my $seek_country = $gi->_seek_country(addr_to_num($ip_address));
443 0 ▶ if ($seek_country == $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek_country == $gi->{"databaseSegments"}) 0 0 0
444 0 return;
445 }
446 #set the record pointer to location of the city record
447 0 my $record_pointer = $seek_country + (2 * $gi->{"record_length"} - 1) * $gi->{"databaseSegments"};
448  
449 0 ▶ unless ( exists $gi->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{'buf'}) { } 0 0 0
450 0 seek( $gi->{"fh"}, $record_pointer, 0 );
451 0 read( $gi->{"fh"}, $record_buf, FULL_RECORD_LENGTH );
452 0 $record_buf_pos = 0;
453 }
454 else {
455 0 $record_buf = substr($gi->{buf}, $record_pointer, FULL_RECORD_LENGTH);
456 0 $record_buf_pos = 0;
457 }
458  
459 #get the country
460 0 $char = ord(substr($record_buf,$record_buf_pos,1));
461 0 $record_country_code = $countries[$char];#get the country code
462 0 $record_country_code3 = $code3s[$char];#get the country code with 3 letters
463 0 $record_country_name = $names[$char];#get the country name
464 0 $record_buf_pos++;
465  
466 #get the region
467 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
468 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
469 0 $str_length++;#get the length of string
470 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
471 }
472 0 ▶ if ($str_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_length > 0) 0 0 0
473 0 $record_region = substr($record_buf,$record_buf_pos,$str_length);
474 }
475 0 $record_buf_pos += $str_length + 1;
476 0 $str_length = 0;
477  
478 #get the city
479 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
480 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
481 0 $str_length++;#get the length of string
482 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
483 }
484 0 ▶ if ($str_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_length > 0) 0 0 0
485 0 $record_city = substr($record_buf,$record_buf_pos,$str_length);
486 }
487 0 $record_buf_pos += $str_length + 1;
488 0 $str_length = 0;
489  
490 #get the postal code
491 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
492 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
493 0 $str_length++;#get the length of string
494 0 $char = ord(substr($record_buf,$record_buf_pos+$str_length,1));
495 }
496 0 ▶ if ($str_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_length > 0) 0 0 0
497 0 $record_postal_code = substr($record_buf,$record_buf_pos,$str_length);
498 }
499 0 $record_buf_pos += $str_length + 1;
500 0 $str_length = 0;
501 0 my $latitude = 0;
502 0 my $longitude = 0;
503  
504 #get the latitude
505 0 ▶ for ($j = 0;$j < 3; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
506 0 $char = ord(substr($record_buf,$record_buf_pos++,1));
507 0 $latitude += ($char << ($j * 8));
508 }
509 0 $record_latitude = ($latitude/10000) - 180;
510  
511 #get the longitude
512 0 ▶ for ($j = 0;$j < 3; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
513 0 $char = ord(substr($record_buf,$record_buf_pos++,1));
514 0 $longitude += ($char << ($j * 8));
515 }
516 0 $record_longitude = ($longitude/10000) - 180;
517  
518 #get the metro code and the area code
519 0 ▶ if (GEOIP_CITY_EDITION_REV1 == $gi->{"databaseType"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 == $gi->{"databaseType"}) 0 0 0
520 0 $metroarea_combo = 0;
521 0 ▶ if ($record_country_code eq "US") {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_country_code eq "US") 0 0 0
522 #if the country is US then read the metro area combo
523 0 ▶ for ($j = 0;$j < 3;++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 0 0 0
524 0 $char = ord(substr($record_buf,$record_buf_pos++,1));
525 0 $metroarea_combo += ($char << ($j * 8));
526 }
527 #split the metro area combo into the metro code and the area code
528 0 $record_metro_code = int($metroarea_combo/1000);
529 0 $record_area_code = $metroarea_combo%1000;
530 }
531 }
532
533 # the pureperl API must convert the string by themself to UTF8
534 # using Encode for perl >= 5.008 otherwise use it's own iso-8859-1 to utf8 converter
535 0 ▶ $record_city = decode( 'iso-8859-1' => $record_city )
Branch: 1 branch
BranchTrueFalseTotal
if $gi->charset == 1 0 0 0
536 if $gi->charset == GEOIP_CHARSET_UTF8;
537  
538 0 return ($record_country_code,$record_country_code3,$record_country_name,$record_region,$record_city,$record_postal_code,$record_latitude,$record_longitude,$record_metro_code,$record_area_code);
539 }
540  
541 #this function returns the city record as a hash ref
542 sub get_city_record_as_hash {
543 0 ▶ my ($gi, $host) = @_;
Subroutine get_city_record_as_hash: not called CC=1 SCAR=6.9
Pod documented
544 0 my %h;
545 0 @h{qw/ country_code country_code3 country_name
546 region city postal_code
547 latitude longitude metro_code
548 area_code /}
549 = $gi->get_city_record($host);
550 0 $h{dma_code} = $h{metro_code}; # alias for depreciated dma_code
551 0 return \%h;
552 }
553  
554 #this function returns isp or org of the domain name
555 sub org_by_name {
556 0 ▶ my ($gi, $host) = @_;
Subroutine org_by_name: not called CC=4 SCAR=30.0
Pod documented
557 0 my $ip_address = $gi->get_ip_address($host);
558 0 my $seek_org = $gi->_seek_country(addr_to_num($ip_address));
559 0 my $char;
560 my $org_buf;
561 0 my $org_buf_length = 0;
562 0 my $record_pointer;
563  
564 0 ▶ if ($seek_org == $gi->{"databaseSegments"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek_org == $gi->{"databaseSegments"}) 0 0 0
565 0 return undef;
566 }
567  
568 0 $record_pointer = $seek_org + (2 * $gi->{"record_length"} - 1) * $gi->{"databaseSegments"};
569  
570 0 ▶ unless ( exists $gi->{buf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{'buf'}) { } 0 0 0
571 0 seek( $gi->{"fh"}, $record_pointer, 0 );
572 0 read( $gi->{"fh"}, $org_buf, MAX_ORG_RECORD_LENGTH );
573 }
574 else {
575 0 $org_buf = substr($gi->{buf}, $record_pointer, MAX_ORG_RECORD_LENGTH );
576 }
577  
578 0 $char = ord(substr($org_buf,0,1));
579 0 ▶ while ($char != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char != 0) 0 0 0
580 0 $org_buf_length++;
581 0 $char = ord(substr($org_buf,$org_buf_length,1));
582 }
583  
584 0 $org_buf = substr($org_buf, 0, $org_buf_length);
585 0 return $org_buf;
586 }
587  
588 #this function returns isp or org of the domain name
589 1 *isp_by_name = \*org_by_name;
590  
591 1 *org_by_addr = \*org_by_name;
592 1 *isp_by_addr = \*org_by_name;
593  
594 #this function returns the region
595 sub region_by_name {
596 0 ▶ my ($gi, $host) = @_;
Subroutine region_by_name: not called CC=8 SCAR=42.8
Pod undocumented
597 0 my $ip_address = $gi->get_ip_address($host);
598 0 ▶ return unless $ip_address;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_address 0 0 0
599 0 ▶ if ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gi->{'databaseType'} == 7) { } 0 0 0
elsif ($gi->{'databaseType'} == 3) { } 0 0 0
600 0 my $seek_region = $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_STATE_BEGIN_REV0;
601 0 ▶ if ($seek_region >= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seek_region >= 1000) { } 0 0 0
602 0 return ("US",chr(($seek_region - 1000)/26 + 65) . chr(($seek_region - 1000)%26 + 65));
603 } else {
604 0 return ($countries[$seek_region],"");
605 }
606 } elsif ($gi->{"databaseType"} == GEOIP_REGION_EDITION_REV1) {
607 0 my $seek_region = $gi->_seek_country(addr_to_num($ip_address)) - GEOIP_STATE_BEGIN_REV1;
608 0 ▶ if ($seek_region < US_OFFSET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($seek_region < 1) { } 0 0 0
elsif ($seek_region < 677) { } 0 0 0
elsif ($seek_region < 1353) { } 0 0 0
609 0 return ("","");
610 } elsif ($seek_region < CANADA_OFFSET) {
611 # return a us state
612 0 return ("US",chr(($seek_region - US_OFFSET)/26 + 65) . chr(($seek_region - US_OFFSET)%26 + 65));
613 } elsif ($seek_region < WORLD_OFFSET) {
614 # return a canada province
615 0 return ("CA",chr(($seek_region - CANADA_OFFSET)/26 + 65) . chr(($seek_region - CANADA_OFFSET)%26 + 65));
616 } else {
617 # return a country of the world
618 0 my $c = $countries[($seek_region - WORLD_OFFSET) / FIPS_RANGE];
619 0 my $a2 = ($seek_region - WORLD_OFFSET) % FIPS_RANGE;
620 0 my $r = chr(($a2 / 100)+48) . chr((($a2 / 10) % 10)+48) . chr(($a2 % 10)+48);
621 0 return ($c,$r);
622 }
623 }
624 }
625  
626 sub get_ip_address {
627 0 ▶ my ($gi, $host) = @_;
Subroutine get_ip_address: not called CC=2 SCAR=17.9
Pod undocumented
628 0 my $ip_address;
629 #check if host is ip address
630 0 ▶ if ($host =~ m!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { } 0 0 0
631 #host is ip address
632 0 $ip_address = $host;
633 } else {
634 #host is domain name do a dns lookup
635 0 $ip_address = join('.',unpack('C4',(gethostbyname($host))[4]));
636 }
637 0 return $ip_address;
638 }
639  
640 0 ▶sub addr_to_num { unpack( N => pack( C4 => split( /\./, $_[0] ) ) ) }
Subroutine addr_to_num: not called CC=1 SCAR=6.9
Pod undocumented
641 0 ▶sub num_to_addr { join q{.}, unpack( C4 => pack( N => $_[0] ) ) }
Subroutine num_to_addr: not called CC=1 SCAR=6.9
Pod undocumented
642  
643 sub database_info {
644 0 ▶ my $gi = shift;
Subroutine database_info: not called CC=6 SCAR=37.4
Pod documented
645 0 my $i = 0;
646 0 my $buf;
647 my $retval;
648 0 my $hasStructureInfo;
649 0 seek($gi->{fh},-3,2);
650 0 ▶ for (my $i = 0;$i < STRUCTURE_INFO_MAX_SIZE;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 20) 0 0 0
651 0 read($gi->{fh},$buf,3);
652 0 ▶ if ($buf eq (chr(255) . chr(255) . chr(255))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "\377\377\377") 0 0 0
653 0 $hasStructureInfo = 1;
654 0 last;
655 }
656 0 seek($gi->{fh},-4,1);
657 }
658 0 ▶ if ($hasStructureInfo == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hasStructureInfo == 1) { } 0 0 0
659 0 seek($gi->{fh},-6,1);
660 } else {
661 # no structure info, must be pre Sep 2002 database, go back to
662 0 seek($gi->{fh},-3,2);
663 }
664 0 ▶ for (my $i = 0;$i < DATABASE_INFO_MAX_SIZE;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 100) 0 0 0
665 0 read($gi->{fh},$buf,3);
666 0 ▶ if ($buf eq (chr(0). chr(0). chr(0))){
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "\0\0\0") 0 0 0
667 0 read($gi->{fh},$retval,$i);
668 0 return $retval;
669 }
670 0 seek($gi->{fh},-4,1);
671 }
672 0 return "";
673 }
674  
675 sub range_by_ip {
676 0 ▶ my $gi = shift;
Subroutine range_by_ip: not called CC=5 SCAR=34.0
Pod documented
677 0 my $ipnum = addr_to_num( shift );
678 0 my $c = $gi->_seek_country( $ipnum );
679 0 my $nm = $gi->last_netmask;
680 0 my $m = 0xffffffff << 32 - $nm;
681 0 my $left_seek_num = $ipnum & $m;
682 0 my $right_seek_num = $left_seek_num + ( 0xffffffff & ~$m );
683  
684 0 ▶ while ( $left_seek_num != 0
Branch: 1 branch
BranchTrueFalseTotal
if ($left_seek_num != 0 and $c == $gi->_seek_country($left_seek_num - 1)) 0 0 0
Condition0%0/3
$left_seek_num != 0 and $c == $gi->_seek_country($left_seek_num - 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$left_seek_num != 0 and $c == $gi->_seek_country($left_seek_num - 1)
$left_seek_num != 0 $c == $gi->_seek_country($left_seek_num - 1)
Truth table0%0/3
A and B
A= $left_seek_num != 0B= $c == $gi->_seek_country($left_seek_num - 1)
ABresult
0X0
100
111
685 and $c == $gi->_seek_country( $left_seek_num - 1) ) {
686 0 my $lm = 0xffffffff << 32 - $gi->last_netmask;
687 0 $left_seek_num = ( $left_seek_num - 1 ) & $lm;
688 }
689 0 ▶ while ( $right_seek_num != 0xffffffff
Branch: 1 branch
BranchTrueFalseTotal
if ($right_seek_num != 4294967295 and $c == $gi->_seek_country($right_seek_num + 1)) 0 0 0
Condition0%0/3
$right_seek_num != 4294967295 and $c == $gi->_seek_country($right_seek_num + 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$right_seek_num != 4294967295 and $c == $gi->_seek_country($right_seek_num + 1)
$right_seek_num != 4294967295 $c == $gi->_seek_country($right_seek_num + 1)
Truth table0%0/3
A and B
A= $right_seek_num != 4294967295B= $c == $gi->_seek_country($right_seek_num + 1)
ABresult
0X0
100
111
690 and $c == $gi->_seek_country( $right_seek_num + 1 ) ) {
691 0 my $rm = 0xffffffff << 32 - $gi->last_netmask;
692 0 $right_seek_num = ( $right_seek_num + 1 ) & $rm;
693 0 $right_seek_num += ( 0xffffffff & ~$rm );
694 }
695 0 return ( num_to_addr($left_seek_num), num_to_addr($right_seek_num) );
696 }
697  
698 0 ▶sub netmask { $_[0]->{last_netmask} = $_[1] }
Subroutine netmask: not called CC=1 SCAR=6.9
Pod documented
699  
700 sub last_netmask {
701 0 ▶ return $_[0]->{last_netmask};
Subroutine last_netmask: not called CC=1 SCAR=6.9
Pod documented
702 }
703  
704 sub DESTROY {
705 0 ▶ my $gi = shift;
Subroutine DESTROY: not called CC=5 SCAR=34.0
706  
707 0 ▶ if ( exists $gi->{buf} && $gi->{flags} && ( $gi->{flags} & GEOIP_MMAP_CACHE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $gi->{"buf"} and $gi->{"flags"} and $gi->{"flags"} & 8) 0 0 0
Condition0%0/6
exists $gi->{"buf"} and $gi->{"flags"} and $gi->{"flags"} & 8
!ll&&!rl&&r
000
exists $gi->{"buf"} and $gi->{"flags"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $gi->{"buf"} and $gi->{"flags"} and $gi->{"flags"} & 8
exists $gi->{"buf"} $gi->{"flags"} $gi->{"flags"} & 8
Truth table0%0/4
A and B and C
A= exists $gi->{"buf"}B= $gi->{"flags"}C= $gi->{"flags"} & 8
ABCresult
0XX0
10X0
1100
1111
708 0 ▶ munmap( $gi->{buf} ) or die "munmap: $!";
Branch: 1 branch
BranchTrueFalseTotal
munmap($gi->{"buf"}) or die "munmap: $!" 0 0 0
709 0 delete $gi->{buf};
710 }
711 }
712 1 1;
713 __END__
↑ Summary