Class: BinTools::Reader
- Inherits:
-
Object
- Object
- BinTools::Reader
- Defined in:
- lib/bin_tools/reader.rb
Overview
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
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(str) ⇒ Reader
constructor
A new instance of Reader.
- #msg(str) ⇒ Object
- #read_bin(len) ⇒ Object
- #read_binswap(len) ⇒ Object
- #read_bool ⇒ Object
- #read_byte ⇒ Object
- #read_f32_le ⇒ Object
- #read_s16_le ⇒ Object
- #read_s32_le ⇒ Object
- #read_s8 ⇒ Object
- #read_str(len) ⇒ Object
- #read_str_varlen ⇒ Object
- #read_u16_be ⇒ Object
- #read_u16_le ⇒ Object
- #read_u32_be ⇒ Object
- #read_u32_le ⇒ Object
- #read_varlen_be ⇒ Object
- #read_varlen_le ⇒ Object
- #seek(pos) ⇒ Object
- #seek_rel(pos) ⇒ Object
- #set_base(pos) ⇒ Object
- #tell ⇒ Object
Constructor Details
#initialize(str) ⇒ Reader
Returns a new instance of Reader.
13 14 15 16 17 |
# File 'lib/bin_tools/reader.rb', line 13 def initialize str @rom = str @cur = 0 @base = 0 end |
Class Method Details
.from_file(fn) ⇒ Object
19 20 21 22 |
# 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 |
Instance Method Details
#msg(str) ⇒ Object
148 149 150 |
# File 'lib/bin_tools/reader.rb', line 148 def msg str puts "%08X(%08X): %s" % [@cur, tell, str] end |
#read_bin(len) ⇒ Object
111 112 113 114 115 |
# 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) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# 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 ⇒ Object
59 60 61 |
# File 'lib/bin_tools/reader.rb', line 59 def read_bool read_byte == 1 end |
#read_byte ⇒ Object
53 54 55 56 57 |
# File 'lib/bin_tools/reader.rb', line 53 def read_byte r = @rom[@cur].ord @cur += 1 r end |
#read_f32_le ⇒ Object
105 106 107 108 109 |
# 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 ⇒ Object
75 76 77 78 79 |
# 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 ⇒ Object
93 94 95 96 97 |
# 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 ⇒ Object
63 64 65 66 67 |
# File 'lib/bin_tools/reader.rb', line 63 def read_s8 r = @rom[@cur].unpack("c").first @cur += 1 r end |
#read_str(len) ⇒ Object
40 41 42 43 44 |
# 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 ⇒ Object
46 47 48 49 50 51 |
# 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 ⇒ Object
81 82 83 84 85 |
# 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 ⇒ Object
69 70 71 72 73 |
# 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 ⇒ Object
99 100 101 102 103 |
# 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 ⇒ Object
87 88 89 90 91 |
# 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 ⇒ Object
138 139 140 141 142 143 144 145 146 |
# 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 ⇒ Object
129 130 131 132 133 134 135 136 |
# 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) ⇒ Object
28 29 30 |
# File 'lib/bin_tools/reader.rb', line 28 def seek pos @cur = pos + @base end |
#seek_rel(pos) ⇒ Object
32 33 34 |
# File 'lib/bin_tools/reader.rb', line 32 def seek_rel pos @cur += pos end |
#set_base(pos) ⇒ Object
24 25 26 |
# File 'lib/bin_tools/reader.rb', line 24 def set_base pos @base = pos end |
#tell ⇒ Object
36 37 38 |
# File 'lib/bin_tools/reader.rb', line 36 def tell @cur - @base end |