String::CRC32#

📦 std

Compute the 32-bit CRC of a string or of what a filehandle reads, the CRC-32 of zip, gzip and PNG.

crc32 is exported by default. Its last argument, when it has more than one, is the CRC to continue from, so a large input can be taken in pieces. A filehandle is read from where it stands to its end.

The CRC is computed by the zlib engine of the built-in Compress::Raw::Zlib, so the two modules always agree.

This is a native implementation of String::CRC32 2.100’s CRC32.xs; its String/CRC32.pm is used unchanged.

Synopsis#

use String::CRC32;

printf "%08x\n", crc32("123456789");          # cbf43926
my $crc = crc32("12345");
printf "%08x\n", crc32("6789", $crc);         # cbf43926, continued

open my $fh, '<:raw', $path or die $!;
my $file_crc = crc32($fh);                    # the rest of the file

Differences from upstream#

One, where perl 5.44 with CRC32.xs fails (t/81-xs-native/String-CRC32/150-or-better.t): a filehandle whose read fails, a closed one for instance, gives the CRC of no data. perl5 keeps the failed read’s -1 as 65535 bytes read, from a buffer of 32768, and dumps core.

Functions#

Checksums#

crc32#

crc32($data), crc32($data, $crc): the CRC-32 of a string (its bytes; a character string’s UTF-8), or of what a filehandle - a glob, a reference to one, an IO object - reads from where it stands to its end. With more than one argument, the last is the CRC to continue from (0 when there is none), read as a number. The CRC is an unsigned integer.