view bin/cek.pl @ 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
children fef22522dca3
line wrap: on
line source

#!/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");
}