Class: PacketGen::Plugin::SMB::String
- Inherits:
-
Types::CString
- Object
- Types::CString
- PacketGen::Plugin::SMB::String
- Defined in:
- lib/packetgen/plugin/smb/string.rb
Overview
SMB strings (UTF-16 little-endian or OEM).
Instance Attribute Summary collapse
- #null_terminated ⇒ Boolean writeonly
- #unicode ⇒ Boolean writeonly
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ String
constructor
A new instance of String.
- #null_terminated? ⇒ Boolean
-
#read(str) ⇒ String
Self.
- #to_human ⇒ String
- #to_s ⇒ String
- #unicode? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ String
Returns a new instance of String.
26 27 28 29 30 31 32 |
# File 'lib/packetgen/plugin/smb/string.rb', line 26 def initialize(={}) super @unicode = .key?(:unicode) ? [:unicode] : true @null_terminated = .key?(:null_terminated) ? [:null_terminated] : true encoding = unicode? ? 'UTF-16LE' : 'ASCII-8BIT' self.encode!(encoding) end |
Instance Attribute Details
#null_terminated=(value) ⇒ Boolean (writeonly)
18 19 20 |
# File 'lib/packetgen/plugin/smb/string.rb', line 18 def null_terminated=(value) @null_terminated = value end |
#unicode=(value) ⇒ Boolean (writeonly)
15 16 17 |
# File 'lib/packetgen/plugin/smb/string.rb', line 15 def unicode=(value) @unicode = value end |
Instance Method Details
#null_terminated? ⇒ Boolean
40 41 42 |
# File 'lib/packetgen/plugin/smb/string.rb', line 40 def null_terminated? @null_terminated end |
#read(str) ⇒ String
Returns self.
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_human ⇒ String
81 82 83 |
# File 'lib/packetgen/plugin/smb/string.rb', line 81 def to_human super.encode('UTF-8') end |
#to_s ⇒ String
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
35 36 37 |
# File 'lib/packetgen/plugin/smb/string.rb', line 35 def unicode? @unicode end |