Class: Optimize::Codec::BinaryReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ BinaryReader

Returns a new instance of BinaryReader.



8
9
10
11
# File 'lib/optimize/codec/binary_reader.rb', line 8

def initialize(buffer)
  @buffer = buffer.b
  @pos = 0
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



6
7
8
# File 'lib/optimize/codec/binary_reader.rb', line 6

def pos
  @pos
end

Instance Method Details

#bytesizeObject

Total byte length of the underlying buffer.



45
# File 'lib/optimize/codec/binary_reader.rb', line 45

def bytesize = @buffer.bytesize

#peek_bytes(offset, length) ⇒ Object

Returns a slice of the underlying buffer without advancing pos.

Raises:

  • (RangeError)


39
40
41
42
# File 'lib/optimize/codec/binary_reader.rb', line 39

def peek_bytes(offset, length)
  raise RangeError, "peek past end" if offset + length > @buffer.bytesize
  @buffer.byteslice(offset, length)
end

#read_bytes(n) ⇒ Object

Raises:

  • (RangeError)


18
19
20
21
22
23
# File 'lib/optimize/codec/binary_reader.rb', line 18

def read_bytes(n)
  raise RangeError, "read past end" if @pos + n > @buffer.bytesize
  bytes = @buffer.byteslice(@pos, n)
  @pos += n
  bytes
end

#read_cstrObject

Raises:

  • (RangeError)


25
26
27
28
29
30
31
# File 'lib/optimize/codec/binary_reader.rb', line 25

def read_cstr
  nul = @buffer.index("\x00".b, @pos)
  raise RangeError, "unterminated cstr" unless nul
  s = @buffer.byteslice(@pos, nul - @pos)
  @pos = nul + 1
  s
end

#read_small_valueObject

Decode a small_value (variable-length unsigned integer) from the current position. See research/cruby/ibf-format.md ยง6 for the encoding.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/optimize/codec/binary_reader.rb', line 49

def read_small_value
  b0 = read_u8
  if b0 == 0
    # 9-byte form: full uint64 in next 8 bytes (big-endian).
    # ibf_dump_small_value uses the same "value = (value << 8) | byte[i]"
    # algorithm for all multi-byte forms, including the 9-byte case.
    read_bytes(8).unpack1("Q>")
  else
    # Count trailing zero bits of b0 to determine total byte count
    n = 0
    tmp = b0
    while tmp & 1 == 0
      n += 1
      tmp >>= 1
    end
    n_bytes = n + 1  # total bytes including b0
    value = b0 >> n_bytes
    (1...n_bytes).each { |_| value = (value << 8) | read_u8 }
    value
  end
end

#read_u16Object



14
# File 'lib/optimize/codec/binary_reader.rb', line 14

def read_u16 = read_int(2, "v")

#read_u32Object



15
# File 'lib/optimize/codec/binary_reader.rb', line 15

def read_u32 = read_int(4, "V")

#read_u64Object



16
# File 'lib/optimize/codec/binary_reader.rb', line 16

def read_u64 = read_int(8, "Q<")

#read_u8Object



13
# File 'lib/optimize/codec/binary_reader.rb', line 13

def read_u8  = read_int(1, "C")

#seek(offset) ⇒ Object

Raises:

  • (RangeError)


33
34
35
36
# File 'lib/optimize/codec/binary_reader.rb', line 33

def seek(offset)
  raise RangeError if offset.negative? || offset > @buffer.bytesize
  @pos = offset
end