Class: PacketGen::Types::IntString
- Inherits:
-
Object
- Object
- PacketGen::Types::IntString
- Defined in:
- lib/packetgen/types/int_string.rb
Overview
Provides a class for creating strings preceeded by their length as a Int. By default, a null string will have one byte length (length byte set to 0).
Instance Attribute Summary collapse
-
#string ⇒ String
internal string.
Instance Method Summary collapse
-
#calc_length ⇒ Integer
Set length from internal string length.
-
#from_human(str) ⇒ self
Set from a human readable string.
-
#initialize(len_type = Int8, string: '') ⇒ IntString
constructor
A new instance of IntString.
- #length ⇒ Integer
- #length=(len) ⇒ Integer
-
#read(str) ⇒ IntString
Self.
-
#sz ⇒ Integer
Give binary string length (including
lengthfield). -
#to_human ⇒ ::String
Get human readable string.
-
#to_s ⇒ ::String
Get binary string.
Constructor Details
Instance Attribute Details
#string ⇒ String
internal string
17 18 19 |
# File 'lib/packetgen/types/int_string.rb', line 17 def string @string end |
Instance Method Details
#calc_length ⇒ Integer
Set length from internal string length
82 83 84 |
# File 'lib/packetgen/types/int_string.rb', line 82 def calc_length @length.read @string.length end |
#from_human(str) ⇒ self
Set from a human readable string
67 68 69 70 71 |
# File 'lib/packetgen/types/int_string.rb', line 67 def from_human(str) @string.read str calc_length self end |
#length ⇒ Integer
47 48 49 |
# File 'lib/packetgen/types/int_string.rb', line 47 def length @length.to_i end |
#length=(len) ⇒ Integer
41 42 43 44 |
# File 'lib/packetgen/types/int_string.rb', line 41 def length=(len) @length.read len len end |
#read(str) ⇒ IntString
Returns self.
29 30 31 32 33 34 35 36 37 |
# File 'lib/packetgen/types/int_string.rb', line 29 def read(str) unless str[0, @length.width].size == @length.width raise ParseError, "String too short for type #{@length.class.to_s.gsub(/.*::/, '')}" end @length.read str[0, @length.width] @string.read str[@length.width, @length.to_i] self end |
#sz ⇒ Integer
Give binary string length (including length field)
88 89 90 |
# File 'lib/packetgen/types/int_string.rb', line 88 def sz to_s.size end |
#to_human ⇒ ::String
Get human readable string
76 77 78 |
# File 'lib/packetgen/types/int_string.rb', line 76 def to_human @string end |
#to_s ⇒ ::String
Get binary string
60 61 62 |
# File 'lib/packetgen/types/int_string.rb', line 60 def to_s @length.to_s << @string.to_s end |