#!/usr/bin/perl -w use strict; # (C) 2003-2007 Willem Jan Hengeveld # Web: http://www.xs4all.nl/~itsme/ # http://wiki.xda-developers.com/ # # $Id: crc32 1502 2007-04-15 07:54:20Z itsme $ # # computes and prints to stdout the CRC-32 values of the given files use Archive::Zip; use XdaDevelopers::NbfUtils; use IO::File; use Getopt::Long; my $initialvalue=0; my $negate_value=0; my $lib=0; GetOptions( "i=s" => sub { $initialvalue= eval($_[1]); }, "n" => \$negate_value, "z" => \$lib, ) or die usage(); sub usage { print "Usage: crc32 [-i Initialvalue] [-n] [files]\n"; print " -i : specify value other than 0\n"; print " -n : negate result and initial value\n"; print " -z : use zip lib crc\n"; } if (@ARGV) { my $totalFiles = scalar(@ARGV); foreach my $file (@ARGV) { processfile($file, $totalFiles>1); } } else { processfile("-"); } exit(0); sub processfile { my ($file, $printnames)= @_; if (-d $file) { warn "$0: ${file}: Is a directory\n"; return; } my $fh = IO::File->new(); if ($file ne "-") { if (! $fh->open($file, 'r')) { warn "$0: '$file' - $!\n"; return; } } else { $fh->fdopen(fileno(STDIN), "r"); } binmode($fh); my $buffer; my $bytesRead; my $crc = $initialvalue; $crc = ~$crc if ($negate_value); while ($bytesRead = $fh->read($buffer, 32768)) { if ($lib) { $crc = Archive::Zip::computeCRC32($buffer, $crc); } else { $crc = XdaDevelopers::NbfUtils::crc32($buffer, $crc); } } $crc = ~$crc if ($negate_value); printf("%08x", $crc); print("\t$file") if ($printnames); print("\n"); }