File tree Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -42,17 +42,21 @@ def to_ber_internal
4242 # Looks for the first byte in the fixnum that is not all zeroes. It does
4343 # this by masking one byte after another, checking the result for bits
4444 # that are left on.
45- size = Net ::BER ::MAX_FIXNUM_SIZE
46- while size > 1
47- break if ( self & ( 0xff << ( size - 1 ) * 8 ) ) > 0
48- size -= 1
49- end
45+ val = ( self < 0 ) ? ~self : self
46+ size = 1
47+ size += 1 until ( val >> ( size * 8 ) ) . zero?
5048
5149 # for positive integers, if most significant bit in an octet is set to one,
5250 # pad the result (otherwise it's decoded as a negative value)
5351 # See section 8.5 of ITU-T X.690:
5452 # http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
55- if self > 0 && ( self & ( 0b10000000 << ( size - 1 ) * 8 ) ) > 0
53+ if self > 0 && ( self & ( 0x80 << ( size - 1 ) * 8 ) ) > 0
54+ size += 1
55+ end
56+
57+ # and for negative integers, pad if the most significant bit in the octet
58+ # is not set to one.
59+ if self < 0 && ( self & ( 0x80 << ( size - 1 ) * 8 ) ) == 0
5660 size += 1
5761 end
5862
Original file line number Diff line number Diff line change @@ -45,9 +45,9 @@ def test_false
4545 5_000_000_000 => "\x02 \x05 \x01 \x2a \x05 \xF2 \x00 " ,
4646
4747 # negatives
48- # -1 => "\x02\x01\xFF",
49- # -127 => "\x02\x01\x81",
50- # -128 => "\x02\x01\x80"
48+ -1 => "\x02 \x01 \xFF " ,
49+ -127 => "\x02 \x01 \x81 " ,
50+ -128 => "\x02 \x01 \x80 "
5151 } . each do |number , expected_encoding |
5252 define_method "test_encode_#{ number } " do
5353 assert_equal expected_encoding . b , number . to_ber
You can’t perform that action at this time.
0 commit comments