Algorithm-Hamming-Perl
view release on metacpan or search on metacpan
#!/bin/perl
#
# Perl.pm - Algorithm::Hamming::Perl library. Implements 8,4 bit Hamming ECC.
#
# This code will be unusual to read - instead of finding the Hamming
# algorithm you will see hash after hash after hash. These are used to
# improve the speed of the library, and act as a cache of preprocessed
# results. An optional subrourine may be run:
# Algorithm::Hamming::Perl::hamming_faster()
# which uses a bigger cache for faster encoding/decoding (but more memory
# and slower startups).
#
# 18-Oct-2003 Brendan Gregg Created this.
package Algorithm::Hamming::Perl;
our @EXPORT_OK = qw(hamming unhamming unhamming_err);
our $VERSION = '0.05';
my %Hamming8raw; # This hash is used during initialisation only. It
# contains binary text keys and binary text values
# as [data] -> [Hamming code] lookups,
# eg "00001010" => "000001010010"
my %Hamming8semi; # This hash is semi-processed, and is used in "slow"
# encoding mode. It contains byte keys and binary
# text values as [data] -> [Hamming code] lookups,
# eg "A" => "010010000100"
my %Hamming8by2; # This hash is fully-processed and provides speed at
# the cost of memory. It contains 2 byte keys and
# 3 byte values as [data] -> [Hamming code] lookups,
# eg "AA" => "HD " # (whatever the code is!)
# By using this hash, the program can read an
# input stream 2 bytes at a time, writing an output
# stream 3 bytes at a time - no messing aroung
# with half bytes or byte boundaries.
my %Hamming8rev; # This hash is semi-processed, and is used for
# decoding Hamming code to data. It contains
# binary text values for keys and bytes for values
# as [Hamming code] -> [data] lookups,
# eg "010010000100" => "A"
my %Hamming8by2rev; # This hash is fully-processed and provides speed at
# the cost of memory. It contains 3 byte keys and
# 2 byte values as [Hamming code] -> [data] lookups,
# eg "HD " => "AA" # (whatever the code is!)
# By using this hash, the program can read an
# input stream 3 bytes at a time, writing an output
# stream 2 bytes at a time - no messing aroung
# with half bytes or byte boundaries.
my ($x,$y,$key,$char,$char1,$char2,$chars,$char_out,$ham_text,$number);
#
# Hamming8raw is NOT the lookup table used! :)
# (that would be dreadfully inefficient).
# This hash is processed into a bytes -> bytes lookup.
#
%Hamming8raw = ("00000000" => "000000000000",
"00000001" => "000000000111",
"00000010" => "000000011001",
"00000011" => "000000011110",
"00000100" => "000000101010",
"00000101" => "000000101101",
"00000110" => "000000110011",
"00000111" => "000000110100",
"00001000" => "000001001011",
=back
=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
unhamming_err (SCALAR)
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
( run in 0.553 second using v1.01-cache-2.11-cpan-8d75d55dd25 )