Class: RASN1::Types::Any

Inherits:
Base
  • Object
show all
Defined in:
lib/rasn1/types/any.rb

Overview

ASN.1 ANY: accepts any types

If ‘any#value` is `nil`, `any` will be encoded as a Null object.

Author:

  • Sylvain Daubert

Constant Summary

Constants inherited from Base

Base::CLASSES, Base::INDEFINITE_LENGTH, Base::MAX_TAG

Instance Attribute Summary

Attributes inherited from Base

#asn1_class, #default, #name, #value

Instance Method Summary collapse

Methods inherited from Base

#==, #constructed?, encode_type, #explicit?, #implicit?, #initialize, #initialize_copy, #optional?, parse, #primitive?, #tag, #tagged?, type, #type, #value_size

Constructor Details

This class inherits a constructor from RASN1::Types::Base

Instance Method Details

#inspect(level = 0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rasn1/types/any.rb', line 33

def inspect(level=0)
  str = ''
  str << '  ' * level if level > 0
  str << "#{@name} " unless @name.nil?
  if @value.nil?
    str << "(ANY) NULL"
  elsif @value.is_a?(OctetString) or @value.is_a?(BitString)
    str << "(ANY) #{@value.type}: #{value.value.inspect}"
  elsif @value.class < Base
    str << "(ANY) #{@value.type}: #{value.value}"
  else
    str << "ANY: #{value.to_s.inspect}"
  end
end

#parse!(der, ber: false) ⇒ Integer

Parse a DER string. This method updates object: Base#value will be a DER string.

Parameters:

  • der (String)

    DER string

  • ber (Boolean) (defaults to: false)

    if true, accept BER encoding

Returns:

  • (Integer)

    total number of parsed bytes



27
28
29
30
31
# File 'lib/rasn1/types/any.rb', line 27

def parse!(der, ber: false)
  total_length,  = get_data(der, ber)
  @value = der[0, total_length]
  total_length
end

#to_derString

Returns DER-formated string.

Returns:

  • (String)

    DER-formated string



11
12
13
14
15
16
17
18
19
20
# File 'lib/rasn1/types/any.rb', line 11

def to_der
  case @value
  when Base, Model
    @value.to_der
  when nil
    Null.new.to_der
  else
    @value.to_s
  end
end