Created
June 5, 2011 08:15
-
-
Save deplinenoise/1008773 to your computer and use it in GitHub Desktop.
Ruby 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 ruby | |
def compute_chksum(data) | |
sum = 0 | |
(0...256).each do |x| | |
i = 4 * x | |
val = data[i,i+4].unpack('N')[0] | |
sum = sum + val | |
if sum > 0xffffffff | |
sum = (sum + 1) & 0xffffffff | |
end | |
end | |
(~sum) & 0xffffffff | |
end | |
if not ARGV[0] or not ARGV[1] | |
puts("two arguments needed") | |
exit(1) | |
end | |
data = IO.read(ARGV[0], 1024) | |
data += "\0" * (1024 - data.length) # pad to 1024 bytes | |
data[4,4] = [compute_chksum(data)].pack('N') | |
File.open(ARGV[1], "wb") { |f| f.write(data) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment