use Fcntl qw(O_RDONLY);
use integer;
-$VERSION = '5.97';
+$VERSION = '5.98';
require Exporter;
require DynaLoader;
return(_addfile($self, $file)) unless ref(\$file) eq 'SCALAR';
$mode = defined($mode) ? $mode : "";
- my ($binary, $UNIVERSAL, $BITS, $portable) =
- map { $_ eq $mode } ("b", "U", "0", "p");
+ my ($binary, $UNIVERSAL, $BITS) =
+ map { $_ eq $mode } ("b", "U", "0");
## Always interpret "-" to mean STDIN; otherwise use
## sysopen to handle full range of POSIX file names
return($self);
}
- binmode(FH) if $binary || $portable || $UNIVERSAL;
+ binmode(FH) if $binary || $UNIVERSAL;
if ($UNIVERSAL && _istext(*FH, $file)) {
$self->_addfileuniv(*FH);
}
- elsif ($portable && _istext(*FH, $file)) {
- while (<FH>) {
- s/\015?\015\012/\012/g;
- s/\015/\012/g;
- $self->add($_);
- }
- }
else { $self->_addfilebin(*FH) }
close(FH);
"0" use BITS mode
- "p" use portable mode (to be deprecated)
-
The "U" mode is modeled on Python's "Universal Newlines" concept, whereby
DOS and Mac OS line terminators are converted internally to UNIX newlines
before processing. This ensures consistent digest values when working
only text files>, namely those passing Perl's I<-T> test; binary files
are processed with no translation whatsoever.
-The "p" mode differs from "U" only in that it treats "\r\r\n" as a single
-newline, a quirky feature designed to accommodate legacy applications that
-occasionally added an extra carriage return before DOS line terminators.
-The "p" mode will be phased out eventually in favor of the cleaner and
-more well-established Universal Newlines concept.
-
The BITS mode ("0") interprets the contents of I<$filename> as a logical
stream of bits, where each ASCII '0' or '1' character represents a 0 or
1 bit, respectively. All other characters are ignored. This provides
##
## Copyright (C) 2003-2017 Mark Shelor, All Rights Reserved
##
- ## Version: 5.97
- ## Wed Sep 6 02:23:02 MST 2017
+ ## Version: 5.98
+ ## Wed Oct 4 00:40:02 MST 2017
## shasum SYNOPSIS adapted from GNU Coreutils sha1sum. Add
## "-a" option for algorithm selection,
- ## "-U" option for Universal Newlines support,
- ## "-0" option for reading bit strings, and
- ## "-p" option for portable digests (to be deprecated).
+ ## "-U" option for Universal Newlines support, and
+ ## "-0" option for reading bit strings.
BEGIN { pop @INC if $INC[-1] eq '.' }
use warnings;
use Fcntl;
use Getopt::Long;
+use Digest::SHA;
my $POD = <<'END_OF_POD';
ASCII '0' interpreted as 0-bit,
ASCII '1' interpreted as 1-bit,
all other characters ignored
- -p, --portable read in portable mode (to be deprecated)
The following three options are useful only when verifying checksums:
-s, --status don't output anything, status code shows success
The sums are computed as described in FIPS PUB 180-4. When checking,
the input should be a former output of this program. The default
mode is to print a line with checksum, a character indicating type
- (`*' for binary, ` ' for text, `U' for UNIVERSAL, `^' for BITS, `?'
- for portable), and name for each FILE. The line starts with a `\'
- character if the FILE name contains either newlines or backslashes,
- which are then replaced by the two-character sequences `\n' and `\\'
- respectively.
+ (`*' for binary, ` ' for text, `U' for UNIVERSAL, `^' for BITS),
+ and name for each FILE. The line starts with a `\' character if the
+ FILE name contains either newlines or backslashes, which are then
+ replaced by the two-character sequences `\n' and `\\' respectively.
Report shasum bugs to mshelor@cpan.org
=head1 SEE ALSO
-I<shasum> is implemented using the Perl module L<Digest::SHA> or
-L<Digest::SHA::PurePerl>.
+I<shasum> is implemented using the Perl module L<Digest::SHA>.
=cut
END_OF_POD
-my $VERSION = "5.97";
+my $VERSION = "5.98";
sub usage {
my($err, $msg) = @_;
## Collect options from command line
-my ($alg, $binary, $check, $text, $status, $quiet, $warn, $help, $version);
-my ($portable, $BITS, $reverse, $UNIVERSAL, $versions);
+my ($alg, $binary, $check, $text, $status, $quiet, $warn, $help);
+my ($version, $BITS, $UNIVERSAL);
eval { Getopt::Long::Configure ("bundling") };
GetOptions(
's|status' => \$status, 'w|warn' => \$warn,
'q|quiet' => \$quiet,
'h|help' => \$help, 'v|version' => \$version,
- 'p|portable' => \$portable,
'0|01' => \$BITS,
- 'R|REVERSE' => \$reverse,
'U|UNIVERSAL' => \$UNIVERSAL,
- 'V|VERSIONS' => \$versions,
) or usage(1, "");
if $help;
usage(1, "shasum: Ambiguous file mode\n")
if scalar(grep {defined $_}
- ($binary, $portable, $text, $BITS, $UNIVERSAL)) > 1;
+ ($binary, $text, $BITS, $UNIVERSAL)) > 1;
usage(1, "shasum: --warn option used only when verifying checksums\n")
if $warn && !$check;
usage(1, "shasum: --status option used only when verifying checksums\n")
if $quiet && !$check;
- ## Try to use Digest::SHA. If not installed, use the slower
- ## but functionally equivalent Digest::SHA::PurePerl instead.
-
- ## If option -R is invoked, reverse the module preference,
- ## i.e. try Digest::SHA::PurePerl first, then Digest::SHA.
-
-my @MODS = qw(Digest::SHA Digest::SHA::PurePerl);
-@MODS[0, 1] = @MODS[1, 0] if $reverse;
-
-my $module;
-for (@MODS) {
- my $mod = $_;
- if (eval "require $mod") {
- $module = $mod;
- last;
- }
-}
-die "shasum: Unable to find " . join(" or ", @MODS) . "\n"
- unless defined $module;
-
-
## Default to SHA-1 unless overridden by command line option
$alg = 1 unless defined $alg;
exit(0);
}
-if ($versions) {
- print "shasum $VERSION\n";
- print "$module ", eval "\$${module}::VERSION", "\n";
- print "perl ", defined $^V ? sprintf("%vd", $^V) : $], "\n";
- exit(0);
-}
-
## Try to figure out if the OS is DOS-like. If it is,
## default to binary mode when reading files, unless
## explicitly overridden by command line "--text" or
- ## "--UNIVERSAL" or "--portable" options.
+ ## "--UNIVERSAL" options.
my $isDOSish = ($^O =~ /^(MSWin\d\d|os2|dos|mint|cygwin)$/);
-if ($isDOSish) { $binary = 1 unless $text || $UNIVERSAL || $portable }
+if ($isDOSish) { $binary = 1 unless $text || $UNIVERSAL }
-my $modesym = $binary ? '*' : ($UNIVERSAL ? 'U' :
- ($BITS ? '^' : ($portable ? '?' : ' ')));
+my $modesym = $binary ? '*' : ($UNIVERSAL ? 'U' : ($BITS ? '^' : ' '));
## Read from STDIN (-) if no files listed on command line
sub sumfile {
my $file = shift;
- my $mode = $binary ? 'b' : ($UNIVERSAL ? 'U' :
- ($BITS ? '0' : ($portable ? 'p' : '')));
- my $digest = eval { $module->new($alg)->addfile($file, $mode) };
+ my $mode = $binary ? 'b' : ($UNIVERSAL ? 'U' : ($BITS ? '0' : ''));
+ my $digest = eval { Digest::SHA->new($alg)->addfile($file, $mode) };
if ($@) { warn "shasum: $file: $!\n"; return }
$digest->hexdigest;
}
$_ = shift;
s/\\\\/\0/g;
s/\\n/\n/g;
- return if /\\/;
s/\0/\\/g;
return $_;
}
or sysopen(FH, $checkfile, O_RDONLY)
or die "shasum: $checkfile: $!\n";
while (<FH>) {
- next if /^#/; s/\n$//; s/^[ \t]+//; $num_lines++;
- $bslash = s/^\\//;
- ($sum, $modesym, $fname) =
- /^([\da-fA-F]+)[ \t]([ *?^U])([^\0]*)/;
+ next if /^#/; $num_lines++;
+ ($bslash, $sum, $modesym, $fname) =
+ /^[ \t]*(\\?)([\da-fA-F]+)[ \t]([ *^U])(.+)/;
$alg = defined $sum ? $len2alg{length($sum)} : undef;
- $fname = unescape($fname) if defined $fname && $bslash;
if (grep { ! defined $_ } ($alg, $sum, $modesym, $fname)) {
$alg = 1 unless defined $alg;
warn("shasum: $checkfile: $.: improperly " .
$fmt_errs++;
next;
}
- $fname =~ s/\r$// unless -e $fname;
+ $fname = unescape($fname) if $bslash;
$rsp = "$fname: "; $num_files++;
- ($binary, $text, $UNIVERSAL, $BITS, $portable) =
- map { $_ eq $modesym } ('*', ' ', 'U', '^', 'p');
+ ($binary, $text, $UNIVERSAL, $BITS) =
+ map { $_ eq $modesym } ('*', ' ', 'U', '^');
$isOK = 0;
unless ($digest = sumfile($fname)) {
$rsp .= "FAILED open or read\n";
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
);
-my $numtests = 9 + scalar @out;
+my $numtests = 6 + scalar @out;
print "1..$numtests\n";
# attempt to use an invalid algorithm, and check for failure
print "not " unless $sha->addfile($tempfile, "b")->hexdigest eq $rsp;
print "ok ", $testnum++, "\n";
- # test addfile portable mode
-
-$fh = FileHandle->new($tempfile, "w");
-binmode($fh);
-print $fh "abc\012" x 2048; # using UNIX newline
-$fh->close;
-
-print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
- "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
-print "ok ", $testnum++, "\n";
-
-$fh = FileHandle->new($tempfile, "w");
-binmode($fh);
-print $fh "abc\015\012" x 2048; # using DOS/Windows newline
-$fh->close;
-
-print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
- "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
-print "ok ", $testnum++, "\n";
-
-$fh = FileHandle->new($tempfile, "w");
-binmode($fh);
-print $fh "abc\015" x 2048; # using early-Mac newline
-$fh->close;
-
-print "not " unless $sha->new(1)->addfile($tempfile, "p")->hexdigest eq
- "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51";
-print "ok ", $testnum++, "\n";
-
# test addfile "universal newlines" mode
$fh = FileHandle->new($tempfile, "w");