Algorithm-Hamming-Perl
view release on metacpan or search on metacpan
$Hamming8semi{$char} = $Hamming8raw{$key};
}
%Hamming8rev = reverse(%Hamming8semi);
# hamming_faster - this subroutine builds two large hashes of,
# %Hamming8by2 2 byte data -> 3 byte Hamming code
# %Hamming8by2rev 3 byte Hamming code -> 2 byte data
# for faster encodings and decodings. Running this subroutine is
# optional. If it is used then conversions are faster, however more
# memory is used to store the hashes, and a couple of seconds is added
# to the startup time. If it is not used, conversions are slower -
# taking up to 5 times the usual time. A good measure is the data you
# with to encode - more than 1 Mb would benifit from this subroutine.
#
sub hamming_faster {
#
# Step through 0,0 to 255,255 to build a hash that can convert
# any 2 byte combinations.
#
}
# hamming - this turns data into hamming code. This has been written
# with memory and CPU efficiency in mind (without resorting to C).
#
sub hamming {
my $data = shift; # input data
my $pos; # counter to step through data string
my $char_in1; # first input byte
my $char_in2; # second input byte
my $chars_in; # both input bytes
my $ham_text; # hamming code in binary text "0101.."
my $char_out; # hamming code as bytes
my $output = ""; # full output hamming code as bytes
my $length = length($data);
#
# Step through the $data 2 bytes at a time, generating a
# Hamming encoded $output.
# unhamming_err - this turns hamming code into data. This has been written
# with memory and CPU efficiencu in mind (without resorting to C).
#
sub unhamming_err {
my $data = shift; # input data
my $pos; # counter to step through data string
my $err; # corrected bit error
my $chars_in; # input bytes
my $ham_text; # hamming code in binary text "0101..", 2 bytes
my $ham_text1; # hamming code for first byte
my $ham_text2; # hamming code for second byte
my $char_out1; # output data byte 1
my $char_out2; # output data byte 2
my $output = ""; # full output data as bytes
my $err_all = 0; # count of corrected bit errors
my $length = length($data);
#
# Step through the $data 3 bytes at a time, decoding it back into
# the $output data.
=head1 OTHER SUBROUTINES
=over 4
=item Algorithm::Hamming::Perl::hamming_faster ()
This is an optional subroutine that will speed Hamming encoding if it is
run once at the start of the program. It does this by using a larger (hash)
cache of preprocessed results. The disadvantage is that it uses more memory,
and can add several seconds to invocation time. Only use this if you are
encoding more than 1 Mb of data.
=back
=head1 INSTALLATION
perl Makefile.PL
make
make test
make install
Returns the original data from the provided Hamming
code, and a number counting the number of bytes that
were corrected. Single bit errors are auto corrected.
OTHER SUBROUTINES
Algorithm::Hamming::Perl::hamming_faster ()
This is an optional subroutine that will speed Hamming
encoding if it is run once at the start of the program.
It does this by using a larger (hash) cache of
preprocessed results. The disadvantage is that it uses
more memory, and can add several seconds to invocation
time. Only use this if you are encoding more than 1 Mb
of data.
INSTALLATION
perl Makefile.PL
make
make test
make install
DEPENDENCIES
$infile = $ARGV[0];
$outfile = $ARGV[1];
### Open filehandles
open(IN,"$infile") || die "ERROR: Can't open $infile: $!\n";
open(OUT,">$outfile") || die "ERROR: Can't open $outfile: $!\n";
binmode(IN);
binmode(OUT);
### Use optional faster (but bigger) cache - adds several seconds to startup!
Algorithm::Hamming::Perl::hamming_faster();
### Process data
while (read(IN,$buffer,3072)) {
$hamcode = hamming($buffer);
print OUT $hamcode;
}
close IN;
$infile = $ARGV[0];
$outfile = $ARGV[1];
### Open filehandles
open(IN,"$infile") || die "ERROR: Can't open $infile: $!\n";
open(OUT,">$outfile") || die "ERROR: Can't open $outfile: $!\n";
binmode(IN);
binmode(OUT);
### Use optional faster (but bigger) cache - adds several seconds to startup!
Algorithm::Hamming::Perl::hamming_faster();
### Process data
while (read(IN,$buffer,3072)) {
$data = unhamming($buffer);
print OUT $data;
}
close IN;
( run in 0.753 second using v1.01-cache-2.11-cpan-39bf76dae61 )