Class: PacketGen::Types::CString

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Fieldable
Defined in:
lib/packetgen/types/cstring.rb

Overview

This class handles null-terminated strings (aka C strings).

Author:

  • Sylvain Daubert

Since:

  • 3.1.6 no more a subclass or regular String

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fieldable

#format_inspect, #type_name

Constructor Details

#initialize(options = {}) ⇒ CString

Returns a new instance of CString.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :static_length (Integer)

    set a static length for this string

Since:

  • 3.1.6 no more a subclass or regular String



31
32
33
34
# File 'lib/packetgen/types/cstring.rb', line 31

def initialize(options={})
  register_internal_string(+'')
  @static_length = options[:static_length]
end

Instance Attribute Details

#static_lengthInteger (readonly)

Returns:

  • (Integer)

Since:

  • 3.1.6 no more a subclass or regular String



27
28
29
# File 'lib/packetgen/types/cstring.rb', line 27

def static_length
  @static_length
end

#string::String (readonly)

Returns:

  • (::String)

Since:

  • 3.1.6 no more a subclass or regular String



25
26
27
# File 'lib/packetgen/types/cstring.rb', line 25

def string
  @string
end

Instance Method Details

#<<(str) ⇒ self

Append the given string to CString

Parameters:

Returns:

  • (self)

Since:

  • 3.1.6 no more a subclass or regular String



61
62
63
64
65
# File 'lib/packetgen/types/cstring.rb', line 61

def <<(str)
  @string << str.to_s
  remove_null_character
  self
end

#from_human(str) ⇒ self

Populate CString from a human readable string

Parameters:

Returns:

  • (self)

Since:

  • 3.1.6 no more a subclass or regular String



86
87
88
# File 'lib/packetgen/types/cstring.rb', line 86

def from_human(str)
  read str
end

#read(str) ⇒ String

Returns self.

Parameters:

  • str (::String)

Returns:

Since:

  • 3.1.6 no more a subclass or regular String



38
39
40
41
42
43
44
# File 'lib/packetgen/types/cstring.rb', line 38

def read(str)
  s = str.to_s
  s = s[0, static_length] if static_length?
  register_internal_string s
  remove_null_character
  self
end

#static_length?Boolean

Say if a static length is defined

Returns:

  • (Boolean)

Since:

  • 3.1.6



79
80
81
# File 'lib/packetgen/types/cstring.rb', line 79

def static_length?
  !static_length.nil?
end

#szInteger

Returns:

  • (Integer)

Since:

  • 3.1.6 no more a subclass or regular String



68
69
70
71
72
73
74
# File 'lib/packetgen/types/cstring.rb', line 68

def sz
  if static_length?
    static_length
  else
    to_s.size
  end
end

#to_humanString

Returns:

Since:

  • 3.1.6 no more a subclass or regular String



91
92
93
# File 'lib/packetgen/types/cstring.rb', line 91

def to_human
  string
end

#to_sString

get null-terminated string

Returns:

Since:

  • 3.1.6 no more a subclass or regular String



48
49
50
51
52
53
54
55
56
# File 'lib/packetgen/types/cstring.rb', line 48

def to_s
  if static_length?
    s = string[0, static_length - 1]
    s << "\x00" * (static_length - s.length)
  else
    s = "#{string}\x00"
  end
  PacketGen.force_binary(s)
end