File Coverage

blib/lib/Digest/Crc32.pm
Criterion Covered Total %
statement 25 35 71.4
branch 2 4 50.0
condition 1 3 33.3
subroutine 6 8 75.0
pod 0 4 0.0
total 34 54 62.9


line stmt bran cond sub pod time code
1             package Digest::Crc32;
2            
3             # Cyclic Redundency Check interface for buffers and files
4            
5 1     1   5965 use strict;
  1         2  
  1         40  
6 1     1   8 use Carp;
  1         1  
  1         87  
7 1     1   4 use vars qw($VERSION $poly);
  1         7  
  1         355  
8            
9             $VERSION = 0.01;
10            
11             $poly = 0xEDB88320;
12            
13 0     0 0 0 sub version { sprintf("%f", $VERSION); }
14            
15             sub new {
16 1     1 0 12 my $self = {};
17 1         3 my $proto = shift;
18 1   33     6 my $class = ref($proto) || $proto;
19 1         3 bless($self,$class);
20 1         4 return $self;
21             }
22            
23             sub _crc32 {
24 3     3   4 my $self = shift;
25 3         4 my $comp = shift;
26 3 100       7 for (my $cnt = 0; $cnt < 8; $cnt++) { $comp = $comp & 1 ? $poly ^ ($comp >> 1) : $comp >> 1; }
  24         60  
27 3         8 return $comp;
28             }
29            
30             sub strcrc32 {
31 1     1 0 5 my $self = shift;
32 1         2 my $crc = 0xFFFFFFFF;
33 1         3 my ($tcmp) = @_;
34 1         6 foreach (split(//,$tcmp)) { $crc = (($crc>>8) & 0x00FFFFFF) ^ $self->_crc32(($crc ^ ord($_)) & 0xFF); }
  3         10  
35 1         3 return $crc^0xFFFFFFFF;
36             }
37            
38             sub filecrc32 {
39 0     0 0   my $self = shift;
40 0           my $file = shift;
41 0           my $crc = 0xFFFFFFFF;
42 0 0         open(FILE, $file) or croak "Failed to open the file";
43 0           while () {
44 0           foreach (split(//,$_)) { $crc = (($crc>>8) & 0x00FFFFFF) ^ $self->_crc32(($crc ^ ord($_)) & 0xFF); }
  0            
45             }
46 0           close(FILE);
47 0           return $crc^0xFFFFFFFF;
48             }
49            
50            
51             =head1 NAME
52            
53             Digest::CRC32 - Cyclic Redundency Check digests implementation
54            
55             =head1 VERSION
56            
57             0.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            
73             This module provides a perl implementation to generate 32 bits CRC digests for buffers and files.
74            
75             =head1 COPYRIGHT
76            
77             Copyright 2004 by Faycal Chraibi. All rights reserved.
78            
79             This 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            
83             Faycal Chraibi
84            
85             =head1 SEE ALSO
86            
87             L