line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Checksum::CRC32; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
45
|
|
4
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
63
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.080'; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
8
|
use base qw(Crypt::Checksum Exporter); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
392
|
|
8
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [qw( crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int )] ); |
9
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
10
|
|
|
|
|
|
|
our @EXPORT = qw(); |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
108
|
|
13
|
|
|
|
|
|
|
$Carp::Internal{(__PACKAGE__)}++; |
14
|
2
|
|
|
2
|
|
322
|
use CryptX; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
325
|
|
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
1
|
11277
|
sub crc32_file { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->digest } |
|
1
|
|
|
|
|
16
|
|
17
|
4
|
|
|
4
|
1
|
17
|
sub crc32_file_hex { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->hexdigest } |
|
4
|
|
|
|
|
27
|
|
18
|
1
|
|
|
1
|
1
|
6
|
sub crc32_file_int { local $SIG{__DIE__} = \&CryptX::_croak; Crypt::Checksum::CRC32->new->addfile(@_)->intdigest } |
|
1
|
|
|
|
|
9
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
1; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=pod |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 NAME |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Crypt::Checksum::CRC32 - Compute CRC32 checksum |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
### Functional interface: |
31
|
|
|
|
|
|
|
use Crypt::Checksum::CRC32 ':all'; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# calculate CRC32 checksum from string/buffer |
34
|
|
|
|
|
|
|
$checksum_raw = crc32_data($data); |
35
|
|
|
|
|
|
|
$checksum_hex = crc32_data_hex($data); |
36
|
|
|
|
|
|
|
$checksum_int = crc32_data_int($data); |
37
|
|
|
|
|
|
|
# calculate CRC32 checksum from file |
38
|
|
|
|
|
|
|
$checksum_raw = crc32_file('filename.dat'); |
39
|
|
|
|
|
|
|
$checksum_hex = crc32_file_hex('filename.dat'); |
40
|
|
|
|
|
|
|
$checksum_int = crc32_file_int('filename.dat'); |
41
|
|
|
|
|
|
|
# calculate CRC32 checksum from filehandle |
42
|
|
|
|
|
|
|
$checksum_raw = crc32_file(*FILEHANDLE); |
43
|
|
|
|
|
|
|
$checksum_hex = crc32_file_hex(*FILEHANDLE); |
44
|
|
|
|
|
|
|
$checksum_int = crc32_file_int(*FILEHANDLE); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
### OO interface: |
47
|
|
|
|
|
|
|
use Crypt::Checksum::CRC32; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$d = Crypt::Checksum::CRC32->new; |
50
|
|
|
|
|
|
|
$d->add('any data'); |
51
|
|
|
|
|
|
|
$d->add('another data'); |
52
|
|
|
|
|
|
|
$d->addfile('filename.dat'); |
53
|
|
|
|
|
|
|
$d->addfile(*FILEHANDLE); |
54
|
|
|
|
|
|
|
$checksum_raw = $d->digest; # raw 4 bytes |
55
|
|
|
|
|
|
|
$checksum_hex = $d->hexdigest; # hexadecimal form |
56
|
|
|
|
|
|
|
$checksum_int = $d->intdigest; # 32bit unsigned integer |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 DESCRIPTION |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Calculating CRC32 checksums. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
I |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 EXPORT |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Nothing is exported by default. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
You can export selected functions: |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
use Crypt::Checksum::CRC32 qw(crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Or all of them at once: |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Crypt::Checksum::CRC32 ':all'; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 FUNCTIONS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 crc32_data |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Returns checksum as raw octects. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$checksum_raw = crc32_data('data string'); |
83
|
|
|
|
|
|
|
#or |
84
|
|
|
|
|
|
|
$checksum_raw = crc32_data('any data', 'more data', 'even more data'); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 crc32_data_hex |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Returns checksum as a hexadecimal string. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$checksum_hex = crc32_data_hex('data string'); |
91
|
|
|
|
|
|
|
#or |
92
|
|
|
|
|
|
|
$checksum_hex = crc32_data_hex('any data', 'more data', 'even more data'); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 crc32_data_int |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Returns checksum as unsigned 32bit integer. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$checksum_int = crc32_data_int('data string'); |
99
|
|
|
|
|
|
|
#or |
100
|
|
|
|
|
|
|
$checksum_int = crc32_data_int('any data', 'more data', 'even more data'); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 crc32_file |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Returns checksum as raw octects. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$checksum_raw = crc32_file('filename.dat'); |
107
|
|
|
|
|
|
|
#or |
108
|
|
|
|
|
|
|
$checksum_raw = crc32_file(*FILEHANDLE); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 crc32_file_hex |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Returns checksum as a hexadecimal string. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$checksum_hex = crc32_file_hex('filename.dat'); |
115
|
|
|
|
|
|
|
#or |
116
|
|
|
|
|
|
|
$checksum_hex = crc32_file_hex(*FILEHANDLE); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 crc32_file_int |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Returns checksum as unsigned 32bit integer. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$checksum_int = crc32_file_int('filename.dat'); |
123
|
|
|
|
|
|
|
#or |
124
|
|
|
|
|
|
|
$checksum_int = crc32_file_int(*FILEHANDLE); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 METHODS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 new |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Constructor, returns a reference to the checksum object. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$d = Crypt::Checksum::CRC32->new; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 clone |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Creates a copy of the checksum object state and returns a reference to the copy. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
$d->clone(); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 reset |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Reinitialize the checksum object state and returns a reference to the checksum object. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
$d->reset(); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 add |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
All arguments are appended to the message we calculate checksum for. |
149
|
|
|
|
|
|
|
The return value is the checksum object itself. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
$d->add('any data'); |
152
|
|
|
|
|
|
|
#or |
153
|
|
|
|
|
|
|
$d->add('any data', 'more data', 'even more data'); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 addfile |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The content of the file (or filehandle) is appended to the message we calculate checksum for. |
158
|
|
|
|
|
|
|
The return value is the checksum object itself. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$d->addfile('filename.dat'); |
161
|
|
|
|
|
|
|
#or |
162
|
|
|
|
|
|
|
$d->addfile(*FILEHANDLE); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
B You have to make sure that the filehandle is in binary mode before you pass it as argument to the addfile() method. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 digest |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Returns the binary checksum (raw bytes). |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$result_raw = $d->digest(); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 hexdigest |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Returns the checksum encoded as a hexadecimal string. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
$result_hex = $d->hexdigest(); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 intdigest |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Returns the checksum encoded as unsigned 32bit integer. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
$result_int = $d->intdigest(); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 SEE ALSO |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=over |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item * L |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item * L |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=back |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |