Algorithm-Hamming-Perl

 view release on metacpan or  search on metacpan

Perl.pm  view on Meta::CPAN

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.
	#
	for ($pos = 0; $pos < ($length-1); $pos+=2) {

		$chars_in = substr($data,$pos,2);
		if (defined $Hamming8by2{$chars_in}) {
			#
			#  Fast method
			#
			$output .= $Hamming8by2{$chars_in};
		} else {
			#
			#  Slow method

Perl.pm  view on Meta::CPAN

			$char_out = pack("B*",$ham_text);

			### Add to output
			$output .= $char_out;
		}
	}

	#
	#  A leftover byte (if present) is padded with 0's.
	#
	if ($length == ($pos + 1)) {

		### Get the last character
		$char_in1 = substr($data,$pos,1);

		### Generate padded hamming text
		$ham_text = $Hamming8semi{$char_in1} . "0000";
	
		### Turn into 2 bytes
		$char_out = pack("B*",$ham_text);

Perl.pm  view on Meta::CPAN

	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.
	#
	for ($pos = 0; $pos < ($length-2); $pos+=3) {

		### Fetch 3 bytes
		$chars_in = substr($data,$pos,3);

		if (defined $Hamming8by2rev{$chars_in}) {
			#
			#  Fast method
			#
			$output .= $Hamming8by2rev{$chars_in};
		} else {

Perl.pm  view on Meta::CPAN

			$err_all += $err;

			### Add bytes to output
			$output .= $char_out1 . $char_out2;
		}
	}

	#
	#  Decode leftover bytes (if present).
	#
	if ($length == ($pos + 2)) {
		### Fetch the 2 leftover bytes
		$chars_in = substr($data,$pos,2);

		### Fetch the Hamming code
		$ham_text = unpack("B*",$chars_in);
		$ham_text1 = substr($ham_text,0,12);
	
		### Convert the code to the original byte
		($char_out1,$err) = unhamchar($ham_text1);
		$err_all += $err;

Perl.pm  view on Meta::CPAN

the one byte then this encoding cannot auto correct the error.

=head1 BUGS

Try not to join Hamming encoded strings together - this may give results
that look like a bug. If an odd number of input byes is encoded, the output
code is short half a byte - and so is padded with '0' bits. Joining these 
with a string concatenation will contain the padding bits that will confuse 
decoding. 

The above problem can occur when inputing and outputing certain lengths
to filehandles. To be safe, my example code uses a buffer of 3072 bytes - 
a safe size to use with filehandles.

=head1 COPYRIGHT

Copyright (c) 2003 Brendan Gregg. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself

=head1 AUTHOR

README  view on Meta::CPAN


BUGS
     Try not to join Hamming encoded strings together - this may
     give results that look like a bug. If an odd number of input
     byes is encoded, the output code is short half a byte - and
     so is padded with '0' bits. Joining these with a string
     concatenation will contain the padding bits that will
     confuse decoding.

     The above problem can occur when inputing and outputing
     certain lengths to filehandles. To be safe, my example code
     uses a buffer of 3072 bytes - a safe size to use with
     filehandles.

AUTHOR
     Brendan Gregg <brendan.gregg@tpg.com.au> [Sydney, Australia]



( run in 0.571 second using v1.01-cache-2.11-cpan-65fba6d93b7 )