+ Adds comments (and some layouting)

This commit is contained in:
Kaspar Schiess 2010-03-18 09:28:27 +01:00
parent 31ba47cf1d
commit c01dc9ee89
3 changed files with 56 additions and 48 deletions

View file

@ -4,9 +4,9 @@ module Net
module Bignum
def to_ber
# NOTE: Array#pack's 'w' is a BER _compressed_ integer. We need uncompressed
# BER integers, so we're not using that.
# See also: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/228864
# NOTE: Array#pack's 'w' is a BER _compressed_ integer. We need
# uncompressed BER integers, so we're not using that. See also:
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/228864
result = []
n = self

View file

@ -36,23 +36,33 @@ module Net
end
#--
# Called internally to BER-encode the length and content bytes of a Fixnum.
# The caller will prepend the tag byte.
# Called internally to BER-encode the length and content bytes of a
# Fixnum. The caller will prepend the tag byte.
#
MAX_SIZE = 0.size
def to_ber_internal
# CAUTION: Bit twiddling ahead. You might want to shield your eyes
# or something.
# Looks for the first byte in the fixnum that is not all zeroes. It
# does this by masking one byte after another, checking the result
# for bits that are left on.
size = MAX_SIZE
while size>1
break if (self & (0xff << (size-1)*8)) > 0
size -= 1
break if (self & (0xff << (size-1)*8)) > 0
size -= 1
end
# Store the size of the fixnum in the result
result = [size]
# Appends bytes to result, starting with higher orders first.
# Extraction of bytes is done by right shifting the original fixnum
# by an amount and then masking that with 0xff.
while size>0
# right shift size-1 bytes, mask with 0xff
result << ((self >> ((size-1)*8)) & 0xff)
size -= 1
# right shift size-1 bytes, mask with 0xff
result << ((self >> ((size-1)*8)) & 0xff)
size -= 1
end
result.pack('C*')