File tree Expand file tree Collapse file tree 16 files changed +177
-179
lines changed Expand file tree Collapse file tree 16 files changed +177
-179
lines changed Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ def initialize args
7777
7878 class BerIdentifiedArray < Array
7979 attr_accessor :ber_identifier
80- def initialize
80+ def initialize ( * args )
8181 super
8282 end
8383 end
@@ -88,16 +88,6 @@ def to_ber
8888 "\005 \000 "
8989 end
9090 end
91-
92- class BerIdentifiedOid
93- attr_accessor :ber_identifier
94- def initialize oid
95- if oid . is_a? ( String )
96- oid = oid . split ( /\. / ) . map { |s | s . to_i }
97- end
98- @value = oid
99- end
100- end
10191 end
10292end
10393
Original file line number Diff line number Diff line change @@ -33,19 +33,10 @@ def read_ber syntax=nil
3333 # packets coming from streams that don't block when
3434 # we ask for more data (like StringIOs). At it is,
3535 # this can throw TypeErrors and other nasties.
36-
37- # We might have been included in two kinds of things: IO-like
38- # (answering to getbyte) and String-like (answering to to_s). Have
39- # stream be a stream that is IO-like in both cases.
40- if respond_to? :getbyte
41- stream = self
42- else
43- stream = StringIO . new ( self . to_s )
44- end
45-
46- id = stream . getbyte or return nil # don't trash this value, we'll use it later
36+
37+ id = getbyte or return nil # don't trash this value, we'll use it later
4738
48- n = stream . getbyte
39+ n = getbyte
4940 lengthlength , contentlength = if n <= 127
5041 [ 1 , n ]
5142 else
@@ -56,7 +47,7 @@ def read_ber syntax=nil
5647 [ 1 + ( n & 127 ) , j ]
5748 end
5849
59- newobj = stream . read contentlength
50+ newobj = read contentlength
6051
6152 # This exceptionally clever and clear bit of code is verrrry slow.
6253 objtype = ( syntax && syntax [ id ] ) || BuiltinSyntax [ id ]
Original file line number Diff line number Diff line change @@ -10,8 +10,8 @@ class Array
1010end
1111
1212class String
13- include Net ::LDAP ::Extensions ::String
1413 include Net ::BER ::BERParser
14+ include Net ::LDAP ::Extensions ::String
1515end
1616
1717class Bignum
@@ -40,4 +40,4 @@ class StringIO
4040
4141class OpenSSL ::SSL ::SSLSocket
4242 include Net ::BER ::BERParser
43- end
43+ end
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ def to_ber_oid
3333
3434 private
3535 def to_ber_seq_internal code
36- s = self . to_s
36+ s = self . join
3737 [ code ] . pack ( 'C' ) + s . length . to_ber_length_encoding + s
3838 end
3939 end
Original file line number Diff line number Diff line change @@ -4,32 +4,19 @@ module Extensions
44 module Bignum
55
66 def to_ber
7- #i = [self].pack('w')
8- #i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
9- #[2, i.length].pack("CC") + i
7+ # NOTE: Array#pack's 'w' is a BER _compressed_ integer. We need uncompressed
8+ # BER integers, so we're not using that.
9+ # See also: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/228864
10+ result = [ ]
1011
11- # Ruby represents Bignums as two's-complement numbers so we may actually be
12- # good as far as representing negatives goes.
13- # I'm sure this implementation can be improved performance-wise if necessary.
14- # Ruby's Bignum#size returns the number of bytes in the internal representation
15- # of the number, but it can and will include leading zero bytes on at least
16- # some implementations. Evidently Ruby stores these as sets of quadbytes.
17- # It's not illegal in BER to encode all of the leading zeroes but let's strip
18- # them out anyway.
19- #
20- sz = self . size
21- out = "\000 " * sz
22- ( sz *8 ) . times { |bit |
23- if self [ bit ] == 1
24- out [ bit /8 ] += ( 1 << ( bit % 8 ) )
25- end
26- }
27-
28- while out . length > 1 and out [ -1 ] == 0
29- out . slice! ( -1 , 1 )
12+ n = self
13+ while n >0
14+ b = n & 0xff
15+ result << b
16+ n = n >> 8
3017 end
3118
32- [ 2 , out . length ] . pack ( "CC" ) + out . reverse
19+ " \002 " + ( [ result . size ] + result . reverse ) . pack ( 'C*' )
3320 end
3421
3522 end
Original file line number Diff line number Diff line change @@ -2,14 +2,23 @@ module Net
22 class LDAP
33 module Extensions
44 module Fixnum
5+ #
6+ # to_ber
7+ #
58 def to_ber
69 "\002 " + to_ber_internal
710 end
811
12+ #
13+ # to_ber_enumerated
14+ #
915 def to_ber_enumerated
1016 "\012 " + to_ber_internal
1117 end
1218
19+ #
20+ # to_ber_length_encoding
21+ #
1322 def to_ber_length_encoding
1423 if self <= 127
1524 [ self ] . pack ( 'C' )
@@ -29,21 +38,24 @@ def to_ber_application tag
2938 #--
3039 # Called internally to BER-encode the length and content bytes of a Fixnum.
3140 # The caller will prepend the tag byte.
41+ MAX_SIZE = 0 . size
3242 def to_ber_internal
33- # PLEASE optimize this code path. It's awfully ugly and probably slow.
34- # It also doesn't understand negative numbers yet.
35- raise Net ::BER ::BerError . new ( "range error in fixnum" ) unless self >= 0
36- z = [ self ] . pack ( "N" )
37- zlen = if self < 0x80
38- 1
39- elsif self < 0x8000
40- 2
41- elsif self < 0x800000
42- 3
43- else
44- 4
43+ size = MAX_SIZE
44+ while size >1
45+ break if ( self & ( 0xff << ( size -1 ) *8 ) ) > 0
46+ size -= 1
47+ end
48+
49+ result = [ size ]
50+
51+ while size >0
52+ # right shift size-1 bytes, mask with 0xff
53+ result << ( ( self >> ( ( size -1 ) *8 ) ) & 0xff )
54+
55+ size -= 1
4556 end
46- [ zlen ] . pack ( "C" ) + z [ 0 -zlen , zlen ]
57+
58+ result . pack ( 'C*' )
4759 end
4860 private :to_ber_internal
4961 end
Original file line number Diff line number Diff line change 1+ require 'stringio'
2+
13module Net
24 class LDAP
35 module Extensions
@@ -29,18 +31,9 @@ def to_ber_contextspecific code
2931 end
3032
3133 def read_ber syntax = nil
32- StringIO . new ( self ) . read_ber ( syntax )
34+ StringIO . new ( self ) .
35+ read_ber ( syntax )
3336 end
34-
35- # def read_ber! syntax=nil
36- # obj,n_consumed = read_ber_from_string(self, syntax)
37- # if n_consumed
38- # self.slice!(0...n_consumed)
39- # obj
40- # else
41- # nil
42- # end
43- # end
4437 end
4538 end
4639 end
Original file line number Diff line number Diff line change 1- # $Id$
2- #
3- #
4- #----------------------------------------------------------------------------
5- #
61# Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
72#
83# Gmail: garbagecat10
2520#
2621#
2722
23+ require 'strscan'
2824
2925module Net
3026class LDAP
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ def generate( type, str )
4343 when :md5
4444 [ Digest ::MD5 . new , 'MD5' ]
4545 when :sha
46- [ Digest ::SHA1 . new , 'sha ' ]
46+ [ Digest ::SHA1 . new , 'SHA ' ]
4747 # when ssha
4848 else
4949 raise Net ::LDAP ::LdapError . new ( "unsupported password-hash type (#{ type } )" )
Original file line number Diff line number Diff line change 1+ require 'spec_helper'
2+
3+ require 'socket'
4+ require 'openssl'
5+
6+ require 'net/ldap'
7+
8+ describe "BER serialisation (SSL)" do
9+ # Transmits str to #to and reads it back from #from.
10+ #
11+ def transmit ( str )
12+ to . write ( str )
13+ to . close
14+
15+ from . read
16+ end
17+
18+ attr_reader :to , :from
19+ before ( :each ) do
20+ @from , @to = IO . pipe
21+
22+ @to = Net ::LDAP ::SSLSocket . wrap ( to )
23+ @from = Net ::LDAP ::SSLSocket . wrap ( from )
24+ end
25+
26+ it "should transmit strings" do
27+ transmit ( 'foo' ) . should == 'foo'
28+ end
29+ it "should correctly transmit numbers" do
30+ to . write 1234 . to_ber
31+ from . read_ber . should == 1234
32+ end
33+ end
You can’t perform that action at this time.
0 commit comments