Class: Rex::Struct2::Generic

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/rex/struct2/generic.rb

Instance Attribute Summary collapse

Attributes included from Element

#container, #restraint, #value

Instance Method Summary collapse

Methods included from Element

#slength, #update_restraint

Constructor Details

#initialize(packspec, signed = false, default = nil) ⇒ Generic

Returns a new instance of Generic.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rex/struct2/generic.rb', line 17

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

Instance Attribute Details

#check_maskObject

Returns the value of attribute check_mask.



15
16
17
# File 'lib/rex/struct2/generic.rb', line 15

def check_mask
  @check_mask
end

#defaultObject

Returns the value of attribute default.



12
13
14
# File 'lib/rex/struct2/generic.rb', line 12

def default
  @default
end

#maskObject

Returns the value of attribute mask.



15
16
17
# File 'lib/rex/struct2/generic.rb', line 15

def mask
  @mask
end

Instance Method Details

#from_s(bytes) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rex/struct2/generic.rb', line 51

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

#resetObject



33
34
35
# File 'lib/rex/struct2/generic.rb', line 33

def reset
  self.value = @default
end

#to_sObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rex/struct2/generic.rb', line 37

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