Class: PacketGen::StructFu::IntString
- Defined in:
- lib/packetgen/structfu.rb
Overview
Provides a primitive for creating strings, preceeded by an Int type of length. By default, a string of length zero with a one-byte length is presumed.
Note that IntStrings aren’t used for much, but it seemed like a good idea at the time.
Instance Attribute Summary collapse
-
#int ⇒ Object
Returns the value of attribute int.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#string ⇒ Object
Returns the value of attribute string.
Instance Method Summary collapse
-
#calc ⇒ Object
Calculates the size of a string, and sets it as the value.
-
#initialize(string = '', int = Int8, mode = nil) ⇒ IntString
constructor
A new instance of IntString.
-
#len ⇒ Object
Shorthand for querying a length.
-
#len=(i) ⇒ Object
Override the size, if you must.
-
#parse(s) ⇒ Object
parse() is like read(), except that it interprets the string, either based on the declared length, or the actual length.
-
#read(s) ⇒ Object
Read takes a string, assumes an int width as previously defined upon initialization, but makes no guarantees the int value isn’t lying.
-
#to_s ⇒ Object
Returns the object as a string, depending on the mode set upon object creation.
Methods inherited from Struct
Constructor Details
#initialize(string = '', int = Int8, mode = nil) ⇒ IntString
Returns a new instance of IntString.
255 256 257 258 259 260 261 262 |
# File 'lib/packetgen/structfu.rb', line 255 def initialize(string='',int=Int8,mode=nil) if int < Int super(int.new,string,mode) calc else raise "IntStrings need a StructFu::Int for a length." end end |
Instance Attribute Details
#int ⇒ Object
Returns the value of attribute int
253 254 255 |
# File 'lib/packetgen/structfu.rb', line 253 def int @int end |
#mode ⇒ Object
Returns the value of attribute mode
253 254 255 |
# File 'lib/packetgen/structfu.rb', line 253 def mode @mode end |
#string ⇒ Object
Returns the value of attribute string
253 254 255 |
# File 'lib/packetgen/structfu.rb', line 253 def string @string end |
Instance Method Details
#calc ⇒ Object
Calculates the size of a string, and sets it as the value.
265 266 267 268 |
# File 'lib/packetgen/structfu.rb', line 265 def calc int.v = string.to_s.size self.to_s end |
#len ⇒ Object
Shorthand for querying a length. Note that the usual “length” and “size” refer to the number of elements of this struct.
296 297 298 |
# File 'lib/packetgen/structfu.rb', line 296 def len self[:int].value end |
#len=(i) ⇒ Object
Override the size, if you must.
301 302 303 |
# File 'lib/packetgen/structfu.rb', line 301 def len=(i) self[:int].value=i end |
#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.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/packetgen/structfu.rb', line 331 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 |
#read(s) ⇒ Object
Read takes a string, assumes an int width as previously defined upon initialization, but makes no guarantees the int value isn’t lying. You’re on your own to test for that (or use parse() with a :mode set).
309 310 311 312 313 314 315 316 317 |
# File 'lib/packetgen/structfu.rb', line 309 def read(s) unless s[0,int.width].size == int.width raise StandardError, "String is too short for type #{int.class}" else int.read(s[0,int.width]) self[:string] = s[int.width,s.size] end self.to_s end |
#to_s ⇒ Object
Returns the object as a string, depending on the mode set upon object creation.
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/packetgen/structfu.rb', line 271 def to_s if mode == :parse "#{int}" + [string].pack("a#{len}") elsif mode == :fix self.int.v = string.size "#{int}#{string}" else "#{int}#{string}" end end |