Class: Rex::Struct2::SStruct

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

Direct Known Subclasses

CStruct

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(*opts) ⇒ SStruct

watch out!, leftover returns our copy of the string! so don’t do anything stupid like struct.leftover.slice! !!



21
22
23
24
# File 'lib/rex/struct2/s_struct.rb', line 21

def initialize(*opts)
  self.elements = [ ]
  self.add_element(*opts)
end

Instance Attribute Details

#leftoverObject

Returns the value of attribute leftover.



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

def leftover
  @leftover
end

Instance Method Details

#<<(obj) ⇒ Object



40
41
42
# File 'lib/rex/struct2/s_struct.rb', line 40

def <<(obj)
  self.add_element(obj)
end

#[](obj) ⇒ Object



65
66
67
# File 'lib/rex/struct2/s_struct.rb', line 65

def [](obj)
  return elements[obj]
end

#add_element(*objs) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/rex/struct2/s_struct.rb', line 32

def add_element(*objs)
  objs.each { |o|
    elements << o
    o.container = self
  }
  return self
end

#each(&block) ⇒ Object



69
70
71
# File 'lib/rex/struct2/s_struct.rb', line 69

def each(&block)
  return elements.each(&block)
end

#from_s(obytes) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rex/struct2/s_struct.rb', line 73

def from_s(obytes)
  # make my own copy so I can chop it up
  bytes = obytes.dup
  length = 0

  # I don't think we should call update_restraint here, but
  # I could have mis thought or something

  # if we have a restraint (and if there is a val) truncate
  if restraint
    max = restraint.max
    bytes = bytes.slice(0, max) if max
  end

  elements.each { |e|
    used = e.from_s(bytes)
    return if !used
    bytes.slice!(0, used)
    length += used
  }

  # make sure we matched out min restraint, else return failure
  if restraint
    min = restraint.min
    return if min && length < min
  end

  # I guess this is me getting "set", so I should have a value
  # and I should update my restraints on set
  self.value = obytes.slice(0, length)

  self.leftover = bytes
  return(length)
end

#lengthObject



61
62
63
# File 'lib/rex/struct2/s_struct.rb', line 61

def length
  return elements.length
end

#resetObject



27
28
29
30
# File 'lib/rex/struct2/s_struct.rb', line 27

def reset
  elements.each {|e| e.reset}
  return self
end

#to_sObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rex/struct2/s_struct.rb', line 44

def to_s
  # !!! what do we do on mix restraint issues? just fail?
  # maybe throw an exception, because that is most likely
  # a usage error

  buff = ""
  elements.each do |e|
    buff << e.to_s
  end
  
  if restraint && restraint.max
    return buff.slice(0, restraint.max)
  else
    return buff
  end
end