1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
module TTFunk
module Reader
private
def io
@file.contents
end
def read(bytes, format)
io.read(bytes).unpack(format)
end
def read_signed(count)
read(count*2, "n*").map { |i| to_signed(i) }
end
def to_signed(n)
(n>=0x8000) ? -((n ^ 0xFFFF) + 1) : n
end
def parse_from(position)
saved, io.pos = io.pos, position
result = yield position
io.pos = saved
return result
end
# For debugging purposes
def hexdump(string)
bytes = string.unpack("C*")
bytes.each_with_index do |c, i|
print "%02X" % c
if (i+1) % 16 == 0
puts
elsif (i+1) % 8 == 0
print " "
else
print " "
end
end
puts unless bytes.length % 16 == 0
end
end
end
|