From 11d8152d1b17908fe061f5abd702c4b6a603b039 Mon Sep 17 00:00:00 2001 From: blackhedd Date: Fri, 8 Dec 2006 15:47:23 +0000 Subject: [PATCH] re-implemented Fixnum#to_ber, which was wrongly implemented. The new version still doesn't fix representations of negative values. --- lib/net/ber.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/net/ber.rb b/lib/net/ber.rb index 3780008..27628f4 100644 --- a/lib/net/ber.rb +++ b/lib/net/ber.rb @@ -361,8 +361,23 @@ class Fixnum # to_ber # def to_ber - i = [self].pack('w') - [2, i.length].pack("CC") + i + # originally used pack("w") which is WRONG. + #i = [self].pack('w') + + # PLEASE optimize this code path. It's awfully ugly and probably slow. + # It also doesn't understand negative numbers yet. + raise Net::BER::BerError.new( "range error in fixnum" ) unless self > 0 + z = [self].pack("N") + zlen = if self < 0x80 + 1 + elsif self < 0x8000 + 2 + elsif self < 0x800000 + 3 + else + 4 + end + [2, zlen].pack("CC") + z[0-zlen,zlen] end #