Skip to content

Commit 5f197cc

Browse files
author
blackhedd
committed
fixed the implementation of Bignum#to_ber.
1 parent 0bf35f8 commit 5f197cc

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ end
7272

7373
desc "(Provisional) Run tests for SNMP"
7474
task :test_snmp do |t|
75-
run_test_set t, ['tests/testsnmp.rb']
75+
run_test_set t, ['tests/testsnmp.rb', 'tests/testber.rb']
7676
end
7777

7878
spec = eval(File.read("net-ldap.gemspec"))

lib/net/ber.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,22 @@ def to_ber_length_encoding
409409
class Bignum
410410

411411
def to_ber
412-
i = [self].pack('w')
413-
i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
414-
[2, i.length].pack("CC") + i
412+
#i = [self].pack('w')
413+
#i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
414+
#[2, i.length].pack("CC") + i
415+
416+
# Ruby represents Bignums as two's-complement numbers so we may actually be
417+
# good as far as representing negatives goes.
418+
# I'm sure this implementation can be improved performance-wise if necessary.
419+
sz = self.size
420+
out = "\000" * sz
421+
(sz*8).times {|bit|
422+
if self[bit] == 1
423+
out[bit/8] += (1 << (bit % 8))
424+
end
425+
}
426+
427+
[2, sz].pack("CC") + out.reverse
415428
end
416429

417430
end

tests/testber.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ def test_ber_integers
2323
assert_equal( "\002\005\222\320\227\344\000", 5000000000.to_ber )
2424
end
2525

26+
def test_ber_bignums
27+
# Some of these values are Fixnums and some are Bignums. Different BER code.
28+
[
29+
5,
30+
50,
31+
500,
32+
5000,
33+
50000,
34+
500000,
35+
5000000,
36+
50000000,
37+
500000000,
38+
1000000000,
39+
2000000000,
40+
3000000000,
41+
4000000000,
42+
5000000000
43+
].each {|val|
44+
assert_equal( val, val.to_ber.read_ber )
45+
}
46+
end
47+
2648
def test_ber_parsing
2749
assert_equal( 6, "\002\001\006".read_ber( Net::LDAP::AsnSyntax ))
2850
assert_equal( "testing", "\004\007testing".read_ber( Net::LDAP::AsnSyntax ))

0 commit comments

Comments
 (0)