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



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

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



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

def static_length
  @static_length
end

#string::String (readonly)

Returns:

  • (::String)

Since:

  • 3.1.6 no more a subclass or regular String



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

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



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

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



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

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



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

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



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

def static_length?
  !static_length.nil?
end

#szInteger

Returns:

  • (Integer)

Since:

  • 3.1.6 no more a subclass or regular String



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

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



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

def to_human
  string
end

#to_sString

get null-terminated string

Returns:

Since:

  • 3.1.6 no more a subclass or regular String



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

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