Class: RASN1::Types::Sequence

Inherits:
Constructed show all
Defined in:
lib/rasn1/types/sequence.rb,
lib/rasn1/tracer.rb

Overview

ASN.1 sequence

A sequence is a collection of another ASN.1 types.

To encode this ASN.1 example:

Record ::= SEQUENCE {
  id        INTEGER,
  room  [0] INTEGER OPTIONAL,
  house [1] IMPLICIT INTEGER DEFAULT 0
}

do:

seq = RASN1::Types::Sequence.new
seq.value = [
             RASN1::Types::Integer.new
             RASN1::Types::Integer.new(explicit: 0, optional: true),
             RASN1::Types::Integer.new(implicit: 1, default: 0)
            ]

A sequence may also be used without value to not parse sequence content:

seq = RASN1::Types::Sequence.new(:seq)
seq.parse!(der_string)
seq.value    # => String

Direct Known Subclasses

Set

Constant Summary collapse

ID =

Sequence id value

0x10

Constants inherited from Constructed

Constructed::ASN1_PC

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Constructed

#can_build?, #inspect

Methods inherited from Base

#==, #can_build?, constrained?, #constructed?, #do_parse, #do_parse_explicit, #do_parse_explicit_with_tracing, #do_parse_with_tracing, encoded_type, #explicit?, #id, #implicit?, #inspect, #optional?, parse, #parse!, #primitive?, #specific_initializer, #tagged?, #to_der, #trace, type, #type, #value, #value=, #value?, #value_size

Constructor Details

#initialize(options = {}) ⇒ Sequence

Returns a new instance of Sequence.

Options Hash (options):

  • :class (Symbol)

    ASN.1 class. Default value is :universal. If :explicit or :implicit: is defined, default value is :context.

  • :optional (::Boolean)

    define this tag as optional. Default is false

  • :default (Object)

    default value (ASN.1 DEFAULT)

  • :value (Object)

    value to set

  • :implicit (::Integer)

    define an IMPLICIT tagged type

  • :explicit (::Integer)

    define an EXPLICIT tagged type

  • :constructed (::Boolean)

    if true, set type as constructed. May only be used when :explicit is defined, else it is discarded.

  • :name (::String)

    name for this node

See Also:



40
41
42
43
44
# File 'lib/rasn1/types/sequence.rb', line 40

def initialize(options={})
  super
  @no_value = false
  @value ||= []
end

Class Method Details

.start_tracingvoid

This method returns an undefined value.

Patch #der_to_value to add tracing ability



149
150
151
152
# File 'lib/rasn1/tracer.rb', line 149

def start_tracing
  alias_method :der_to_value_without_tracing, :der_to_value
  alias_method :der_to_value, :der_to_value_with_tracing
end

.stop_tracingvoid

This method returns an undefined value.

Unpatch #der_to_value to remove tracing ability



157
158
159
# File 'lib/rasn1/tracer.rb', line 157

def stop_tracing
  alias_method :der_to_value, :der_to_value_without_tracing
end

Instance Method Details

#[](idx_or_name) ⇒ Object?

Get element at index idx, or element of name name



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

def [](idx_or_name)
  return unless @value.is_a?(Array)

  case idx_or_name
  when ::Integer
    @value[idx_or_name.to_i]
  when String, Symbol
    @value.find { |elt| elt.name == idx_or_name }
  end
end

#der_to_value(der, ber: false) ⇒ void

This method returns an undefined value.

Make sequence value from der string



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rasn1/types/sequence.rb', line 80

def der_to_value(der, ber: false) # rubocop:disable Lint/UnusedMethodArgument
  if @value.is_a?(Array) && !@value.empty?
    nb_bytes = 0
    @value.each do |element|
      nb_bytes += element.parse!(der[nb_bytes..])
    end
  else
    @value = der
    der.length
  end
end

#der_to_value_with_tracing(der, ber: false) ⇒ Object

der_to_value der with tracing abillity



165
166
167
168
169
# File 'lib/rasn1/tracer.rb', line 165

def der_to_value_with_tracing(der, ber: false)
  RASN1.tracer.tracing_level += 1
  der_to_value_without_tracing(der, ber: ber)
  RASN1.tracer.tracing_level -= 1
end

#initialize_copyObject

Deep copy @value



47
48
49
50
51
52
53
54
55
# File 'lib/rasn1/types/sequence.rb', line 47

def initialize_copy(*)
  super
  @value = case @value
           when Array
             @value.map(&:dup)
           else
             @value.dup
           end
end

#void_valueArray



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

def void_value
  []
end