4 |
# |
# |
5 |
# 09/28/08 21:54:24 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
# 09/28/08 21:54:24 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
|
|
7 |
|
# CRC info from: http://stackoverflow.com/questions/149617/how-could-i-guess-a-checksum-algorithm |
8 |
|
# and from: http://www.geocities.com/SiliconValley/Pines/8659/crc.htm |
9 |
|
|
10 |
|
# LRC: Polynomial=0x81; 8 bits; Normal; Initial=0; Final=as calculated |
11 |
|
# CRC16: Polynomial=0xa001; 16 bits; Normal; Initial=0; Final=as calculated |
12 |
|
# CCITT: Polynomial=0x1021; 16 bits; reverse; Initial=0xffff; Final=0x1d0f |
13 |
|
# Xmodem: Polynomial=0x1021; 16 bits; reverse; Initial=0; Final=0x1d0f |
14 |
|
# CRC32: Polynomial=0xebd88320; 32 bits; Normal; Initial=0xffffffff; Final=inverted value |
15 |
|
# ZIP32: Polynomial=0x04c11db7; 32 bits; Normal; Initial=0xffffffff; Final=as calculated |
16 |
|
|
17 |
|
# IPX: Polynomial=0x8005; 16 bits; Reverse; Initial=0xffff; Final=as calculated |
18 |
|
# ISO 18000-6B: Polynomial=0x1021; 16 bits; Reverse; Initial=0xffff; Final=as calculated |
19 |
|
# ISO 18000-6C: Polynomial=0x1021; 16 bits; Reverse; Initial=0xffff; Final=as calculated |
20 |
|
# Data must be padded with zeroes to make a multiple of 8 bits |
21 |
|
# ISO CRC5: Polynomial=custom; 5 bits; Reverse; Initial=0x9; Final=shifted left by 3 bits |
22 |
|
# Data must be padded with zeroes to make a multiple of 8 bits |
23 |
|
# EPC class 1: Polynomial=custom 0x1021; 16 bits; Reverse; Initial=0xffff; Final=post processing of 16 zero bits |
24 |
|
|
25 |
use strict; |
use strict; |
26 |
|
|
27 |
use Digest::CRC qw(crc32 crc16 crcccitt crc crc8); |
use Digest::CRC qw(crc32 crc16 crcccitt crc crc8); |
28 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
29 |
|
use File::Slurp; |
30 |
|
|
31 |
my $debug = 1 if @ARGV; |
my $debug = 1 if @ARGV; |
32 |
|
|
36 |
} |
} |
37 |
|
|
38 |
my @check_polys = ( 0x0000 .. 0xffff ); |
my @check_polys = ( 0x0000 .. 0xffff ); |
39 |
@check_polys = ( 32833, 43271, 32833, 43271, 19299, 35925, 47123, 49319, 56971 ); |
#@check_polys = ( 32833, 43271, 32833, 43271, 19299, 35925, 47123, 49319, 56971 ); # crc16 |
40 |
|
@check_polys = ( 31125, 53345, 11642, 24337, 51699, 55122, 15257 ); # ccitt |
41 |
|
|
42 |
my @check_offset = ( 0,1,2,3 ); |
my @check_offset = ( 0,1,2,3 ); |
43 |
@check_offset = ( 1 ); # skip first, second always zero |
@check_offset = ( 1 ); # skip first, second always zero |
44 |
|
|
45 |
|
my @try_polys = ( |
46 |
|
{ name => "CRC-16/CITT", width => 16, poly => 0x1021, init => 0xFFFF, refin => 0, refout => 0, xorout => 0x0000 }, |
47 |
|
{ name => "XMODEM", width => 16, poly => 0x8408, init => 0x0000, refin => 1, refout => 1, xorout => 0x0000 }, |
48 |
|
{ name => "ARC", width => 16, poly => 0x8005, init => 0x0000, refin => 1, refout => 1, xorout => 0x0000 }, |
49 |
|
{ name => "CRC-16", width => 16, poly => 0xA001, init => 0x0000, refin => 0, refout => 0, xorout => 0x0000 }, |
50 |
|
); |
51 |
|
|
52 |
|
my @data = ( |
53 |
|
'D5 00 05 04 00 11 8C 66', |
54 |
|
'D6 00 05 FE 00 05 FA 40', |
55 |
|
); |
56 |
|
#@data = read_file('all.out') if -e 'all.out'; |
57 |
|
|
58 |
my $found_polys; |
my $found_polys; |
59 |
|
|
60 |
sub check_crc { |
sub check_crc { |
64 |
#width=>16, init=>0, xorout=>0, refout=>1, poly=>0x8005, refin=>1 # crc16 |
#width=>16, init=>0, xorout=>0, refout=>1, poly=>0x8005, refin=>1 # crc16 |
65 |
#0x8408, |
#0x8408, |
66 |
#0xa001, |
#0xa001, |
67 |
width=>16, init=>0, xorout=>0, refout=>1, poly=>$poly,refin=>1 |
#width=>16, init=>0, xorout=>0, refout=>1, poly=>$poly,refin=>1 # crc16 |
68 |
#width=>16, init=>0xffff, xorout=>0, refout=>1, poly=>$poly, refin=>0 # ccitt |
width=>16, init=>0xffff, xorout=>0, refout=>1, poly=>$poly, refin=>0 # ccitt |
69 |
); |
); |
70 |
$ctx->add( $bytes ); |
$ctx->add( $bytes ); |
71 |
my $try = $ctx->digest; |
my $try = $ctx->digest; |
72 |
my $str = pack('n*', $try); |
my $n = pack('n*', $try); |
73 |
warn "## check_crc( $poly, ",dump($bytes)," ) = ",dump( $str, $wanted ) if $debug; |
my $v = pack('n*', $try); |
74 |
return $str eq $wanted; |
warn "## check_crc( $poly, ",dump($bytes)," ) = ",dump( $n, $v, $wanted ) if $debug; |
75 |
|
return $n eq $wanted || $v eq $wanted; |
76 |
} |
} |
77 |
|
|
78 |
foreach ( |
foreach ( @data ) { |
79 |
'D5 00 05 04 00 11 8C 66', |
|
|
'D6 00 05 FE 00 05 FA 40', |
|
|
) { |
|
80 |
my $hex = $_; |
my $hex = $_; |
81 |
$hex =~ s/\s+//g; |
$hex =~ s/\s+//g; |
82 |
|
|
83 |
my $bytes = pack('H*', substr($hex, 0, -4)); |
my $bytes = pack('H*', substr($hex, 0, -4)); |
84 |
|
|
85 |
warn "?? guess crc ",substr($hex,-4); |
print "?? guess crc ",substr($hex,-4),"\n"; |
86 |
my $crc = pack('H4', substr($hex,-4)); |
my $crc = pack('H4', substr($hex,-4)); |
87 |
|
|
88 |
warn "input $_ => ",dump( $bytes, $crc ); |
warn "input $_ => ",dump( $bytes, $crc ) if $debug; |
89 |
|
|
90 |
foreach my $o ( @check_offset ) { |
foreach my $o ( @check_offset ) { |
91 |
my $b = substr( $bytes, $o ); |
my $b = substr( $bytes, $o ); |