summaryrefslogtreecommitdiffstats
path: root/data/crc16.pl
blob: 2515fb928d3c0b4254dbd3a8481c42a38aee2f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl

use bytes;

$c = 0;

while (read(STDIN, $data, 4096) > 0) {
    foreach $b (unpack("C*", $data)) {

	for ($i = 7; $i >= 0; $i--) {
	    $x = ($b >> $i) & 1;

	    $x ^= ($c & 0x8000) >> 15;
	    $c = ($c & 0x7fff) << 1;
	    $c ^= ($x ? 0x1021 : 0x0000);
	}
    }
}

printf "%04x\n", $c;