39 |
|
|
40 |
L<perl(1)> |
L<perl(1)> |
41 |
|
|
42 |
|
L<http://stackoverflow.com/questions/149617/how-could-i-guess-a-checksum-algorithm> |
43 |
|
|
44 |
=head1 AUTHOR |
=head1 AUTHOR |
45 |
|
|
46 |
Dobrica Pavlinusic <dpavlin@rot13.org> L<http://www.rot13.org/~dpavlin/> |
Dobrica Pavlinusic <dpavlin@rot13.org> L<http://www.rot13.org/~dpavlin/> |
221 |
} |
} |
222 |
} |
} |
223 |
|
|
224 |
|
use Digest::CRC; |
225 |
|
|
226 |
|
sub crcccitt { |
227 |
|
my $bytes = shift; |
228 |
|
my $crc = Digest::CRC->new( |
229 |
|
# midified CCITT to xor with 0xffff instead of 0x0000 |
230 |
|
width => 16, init => 0xffff, xorout => 0xffff, refout => 0, poly => 0x1021, refin => 0, |
231 |
|
) or die $!; |
232 |
|
$crc->add( $bytes ); |
233 |
|
pack('n', $crc->digest); |
234 |
|
} |
235 |
|
|
236 |
# my $checksum = checksum( $bytes ); |
# my $checksum = checksum( $bytes ); |
237 |
# my $checksum = checksum( $bytes, $original_checksum ); |
# my $checksum = checksum( $bytes, $original_checksum ); |
238 |
sub checksum { |
sub checksum { |
239 |
my ( $bytes, $checksum ) = @_; |
my ( $bytes, $checksum ) = @_; |
240 |
|
|
241 |
my $xor = $checksum; # FIXME |
my $xor = crcccitt( substr($bytes,1) ); # skip D6 |
242 |
|
warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; |
243 |
|
|
244 |
if ( defined $checksum && $xor ne $checksum ) { |
if ( defined $checksum && $xor ne $checksum ) { |
245 |
print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; |
print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; |