LDAP uses UTF-8 strings

This commit is contained in:
Daniel Abrahamsson 2011-09-09 18:47:39 +02:00
parent 7dd6c3a107
commit 3a8b8a2e00
3 changed files with 31 additions and 2 deletions

View file

@ -295,6 +295,8 @@ class Net::BER::BerIdentifiedString < String
attr_accessor :ber_identifier
def initialize args
super args
# LDAP uses UTF-8 encoded strings
force_encoding('UTF-8') if respond_to?(:encoding)
end
end

View file

@ -12,9 +12,21 @@ module Net::BER::Extensions::String
# User code should call either #to_ber_application_string or
# #to_ber_contextspecific.
def to_ber(code = 0x04)
[code].pack('C') + length.to_ber_length_encoding + self
raw_string = raw_utf8_encoded
[code].pack('C') + raw_string.length.to_ber_length_encoding + raw_string
end
def raw_utf8_encoded
if self.respond_to?(:encode)
# Strings should be UTF-8 encoded according to LDAP.
# However, the BER code is not necessarily valid UTF-8
self.encode('UTF-8').force_encoding('ASCII-8BIT')
else
self
end
end
private :raw_utf8_encoded
##
# Creates an application-specific BER string encoded value with the
# provided syntax code value.