1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# guess-crc.pl |
4 |
# |
5 |
# 09/28/08 21:54:24 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
|
7 |
use strict; |
8 |
|
9 |
use Digest::CRC qw(crc32 crc16 crcccitt crc crc8); |
10 |
use Data::Dump qw/dump/; |
11 |
|
12 |
sub hex2b { |
13 |
my $hex = shift; |
14 |
return map { hex($_) } split(/\s+/, $hex); |
15 |
} |
16 |
|
17 |
my @poly; |
18 |
# ( 32833, 43271, 32833, 43271, 19299, 35925, 47123, 49319, 56971 ); |
19 |
|
20 |
foreach ( |
21 |
'D5 00 05 04 00 11 8C 66', |
22 |
'D6 00 05 FE 00 05 FA 40', |
23 |
) { |
24 |
my $hex = $_; |
25 |
$hex =~ s/\s+//g; |
26 |
|
27 |
my $bytes = pack('H*', substr($hex, 0, -4)); |
28 |
|
29 |
warn "?? crc ",substr($hex,-4); |
30 |
my $crc = pack('H4', substr($hex,-4)); |
31 |
|
32 |
warn "input $_ => ",dump( $bytes, $crc ); |
33 |
|
34 |
foreach my $o ( 0,1,2,3 ) { |
35 |
my $b = substr( $bytes, $o ); |
36 |
|
37 |
foreach my $poly ( 0x0000 .. 0xffff ) { |
38 |
|
39 |
print "## poly $poly\n" if $poly % 1000 == 0; |
40 |
|
41 |
my $ctx = Digest::CRC->new( |
42 |
#width=>16, init=>0xffff, xorout=>0, refout=>1, poly=>0x1021, refin=>0 # ccitt |
43 |
#width=>16, init=>0, xorout=>0, refout=>1, poly=>0x8005, refin=>1 # crc16 |
44 |
#0x8408, |
45 |
#0xa001, |
46 |
width=>16, init=>0, xorout=>0, refout=>1, poly=>$poly,refin=>1 |
47 |
); |
48 |
$ctx->add( $b ); |
49 |
my $try = $ctx->digest; |
50 |
|
51 |
my $v = pack('v*', $try); |
52 |
my $n = pack('n*', $try); |
53 |
|
54 |
if ( $v eq $crc or $n eq $crc ) { |
55 |
warn "HIT: $o poly: $poly ",dump( $crc, $v, $n ); |
56 |
push @poly, $poly; |
57 |
} |
58 |
} |
59 |
} |
60 |
} |
61 |
|
62 |
print "FOUND poly = ", dump( @poly ); |