Class: PacketGen::Plugin::SMB::String

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Types::Fieldable
Defined in:
lib/packetgen/plugin/smb/string.rb

Overview

SMB strings (UTF-16 little-endian or OEM).

Author:

  • Sylvain Daubert

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ String

Returns a new instance of String.

Parameters:

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

Options Hash (options):

  • :unicode (Boolean)

    If true, string is encoded as a UTF-16 unicode string. If false, string is encode in ASCII. Defaults to true.

  • :null_terminated (Boolean)

    If true, string is null-terminated. If false, string is not null-terminated. Defaults to true.



33
34
35
36
37
# File 'lib/packetgen/plugin/smb/string.rb', line 33

def initialize(options={})
  @unicode = options.key?(:unicode) ? options[:unicode] : true
  @null_terminated = options.key?(:null_terminated) ? options[:null_terminated] : true
  @string = +''.encode(self_encoding)
end

Instance Attribute Details

#null_terminated=(value) ⇒ Boolean (writeonly)

Parameters:

  • null_terminated (Boolean)

Returns:

  • (Boolean)


26
27
28
# File 'lib/packetgen/plugin/smb/string.rb', line 26

def null_terminated=(value)
  @null_terminated = value
end

#string::String (readonly)

Returns:

  • (::String)


23
24
25
# File 'lib/packetgen/plugin/smb/string.rb', line 23

def string
  @string
end

Instance Method Details

#from_human(str) ⇒ self

Populate String from a human readable (ie UTF-8) string

Parameters:

Returns:

  • (self)


85
86
87
88
89
90
# File 'lib/packetgen/plugin/smb/string.rb', line 85

def from_human(str)
  return self if str.nil?

  @string = str.encode(self_encoding)
  self
end

#null_terminated?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/packetgen/plugin/smb/string.rb', line 53

def null_terminated?
  @null_terminated
end

#read(str) ⇒ String

Returns self.

Parameters:

  • str (::String)

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/packetgen/plugin/smb/string.rb', line 59

def read(str)
  return self if str.nil?

  str2 = case str.encoding
         when Encoding::BINARY
           str.dup.force_encoding(self_encoding)
         else
           str.encode(self_encoding)
         end
  idx = str2.index(+"\x00".encode(self_encoding))
  str2 = str2[0, idx] unless idx.nil?
  @string = str2
  self
end

#to_humanString

Returns:



93
94
95
# File 'lib/packetgen/plugin/smb/string.rb', line 93

def to_human
  string.encode('UTF-8')
end

#to_sString

Returns:



75
76
77
78
79
80
# File 'lib/packetgen/plugin/smb/string.rb', line 75

def to_s
  str = string.dup.force_encoding('BINARY')
  return str unless null_terminated?

  str << binary_terminator.force_encoding('BINARY')
end

#unicode=(bool) ⇒ Boolean

Parameters:

  • bool (Boolean)

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/packetgen/plugin/smb/string.rb', line 46

def unicode=(bool)
  @unicode = bool
  @string.force_encoding(self_encoding)
  bool
end

#unicode?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/packetgen/plugin/smb/string.rb', line 40

def unicode?
  @unicode
end