2010-02-09 23:46:49 +01:00
|
|
|
module Net
|
|
|
|
class LDAP
|
|
|
|
module Extensions
|
|
|
|
module Fixnum
|
2010-02-12 14:39:57 +01:00
|
|
|
#
|
|
|
|
# to_ber
|
|
|
|
#
|
2010-02-09 23:46:49 +01:00
|
|
|
def to_ber
|
|
|
|
"\002" + to_ber_internal
|
|
|
|
end
|
|
|
|
|
2010-02-12 14:39:57 +01:00
|
|
|
#
|
|
|
|
# to_ber_enumerated
|
|
|
|
#
|
2010-02-09 23:46:49 +01:00
|
|
|
def to_ber_enumerated
|
|
|
|
"\012" + to_ber_internal
|
|
|
|
end
|
|
|
|
|
2010-02-12 14:39:57 +01:00
|
|
|
#
|
|
|
|
# to_ber_length_encoding
|
|
|
|
#
|
2010-02-09 23:46:49 +01:00
|
|
|
def to_ber_length_encoding
|
|
|
|
if self <= 127
|
|
|
|
[self].pack('C')
|
|
|
|
else
|
|
|
|
i = [self].pack('N').sub(/^[\0]+/,"")
|
|
|
|
[0x80 + i.length].pack('C') + i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate a BER-encoding for an application-defined INTEGER.
|
|
|
|
# Example: SNMP's Counter, Gauge, and TimeTick types.
|
|
|
|
#
|
|
|
|
def to_ber_application tag
|
|
|
|
[0x40 + tag].pack("C") + to_ber_internal
|
|
|
|
end
|
|
|
|
|
|
|
|
#--
|
|
|
|
# Called internally to BER-encode the length and content bytes of a Fixnum.
|
|
|
|
# The caller will prepend the tag byte.
|
2010-02-12 14:39:57 +01:00
|
|
|
MAX_SIZE = 0.size
|
2010-02-09 23:46:49 +01:00
|
|
|
def to_ber_internal
|
2010-02-12 14:39:57 +01:00
|
|
|
size = MAX_SIZE
|
|
|
|
while size>1
|
|
|
|
break if (self & (0xff << (size-1)*8)) > 0
|
|
|
|
size -= 1
|
|
|
|
end
|
|
|
|
|
|
|
|
result = [size]
|
|
|
|
|
|
|
|
while size>0
|
|
|
|
# right shift size-1 bytes, mask with 0xff
|
|
|
|
result << ((self >> ((size-1)*8)) & 0xff)
|
|
|
|
|
|
|
|
size -= 1
|
2010-02-09 23:46:49 +01:00
|
|
|
end
|
2010-02-12 14:39:57 +01:00
|
|
|
|
|
|
|
result.pack('C*')
|
2010-02-09 23:46:49 +01:00
|
|
|
end
|
|
|
|
private :to_ber_internal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|