3 # perlcrc32.pl - display CRC-32 checksum value
5 # Copyright (C) 2020 Kyle J. McKay.
8 # Permission to use, copy, modify, and distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
12 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 use File
::Basename
qw(basename);
27 use Compress
::Zlib
qw(crc32);
31 eval 'require Pod::Text::Termcap; 1;' and
32 @Pod::Usage
::ISA
= (qw( Pod::Text::Termcap ));
34 my $me = basename
($0);
35 close(DATA
) if fileno(DATA
);
37 exit(&main
(@ARGV)||0);
39 my ($opt_c, $opt_d, $opt_v, $opt_x);
41 my $totalbytesread; BEGIN { $totalbytesread = 0 }
45 select((select(STDERR
),$|=1)[0]); # just in case
46 Getopt
::Long
::Configure
('bundling');
48 'h' => sub {pod2usage
(-verbose
=> 0, -exitval
=> 0)},
49 'help' => sub {pod2usage
(-verbose
=> 2, -exitval
=> 0)},
53 'x' => sub {$opt_x = 'x'},
54 'X' => sub {$opt_x = 'X'},
55 ) && !@ARGV or pod2usage
(-verbose
=> 0, -exitval
=> 2);
56 $opt_x = 'x' if !defined($opt_x) && !defined($opt_d) && !defined($opt_c);
57 $opt_x = '' if !defined($opt_x) && (defined($opt_d) || defined($opt_c));
59 my $crc32 = crc32
("", undef);
65 my $bytes = sysread(STDIN
, $buf, 32768);
66 defined($bytes) or die "$me: error: failed reading input: $!\n";
68 $crc32 = crc32
($buf, $crc32);
74 if ($crc32 == 0xFFFFFFFF) {
75 $tapline = "ok - checksum good\n";
78 $tapline = "not ok - checksum bad\n";
80 $opt_v or $tapline = "";
83 $fmt .= "%08$opt_x" if $opt_x;
84 $fmt .= ($opt_x ?
' ' : '') . '%u' if $opt_d;
85 $fmt .= "\n" if $opt_x || $opt_d;
88 printf $fmt, $tapline, $crc32, $crc32;
98 perlcrc32.pl - display CRC-32 checksum value
102 B<perlcrc32.pl> [options]
105 -h show short usage help
106 --help show long detailed help
107 -c check the checksum is 0xffffffff
108 -d show CRC-32 in decimal
109 -v include TAP line with -c
110 -x show CRC-32 in lowercase hexadecimal
111 -X show CRC-32 in UPPERCASE hexadecimal
115 B<perlcrc32.pl> computes the ISO CRC-32 value of standard input
116 and displays it as an 8-digit lowercase hexadecimal number (by
117 default) to standard output.
125 Verify that the computed checksum is 0xFFFFFFFF ("checksum good")
126 and set the exit status to 0 if it is or 1 if it's not ("checksum
127 bad"). Giving option B<-c> by itself suppresses other output by
132 Show the computed CRC-32 value in decimal. If this option is given,
133 then the decimal version of the computed CRC-32 value is always
134 output (preceded by the hexadecimal value and a space if the
135 hexadecimal value is also being output).
137 The checksum always appears on a separate, following line,
138 I<after> any "ok/not ok" line (as produced by B<-cv>).
140 The decimal value of the computed CRC-32 value is never output by
145 Be verbose. Currently the B<-v> option only affects the B<-c>
148 If both B<-c> and B<-v> have been specified then, in addition to
149 setting the exit status, an S<"ok - checksum good"> or
150 S<"not ok - checksum bad"> line will be output to standard output
151 as the very first line (before the checksum itself).
155 Output the computed checksum using lowercase hexadecimal as exactly
156 8 hexadecimal digits at the very beginning of the line.
158 This is the default output unless B<-c>, B<-d> or B<-X> have been
159 given. Giving an explicit option B<-x> will force output even with
160 B<-c> and supersedes any B<-X> option.
162 The checksum always appears on a separate, following line,
163 I<after> any "ok/not ok" line (as produced by B<-cv>).
167 Output the computed checksum using UPPERCASE hexadecimal as exactly
168 8 hexadecimal digits at the very beginning of the line.
170 The B<-X> option supersedes any B<-x> option.
172 The checksum always appears on a separate, following line,
173 I<after> any "ok/not ok" line (as produced by B<-cv>).
179 The CRC-32 value being computed can be fully described as follows:
181 CRC 32 polynomial: 0x04C11DB7
182 initialization value: 0xFFFFFFFF
183 bitwise endianness: little
184 bit reversed result: yes
185 xor final value with: 0xFFFFFFFF
186 checksum "123456789": 0xCBF43926
188 This is the standard Ethernet/zlib CRC-32 computation and, in fact,
189 is performed by the zlib library's C<crc32> function courtesy of
190 the Compress::Zlib perl module.
192 Any output produced by perlcpio -c will result in a "good checksum"
193 result when using perlcrc32 -c.
197 Requires the Compress::Zlib module to compute the CRC-32 value.
203 =item Test Anything Protocol (TAP) specification (version 12)
205 =item L<https://testanything.org/tap-specification.html>
209 =head1 COPYRIGHT AND LICENSE
213 =item Copyright (C) 2020 Kyle J. McKay
215 =item All rights reserved.
219 Permission to use, copy, modify, and distribute this software for any
220 purpose with or without fee is hereby granted, provided that the above
221 copyright notice and this permission notice appear in all copies.
223 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
224 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
225 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
226 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
227 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
228 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
229 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.