Skip to content

Instantly share code, notes, and snippets.

@deplinenoise
Created June 5, 2011 08:15
Show Gist options
  • Save deplinenoise/1008773 to your computer and use it in GitHub Desktop.
Save deplinenoise/1008773 to your computer and use it in GitHub Desktop.
Ruby bootblock checksummer
#! /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