class BinTools::Reader

8 16 32 64 C S L Q - unsigned c s l q - unsigned < LE > BE A - binary string z - null terminated H - hex string

Public Class Methods

from_file(fn) click to toggle source
# File lib/bin_tools/reader.rb, line 19
def self.from_file fn
  data = File.open(fn, "rb") { |io| io.read }
  self.new data
end
new(str) click to toggle source
# File lib/bin_tools/reader.rb, line 13
def initialize str
  @rom = str
  @cur = 0
  @base = 0
end

Public Instance Methods

msg(str) click to toggle source
# File lib/bin_tools/reader.rb, line 148
def msg str
  puts "%08X(%08X): %s" % [@cur, tell, str]
end
read_bin(len) click to toggle source
# File lib/bin_tools/reader.rb, line 111
def read_bin len
  r = @rom[@cur..(@cur + len - 1)]
  @cur += len
  r
end
read_binswap(len) click to toggle source
# File lib/bin_tools/reader.rb, line 117
def read_binswap len
  togo = len
  bin_data = "".b
  while togo > 0
    r = @rom[@cur..(@cur + 3)].unpack('L>')
    bin_data += r.pack('L<')
    @cur += 4
    togo -= 4
  end
  bin_data
end
read_bool() click to toggle source
# File lib/bin_tools/reader.rb, line 59
def read_bool
  read_byte == 1
end
read_byte() click to toggle source
# File lib/bin_tools/reader.rb, line 53
def read_byte
  r = @rom[@cur].ord
  @cur += 1
  r
end
read_f32_le() click to toggle source
# File lib/bin_tools/reader.rb, line 105
def read_f32_le
  r = @rom[@cur..(@cur + 3)].unpack('e').first
  @cur += 4
  r
end
read_s16_le() click to toggle source
# File lib/bin_tools/reader.rb, line 75
def read_s16_le
  r = @rom[@cur..(@cur + 3)].unpack('s<').first
  @cur += 2
  r
end
read_s32_le() click to toggle source
# File lib/bin_tools/reader.rb, line 93
def read_s32_le
  r = @rom[@cur..(@cur + 3)].unpack('l<').first
  @cur += 4
  r
end
read_s8() click to toggle source
# File lib/bin_tools/reader.rb, line 63
def read_s8
  r = @rom[@cur].unpack("c").first
  @cur += 1
  r
end
read_str(len) click to toggle source
# File lib/bin_tools/reader.rb, line 40
def read_str len
  r = @rom[@cur..(@cur + len - 1)].unpack("A#{len}").first
  @cur += len
  r
end
read_str_varlen() click to toggle source
# File lib/bin_tools/reader.rb, line 46
def read_str_varlen
  len = read_varlen_le
  r = @rom[@cur..(@cur + len - 1)].unpack("A#{len}").first
  @cur += len
  r
end
read_u16_be() click to toggle source
# File lib/bin_tools/reader.rb, line 81
def read_u16_be
  r = @rom[@cur..(@cur + 3)].unpack('S>').first
  @cur += 2
  r
end
read_u16_le() click to toggle source
# File lib/bin_tools/reader.rb, line 69
def read_u16_le
  r = @rom[@cur..(@cur + 3)].unpack('S<').first
  @cur += 2
  r
end
read_u32_be() click to toggle source
# File lib/bin_tools/reader.rb, line 99
def read_u32_be
  r = @rom[@cur..(@cur + 3)].unpack('L>').first
  @cur += 4
  r
end
read_u32_le() click to toggle source
# File lib/bin_tools/reader.rb, line 87
def read_u32_le
  r = @rom[@cur..(@cur + 3)].unpack('L<').first
  @cur += 4
  r
end
read_varlen_be() click to toggle source
# File lib/bin_tools/reader.rb, line 138
def read_varlen_be
  val = 0
  r = read_byte
  val = r & 0x7F
  return val if r < 0x80
  r = read_byte
  val = (val << 8) + r
  val
end
read_varlen_le() click to toggle source
# File lib/bin_tools/reader.rb, line 129
def read_varlen_le
  val = 0
  r = read_byte
  val = r & 0x7F
  return val if r < 0x80
  r = read_byte << 7
  val + r
end
seek(pos) click to toggle source
# File lib/bin_tools/reader.rb, line 28
def seek pos
  @cur = pos + @base
end
seek_rel(pos) click to toggle source
# File lib/bin_tools/reader.rb, line 32
def seek_rel pos
  @cur += pos
end
set_base(pos) click to toggle source
# File lib/bin_tools/reader.rb, line 24
def set_base pos
  @base = pos
end
tell() click to toggle source
# File lib/bin_tools/reader.rb, line 36
def tell
  @cur - @base
end