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.



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

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.



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

def check_mask
  @check_mask
end

#defaultObject

Returns the value of attribute default.



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

def default
  @default
end

#maskObject

Returns the value of attribute mask.



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

def mask
  @mask
end

Instance Method Details

#from_s(bytes) ⇒ Object



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

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



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

def reset
	self.value = @default
end

#to_sObject



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

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