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

Inherits:
Types::CString
  • Object
show all
Defined in:
lib/packetgen/plugin/smb/string.rb

Overview

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

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):

  • :static_length (Integer)

    set a static length for this string

  • :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.



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

def initialize(options={})
  super
  @unicode = options.key?(:unicode) ? options[:unicode] : true
  @null_terminated = options.key?(:null_terminated) ? options[:null_terminated] : true
  encoding = unicode? ? 'UTF-16LE' : 'ASCII-8BIT'
  self.encode!(encoding)
end

Instance Attribute Details

#null_terminated=(value) ⇒ Boolean (writeonly)

Parameters:

  • null_terminated (Boolean)

Returns:

  • (Boolean)


18
19
20
# File 'lib/packetgen/plugin/smb/string.rb', line 18

def null_terminated=(value)
  @null_terminated = value
end

#unicode=(value) ⇒ Boolean (writeonly)

Parameters:

  • value (Boolean)

Returns:

  • (Boolean)


15
16
17
# File 'lib/packetgen/plugin/smb/string.rb', line 15

def unicode=(value)
  @unicode = value
end

Instance Method Details

#null_terminated?Boolean

Returns:

  • (Boolean)


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

def null_terminated?
  @null_terminated
end

#read(str) ⇒ String

Returns self.

Parameters:

  • str (::String)

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/packetgen/plugin/smb/string.rb', line 46

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

  str2 = case str.encoding
         when Encoding::BINARY
           binidx = nil
           0.step(to: str.size, by: 2) do |i|
             binidx = i if str[i, 2] == binary_terminator
           end
           s = if binidx.nil?
                 str
               else
                 str[0, binidx]
               end
           s.force_encoding(self_encoding)
         else
           str.encode(self_encoding)
         end
  str2 = str2[0, @static_length / 2] if @static_length.is_a? Integer
  idx = str2.index(+"\x00".encode(self_encoding))
  str2 = str2[0, idx] unless idx.nil?
  self.replace str2
  self
end

#to_humanString

Returns:



81
82
83
# File 'lib/packetgen/plugin/smb/string.rb', line 81

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

#to_sString

Returns:



72
73
74
75
76
77
78
# File 'lib/packetgen/plugin/smb/string.rb', line 72

def to_s
  s = super
  s.encode(self_encoding)
  return s if null_terminated?

  s[0...-binary_terminator.size]
end

#unicode?Boolean

Returns:

  • (Boolean)


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

def unicode?
  @unicode
end