Class: OpenASN::BinaryFormat::OrgIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/openasn/binary_format.rb

Overview

--- OORG v1: ASN -> organization name ----------------------------------

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ OrgIndex

Returns a new instance of OrgIndex.

Raises:



275
276
277
278
279
280
281
282
283
284
# File 'lib/openasn/binary_format.rb', line 275

def initialize(bytes)
  raise FormatError, "not an OORG file (bad magic)" unless bytes[0, 4] == ORG_MAGIC
  raise FormatError, "unsupported OORG version #{bytes.getbyte(4)}" unless bytes.getbyte(4) == 0x01

  @bytes = bytes.freeze
  @count, @blob_size = bytes[8, 8].unpack("NN")
  @blob_base = ORG_HEADER_SIZE + @count * 8
  expected = @blob_base + @blob_size
  raise FormatError, "OORG truncated: #{bytes.bytesize} bytes, header implies #{expected}" unless bytes.bytesize == expected
end

Class Method Details

.load(path) ⇒ Object



271
272
273
# File 'lib/openasn/binary_format.rb', line 271

def self.load(path)
  new(File.binread(path))
end

Instance Method Details

#name(asn) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/openasn/binary_format.rb', line 286

def name(asn)
  lo = 0
  hi = @count - 1
  while lo <= hi
    mid = (lo + hi) / 2
    a, off = @bytes[ORG_HEADER_SIZE + mid * 8, 8].unpack("NN")
    if asn < a
      hi = mid - 1
    elsif asn > a
      lo = mid + 1
    else
      nxt = mid + 1 < @count ? @bytes[ORG_HEADER_SIZE + (mid + 1) * 8 + 4, 4].unpack1("N") : @blob_size
      return @bytes[@blob_base + off, nxt - off].force_encoding(Encoding::UTF_8)
    end
  end
  nil
end