crc.pl 976 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/perl
  2. die("Usage: crc <HEX-MESSAGE> <CRC>\n") if(int(@ARGV) != 2);
  3. my $msg = $ARGV[0];
  4. $msg =~ s/ //g;
  5. my $des = $ARGV[1];
  6. $des =~ s/ //g;
  7. # FFFF: 77 72 statt 2c 7f
  8. # FFFF: 5C AC statt DC D9
  9. #for(my $ic = 0; $ic < 65536; $ic++) {
  10. for(my $ic = 0; $ic < 2; $ic++) {
  11. my $crc = ($ic == 0?0:0xffffffff);
  12. for(my $i = 0; $i < length($msg); $i += 2) {
  13. my $n = ord(pack('H*', substr($msg, $i, 2)));
  14. my $od = $n;
  15. for my $b (0..7) {
  16. my $crcbit = ($crc & 0x80000000) ? 1 : 0;
  17. my $databit = ($n & 0x80) ? 1 : 0;
  18. $crc <<= 1;
  19. $n <<= 1;
  20. $crc ^= 0x04C11DB7 if($crcbit != $databit);
  21. # printf("%3d.%d %02x CRC %x ($crcbit $databit)\n", $i/2, $b, $n, $crc);
  22. }
  23. # printf("%3d %02x CRC %02x %02x\n", $i/2, $od, ($crc&0xff00)>>8, $crc&0xff);
  24. }
  25. # print "$ic\n" if($ic % 10000 == 0);
  26. printf("%02x %02x\n",($crc&0xff00)>>8,$crc&0xff);
  27. print "got $ic\n"
  28. if(sprintf("%02x%02x",($crc&0xff00)>>8,$crc&0xff) eq $des);
  29. }