Sha256: d6cfddfe09d00342845296bdf9127fe66a0c5f9b5d085f5a5a675af097f1aa52

Contents?: true

Size: 1.46 KB

Versions: 11

Compression:

Stored size: 1.46 KB

Contents

module RubySMB
  module Field
    # Represents a NULL-Terminated String in UTF-16
    class Stringz16 < BinData::Stringz

      def assign(val)
        super(binary_string(val.encode("utf-16le")))
      end

      def snapshot
        # override to always remove trailing zero bytes
        result = _value
        result
        result = trim_and_zero_terminate(result)
        result.chomp("\0\0").force_encoding("utf-16le")
      end

      private

      def append_zero_byte_if_needed!(str)
        if str.length == 0 || !(str.end_with?("\0\0"))
          str << "\0\0"
        end
      end

      # Override parent on {BinData::Stringz} to use
      # a double NULL-byte instead of a single NULL-byte
      # as a terminator
      # @see BinData::Stringz
      def read_and_return_value(io)
        max_length = eval_parameter(:max_length)
        str = ''
        i = 0
        ch = nil

        # read until double NULL-byte or we have read in the max number of bytes
        while (ch != "\0\0") && (i != max_length)
          ch = io.readbytes(2)
          str << ch
          i += 2
        end

        trim_and_zero_terminate(str)
      end

      # Override parent method of #truncate_after_first_zero_byte! on
      # {BinData::Stringz} to use two consecutive NULL-bytes as the terimnator
      # instead of a single NULL-nyte.
      # @see BinData::Stringz
      def truncate_after_first_zero_byte!(str)
        str.sub!(/([^\0]*\0\0\0).*/, '\1')
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ruby_smb-0.0.18 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.17 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.16 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.15 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.14 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.13 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.12 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.11 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.10 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.9 lib/ruby_smb/field/stringz16.rb
ruby_smb-0.0.8 lib/ruby_smb/field/stringz16.rb