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.



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

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.



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

def default
  @default
end

#padObject

Returns the value of attribute pad.



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

def pad
  @pad
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#from_s(bytes) ⇒ Object



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

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



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

def reset
  self.value = @default
end

#to_sObject



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

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