Class: MaxMind::DB::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/maxmind/db/decoder.rb

Overview

Decoder decodes a MaxMind DB data section.

Typically you will interact with this class through a Reader rather than directly.

Instance Method Summary collapse

Constructor Details

#initialize(io, pointer_base = 0, pointer_test = false) ⇒ Decoder

Create a Decoder.

io is the DB. It must provide a read method. It must be opened in binary mode.

pointer_base is the base number to use when decoding a pointer. It is where the data section begins rather than the beginning of the file. The specification states the formula in the ‘Data Section Separator’ section.

pointer_test is used for testing pointer code.



24
25
26
27
28
# File 'lib/maxmind/db/decoder.rb', line 24

def initialize(io, pointer_base = 0, pointer_test = false)
  @io = io
  @pointer_base = pointer_base
  @pointer_test = pointer_test
end

Instance Method Details

#decode(offset) ⇒ Object

Decode a section of the data section starting at offset.

offset is the location of the data structure to decode.

Returns an array where the first element is the decoded value and the second is the offset after decoding it.

Throws an exception if there is an error.



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/maxmind/db/decoder.rb', line 184

def decode(offset)
  new_offset = offset + 1
  buf = @io.read(offset, 1)
  ctrl_byte = buf.ord
  type_num = ctrl_byte >> 5
  type_num, new_offset = read_extended(new_offset) if type_num == 0

  size, new_offset = size_from_ctrl_byte(ctrl_byte, new_offset, type_num)
  # We could check an element exists at `type_num', but for performance I
  # don't.
  send(TYPE_DECODER[type_num], size, new_offset)
end