Sha256: d969abf92092b66310f9d07ecb126a447d57b2c71137e5d4043290ea59e16eca

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

module Net
module NTLM

  class EncodeUtil
    if RUBY_VERSION == "1.8.7"
      require "kconv"

      # Decode a UTF16 string to a ASCII string
      # @param [String] str The string to convert
      def self.decode_utf16le(str)
        Kconv.kconv(swap16(str), Kconv::ASCII, Kconv::UTF16)
      end

      # Encodes a ASCII string to a UTF16 string
      # @param [String] str The string to convert
      def self.encode_utf16le(str)
        swap16(Kconv.kconv(str, Kconv::UTF16, Kconv::ASCII))
      end

      # Taggle the strings endianness between big/little and little/big
      # @param [String] str The string to swap the endianness on
      def self.swap16(str)
        str.unpack("v*").pack("n*")
      end
    else # Use native 1.9 string encoding functions

      # Decode a UTF16 string to a ASCII string
      # @param [String] str The string to convert
      def self.decode_utf16le(str)
        str.force_encoding(Encoding::UTF_16LE)
        str.encode(Encoding::UTF_8, Encoding::UTF_16LE).force_encoding('UTF-8')
      end

      # Encodes a ASCII string to a UTF16 string
      # @param [String] str The string to convert
      # @note This implementation may seem stupid but the problem is that UTF16-LE and UTF-8 are incompatiable
      #   encodings. This library uses string contatination to build the packet bytes. The end result is that
      #   you can either marshal the encodings elsewhere of simply know that each time you call encode_utf16le
      #   the function will convert the string bytes to UTF-16LE and note the encoding as UTF-8 so that byte
      #   concatination works seamlessly.
      def self.encode_utf16le(str)
        str = str.force_encoding('UTF-8') if [::Encoding::ASCII_8BIT,::Encoding::US_ASCII].include?(str.encoding)
        str.dup.force_encoding('UTF-8').encode(Encoding::UTF_16LE, Encoding::UTF_8).force_encoding('UTF-8')
      end
    end
  end

end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
vagrant-compose-yaml-0.1.3 vendor/bundle/ruby/2.2.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
vagrant-compose-yaml-0.1.2 vendor/bundle/ruby/2.2.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
vagrant-compose-yaml-0.1.1 vendor/bundle/ruby/2.2.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
vagrant-compose-yaml-0.1.0 vendor/bundle/ruby/2.2.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
vagrant-unbundled-1.8.5.2 vendor/bundle/ruby/2.3.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
vagrant-unbundled-1.8.5.1 vendor/bundle/ruby/2.3.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
vagrant-unbundled-1.8.4.2 vendor/bundle/ruby/2.3.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
vagrant-unbundled-1.8.4.1 vendor/bundle/ruby/2.3.0/gems/rubyntlm-0.6.0/lib/net/ntlm/encode_util.rb
rubyntlm-0.6.0 lib/net/ntlm/encode_util.rb