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;
86 printf $fmt, $tapline, $crc32, $crc32;
95 perlcrc32.pl - display CRC-32 checksum value
99 B<perlcrc32.pl> [options]
102 -h show short usage help
103 --help show long detailed help
104 -c check the checksum is 0xffffffff
105 -d show CRC-32 in decimal
106 -v include TAP line with -c
107 -x show CRC-32 in lowercase hexadecimal
108 -X show CRC-32 in UPPERCASE hexadecimal
112 B<perlcrc32.pl> computes the ISO CRC-32 value of standard input
113 and displays it as an 8-digit lowercase hexadecimal number (by
114 default) to standard output.
122 Verify that the computed checksum is 0xFFFFFFFF ("checksum good")
123 and set the exit status to 0 if it is or 1 if it's not ("checksum
124 bad"). Giving option B<-c> by itself suppresses other output by
129 Show the computed CRC-32 value in decimal. If this option is given,
130 then the decimal version of the computed CRC-32 value is always
131 output (preceded by the hexadecimal value and a space if the
132 hexadecimal value is also being output).
134 The checksum always appears on a separate, following line,
135 I<after> any "ok/not ok" line (as produced by B<-cv>).
137 The decimal value of the computed CRC-32 value is never output by
142 Be verbose. Currently the B<-v> option only affects the B<-c>
145 If both B<-c> and B<-v> have been specified then, in addition to
146 setting the exit status, an S<"ok - checksum good"> or
147 S<"not ok - checksum bad"> line will be output to standard output
148 as the very first line (before the checksum itself).
152 Output the computed checksum using lowercase hexadecimal as exactly
153 8 hexadecimal digits at the very beginning of the line.
155 This is the default output unless B<-c>, B<-d> or B<-X> have been
156 given. Giving an explicit option B<-x> will force output even with
157 B<-c> and supersedes any B<-X> option.
159 The checksum always appears on a separate, following line,
160 I<after> any "ok/not ok" line (as produced by B<-cv>).
164 Output the computed checksum using UPPERCASE hexadecimal as exactly
165 8 hexadecimal digits at the very beginning of the line.
167 The B<-X> option supersedes any B<-x> option.
169 The checksum always appears on a separate, following line,
170 I<after> any "ok/not ok" line (as produced by B<-cv>).
176 The CRC-32 value being computed can be fully described as follows:
178 CRC 32 polynomial: 0x04C11DB7
179 initialization value: 0xFFFFFFFF
180 bitwise endianness: little
181 bit reversed result: yes
182 xor final value with: 0xFFFFFFFF
183 checksum "123456789": 0xCBF43926
185 This is the standard Ethernet/zlib CRC-32 computation and, in fact,
186 is performed by the zlib library's C<crc32> function courtesy of
187 the Compress::Zlib perl module.
189 Any output produced by perlcpio -c will result in a "good checksum"
190 result when using perlcrc32 -c.
194 Requires the Compress::Zlib module to compute the CRC-32 value.
200 =item Test Anything Protocol (TAP) specification (version 12)
202 =item L<https://testanything.org/tap-specification.html>
206 =head1 COPYRIGHT AND LICENSE
210 =item Copyright (C) 2020 Kyle J. McKay
212 =item All rights reserved.
216 Permission to use, copy, modify, and distribute this software for any
217 purpose with or without fee is hereby granted, provided that the above
218 copyright notice and this permission notice appear in all copies.
220 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
221 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
222 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
223 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
224 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
225 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
226 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.