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(data, endian = :big) ⇒ BinaryReader

Constructor

Parameters:

  • data (String)

    binary string

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

    endianness



14
15
16
17
18
19
# File 'lib/mikunyan/binary_reader.rb', line 14

def initialize(data, endian = :big)
    @data = data
    @pos = 0
    @length = data.bytesize
    @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

#lengthInteger

data size

Returns:

  • (Integer)

    the current value of length



8
9
10
# File 'lib/mikunyan/binary_reader.rb', line 8

def length
  @length
end

#posInteger

position

Returns:

  • (Integer)

    the current value of pos



8
9
10
# File 'lib/mikunyan/binary_reader.rb', line 8

def pos
  @pos
end

Instance Method Details

#adv(size = 0) ⇒ Object

Advance position given size

Parameters:

  • size (Integer) (defaults to: 0)

    size



35
36
37
# File 'lib/mikunyan/binary_reader.rb', line 35

def adv(size=0)
    @pos += size
end

#align(size) ⇒ Object

Round up position to multiple of given size

Parameters:

  • size (Integer)

    size



41
42
43
# File 'lib/mikunyan/binary_reader.rb', line 41

def align(size)
    @pos = (@pos + size - 1) / size * size
end

#cstrString

Read string until null character

Returns:

  • (String)

    string



56
57
58
59
60
# File 'lib/mikunyan/binary_reader.rb', line 56

def cstr
    r = @data.unpack("@#{pos}Z*")[0]
    @pos += r.bytesize + 1
    r
end

#doubleObject

Read 64bit floating point value



146
147
148
149
150
# File 'lib/mikunyan/binary_reader.rb', line 146

def double
    r = little? ? @data.byteslice(@pos, 8).unpack('E')[0] : @data.byteslice(@pos, 8).unpack('G')[0]
    @pos += 8
    r
end

#floatObject

Read 32bit floating point value



139
140
141
142
143
# File 'lib/mikunyan/binary_reader.rb', line 139

def float
    r = little? ? @data.byteslice(@pos, 4).unpack('e')[0] : @data.byteslice(@pos, 4).unpack('g')[0]
    @pos += 4
    r
end

#i16Object

Read 16bit signed integer



82
83
84
# File 'lib/mikunyan/binary_reader.rb', line 82

def i16
    i16s
end

#i16sObject

Read 16bit signed integer



87
88
89
90
91
# File 'lib/mikunyan/binary_reader.rb', line 87

def i16s
    r = little? ? BinUtils.get_sint16_le(@data, @pos) : BinUtils.get_sint16_be(@data, @pos)
    @pos += 2
    r
end

#i16uObject

Read 16bit unsigned integer



94
95
96
97
98
# File 'lib/mikunyan/binary_reader.rb', line 94

def i16u
    r = little? ? BinUtils.get_int16_le(@data, @pos) : BinUtils.get_int16_be(@data, @pos)
    @pos += 2
    r
end

#i32Object

Read 32bit signed integer



101
102
103
# File 'lib/mikunyan/binary_reader.rb', line 101

def i32
    i32s
end

#i32sObject

Read 32bit signed integer



106
107
108
109
110
# File 'lib/mikunyan/binary_reader.rb', line 106

def i32s
    r = little? ? BinUtils.get_sint32_le(@data, @pos) : BinUtils.get_sint32_be(@data, @pos)
    @pos += 4
    r
end

#i32uObject

Read 32bit unsigned integer



113
114
115
116
117
# File 'lib/mikunyan/binary_reader.rb', line 113

def i32u
    r = little? ? BinUtils.get_int32_le(@data, @pos) : BinUtils.get_int32_be(@data, @pos)
    @pos += 4
    r
end

#i64Object

Read 64bit signed integer



120
121
122
# File 'lib/mikunyan/binary_reader.rb', line 120

def i64
    i64s
end

#i64sObject

Read 64bit signed integer



125
126
127
128
129
# File 'lib/mikunyan/binary_reader.rb', line 125

def i64s
    r = little? ? BinUtils.get_sint64_le(@data, @pos) : BinUtils.get_sint64_be(@data, @pos)
    @pos += 8
    r
end

#i64uObject

Read 64bit unsigned integer



132
133
134
135
136
# File 'lib/mikunyan/binary_reader.rb', line 132

def i64u
    r = little? ? BinUtils.get_int64_le(@data, @pos) : BinUtils.get_int64_be(@data, @pos)
    @pos += 8
    r
end

#i8Object

Read 8bit signed integer



63
64
65
# File 'lib/mikunyan/binary_reader.rb', line 63

def i8
    i8s
end

#i8sObject

Read 8bit signed integer



68
69
70
71
72
# File 'lib/mikunyan/binary_reader.rb', line 68

def i8s
    r = BinUtils.get_sint8(@data, @pos)
    @pos += 1
    r
end

#i8uObject

Read 8bit unsigned integer



75
76
77
78
79
# File 'lib/mikunyan/binary_reader.rb', line 75

def i8u
    r = BinUtils.get_int8(@data, @pos)
    @pos += 1
    r
end

#jmp(pos = 0) ⇒ Object

Jump to given position

Parameters:

  • pos (Integer) (defaults to: 0)

    position



29
30
31
# File 'lib/mikunyan/binary_reader.rb', line 29

def jmp(pos=0)
    @pos = 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

#read(size) ⇒ String

Read given size of binary string and seek

Parameters:

  • size (Integer)

    size

Returns:

  • (String)

    data



48
49
50
51
52
# File 'lib/mikunyan/binary_reader.rb', line 48

def read(size)
    data = @data.byteslice(@pos, size)
    @pos += size
    data
end