Class: OpenASN::BinaryFormat::PackedOverlay

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) ⇒ PackedOverlay

Returns a new instance of PackedOverlay.



204
205
206
207
208
209
210
# File 'lib/openasn/binary_format.rb', line 204

def initialize(bytes, family)
  @bytes = bytes.freeze
  @family = family
  @asz = BinaryFormat.addr_size(family)
  @rec = BinaryFormat.overlay_rec_size(family)
  @count = bytes.bytesize / @rec
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



202
203
204
# File 'lib/openasn/binary_format.rb', line 202

def count
  @count
end

Instance Method Details

#cover?(ip_int) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/openasn/binary_format.rb', line 212

def cover?(ip_int)
  return false if @count.zero?

  key = BinaryFormat.pack_addr(ip_int, @family)
  lo = 0
  hi = @count - 1
  while lo <= hi
    mid = (lo + hi) / 2
    off = mid * @rec
    if key < @bytes[off, @asz]
      hi = mid - 1
    elsif key > @bytes[off + @asz, @asz]
      lo = mid + 1
    else
      return true
    end
  end
  false
end