Created
June 6, 2014 17:54
-
-
Save mwgamera/96f29d3a1fc0e4d886e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| # Check valitidy of a leap-seconds.list file. | |
| # klg, Jan 2014 | |
| use strict; | |
| use open ':std', IN => ':bytes'; | |
| use Digest::SHA 'sha1'; | |
| use Time::Local 'timegm'; | |
| use constant EPOCH => timegm 0, 0, 0, 1, 0, 1900; | |
| my ($v, $b, $h); | |
| shift @ARGV if $v = @ARGV && $ARGV[0] =~ /^--?v/i; | |
| while (<>) { | |
| if ($v && m/#\$\s*([0-9]++)/) { | |
| printf "Last update: %s UTC\n", | |
| scalar gmtime EPOCH + $1; | |
| } | |
| if (m/#@\s*([0-9]++)/) { | |
| my $exp = EPOCH + $1; | |
| if (time > $exp) { | |
| printf STDERR "File EXPIRED on: %s UTC\n", | |
| scalar gmtime $exp; | |
| exit 1; | |
| } | |
| printf "File expires on: %s UTC\n", | |
| scalar gmtime $exp if $v; | |
| } | |
| $h = $1 if m/#h\s*(.++)/; | |
| s/#(\$|@|.*$)//, s/[^0-9]//g; | |
| $b .= $_; | |
| } | |
| $b = sha1($b); | |
| printf "#h %x %x %x %x %x\n", unpack 'N5', $b if $v; | |
| if (defined $h) { | |
| $h = pack 'N5', map hex, $h =~ m/([0-9a-f]+)/gi; | |
| if ($b ne $h) { | |
| printf STDERR "File corrupted!\n"; | |
| exit 1; | |
| } | |
| } elsif ($v) { | |
| printf STDERR "Checksum missing.\n"; | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment