Class: RASN1::Types::Any

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

Overview

ASN.1 ANY: accepts any types

ANY is often used with an ObjectId to detect real type.

On encoding, ANY is replaced by its values (which must be an RASN1 type). If ‘any#value` is `nil` and Any object is not Base#optional?, `any` will be encoded as a Null object.

Examples:

Parsing with any model

class AnyModel < RASN1::Model
  sequence :seq,
           content: [objectid(:id), any(:data)]
end
model = AnyModel.new
model.parse!("\x30\x09\x06\x03\x2a\x03\x04\x02\x02\x01\xff")
model[:id].value    #=> "1.2.3.4"
model[:data].value  #=> "\x02\x02\x01\xff"
# As we knwon object with id 1.2.3.4 is an integer, with then parse data as such
data = RASN1::Types::Integer.new
data.parse!(model[:data].value)
data.value          #=> 511

Constant Summary

Constants inherited from Base

Base::CLASSES, Base::CLASS_MASK, Base::INDEFINITE_LENGTH, Base::MULTI_OCTETS_ID

Instance Attribute Summary

Attributes inherited from Base

#asn1_class, #default, #name, #options

Instance Method Summary collapse

Methods inherited from Base

#==, constrained?, #constructed?, #der_to_value, #do_parse_explicit, #do_parse_explicit_with_tracing, #do_parse_with_tracing, encoded_type, #explicit?, #id, #implicit?, #initialize, #initialize_copy, #optional?, parse, #primitive?, #specific_initializer, start_tracing, stop_tracing, #tagged?, #type, type, #value, #value=, #value?, #value_size, #void_value

Constructor Details

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

Instance Method Details

#can_build?Boolean

Returns:



48
49
50
# File 'lib/rasn1/types/any.rb', line 48

def can_build?
  value? || !optional?
end

#inspect(level = 0) ⇒ String

Parameters:

  • level (::Integer) (defaults to: 0)

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rasn1/types/any.rb', line 64

def inspect(level=0)
  str = common_inspect(level)
  str << if !value?
           'NULL'
         elsif @value.is_a?(OctetString) || @value.is_a?(BitString)
           "#{@value.type}: #{value.value.inspect}"
         elsif @value.class < Base
           "#{@value.type}: #{value.value}"
         else
           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



57
58
59
60
# File 'lib/rasn1/types/any.rb', line 57

def parse!(der, ber: false)
  total_length, _data = do_parse(der, ber: ber)
  total_length
end

#to_derString

Returns DER-formated string.

Returns:

  • (String)

    DER-formated string



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

def to_der
  if value?
    case @value
    when Base, Model
      @value.to_der
    else
      @value.to_s
    end
  else
    optional? ? '' : Null.new.to_der
  end
end

#traceString

Returns:

  • (String)


79
80
81
82
83
# File 'lib/rasn1/types/any.rb', line 79

def trace
  return trace_any if value?

  msg_type(no_id: true) << ' NONE'
end