I am trying to implement Perl digest for CRC, but unfortunately I am getting:
Digest Read failed: Bad File Descriptor
How do I fix this problem?
This is the module sample code here:
sub crc3439() {
$ctx = Digest::CRC->new(type=>"crc16");
$ctx = Digest::CRC->new(width=>16, init=>0x2345, xorout=>0x0000,
refout=>1, poly=>0x8005, refin=>1, cont=>1);
my $binfile = 'envbin.bin';
open(fhbin, '>', $binfile) or die "Could not open bin file '$binfile' $!";
binmode(fhbin);
$ctx->add($binfile);
$ctx->addfile(*binfile);
$digest = $ctx->hexdigest;
return $digest;
}
First, you're overwriting $binfile
instead of reading it. Changing the open mode to '<'
should fix that.
Your ->addfile
is adding a file handle that doesn't exist; you probably want *fhbin
there, or a lexical (my $fhbin
) file handle instead.
Also, you overwrite $ctx
with the extra ->new
call.
sub crc3439 {
my $binfile = shift;
my $ctx = Digest::CRC->new(
type => "crc16",
width => 16,
init => 0x2345,
xorout => 0x0000,
refout => 1,
poly => 0x8005,
refin => 1,
cont => 1,
);
open(my $fhbin, '<', $binfile) or die "Could not open bin file '$binfile' $!";
binmode($fhbin);
$ctx->add($binfile);
$ctx->addfile($fhbin);
return $ctx->hexdigest;
}
print crc3439('foo.bin');