Method: StructFu::IntString#parse

Defined in:
lib/packetfu/structfu.rb

#parse(s) ⇒ Object

parse() is like read(), except that it interprets the string, either based on the declared length, or the actual length. Which strategy is used is dependant on which :mode is set (with self.mode).

:parse : Read the length, and then read in that many bytes of the string. The string may be truncated or padded out with nulls, as dictated by the value.

:fix : Skip the length, read the rest of the string, then set the length to what it ought to be.

else : If neither of these modes are set, just perfom a normal read(). This is the default.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/packetfu/structfu.rb', line 288

def parse(s)
  unless s[0,int.width].size == int.width
    raise StandardError, "String is too short for type #{int.class}"
  else
    case mode
    when :parse
      int.read(s[0,int.width])
      self[:string] = s[int.width,int.value]
      if string.size < int.value
        self[:string] += ("\x00" * (int.value - self[:string].size))
      end
    when :fix
      self.string = s[int.width,s.size]
    else
      return read(s)
    end
  end
  self.to_s
end