Class: BinaryCodec::BinaryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/binary-codec/serdes/binary_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex_bytes = '') ⇒ BinaryParser

Returns a new instance of BinaryParser.



9
10
11
12
# File 'lib/binary-codec/serdes/binary_parser.rb', line 9

def initialize(hex_bytes = '')
  @bytes = hex_to_bytes(hex_bytes)
  @definitions = Definitions.instance
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



7
8
9
# File 'lib/binary-codec/serdes/binary_parser.rb', line 7

def definitions
  @definitions
end

Instance Method Details

#end?(custom_end = nil) ⇒ Boolean

Checks if the end of the stream has been reached.

Parameters:

  • custom_end (Integer, nil) (defaults to: nil)

    Optional offset to check against.

Returns:

  • (Boolean)

    True if at the end, false otherwise.



82
83
84
85
# File 'lib/binary-codec/serdes/binary_parser.rb', line 82

def end?(custom_end = nil)
  length = @bytes.length
  length == 0 || (!custom_end.nil? && length <= custom_end)
end

#peekInteger

Returns the first byte in the stream without consuming it.

Returns:

  • (Integer)

    The first byte.



16
17
18
19
20
21
# File 'lib/binary-codec/serdes/binary_parser.rb', line 16

def peek
  if @bytes.empty?
    raise StandardError.new
  end
  @bytes[0]
end

#read(n) ⇒ Array<Integer>

Reads n bytes from the stream.

Parameters:

  • n (Integer)

    The number of bytes to read.

Returns:

  • (Array<Integer>)

    The read bytes.



35
36
37
38
39
40
41
42
43
# File 'lib/binary-codec/serdes/binary_parser.rb', line 35

def read(n)
  if n > @bytes.length
    raise StandardError.new('End of byte stream reached')
  end

  slice = @bytes[0, n]
  skip(n)
  slice
end

#read_fieldFieldInstance

Reads a field instance from the stream.

Returns:



131
132
133
134
135
136
137
138
# File 'lib/binary-codec/serdes/binary_parser.rb', line 131

def read_field
  field_header = read_field_header
  field_name = @definitions.get_field_name_from_header(field_header)
  
  raise "Unknown field for header: type=#{field_header.type}, nth=#{field_header.nth}" if field_name.nil?

  @definitions.get_field_instance(field_name)
end

#read_field_headerFieldHeader

Reads a field header from the stream.

Returns:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/binary-codec/serdes/binary_parser.rb', line 113

def read_field_header
  first_byte = read_uint8
  type = first_byte >> 4
  nth = first_byte & 15

  if type == 0
    type = read_uint8
  end

  if nth == 0
    nth = read_uint8
  end

  FieldHeader.new(type: type, nth: nth)
end

#read_field_value(field) ⇒ SerializedType

Reads the value of a specific field from the stream.

Parameters:

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/binary-codec/serdes/binary_parser.rb', line 157

def read_field_value(field)
  type = SerializedType.get_type_by_name(field.type)

  if type.nil?
    raise StandardError.new("unsupported: (#{field.name}, #{field.type.name})")
  end

  size_hint = field.is_variable_length_encoded ? read_variable_length_length : nil
  value = type.from_parser(self, size_hint)

  if value.nil?
    raise StandardError.new("from_parser for (#{field.name}, #{field.type.name}) -> nil")
  end

  value
end

#read_type(type) ⇒ SerializedType

Reads a value of the specified type from the stream.

Parameters:

  • type (Class)

    The class of the type to read (subclass of SerializedType).

Returns:



143
144
145
# File 'lib/binary-codec/serdes/binary_parser.rb', line 143

def read_type(type)
  type.from_parser(self)
end

#read_uint16Integer

Reads a 2-byte unsigned integer.

Returns:

  • (Integer)

    The 16-bit integer.



63
64
65
# File 'lib/binary-codec/serdes/binary_parser.rb', line 63

def read_uint16
  read_uint_n(2)
end

#read_uint32Integer

Reads a 4-byte unsigned integer.

Returns:

  • (Integer)

    The 32-bit integer.



69
70
71
# File 'lib/binary-codec/serdes/binary_parser.rb', line 69

def read_uint32
  read_uint_n(4)
end

#read_uint8Integer

Reads a 1-byte unsigned integer.

Returns:

  • (Integer)

    The 8-bit integer.



57
58
59
# File 'lib/binary-codec/serdes/binary_parser.rb', line 57

def read_uint8
  read_uint_n(1)
end

#read_uint_n(n) ⇒ Integer

Reads n bytes and converts them to an unsigned integer.

Parameters:

  • n (Integer)

    The number of bytes to read (1-4).

Returns:

  • (Integer)

    The resulting integer.



48
49
50
51
52
53
# File 'lib/binary-codec/serdes/binary_parser.rb', line 48

def read_uint_n(n)
  if n <= 0 || n > 4
    raise StandardError.new('invalid n')
  end
  read(n).reduce(0) { |a, b| (a << 8) | b }
end

#read_variable_lengthArray<Integer>

Reads variable length data from the stream.

Returns:

  • (Array<Integer>)

    The read bytes.



89
90
91
# File 'lib/binary-codec/serdes/binary_parser.rb', line 89

def read_variable_length
  read(read_variable_length_length)
end

#read_variable_length_lengthInteger

Reads the length of a variable length data segment.

Returns:

  • (Integer)

    The length.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/binary-codec/serdes/binary_parser.rb', line 95

def read_variable_length_length
  b1 = read_uint8
  if b1 <= 192
    b1
  elsif b1 <= 240
    b2 = read_uint8
    193 + (b1 - 193) * 256 + b2
  elsif b1 <= 254
    b2 = read_uint8
    b3 = read_uint8
    12481 + (b1 - 241) * 65536 + b2 * 256 + b3
  else
    raise StandardError.new('Invalid variable length indicator')
  end
end

#sizeInteger

Returns the number of bytes remaining in the stream.

Returns:

  • (Integer)

    The remaining size.



75
76
77
# File 'lib/binary-codec/serdes/binary_parser.rb', line 75

def size
  @bytes.length
end

#skip(n) ⇒ Object

Consumes n bytes from the stream.

Parameters:

  • n (Integer)

    The number of bytes to skip.



25
26
27
28
29
30
# File 'lib/binary-codec/serdes/binary_parser.rb', line 25

def skip(n)
  if n > @bytes.length
    raise StandardError.new
  end
  @bytes = @bytes[n..-1]
end

#type_for_field(field) ⇒ Class

Returns the associated type for a given field.

Parameters:

Returns:

  • (Class)

    The associated SerializedType subclass.



150
151
152
# File 'lib/binary-codec/serdes/binary_parser.rb', line 150

def type_for_field(field)
  field.associated_type
end