Class: Rex::Struct2::SString

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/rex/struct2/s_string.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(size = nil, default = nil, pad = nil) ⇒ SString

Returns a new instance of SString.



17
18
19
20
21
22
# File 'lib/rex/struct2/s_string.rb', line 17

def initialize(size=nil, default=nil, pad=nil)
	self.size = size
	@default  = default
	@pad      = pad
	reset()
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



14
15
16
# File 'lib/rex/struct2/s_string.rb', line 14

def default
  @default
end

#padObject

Returns the value of attribute pad.



14
15
16
# File 'lib/rex/struct2/s_string.rb', line 14

def pad
  @pad
end

#sizeObject

Returns the value of attribute size.



14
15
16
# File 'lib/rex/struct2/s_string.rb', line 14

def size
  @size
end

Instance Method Details

#from_s(bytes) ⇒ Object



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

def from_s(bytes)
	# we don't have enough bytes to satisfy our minimum
	if restraint && restraint.min && bytes.length < restraint.min
		return
	end

	if restraint && restraint.max
		self.value = bytes.slice(0, restraint.max)
	else
		self.value = bytes.dup
	end


	return(self.slength)
end

#resetObject



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

def reset
	self.value = @default
end

#to_sObject



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

def to_s
	string = self.value

	return if !string

	# pad if short
	if restraint && restraint.min && self.pad && restraint.min > string.length
		string += self.pad * (restraint.min - string.length)
	end
	# truncate if long
	if restraint && restraint.max
		string = string.slice(0, restraint.max)
	end

	return string
end