Skip to content

Commit 8d53891

Browse files
committed
Consolidate Fixnum, Bignum ext into Integer
1 parent c56897f commit 8d53891

File tree

3 files changed

+20
-53
lines changed

3 files changed

+20
-53
lines changed

lib/net/ber/core_ext.rb

+3-10
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,10 @@ class Array
3333
end
3434
# :startdoc:
3535

36-
require 'net/ber/core_ext/bignum'
36+
require 'net/ber/core_ext/integer'
3737
# :stopdoc:
38-
class Bignum
39-
include Net::BER::Extensions::Bignum
40-
end
41-
# :startdoc:
42-
43-
require 'net/ber/core_ext/fixnum'
44-
# :stopdoc:
45-
class Fixnum
46-
include Net::BER::Extensions::Fixnum
38+
class Integer
39+
include Net::BER::Extensions::Integer
4740
end
4841
# :startdoc:
4942

lib/net/ber/core_ext/bignum.rb

-22
This file was deleted.

lib/net/ber/core_ext/fixnum.rb renamed to lib/net/ber/core_ext/integer.rb

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# -*- ruby encoding: utf-8 -*-
22
##
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
55
##
6-
# Converts the fixnum to BER format.
6+
# Converts the Integer to BER format.
77
def to_ber
88
"\002#{to_ber_internal}"
99
end
1010

1111
##
12-
# Converts the fixnum to BER enumerated format.
12+
# Converts the Integer to BER enumerated format.
1313
def to_ber_enumerated
1414
"\012#{to_ber_internal}"
1515
end
1616

1717
##
18-
# Converts the fixnum to BER length encoding format.
18+
# Converts the Integer to BER length encoding format.
1919
def to_ber_length_encoding
2020
if self <= 127
2121
[self].pack('C')
@@ -33,38 +33,34 @@ def to_ber_application(tag)
3333
end
3434

3535
##
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
3737
# must prepend the tag byte for the contained value.
3838
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.
4641
size = 1
47-
size += 1 until (val >> (size * 8)).zero?
42+
size += 1 until (((self < 0) ? ~self : self) >> (size * 8)).zero?
4843

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:
5245
# 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).
5349
if self > 0 && (self & (0x80 << (size - 1) * 8)) > 0
5450
size += 1
5551
end
5652

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).
5955
if self < 0 && (self & (0x80 << (size - 1) * 8)) == 0
6056
size += 1
6157
end
6258

63-
# Store the size of the fixnum in the result
59+
# Store the size of the Integer in the result
6460
result = [size]
6561

6662
# 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
6864
# and then masking that with 0xff.
6965
while size > 0
7066
# right shift size - 1 bytes, mask with 0xff

0 commit comments

Comments
 (0)