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

Author:

  • Sylvain Daubert

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ String

Returns a new instance of String.

Parameters:

  • is (Boolean, Proc)

    string UTF-16 encoded?

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



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

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

Instance Attribute Details

#unicode=(value) ⇒ Object (writeonly)

Parameters:

  • unicode (Boolean)


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

def unicode=(value)
  @unicode = value
end

Instance Method Details

#read(str) ⇒ String

Returns self.

Parameters:

  • str (::String)

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/packetgen/plugin/smb/string.rb', line 35

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

#unicode?Boolean

Returns:

  • (Boolean)


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

def unicode?
  @unicode
end