Created
June 5, 2011 08:14
-
-
Save deplinenoise/1008771 to your computer and use it in GitHub Desktop.
Perl bootblock checksummer
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 -w | |
use strict 'subs'; | |
use bytes; | |
sub checksum(\$) { | |
my $data = shift; | |
my $sum = 0; | |
for (my $i = 0; $i < 1024; $i += 4) { | |
$sum += unpack('N', substr($$data, $i, 4)); | |
if ($sum > 0xffffffff) { | |
$sum = ($sum + 1) & 0xffffffff; | |
} | |
} | |
return (~$sum) & 0xffffffff; | |
} | |
die "need two arguments" unless $ARGV[0] and $ARGV[1]; | |
my $data; | |
open(INPUT, '<:raw', $ARGV[0]) or die "can't open $ARGV[0]\n"; | |
read(INPUT, $data, 1024); | |
close(INPUT); | |
my $diff = (1024 - bytes::length($data)); | |
$data .= "\0" x $diff unless $diff == 0; | |
substr($data, 4, 4) = pack('N', checksum($data)); | |
open(OUTPUT, '>:raw', $ARGV[1]) or die "can't open $ARGV[1] for writing\n"; | |
print OUTPUT $data; | |
close(OUTPUT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment