1
1
# -*- ruby encoding: utf-8 -*-
2
2
##
3
- # Ber extensions to the Fixnum class.
4
- module Net ::BER ::Extensions ::Fixnum
3
+ # BER extensions to the Integer class, affecting Fixnum and Bignum objects .
4
+ module Net ::BER ::Extensions ::Integer
5
5
##
6
- # Converts the fixnum to BER format.
6
+ # Converts the Integer to BER format.
7
7
def to_ber
8
8
"\002 #{ to_ber_internal } "
9
9
end
10
10
11
11
##
12
- # Converts the fixnum to BER enumerated format.
12
+ # Converts the Integer to BER enumerated format.
13
13
def to_ber_enumerated
14
14
"\012 #{ to_ber_internal } "
15
15
end
16
16
17
17
##
18
- # Converts the fixnum to BER length encoding format.
18
+ # Converts the Integer to BER length encoding format.
19
19
def to_ber_length_encoding
20
20
if self <= 127
21
21
[ self ] . pack ( 'C' )
@@ -33,38 +33,34 @@ def to_ber_application(tag)
33
33
end
34
34
35
35
##
36
- # Used to BER-encode the length and content bytes of a Fixnum . Callers
36
+ # Used to BER-encode the length and content bytes of an Integer . Callers
37
37
# must prepend the tag byte for the contained value.
38
38
def to_ber_internal
39
- # CAUTION: Bit twiddling ahead. You might want to shield your eyes or
40
- # something.
41
-
42
- # Looks for the first byte in the fixnum that is not all zeroes. It does
43
- # this by masking one byte after another, checking the result for bits
44
- # that are left on.
45
- val = ( self < 0 ) ? ~self : self
39
+ # Compute the byte length, accounting for negative values requiring two's
40
+ # complement.
46
41
size = 1
47
- size += 1 until ( val >> ( size * 8 ) ) . zero?
42
+ size += 1 until ( ( ( self < 0 ) ? ~ self : self ) >> ( size * 8 ) ) . zero?
48
43
49
- # for positive integers, if most significant bit in an octet is set to one,
50
- # pad the result (otherwise it's decoded as a negative value)
51
- # See section 8.5 of ITU-T X.690:
44
+ # Padding for positive, negative values. See section 8.5 of ITU-T X.690:
52
45
# http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
46
+
47
+ # For positive integers, if most significant bit in an octet is set to one,
48
+ # pad the result (otherwise it's decoded as a negative value).
53
49
if self > 0 && ( self & ( 0x80 << ( size - 1 ) * 8 ) ) > 0
54
50
size += 1
55
51
end
56
52
57
- # and for negative integers, pad if the most significant bit in the octet
58
- # is not set to one.
53
+ # And for negative integers, pad if the most significant bit in the octet
54
+ # is not set to one (othwerise, it's decoded as positive value) .
59
55
if self < 0 && ( self & ( 0x80 << ( size - 1 ) * 8 ) ) == 0
60
56
size += 1
61
57
end
62
58
63
- # Store the size of the fixnum in the result
59
+ # Store the size of the Integer in the result
64
60
result = [ size ]
65
61
66
62
# Appends bytes to result, starting with higher orders first. Extraction
67
- # of bytes is done by right shifting the original fixnum by an amount
63
+ # of bytes is done by right shifting the original Integer by an amount
68
64
# and then masking that with 0xff.
69
65
while size > 0
70
66
# right shift size - 1 bytes, mask with 0xff
0 commit comments