Moved core extensions to Net::BER. Documented.

This commit is contained in:
Austin Ziegler 2010-03-20 00:18:10 -04:00 committed by Kaspar Schiess
parent e31af4bead
commit c913bc6fb9
17 changed files with 310 additions and 248 deletions

View file

@ -1,43 +0,0 @@
require 'net/ldap/core_ext/array'
require 'net/ldap/core_ext/string'
require 'net/ldap/core_ext/bignum'
require 'net/ldap/core_ext/fixnum'
require 'net/ldap/core_ext/false_class'
require 'net/ldap/core_ext/true_class'
class Array
include Net::LDAP::Extensions::Array
end
class String
include Net::BER::BERParser
include Net::LDAP::Extensions::String
end
class Bignum
include Net::LDAP::Extensions::Bignum
end
class Fixnum
include Net::LDAP::Extensions::Fixnum
end
class FalseClass
include Net::LDAP::Extensions::FalseClass
end
class TrueClass
include Net::LDAP::Extensions::TrueClass
end
class IO
include Net::BER::BERParser
end
class StringIO
include Net::BER::BERParser
end
class OpenSSL::SSL::SSLSocket
include Net::BER::BERParser
end

View file

@ -1,42 +0,0 @@
module Net
class LDAP
module Extensions
module Array
#
# to_ber_appsequence
# An application-specific sequence usually gets assigned
# a tag that is meaningful to the particular protocol being used.
# This is different from the universal sequence, which usually
# gets a tag value of 16.
# Now here's an interesting thing: We're adding the X.690
# "application constructed" code at the top of the tag byte (0x60),
# but some clients, notably ldapsearch, send "context-specific
# constructed" (0xA0). The latter would appear to violate RFC-1777,
# but what do I know? We may need to change this.
#
def to_ber id = 0; to_ber_seq_internal( 0x30 + id ); end
def to_ber_set id = 0; to_ber_seq_internal( 0x31 + id ); end
def to_ber_sequence id = 0; to_ber_seq_internal( 0x30 + id ); end
def to_ber_appsequence id = 0; to_ber_seq_internal( 0x60 + id ); end
def to_ber_contextspecific id = 0; to_ber_seq_internal( 0xA0 + id ); end
def to_ber_oid
ary = self.dup
first = ary.shift
raise Net::BER::BerError.new( "invalid OID" ) unless [0,1,2].include?(first)
first = first * 40 + ary.shift
ary.unshift first
oid = ary.pack("w*")
[6, oid.length].pack("CC") + oid
end
private
def to_ber_seq_internal code
s = self.join
[code].pack('C') + s.length.to_ber_length_encoding + s
end
end
end
end
end # class Array

View file

@ -1,25 +0,0 @@
module Net
class LDAP
module Extensions
module Bignum
def to_ber
# NOTE: Array#pack's 'w' is a BER _compressed_ integer. We need
# uncompressed BER integers, so we're not using that. See also:
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/228864
result = []
n = self
while n>0
b = n & 0xff
result << b
n = n >> 8
end
"\002" + ([result.size] + result.reverse).pack('C*')
end
end
end
end
end

View file

@ -1,11 +0,0 @@
module Net
class LDAP
module Extensions
module FalseClass
def to_ber
"\001\001\000"
end
end
end
end
end

View file

@ -1,74 +0,0 @@
module Net
class LDAP
module Extensions
module Fixnum
#
# to_ber
#
def to_ber
"\002" + to_ber_internal
end
#
# to_ber_enumerated
#
def to_ber_enumerated
"\012" + to_ber_internal
end
#
# to_ber_length_encoding
#
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.
#
MAX_SIZE = 0.size
def to_ber_internal
# CAUTION: Bit twiddling ahead. You might want to shield your eyes
# or something.
# Looks for the first byte in the fixnum that is not all zeroes. It
# does this by masking one byte after another, checking the result
# for bits that are left on.
size = MAX_SIZE
while size>1
break if (self & (0xff << (size-1)*8)) > 0
size -= 1
end
# Store the size of the fixnum in the result
result = [size]
# Appends bytes to result, starting with higher orders first.
# Extraction of bytes is done by right shifting the original fixnum
# by an amount and then masking that with 0xff.
while size>0
# right shift size-1 bytes, mask with 0xff
result << ((self >> ((size-1)*8)) & 0xff)
size -= 1
end
result.pack('C*')
end
private :to_ber_internal
end
end
end
end

View file

@ -1,40 +0,0 @@
require 'stringio'
module Net
class LDAP
module Extensions
module String
#
# to_ber
# A universal octet-string is tag number 4,
# but others are possible depending on the context, so we
# let the caller give us one.
# The preferred way to do this in user code is via to_ber_application_sring
# and to_ber_contextspecific.
#
def to_ber code = 4
[code].pack('C') + length.to_ber_length_encoding + self
end
#
# to_ber_application_string
#
def to_ber_application_string code
to_ber( 0x40 + code )
end
#
# to_ber_contextspecific
#
def to_ber_contextspecific code
to_ber( 0x80 + code )
end
def read_ber syntax=nil
StringIO.new(self).
read_ber(syntax)
end
end
end
end
end

View file

@ -1,11 +0,0 @@
module Net
class LDAP
module Extensions
module TrueClass
def to_ber
"\001\001\001"
end
end
end
end
end