class RASN1::Types::Any

ASN.1 ANY: accepts any types

If `any#value` is `nil` and Any object is not {#optional?}, `any` will be encoded as a {Null} object. @author Sylvain Daubert

Public Instance Methods

inspect(level=0) click to toggle source
# File lib/rasn1/types/any.rb, line 43
def inspect(level=0)
  lvl = level >= 0 ? level : 0
  str = '  ' * lvl
  str << "#{@name} " unless @name.nil?
  str << if @value.nil?
           '(ANY) NULL'
         elsif @value.is_a?(OctetString) || @value.is_a?(BitString)
           "(ANY) #{@value.type}: #{value.value.inspect}"
         elsif @value.class < Base
           "(ANY) #{@value.type}: #{value.value}"
         else
           "ANY: #{value.to_s.inspect}"
         end
end
parse!(der, ber: false) click to toggle source

Parse a DER string. This method updates object: {#value} will be a DER string. @param [String] der DER string @param [Boolean] ber if true, accept BER encoding @return [Integer] total number of parsed bytes

# File lib/rasn1/types/any.rb, line 27
def parse!(der, ber: false)
  if der.nil? || der.empty?
    return 0 if optional?

    raise ASN1Error, 'Expected ANY but get nothing'
  end

  id_size = Types.decode_identifier_octets(der).last
  total_length, = get_data(der[id_size..-1], ber)
  total_length += id_size

  @value = der[0, total_length]

  total_length
end
to_der() click to toggle source

@return [String] DER-formated string

# File lib/rasn1/types/any.rb, line 11
def to_der
  case @value
  when Base, Model
    @value.to_der
  when nil
    optional? ? '' : Null.new.to_der
  else
    @value.to_s
  end
end