Class: Mikunyan::BinaryReader

Inherits:
Object
  • Object
show all
Defined in:
lib/mikunyan/binary_reader.rb

Overview

Class for manipulating binary string

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, endian = :big) ⇒ BinaryReader

Constructor

Parameters:

  • io (IO, String)

    binary String or IO

  • endian (Symbol) (defaults to: :big)

    endianness



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

#endianSymbol

endianness

Returns:

  • (Symbol)

    the current value of endian



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

Parameters:

  • size (Integer) (defaults to: 0)

    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

Parameters:

  • size (Integer)

    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

#boolObject

Reads an 8bit bool value



84
85
86
# File 'lib/mikunyan/binary_reader.rb', line 84

def bool
  i8u != 0
end

#cstrString

Reads string until null character

Returns:

  • (String)

    string

Raises:

  • (EOFError)


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

#doubleObject

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

#floatObject

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

#i16sObject 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

#i16uObject

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

#i32sObject 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

#i32uObject

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

#i64sObject 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

#i64uObject

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

#i8sObject 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

#i8uObject

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

Parameters:

  • jmp_pos (Integer) (defaults to: 0)

    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

Returns:

  • (Boolean)


23
24
25
# File 'lib/mikunyan/binary_reader.rb', line 23

def little?
  @endian == :little
end

#posInteger

Tells current potision

Returns:

  • (Integer)


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

Parameters:

  • size (Integer)

    size

Returns:

  • (String)

    data

Raises:

  • (EOFError)


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.

Parameters:

  • size (Integer)

    size

  • jmp_pos (Integer)

    position

Returns:

  • (String)

    data



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