changeset 76:8fc79d327ccd

replaced crc calculator with perl version.
author Edho Prima Arief <me@myconan.net>
date Sun, 19 Jun 2011 01:56:31 +0700
parents f993714efa7b
children fef22522dca3
files bin/cek bin/cek.pl bin/cek.rb
diffstat 3 files changed, 42 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/bin/cek	Sun Jun 19 01:45:27 2011 +0700
+++ b/bin/cek	Sun Jun 19 01:56:31 2011 +0700
@@ -2,7 +2,7 @@
 
 bn() { basename "/$*"; }
 #cs() { cksfv -- "$@"; }
-cs() { cek.rb "$@"; }
+cs() { cek.pl "$@"; }
 if [ "$#" -lt 1 ]; then
 	cat <<EOF
 Usage: $(bn "$0") file1 file2 ... fileN
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/cek.pl	Sun Jun 19 01:56:31 2011 +0700
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+use Compress::Zlib;
+
+sub get_file_crc32 {
+  my $block_size = 1*2**20;
+  my $crc32_digest = 0;
+  my $return_code = 0;
+  my $file_name = $_[0];
+  if (!$file_name) {
+    #internal error, empty file name
+    $return_code = 10;
+  } elsif (!-f $file_name) {
+    #input file name is not a file
+    $return_code = 11;
+  } elsif (!-r $file_name) {
+    #input file is not readable
+    $return_code = 12;
+  }
+  if ($return_code == 0) {
+   open(my $file_handle, '<', $file_name);
+   binmode($file_handle);
+    while(sysread($file_handle, $_, $block_size)) {
+      $crc32_digest = crc32($_, $crc32_digest);
+    }
+    close($file_handle);
+  }
+  return ($return_code, sprintf('%08X', $crc32_digest));
+}
+
+foreach (@ARGV) {
+  my $file_name = $_;
+  my @crc32 = get_file_crc32($file_name);
+  printf("%s %s", $file_name, $crc32[1]);
+  if ($crc32[0] > 0) {
+    printf(" [ERR%02d]", $crc32[0]);
+  }
+  printf("\n");
+}
--- a/bin/cek.rb	Sun Jun 19 01:45:27 2011 +0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-#!/usr/bin/env ruby
-require 'zlib'
-
-block = 1048576
-ARGV.each do |filename|
-	crc = 0
-	file = File.open(filename, 'rb')
-	currentbyte = 0
-	while (line = file.read(block)) do
-		crc = Zlib.crc32(line,crc)
-	end
-	file.close
-	printf("%s %08X\n", filename, crc.to_s)
-end