Sha256: 0fc94571c04e91090ffc11bd12e890876bdb2ff2b3dbea19dd18002b1b11b161

Contents?: true

Size: 1.41 KB

Versions: 60

Compression:

Stored size: 1.41 KB

Contents

module RubySMB
  module Nbss
    # Representation of the NetBIOS Name as defined in
    # [4.1. NAME FORMAT](https://tools.ietf.org/html/rfc1002#section-4.1) and
    # [14. REPRESENTATION OF NETBIOS NAMES](https://tools.ietf.org/html/rfc1001#section-14) and
    # [Domain name representation and compression](https://tools.ietf.org/html/rfc883#page-31)
    class NetbiosName < BinData::Primitive
      endian :big

      bit1   :flag1, initial_value: 0
      bit1   :flag2, initial_value: 0
      bit6   :label_length
      string :label, read_length: -> { label_length }
      string :null_label, read_length: 1, value: "\x00"

      def nb_name_encode(name)
        encoded_name = ''
        name.each_byte do |char|
          first_half = (char >> 4) + 'A'.ord
          second_half = (char & 0xF) + 'A'.ord
          encoded_name << first_half.chr
          encoded_name << second_half.chr
        end
        encoded_name
      end

      def nb_name_decode(encoded_name)
        name = encoded_name.scan(/../).map do |char_pair|
          first_half = char_pair[0];
          second_half = char_pair[1]
          char = ((first_half.ord - 'A'.ord) << 4) + (second_half.ord - 'A'.ord)
          char.chr
        end
        name.join
      end

      def get
        nb_name_decode(label)
      end

      def set(label)
        self.label = nb_name_encode(label)
        self.label_length = self.label.length
      end
    end

  end
end

Version data entries

60 entries across 60 versions & 1 rubygems

Version Path
ruby_smb-3.2.1 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.2.0 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.7 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.6 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.5 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.4 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.3 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.2 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.1 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.1.0 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.0.6 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.0.5 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.0.4 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.0.3 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.0.2 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.0.1 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-3.0.0 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-2.0.13 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-2.0.12 lib/ruby_smb/nbss/netbios_name.rb
ruby_smb-2.0.11 lib/ruby_smb/nbss/netbios_name.rb