1package Digest::Crc32; 2 3# Cyclic Redundency Check interface for buffers and files 4 5use strict; 6use Carp; 7use vars qw($VERSION $poly); 8 9$VERSION = 0.01; 10 11$poly = 0xEDB88320; 12 13sub version { sprintf("%f", $VERSION); } 14 15sub new { 16 my $self = {}; 17 my $proto = shift; 18 my $class = ref($proto) || $proto; 19 bless($self,$class); 20 return $self; 21} 22 23sub _crc32 { 24 my $self = shift; 25 my $comp = shift; 26 for (my $cnt = 0; $cnt < 8; $cnt++) { $comp = $comp & 1 ? $poly ^ ($comp >> 1) : $comp >> 1; } 27 return $comp; 28} 29 30sub strcrc32 { 31 my $self = shift; 32 my $crc = 0xFFFFFFFF; 33 my ($tcmp) = @_; 34 foreach (split(//,$tcmp)) { $crc = (($crc>>8) & 0x00FFFFFF) ^ $self->_crc32(($crc ^ ord($_)) & 0xFF); } 35 return $crc^0xFFFFFFFF; 36} 37 38sub filecrc32 { 39 my $self = shift; 40 my $file = shift; 41 my $crc = 0xFFFFFFFF; 42 open(FILE, $file) or croak "Failed to open the file"; 43 while (<FILE>) { 44 foreach (split(//,$_)) { $crc = (($crc>>8) & 0x00FFFFFF) ^ $self->_crc32(($crc ^ ord($_)) & 0xFF); } 45 } 46 close(FILE); 47 return $crc^0xFFFFFFFF; 48} 49 50 51=head1 NAME 52 53Digest::CRC32 - Cyclic Redundency Check digests implementation 54 55=head1 VERSION 56 570.01 58 59=head1 SYNOPSIS 60 61 use Digest::CRC32; 62 63 my $crc = new Digest::CRC32(); 64 65 # Digest for a string 66 printf $crc->strcrc32("Hello world"); 67 68 #Digest for a file 69 print $crc->filecrc32($myfile); 70 71=head1 DESCRIPTION 72 73This module provides a perl implementation to generate 32 bits CRC digests for buffers and files. 74 75=head1 COPYRIGHT 76 77Copyright 2004 by Faycal Chraibi. All rights reserved. 78 79This library is a free software. You can redistribute it and/or modify it under the same terms as Perl itself. 80 81=head1 AUTHOR 82 83Faycal Chraibi <fays@cpan.org> 84 85=head1 SEE ALSO 86 87L<Digest::CRC> 88