File Coverage

blib/lib/Business/BancaSella/Ris/FileFast.pm
Criterion Covered Total %
statement 88 122 72.1
branch 36 62 58.0
condition 4 6 66.6
subroutine 11 13 84.6
pod 5 8 62.5
total 144 211 68.2


line stmt bran cond sub pod time code
1             #
2             # Business::BancaSella::Ris::FileFast
3             #
4             # author : Marco Gazerro
5             # initial date : 06/02/2001 ( originally in Open2b, www.open2b.com )
6             #
7             # version : 0.11
8             # date : 11/01/2002
9             #
10             # Copyright (c) 2001-2002 Marco Gazerro, Mauro Fedele
11             #
12             # This library is free software; you can redistribute it and/or
13             # modify it under the same terms as Perl itself.
14             #
15              
16             package Business::BancaSella::Ris::FileFast;
17              
18             $VERSION = '0.11';
19 0     0 0 0 sub Version { $VERSION }
20              
21             require 5.004;
22              
23 1     1   788 use strict;
  1         2  
  1         2011  
24              
25             my $_DEBUG = 0;
26              
27             sub new {
28 1     1 0 16 my $class = shift;
29 1         4 my $self = bless { }, $class;
30 1         6 return $self->init(@_);
31             }
32              
33             sub init {
34 1     1 0 4 my ($self,%options) = @_;
35 1 50       4 if ( $options{'file'} eq '' ) {
36 0         0 die "You must declare file in " . ref($self) . "::new";
37             }
38 1         6 $self->{'file'} = $options{file};
39 1         3 return $self;
40             }
41              
42             sub file {
43 0     0 1 0 my ($self,$value) = @_;
44 0 0       0 $self->{'file'} = $value if defined $value;
45 0         0 return $self->{'file'};
46             }
47              
48             sub remove {
49 2     2 1 52 my ($self,$password) = @_;
50 2 100       5 unless ( $self->extract($password) ) {
51 1         16 die "Unable to find $password in $self->{'file'}";
52             }
53 1         6 return;
54             }
55              
56             sub check {
57 2     2 1 156 my ($self,$password) = @_;
58 2         10 return $self->extract($password,1);
59             }
60              
61             #
62             # verify if a returned password exists
63             #
64             # param $merchant_code the code of the merchant
65             # param $password the password returned
66             #
67             # return true if the password exists, false elsewhere
68             # raise an exception 'SYSTEM : description' on file error
69             #
70             #
71             sub extract {
72 4     4 1 8 my ($self,$password,$only_test) = @_;
73              
74 4 50       17 unless ( $password =~ /^[a-zA-Z0-9]{32}$/ ) {
75 0         0 die "PARAMETER. password corrupt\n";
76             }
77              
78             # open the file
79 4 50       130 unless ( open(RESPONSE,"+<$self->{'file'}") ) {
80 0         0 die "SYSTEM. opening $self->{'file'} : $!\n";
81             }
82              
83             # lock the file
84 4         8 my $has_lock = eval { flock(RESPONSE,2) };
  4         25  
85 4 50       17 if ( $@ ) {
    50          
86             #warn "WARNING. this platform don't implements 'flock'\n";
87             } elsif ( ! $has_lock ) {
88 0         0 close(RESPONSE);
89 0         0 die "SYSTEM. locking $self->{'file'} : $!\n";
90             }
91              
92             # length of a row of password
93 4         7 my $row_length = 34;
94              
95             # read the size of the file
96 4         4 my $size_bytes;
97 4 50       43 unless ( $size_bytes = (stat(RESPONSE))[7] ) {
98 0         0 close(RESPONSE);
99 0 0       0 if ( $! ne '' ) {
100 0         0 die $!;
101             } else {
102 0         0 die "EMPTY. file $self->{'file'} is empty\n";
103             }
104             }
105 4 50       14 if ( $size_bytes % $row_length != 0 ) {
106 0         0 close(RESPONSE);
107 0         0 die "CORRUPT. dimension of $self->{'file'} is wrong\n";
108             }
109              
110             # number of passwords in the file
111 4         8 my $size = $size_bytes/$row_length;
112              
113             # range
114 4         5 my $left = 0;
115 4         5 my $middle;
116 4         5 my $right = $size - 1;
117             # hash value of the password to search
118 4         9 my $password_hash = _hash($password);
119             # hash values of the range
120 4         9 my $left_hash = _hash_min();
121 4         5 my $middle_hash;
122 4         7 my $right_hash = _hash_max();
123              
124             # sign of the read passwords, '-' if used, '+' elsewhere
125 4         6 my $middle_sign;
126 4         5 my $passwords_used = 0;
127              
128 4         5 my $find = 0;
129 4         5 my $steps = 0;
130 4         5 my $row;
131              
132 4         10 while ( $right >= $left ) {
133              
134             # increment the steps
135 8         8 $steps++;
136              
137             # the middle position to test
138 8         15 $middle = $left + int(( ( $password_hash - $left_hash ) * ( $right - $left ))
139             / ( $right_hash - $left_hash )); # /
140              
141 8 50       17 if ( $_DEBUG ) {
142 0         0 print STDERR "position : [$left,$middle,$right]\n";
143             }
144              
145             # seek at the middle
146 8 50       61 unless ( seek(RESPONSE,$middle*$row_length,0) ) {
147 0         0 close(RESPONSE);
148 0         0 die "SYSTEM.while seek in $self->{'file'} $!\n";
149             }
150              
151             # read the password
152 8 50       79 unless ( read(RESPONSE,$row,$row_length) ) {
153 0         0 close(RESPONSE);
154 0         0 die "SYSTEM. reading $self->{'file'} : $!\n";
155             }
156 8 50       37 unless ( $row =~ /^([+-])([a-zA-Z0-9]{32})\n$/ ) {
157 0         0 close(RESPONSE);
158 0         0 die "CORRUPT. $self->{'file'} corrupted at line ".($middle+1)."\n";
159             }
160 8         15 $middle_sign = $1;
161 8         15 my $password_middle = $2;
162 8         16 $middle_hash = _hash($password_middle);
163              
164 8 100       21 $passwords_used++ if $middle_sign eq '-';
165              
166             # debug information
167 8 50       16 if ( $_DEBUG ) {
168 0         0 print STDERR "hash : [$left_hash,$middle_hash,$right_hash]\n",
169             "password : [?,$password_middle,?]";
170             }
171              
172             # verify the password
173 8 100       18 if ( $password_middle eq $password ) {
    50          
174 4 100       10 if ( $middle_sign eq '+' ) {
175             # first use of the password, set the sign
176 2 100       5 unless ( $only_test ) {
177 1 50       11 unless ( seek(RESPONSE,$middle*$row_length,0) ) {
178 0         0 close(RESPONSE);
179 0         0 die "SYSTEM. while seek in $self->{'file'} : $!\n" }
180 1 50       5 unless ( print RESPONSE '-' ) {
181 0         0 close(RESPONSE);
182 0         0 die "SYSTEM. writing $self->{'file'} : $!\n";
183             }
184             }
185 2         3 $find = 1;
186             }
187             # set the range to exit from the search
188 4         11 $left = $right + 1;
189             }
190             elsif ( $password gt $password_middle ) {
191             # the password is in the right range (middle,right)
192 0         0 $left = $middle + 1;
193 0         0 $left_hash = $middle_hash;
194             } else {
195             # the password is in the left range (left,middle)
196 4         4 $right = $middle - 1;
197 4         11 $right_hash = $middle_hash;
198             }
199              
200             } # end while
201              
202             # close the file
203 4         55 close(RESPONSE);
204              
205             # debug information
206 4 50       10 print STDERR "Search in $steps steps.\n" if $_DEBUG;
207              
208 4         14 return $find;
209             }
210              
211             #
212             # create the work copy of a ris file
213             #
214             # return nothing
215             # raise an exception on error
216             #
217             sub prepare {
218 1     1 1 40 my ($self,$source_file) = @_;
219              
220             # read the passwords
221 1 50       42 open(SOURCE,"<$source_file") || die "SYSTEM. opening $source_file : $!\n";
222 1         391 my @rows = ;
223 1 50       23 if ( $! ) {
224 0         0 die "SYSTEM. reading $source_file : $!\n";
225             }
226 1 50       15 close(SOURCE) || die "SYSTEM. closing $source_file : $!\n";
227              
228             # verify the passwords
229 1         4 my @passwords = ();
230 1         3 my $line = 1;
231 1         2 foreach my $row ( @rows ) {
232 475 50       1558 unless ( $row =~ /^([a-zA-Z0-9]{32})\n+$/ ) {
233 0         0 die "CORRUPT. file $source_file corrupted at line $line\n";
234             }
235 475         984 push @passwords, ($1);
236             }
237              
238             # write the passwords
239 1 50       128 open(TARGET,"+>$self->{'file'}") || die "SYSTEM. opening $self->{'file'} : $!\n";
240 1         5 binmode(TARGET);
241 1         2 $line = 1;
242 1         235 foreach my $password ( sort @passwords ) {
243 475 50       1033 unless ( print TARGET "+$password\n" ) {
244 0         0 close(TARGET);
245 0         0 unlink($self->{'file'});
246 0         0 die "SYSTEM. writing file $self->{'file'} at line $line: $!\n";
247             }
248 475         539 $line++;
249             }
250 1         47 close(TARGET);
251              
252 1         69 return;
253             }
254              
255             ##
256             ## Private methods
257             ##
258              
259             #
260             # calculate a hash of a password
261             #
262             # param $password the password
263             # return a value in [0,242234] from the first two words of the password
264             #
265             sub _hash {
266 12     12   14 my ($password) = @_;
267 12         27 $password =~ /^(\w)(\w)(\w)/;
268 12         19 return _alf2num($1)*62*62 + _alf2num($2)*62 + _alf2num($3);
269             }
270              
271             # return the min hash value
272 4     4   7 sub _hash_min { 0 }
273              
274             # return the min hash value
275 4     4   20 sub _hash_max { 238327 }
276              
277             #
278             # calculate a integer from an alfanumeric character
279             # '0'->0, ... ,'9'->9, 'A'->10, ... ,'Z'->35, 'a'->36, ... ,'z'->61
280             #
281             sub _alf2num {
282 36     36   53 my $asc = ord($_[0]);
283 36 100 66     192 return ( $asc >= 97 && $asc <= 122 ) ? ( $asc - 61 )
    100 66        
284             : (( $asc >= 65 && $asc <= 90 ) ? ( $asc - 55 ) : $asc - 48 );
285             }
286              
287             1;