Business-BancaSella

 view release on metacpan or  search on metacpan

BancaSella/Ris/FileFast.pm  view on Meta::CPAN

    }
    $self->{'file'} = $options{file};
    return $self;
}

sub file {
    my ($self,$value) = @_;
    $self->{'file'} = $value if defined $value;
    return $self->{'file'};
}

sub remove {
    my ($self,$password) = @_;
    unless ( $self->extract($password) ) {
        die "Unable to find $password in $self->{'file'}";
    }
    return;
}

sub check {
    my ($self,$password) = @_;
    return $self->extract($password,1);
}

#
# verify if a returned password exists
#
# param $merchant_code  the code of the merchant
# param $password       the password returned
#
# return true if the password exists, false elsewhere
# raise an exception 'SYSTEM : description' on file error
#
#
sub extract {
    my ($self,$password,$only_test) = @_;

    unless ( $password =~ /^[a-zA-Z0-9]{32}$/ ) {
        die "PARAMETER. password corrupt\n";
    }

    # open the file
    unless ( open(RESPONSE,"+<$self->{'file'}") ) {
        die "SYSTEM. opening $self->{'file'} : $!\n";
    }

    # lock the file
    my $has_lock = eval { flock(RESPONSE,2) };
    if ( $@ ) {
        #warn "WARNING. this platform don't implements 'flock'\n";
    } elsif ( ! $has_lock ) {
        close(RESPONSE);
        die "SYSTEM. locking $self->{'file'} : $!\n";
    }

    # length of a row of password
    my $row_length = 34;

    # read the size of the file
    my $size_bytes;
    unless ( $size_bytes = (stat(RESPONSE))[7] ) {
        close(RESPONSE);
        if ( $! ne '' ) {
        	die $!;
        } else {
        	die  "EMPTY. file $self->{'file'} is empty\n";
        }
    }
    if ( $size_bytes % $row_length != 0 ) {
        close(RESPONSE);
        die "CORRUPT. dimension of $self->{'file'} is wrong\n";
    }

    # number of passwords in the file
    my $size = $size_bytes/$row_length;

    # range
    my $left = 0;
    my $middle;
    my $right = $size - 1;
    # hash value of the password to search
    my $password_hash = _hash($password);
    # hash values of the range
    my $left_hash = _hash_min();
    my $middle_hash;
    my $right_hash = _hash_max();

    # sign of the read passwords, '-' if used, '+' elsewhere
    my $middle_sign;
    my $passwords_used = 0;

    my $find = 0;
    my $steps = 0;
    my $row;

    while ( $right >= $left ) {

        # increment the steps
        $steps++;

        # the middle position to test
        $middle = $left + int(( ( $password_hash - $left_hash ) * ( $right - $left ))
        / ( $right_hash - $left_hash )); # /

        if ( $_DEBUG ) {
            print STDERR "position : [$left,$middle,$right]\n";
        }

        # seek at the middle
        unless ( seek(RESPONSE,$middle*$row_length,0) ) {
            close(RESPONSE);
            die "SYSTEM.while seek in $self->{'file'}  $!\n";
        }

        # read the password
        unless ( read(RESPONSE,$row,$row_length) ) {
            close(RESPONSE);
            die "SYSTEM. reading $self->{'file'} : $!\n";
        }
        unless ( $row =~ /^([+-])([a-zA-Z0-9]{32})\n$/ ) {
            close(RESPONSE);



( run in 0.605 second using v1.01-cache-2.11-cpan-8dfa8b56332 )