Skip to content

Commit

Permalink
Merge pull request #157 from mknos/sum-crc-sign-ext
Browse files Browse the repository at this point in the history
sum: incorrect CRC output
  • Loading branch information
briandfoy committed May 8, 2023
2 parents 2f37350 + b9cfffb commit cd68bf5
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions bin/sum
Expand Up @@ -96,6 +96,7 @@ sub crc32 {
my($crc) = my($len) = 0;
my($buf,$num,$i);
my($buflen) = 4096; # buffer is "4k", you can up it if you want...
my $MASK32 = 0xffffffff;

# crctable/crc32 alg converted from openbsd's cksum program ...
my(@crctable) = (
Expand Down Expand Up @@ -154,18 +155,20 @@ sub crc32 {
);

while($num = sysread $fh, $buf, $buflen) {
for($len = ($len+$num)&0xffffffff, $i = 0; $i<$num; $i++) {
for($len = ($len + $num) & $MASK32, $i = 0; $i < $num; $i++) {
$crc = ($crc << 8 ^ $crctable[$crc >> 24 ^ ord(substr $buf, $i, 1)])
& 0xffffffff;
& $MASK32;
}
}

my($rlen) = $len; # for reporting ...
for(;$len!=0;$len>>=8) { # MSB first
$crc = (($crc << 8) ^ $crctable[($crc >> 24) ^ ($len&0xff)])
& 0xffffffff;
& $MASK32;
}
return $num,~$crc,$rlen;
$crc = ~$crc;
$crc &= $MASK32;
return $num, $crc, $rlen;
}

sub help {
Expand Down

0 comments on commit cd68bf5

Please sign in to comment.