summaryrefslogtreecommitdiff
path: root/ubi-utils/perl/ubicrc32.pl
diff options
context:
space:
mode:
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2008-02-19 14:54:17 +0200
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2008-02-19 16:41:16 +0200
commit86dd9696d3a1acb036d65909d93d73455b8aed3a (patch)
tree9e8bb6797b3bebd0bf83ae1fab2151a6dfeafe6a /ubi-utils/perl/ubicrc32.pl
parentef644090223e5983ca2492850354c1390e3368d1 (diff)
Revert ubi-tools changes
Restore ubi tools to their "original" state, which means to the state they were before I stareted cleaning them up. Instead, create a "new-utils" subdirectory and move my work there. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Diffstat (limited to 'ubi-utils/perl/ubicrc32.pl')
-rwxr-xr-xubi-utils/perl/ubicrc32.pl74
1 files changed, 74 insertions, 0 deletions
diff --git a/ubi-utils/perl/ubicrc32.pl b/ubi-utils/perl/ubicrc32.pl
new file mode 100755
index 0000000..add5f9d
--- /dev/null
+++ b/ubi-utils/perl/ubicrc32.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl -w
+
+# Subroutine crc32(): Calculates the CRC on a given string.
+
+{
+ my @table = ();
+
+ # @brief Calculate CRC32 for a given string.
+ sub crc32
+ {
+ unless (@table) {
+ # Initialize the CRC table
+ my $poly = 0xEDB88320;
+ @table = ();
+
+ for my $i (0..255) {
+ my $c = $i;
+
+ for my $j (0..7) {
+ $c = ($c & 1) ? (($c >> 1) ^ $poly) : ($c >> 1);
+ }
+ $table[$i] = $c;
+ }
+ }
+ my $s = shift; # string to calculate the CRC for
+ my $crc = shift; # CRC start value
+
+ defined($crc)
+ or $crc = 0xffffffff; # Default CRC start value
+
+ for (my $i = 0; $i < length($s); $i++) {
+ $crc = $table[($crc ^ ord(substr($s, $i, 1))) & 0xff]
+ ^ ($crc >> 8);
+ }
+ return $crc;
+ }
+}
+
+sub crc32_on_file
+{
+ my $file = shift;
+
+ my $crc32 = crc32('');
+ my $buf = '';
+ my $ret = 0;
+
+ while ($ret = read($file, $buf, 8192)) {
+ $crc32 = crc32($buf, $crc32);
+ }
+ defined($ret)
+ or return undef;
+ printf("0x%x\n", $crc32);
+}
+
+
+# Main routine: Calculate the CRCs on the given files and print the
+# results.
+
+{
+ if (@ARGV) {
+ while (my $path = shift) {
+ my $file;
+ open $file, "<", $path
+ or die "Error opening '$path'.\n";
+
+ &crc32_on_file($file)
+ or die "Error reading from '$path'.\n";
+ close $file;
+ }
+ } else {
+ &crc32_on_file(\*STDIN)
+ or die "Error reading from stdin.\n";
+ }
+}