class Rex::Struct2::Generic

Attributes

check_mask[RW]
default[RW]
mask[RW]

Public Class Methods

new(packspec, signed=false, default=nil) click to toggle source
# File lib/rex/struct2/generic.rb, line 16
def initialize(packspec, signed=false, default=nil)
  @packspec = packspec
  @default  = default

  bytelen = [ -1 ].pack(@packspec).length
  self.mask = (1 << (8 * bytelen)) - 1

  if signed
    self.check_mask = 1 << (8 * bytelen - 1)
  else
    self.check_mask = 0
  end

  reset()
end

Public Instance Methods

from_s(bytes) click to toggle source
# File lib/rex/struct2/generic.rb, line 50
def from_s(bytes)
  value = bytes.unpack(@packspec)[0]
  # return nil on unpack error
  return if !value
  len = slength()
  # error on any restraint issues
  return if restraint && restraint.max && len > restraint.max
  return if restraint && restraint.min && len < restraint.min
  # else set our value and return length used for this element

  if (value & check_mask) != 0
    value = -((~value & mask) + 1)
  end

  self.value = value
  return(len)
end
reset() click to toggle source
# File lib/rex/struct2/generic.rb, line 32
def reset
  self.value = @default
end
to_s() click to toggle source
# File lib/rex/struct2/generic.rb, line 36
def to_s
  # I realize this will bomb out if this isn't an integer, for
  # example if it is nil.  That should only happen for a user
  # error so that's what I want it to do...
  string = [ @value ].pack(@packspec)

  if restraint && restraint.max
    return string.slice(0, restraint.max)
  else
    return string
  end
  # what to do for restraint.min?!?
end