Method: Rex::Struct2::SStruct#from_s

Defined in:
lib/rex/struct2/s_struct.rb

#from_s(obytes) ⇒ Object



72
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
# File 'lib/rex/struct2/s_struct.rb', line 72

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