module Net::BER::Extensions::Integer
BER
extensions to the Integer
class, affecting Fixnum and Bignum objects.
Public Instance Methods
to_ber()
click to toggle source
to_ber_application(tag)
click to toggle source
Generate a BER-encoding for an application-defined INTEGER. Examples of such integers are SNMP’s Counter, Gauge, and TimeTick types.
# File lib/net/ber/core_ext/integer.rb, line 30 def to_ber_application(tag) [0x40 + tag].pack("C") + to_ber_internal end
to_ber_enumerated()
click to toggle source
Private Instance Methods
to_ber_internal()
click to toggle source
Used to BER-encode the length and content bytes of an Integer
. Callers must prepend the tag byte for the contained value.
# File lib/net/ber/core_ext/integer.rb, line 37 def to_ber_internal # Compute the byte length, accounting for negative values requiring two's # complement. size = 1 size += 1 until (((self < 0) ? ~self : self) >> (size * 8)).zero? # Padding for positive, negative values. See section 8.5 of ITU-T X.690: # http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf # For positive integers, if most significant bit in an octet is set to one, # pad the result (otherwise it's decoded as a negative value). if self > 0 && (self & (0x80 << (size - 1) * 8)) > 0 size += 1 end # And for negative integers, pad if the most significant bit in the octet # is not set to one (othwerise, it's decoded as positive value). if self < 0 && (self & (0x80 << (size - 1) * 8)) == 0 size += 1 end # Store the size of the Integer in the result result = [size] # Appends bytes to result, starting with higher orders first. Extraction # of bytes is done by right shifting the original Integer 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 end result.pack('C*') end