Class: Rex::Struct2::Generic
- Inherits:
-
Object
- Object
- Rex::Struct2::Generic
- Includes:
- Element
- Defined in:
- lib/rex/struct2/generic.rb
Instance Attribute Summary collapse
-
#check_mask ⇒ Object
Returns the value of attribute check_mask.
-
#default ⇒ Object
Returns the value of attribute default.
-
#mask ⇒ Object
Returns the value of attribute mask.
Attributes included from Element
#container, #restraint, #value
Instance Method Summary collapse
- #from_s(bytes) ⇒ Object
-
#initialize(packspec, signed = false, default = nil) ⇒ Generic
constructor
A new instance of Generic.
- #reset ⇒ Object
- #to_s ⇒ Object
Methods included from Element
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_mask ⇒ Object
Returns the value of attribute check_mask.
15 16 17 |
# File 'lib/rex/struct2/generic.rb', line 15 def check_mask @check_mask end |
#default ⇒ Object
Returns the value of attribute default.
12 13 14 |
# File 'lib/rex/struct2/generic.rb', line 12 def default @default end |
#mask ⇒ Object
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 |
#reset ⇒ Object
33 34 35 |
# File 'lib/rex/struct2/generic.rb', line 33 def reset self.value = @default end |
#to_s ⇒ Object
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 |