QQWry.pm

filter: Statementstmt 21.9% 29 / 132 Branchbran 8.7% 4 / 46 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 42.1% 8 / 19 Podpod 100% 6 / 6 totaltotal 22.5% 48 / 213 CC 26 SCAR 28.7
CC
26
Cov
18%
_result
56.0
query
30.0
_index
20.0
CRAP 17.7
« QQWry/Decoded.pm ↑ Summary
1 package IP::QQWry;
2  
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use version; our $VERSION = qv('0.0.21');
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 my %cache;
10 my $tmp; # used for hold temporary data
11  
12 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
13 2 my ( $class, $db ) = @_;
14 2 my $self = {};
15 2 bless $self, $class;
16 2 ▶ if ($db) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db) 1 1 2
17 1 $self->set_db($db);
18 }
19 2 return $self;
20 }
21  
22 # set db file of which the name is `QQWry.Dat' most of the time.
23 1 ▶sub set_db {
Subroutine set_db: called CC=4 SCAR=19.4
Pod documented
24 1 my ( $self, $db ) = @_;
25 1 ▶ if ( $db && -r $db ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db and -r $db) 0 1 1
Condition33%1/3
$db and -r $db
!ll&&!rl&&r
010
MC/DC0%0/2
$db and -r $db
$db -r $db
Truth table33%1/3
A and B
A= $dbB= -r $db
ABresult
0X0
100
111
26 0 ▶ open $self->{fh}, '<', $db or croak "how can this happen? $!";
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"fh"}, "<", $db or croak("how can this happen? $!") 0 0 0
27 0 $self->_init_db;
28 0 return 1;
29 }
30 1 carp 'set_db failed';
31 1 return;
32 }
33  
34 0 ▶sub _init_db {
Subroutine _init_db: not called CC=1 SCAR=6.9
35 0 my $self = shift;
36 0 read $self->{fh}, $tmp, 4;
37 0 $self->{first_index} = unpack 'V', $tmp;
38 0 read $self->{fh}, $tmp, 4;
39 0 $self->{last_index} = unpack 'V', $tmp;
40 }
41  
42 # sub query is the the interface for user.
43 # the parameter is a IPv4 address
44  
45 0 ▶sub query {
Subroutine query: not called CC=5 SCAR=34.0
Pod documented
46 0 my ( $self, $input ) = @_;
47 0 ▶ unless ( $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 0 0 0
48 0 carp 'database is not provided';
49 0 return;
50 }
51  
52 0 my $ip = $self->_convert_input($input);
53  
54 0 ▶ if ($ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip) 0 0 0
55 0 ▶ $cache{$ip} = [ $self->_result($ip) ] unless $self->cached($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cached($ip) 0 0 0
56 0 ▶ return wantarray ? @{ $cache{$ip} } : join '', @{ $cache{$ip} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
57 }
58 0 return;
59 }
60  
61 0 ▶sub _convert_input {
Subroutine _convert_input: not called CC=3 SCAR=24.8
62 0 my ( $self, $input ) = @_;
63 0 ▶ if ( $input =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) { } 0 0 0
elsif ($input =~ /(\d+)/) { } 0 0 0
64 0 return $1 * 256**3 + $2 * 256**2 + $3 * 256 + $4;
65 }
66 elsif ( $input =~ /(\d+)/ ) {
67 0 return $1;
68 }
69 else {
70 0 return;
71 }
72 }
73  
74 0 ▶sub cached {
Subroutine cached: not called CC=2 SCAR=17.9
Pod documented
75 0 my ( $self, $input ) = @_;
76 0 my $ip = $self->_convert_input($input);
77 0 ▶ return $cache{$ip} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cache{$ip} ? : 0 0 0
78 }
79  
80 0 ▶sub clear {
Subroutine clear: not called CC=2 SCAR=17.9
Pod documented
81 0 my ( $self, $ip ) = @_;
82 0 ▶ if ($ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip) { } 0 0 0
83 0 undef $cache{$ip};
84 }
85 else {
86 0 undef %cache;
87 }
88 }
89  
90 0 ▶sub db_version {
Subroutine db_version: not called CC=1 SCAR=6.9
Pod documented
91 0 return shift->query('255.255.255.0'); # db version info is held there
92 }
93  
94 # get the useful infomation which will be returned to user
95  
96 0 ▶sub _result {
Subroutine _result: not called CC=7 SCAR=40.3
97 0 my ( $self, $ip ) = @_;
98 0 my $index = $self->_index($ip);
99 0 ▶ return unless $index; # can't find index
Branch: 1 branch
BranchTrueFalseTotal
unless $index 0 0 0
100  
101 0 my ( $base, $ext ) = (q{}) x 2;
102  
103 0 seek $self->{fh}, $index + 4, 0;
104 0 read $self->{fh}, $tmp, 3;
105  
106 0 my $offset = unpack 'V', $tmp . chr 0;
107 0 seek $self->{fh}, $offset + 4, 0;
108 0 read $self->{fh}, $tmp, 1;
109  
110 0 my $mode = ord $tmp;
111  
112 0 ▶ if ( $mode == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode == 1) { } 0 0 0
elsif ($mode == 2) { } 0 0 0
113 0 $self->_seek;
114 0 $offset = tell $self->{fh};
115 0 read $self->{fh}, $tmp, 1;
116 0 $mode = ord $tmp;
117 0 ▶ if ( $mode == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 2) { } 0 0 0
118 0 $self->_seek;
119 0 $base = $self->_str;
120 0 seek $self->{fh}, $offset + 4, 0;
121 0 $ext = $self->_ext;
122 }
123 else {
124 0 seek $self->{fh}, -1, 1;
125 0 $base = $self->_str;
126 0 $ext = $self->_ext;
127 }
128 }
129 elsif ( $mode == 2 ) {
130 0 $self->_seek;
131 0 $base = $self->_str;
132 0 seek $self->{fh}, $offset + 8, 0;
133 0 $ext = $self->_ext;
134 }
135 else {
136 0 seek $self->{fh}, -1, 1;
137 0 $base = $self->_str;
138 0 $ext = $self->_ext;
139 }
140  
141 # 'CZ88.NET' means we don't have useful information
142 0 ▶ $base = '' if $base =~ /CZ88\.NET/;
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /CZ88\.NET/ 0 0 0
143 0 ▶ $ext = '' if $ext =~ /CZ88\.NET/;
Branch: 1 branch
BranchTrueFalseTotal
if $ext =~ /CZ88\.NET/ 0 0 0
144 0 return ( $base, $ext );
145 }
146  
147 0 ▶sub _index {
Subroutine _index: not called CC=4 SCAR=30.0
148 0 my ( $self, $ip ) = @_;
149 0 my $low = 0;
150 0 my $up = ( $self->{last_index} - $self->{first_index} ) / 7;
151 0 my ( $mid, $ip_start, $ip_end );
152  
153 # find the index using binary search
154 0 ▶ while ( $low <= $up ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low <= $up) 0 0 0
155 0 $mid = int( ( $low + $up ) / 2 );
156 0 seek $self->{fh}, $self->{first_index} + $mid * 7, 0;
157 0 read $self->{fh}, $tmp, 4;
158 0 $ip_start = unpack 'V', $tmp;
159  
160 0 ▶ if ( $ip < $ip_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip < $ip_start) { } 0 0 0
161 0 $up = $mid - 1;
162 }
163 else {
164 0 read $self->{fh}, $tmp, 3;
165 0 $tmp = unpack 'V', $tmp . chr 0;
166 0 seek $self->{fh}, $tmp, 0;
167 0 read $self->{fh}, $tmp, 4;
168 0 $ip_end = unpack 'V', $tmp;
169  
170 0 ▶ if ( $ip > $ip_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip > $ip_end) { } 0 0 0
171 0 $low = $mid + 1;
172 }
173 else {
174 0 return $self->{first_index} + $mid * 7;
175 }
176 }
177 }
178  
179 0 return;
180 }
181  
182 0 ▶sub _seek {
Subroutine _seek: not called CC=1 SCAR=6.9
183 0 my $self = shift;
184 0 read $self->{fh}, $tmp, 3;
185 0 my $offset = unpack 'V', $tmp . chr 0;
186 0 seek $self->{fh}, $offset, 0;
187 }
188  
189 # get string ended by \0
190  
191 0 ▶sub _str {
Subroutine _str: not called CC=2 SCAR=17.9
192 0 my $self = shift;
193 0 my $str = '';
194  
195 0 read $self->{fh}, $tmp, 1;
196 0 ▶ while ( ord $tmp > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $tmp > 0) 0 0 0
197 0 $str .= $tmp;
198 0 read $self->{fh}, $tmp, 1;
199 }
200 0 return $str;
201 }
202  
203 0 ▶sub _ext {
Subroutine _ext: not called CC=3 SCAR=24.8
204 0 my $self = shift;
205 0 read $self->{fh}, $tmp, 1;
206 0 my $mode = ord $tmp;
207  
208 0 ▶ if ( $mode == 1 || $mode == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 1 or $mode == 2) { } 0 0 0
Condition0%0/3
$mode == 1 or $mode == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode == 1 or $mode == 2
$mode == 1 $mode == 2
Truth table0%0/3
A or B
A= $mode == 1B= $mode == 2
ABresult
1X1
011
000
209 0 $self->_seek;
210 0 return $self->_str;
211 }
212 else {
213 0 return chr($mode) . $self->_str;
214 }
215 }
216  
217 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
218 2 my $self = shift;
219 2 ▶ close $self->{fh} if $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"} 0 2 2
220 }
221  
222 2 1;
223  
224 __END__
« QQWry/Decoded.pm ↑ Summary