Class: OpenASN::BinaryFormat::ArraysOverlay

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes, family) ⇒ ArraysOverlay

Returns a new instance of ArraysOverlay.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/openasn/binary_format.rb', line 236

def initialize(bytes, family)
  asz = BinaryFormat.addr_size(family)
  rec = BinaryFormat.overlay_rec_size(family)
  @count = bytes.bytesize / rec
  @starts = Array.new(@count)
  @ends   = Array.new(@count)
  unpack_addr = family == :ipv4 ? ->(s) { s.unpack1("N") } : ->(s) { hi, lo = s.unpack("Q>Q>"); (hi << 64) | lo }
  @count.times do |i|
    off = i * rec
    @starts[i] = unpack_addr.call(bytes[off, asz])
    @ends[i]   = unpack_addr.call(bytes[off + asz, asz])
  end
  [@starts, @ends].each(&:freeze)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



234
235
236
# File 'lib/openasn/binary_format.rb', line 234

def count
  @count
end

Instance Method Details

#cover?(ip_int) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/openasn/binary_format.rb', line 251

def cover?(ip_int)
  lo = 0
  hi = @count - 1
  while lo <= hi
    mid = (lo + hi) / 2
    if ip_int < @starts[mid]
      hi = mid - 1
    elsif ip_int > @ends[mid]
      lo = mid + 1
    else
      return true
    end
  end
  false
end