annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
76
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
1 #!/usr/bin/env perl
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
2
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
3 use warnings;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
4 use strict;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
5 use Compress::Zlib;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
6
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
7 sub get_file_crc32 {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
8 my $block_size = 1*2**20;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
9 my $crc32_digest = 0;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
10 my $return_code = 0;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
11 my $file_name = $_[0];
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
12 if (!$file_name) {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
13 #internal error, empty file name
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
14 $return_code = 10;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
15 } elsif (!-f $file_name) {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
16 #input file name is not a file
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
17 $return_code = 11;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
18 } elsif (!-r $file_name) {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
19 #input file is not readable
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
20 $return_code = 12;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
21 }
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
22 if ($return_code == 0) {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
23 open(my $file_handle, '<', $file_name);
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
24 binmode($file_handle);
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
25 while(sysread($file_handle, $_, $block_size)) {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
26 $crc32_digest = crc32($_, $crc32_digest);
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
27 }
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
28 close($file_handle);
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
29 }
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
30 return ($return_code, sprintf('%08X', $crc32_digest));
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
31 }
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
32
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
33 foreach (@ARGV) {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
34 my $file_name = $_;
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
35 my @crc32 = get_file_crc32($file_name);
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
36 printf("%s %s", $file_name, $crc32[1]);
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
37 if ($crc32[0] > 0) {
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
38 printf(" [ERR%02d]", $crc32[0]);
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
39 }
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
40 printf("\n");
8fc79d327ccd replaced crc calculator with perl version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
41 }