Class: Mikunyan::BinaryReader
- Inherits:
-
Object
- Object
- Mikunyan::BinaryReader
- Defined in:
- lib/mikunyan/binary_reader.rb
Overview
Class for manipulating binary string
Instance Attribute Summary collapse
-
#endian ⇒ Symbol
endianness.
Instance Method Summary collapse
-
#adv(size = 0) ⇒ Object
Advances position given size.
-
#align(size) ⇒ Object
Rounds up position to multiple of given size.
-
#bool ⇒ Object
Reads an 8bit bool value.
-
#cstr ⇒ String
Reads string until null character.
-
#double ⇒ Object
Reads a 64bit floating point value.
-
#float ⇒ Object
Reads a 32bit floating point value.
-
#i16s ⇒ Object
(also: #i16)
Reads a 16bit signed integer value.
-
#i16u ⇒ Object
Reads a 16bit unsigned integer value.
-
#i32s ⇒ Object
(also: #i32)
Reads a 32bit signed integer value.
-
#i32u ⇒ Object
Reads a 32bit unsigned integer value.
-
#i64s ⇒ Object
(also: #i64)
Reads a 64bit signed integer value.
-
#i64u ⇒ Object
Reads a 64bit unsigned integer value.
-
#i8s ⇒ Object
(also: #i8)
Reads an 8bit signed integer value.
-
#i8u ⇒ Object
Reads an 8bit unsigned integer value.
-
#initialize(io, endian = :big) ⇒ BinaryReader
constructor
Constructor.
-
#jmp(jmp_pos = 0) ⇒ Object
(also: #pos=)
Jumps to given position.
-
#little? ⇒ Boolean
Returns whether little endian or not.
-
#pos ⇒ Integer
Tells current potision.
-
#read(size) ⇒ String
Reads given size of binary string and seek.
-
#read_abs(size, jmp_pos) ⇒ String
Reads given size of binary string from specified position.
Constructor Details
#initialize(io, endian = :big) ⇒ BinaryReader
Constructor
14 15 16 17 18 19 |
# File 'lib/mikunyan/binary_reader.rb', line 14 def initialize(io, endian = :big) @io = io.is_a?(String) ? StringIO.new(io, 'r') : io.dup @io.binmode @base_pos = @io.pos @endian = endian end |
Instance Attribute Details
#endian ⇒ Symbol
endianness
8 9 10 |
# File 'lib/mikunyan/binary_reader.rb', line 8 def endian @endian end |
Instance Method Details
#adv(size = 0) ⇒ Object
Advances position given size
42 43 44 |
# File 'lib/mikunyan/binary_reader.rb', line 42 def adv(size = 0) @io.seek(size, IO::SEEK_CUR) end |
#align(size) ⇒ Object
Rounds up position to multiple of given size
48 49 50 51 |
# File 'lib/mikunyan/binary_reader.rb', line 48 def align(size) rem = pos % size adv(size - rem) if rem > 0 end |
#bool ⇒ Object
Reads an 8bit bool value
84 85 86 |
# File 'lib/mikunyan/binary_reader.rb', line 84 def bool i8u != 0 end |
#cstr ⇒ String
Reads string until null character
77 78 79 80 81 |
# File 'lib/mikunyan/binary_reader.rb', line 77 def cstr raise EOFError if @io.eof? @io.each_byte.take_while(&:nonzero?).pack('C*') end |
#double ⇒ Object
Reads a 64bit floating point value
138 139 140 |
# File 'lib/mikunyan/binary_reader.rb', line 138 def double little? ? read(8).unpack1('E') : read(8).unpack1('G') end |
#float ⇒ Object
Reads a 32bit floating point value
133 134 135 |
# File 'lib/mikunyan/binary_reader.rb', line 133 def float little? ? read(4).unpack1('e') : read(4).unpack1('g') end |
#i16s ⇒ Object Also known as: i16
Reads a 16bit signed integer value
100 101 102 |
# File 'lib/mikunyan/binary_reader.rb', line 100 def i16s little? ? BinUtils.get_sint16_le(read(2)) : BinUtils.get_sint16_be(read(2)) end |
#i16u ⇒ Object
Reads a 16bit unsigned integer value
106 107 108 |
# File 'lib/mikunyan/binary_reader.rb', line 106 def i16u little? ? BinUtils.get_int16_le(read(2)) : BinUtils.get_int16_be(read(2)) end |
#i32s ⇒ Object Also known as: i32
Reads a 32bit signed integer value
111 112 113 |
# File 'lib/mikunyan/binary_reader.rb', line 111 def i32s little? ? BinUtils.get_sint32_le(read(4)) : BinUtils.get_sint32_be(read(4)) end |
#i32u ⇒ Object
Reads a 32bit unsigned integer value
117 118 119 |
# File 'lib/mikunyan/binary_reader.rb', line 117 def i32u little? ? BinUtils.get_int32_le(read(4)) : BinUtils.get_int32_be(read(4)) end |
#i64s ⇒ Object Also known as: i64
Reads a 64bit signed integer value
122 123 124 |
# File 'lib/mikunyan/binary_reader.rb', line 122 def i64s little? ? BinUtils.get_sint64_le(read(8)) : BinUtils.get_sint64_be(read(8)) end |
#i64u ⇒ Object
Reads a 64bit unsigned integer value
128 129 130 |
# File 'lib/mikunyan/binary_reader.rb', line 128 def i64u little? ? BinUtils.get_int64_le(read(8)) : BinUtils.get_int64_be(read(8)) end |
#i8s ⇒ Object Also known as: i8
Reads an 8bit signed integer value
89 90 91 |
# File 'lib/mikunyan/binary_reader.rb', line 89 def i8s BinUtils.get_sint8(read(1)) end |
#i8u ⇒ Object
Reads an 8bit unsigned integer value
95 96 97 |
# File 'lib/mikunyan/binary_reader.rb', line 95 def i8u @io.getbyte end |
#jmp(jmp_pos = 0) ⇒ Object Also known as: pos=
Jumps to given position
35 36 37 |
# File 'lib/mikunyan/binary_reader.rb', line 35 def jmp(jmp_pos = 0) @io.pos = jmp_pos + @base_pos end |
#little? ⇒ Boolean
Returns whether little endian or not
23 24 25 |
# File 'lib/mikunyan/binary_reader.rb', line 23 def little? @endian == :little end |
#pos ⇒ Integer
Tells current potision
29 30 31 |
# File 'lib/mikunyan/binary_reader.rb', line 29 def pos @io.pos - @base_pos end |
#read(size) ⇒ String
Reads given size of binary string and seek
56 57 58 59 60 61 |
# File 'lib/mikunyan/binary_reader.rb', line 56 def read(size) ret = @io.read(size) raise EOFError if ret.nil? || size && ret.bytesize < size ret end |
#read_abs(size, jmp_pos) ⇒ String
Reads given size of binary string from specified position. This method does not seek.
67 68 69 70 71 72 73 |
# File 'lib/mikunyan/binary_reader.rb', line 67 def read_abs(size, jmp_pos) orig_pos = pos jmp(jmp_pos) ret = read(size) jmp(orig_pos) ret end |