Sha256: 697600fe0ab9a35850191d6d8a27dfe62058da8ec02101250fe1de59bbe828c3

Contents?: true

Size: 1.76 KB

Versions: 18

Compression:

Stored size: 1.76 KB

Contents

# -*- ruby encoding: utf-8 -*-
##
# Ber extensions to the Fixnum class.
module Net::BER::Extensions::Fixnum
  ##
  # Converts the fixnum to BER format.
  def to_ber
    "\002#{to_ber_internal}"
  end

  ##
  # Converts the fixnum to BER enumerated format.
  def to_ber_enumerated
    "\012#{to_ber_internal}"
  end

  ##
  # Converts the fixnum to BER length encodining format.
  def to_ber_length_encoding
    if self <= 127
      [self].pack('C')
    else
      i = [self].pack('N').sub(/^[\0]+/,"")
      [0x80 + i.length].pack('C') + i
    end
  end

  ##
  # Generate a BER-encoding for an application-defined INTEGER. Examples of
  # such integers are SNMP's Counter, Gauge, and TimeTick types.
  def to_ber_application(tag)
    [0x40 + tag].pack("C") + to_ber_internal
  end

  ##
  # Used to BER-encode the length and content bytes of a Fixnum. Callers
  # must prepend the tag byte for the contained value.
  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 = Net::BER::MAX_FIXNUM_SIZE
    while 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
    end

    result.pack('C*')
  end
  private :to_ber_internal
end

Version data entries

18 entries across 18 versions & 7 rubygems

Version Path
net-ldap-0.10.0 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.9.0 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.8.0 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.7.0 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.6.1 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.6.0 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.5.1 lib/net/ber/core_ext/fixnum.rb
datacom-net-ldap-0.5.0.datacom lib/net/ber/core_ext/fixnum.rb
adams-net-ldap-0.4.0 lib/net/ber/core_ext/fixnum.rb
obis-net-ldap-0.4.0 lib/net/ber/core_ext/fixnum.rb
net-ldap-1-0.4.0 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.3.1 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.3.0 lib/net/ber/core_ext/fixnum.rb
prathe_net-ldap-0.2.20110317223538 lib/net/ber/core_ext/fixnum.rb
prathe-net-ldap-0.2.20110317223538 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.2.2 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.2.1 lib/net/ber/core_ext/fixnum.rb
net-ldap-0.2 lib/net/ber/core_ext/fixnum.rb