Skip to content

Instantly share code, notes, and snippets.

@deplinenoise
Created June 5, 2011 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deplinenoise/1008771 to your computer and use it in GitHub Desktop.
Save deplinenoise/1008771 to your computer and use it in GitHub Desktop.
Perl bootblock checksummer
#! /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