/[RFID]/guess-crc.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /guess-crc.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 11 by dpavlin, Sun Sep 28 22:09:09 2008 UTC revision 12 by dpavlin, Mon Sep 29 16:28:49 2008 UTC
# Line 9  use strict; Line 9  use strict;
9  use Digest::CRC qw(crc32 crc16 crcccitt crc crc8);  use Digest::CRC qw(crc32 crc16 crcccitt crc crc8);
10  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
11    
12    my $debug = 1 if @ARGV;
13    
14  sub hex2b {  sub hex2b {
15          my $hex = shift;          my $hex = shift;
16          return map { hex($_) } split(/\s+/, $hex);          return map { hex($_) } split(/\s+/, $hex);
17  }  }
18    
19  my @poly;  my @check_polys = ( 0x0000 .. 0xffff );
20  # ( 32833, 43271, 32833, 43271, 19299, 35925, 47123, 49319, 56971 );  @check_polys = ( 32833, 43271, 32833, 43271, 19299, 35925, 47123, 49319, 56971 );
21    
22    my @check_offset = ( 0,1,2,3 );
23    @check_offset = ( 1 ); # skip first, second always zero
24    
25    my $found_polys;
26    
27    sub check_crc {
28            my ( $poly, $bytes, $wanted ) = @_;
29            my $ctx = Digest::CRC->new(
30                    #width=>16, init=>0xffff, xorout=>0, refout=>1, poly=>0x1021, refin=>0 # ccitt
31                    #width=>16, init=>0,      xorout=>0, refout=>1, poly=>0x8005, refin=>1 # crc16
32                    #0x8408,
33                    #0xa001,
34                    width=>16, init=>0, xorout=>0, refout=>1, poly=>$poly,refin=>1
35                    #width=>16, init=>0xffff, xorout=>0, refout=>1, poly=>$poly, refin=>0 # ccitt
36            );
37            $ctx->add( $bytes );
38            my $try = $ctx->digest;
39            my $str = pack('n*', $try);
40            warn "## check_crc( $poly, ",dump($bytes)," ) = ",dump( $str, $wanted ) if $debug;
41            return $str eq $wanted;
42    }
43    
44  foreach (  foreach (
45  'D5 00 05 04 00 11 8C 66',  'D5 00 05 04 00 11 8C 66',
# Line 26  foreach ( Line 50  foreach (
50    
51          my $bytes = pack('H*', substr($hex, 0, -4));          my $bytes = pack('H*', substr($hex, 0, -4));
52    
53          warn "?? crc ",substr($hex,-4);          warn "?? guess crc ",substr($hex,-4);
54          my $crc   = pack('H4', substr($hex,-4));          my $crc   = pack('H4', substr($hex,-4));
55    
56          warn "input $_ => ",dump( $bytes, $crc );          warn "input $_ => ",dump( $bytes, $crc );
57    
58          foreach my $o ( 0,1,2,3 ) {          foreach my $o ( @check_offset ) {
59                  my $b = substr( $bytes, $o );                  my $b = substr( $bytes, $o );
60    
61                  foreach my $poly ( 0x0000 .. 0xffff ) {                  foreach my $poly ( @check_polys ) {
62    
63                          print "## poly $poly\n" if $poly % 1000 == 0;                          print "## poly $poly\n" if $poly % 1000 == 0;
64    
65                          my $ctx = Digest::CRC->new(                          if ( check_crc( $poly, $b, $crc ) ) {
66                                  #width=>16, init=>0xffff, xorout=>0, refout=>1, poly=>0x1021, refin=>0 # ccitt                                  warn "HIT: $o poly: $poly\n";
67                                  #width=>16, init=>0,      xorout=>0, refout=>1, poly=>0x8005, refin=>1 # crc16                                  $found_polys->{$poly}->{$o}++;
                                 #0x8408,  
                                 #0xa001,  
                                 width=>16, init=>0, xorout=>0, refout=>1, poly=>$poly,refin=>1  
                         );  
                         $ctx->add( $b );  
                         my $try = $ctx->digest;  
   
                         my $v = pack('v*', $try);  
                         my $n = pack('n*', $try);  
   
                         if ( $v eq $crc or $n eq $crc ) {  
                                 warn "HIT: $o poly: $poly ",dump( $crc, $v, $n );  
                                 push @poly, $poly;  
68                          }                          }
69                  }                  }
70          }          }
71  }  }
72    
73  print "FOUND poly = ", dump( @poly );  print "FOUND polys = ", dump( $found_polys );

Legend:
Removed from v.11  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26