#!/usr/bin/perl -w use strict; package SCX::CRC; our @crctable; sub import { return if @crctable; my @table = (0x31); for my $i (1..7) { my $n = 2 * $table[-1]; push @table, $n > 0xff ? ($n ^ 0x31) & 0xff: $n; } @crctable = (0x00); # print "Table = ", join(' ', map { sprintf("%02x", $_) } @table), "\n"; for my $i (0x01 .. 0xff) { $crctable[$i] ^= $table[0] if $i & 0x01; $crctable[$i] ^= $table[1] if $i & 0x02; $crctable[$i] ^= $table[2] if $i & 0x04; $crctable[$i] ^= $table[3] if $i & 0x08; $crctable[$i] ^= $table[4] if $i & 0x10; $crctable[$i] ^= $table[5] if $i & 0x20; $crctable[$i] ^= $table[6] if $i & 0x40; $crctable[$i] ^= $table[7] if $i & 0x80; } # print "CRCtable = ", join(' ', map { sprintf("%02x", $_) } @crctable), "\n"; } sub digest_str { my ($str) = @_; return digest(unpack("C*", $str)); } sub digest { my (@bytes) = @_; my $res = 0; for my $byte (@bytes) { my $idx = $byte ^ $res; $res = $crctable[$idx]; } return $res ^ 0xbb; } 1;