Class: RASN2::Types::Sequence

Inherits:
Constructed show all
Defined in:
lib/rasn2/types/sequence.rb,
lib/rasn2/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 = RASN2::Types::Sequence.new seq.value = [ RASN2::Types::Integer.new RASN2::Types::Integer.new(explicit: 0, optional: true), RASN2::Types::Integer.new(implicit: 1, default: 0) ]

A sequence may also be used without value to not parse sequence content: seq = RASN2::Types::Sequence.new(:seq) seq.parse!(der_string) seq.value # => String

Author:

  • Sylvain Daubert

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, #model, #name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Constructed

#can_build?

Methods inherited from Base

#==, #asn1_class_to_s, #attributes, #bin2hex, #build, #can_build?, #check_id, #colorize, #common_inspect, constrained?, #constructed?, #der2name, #do_parse, #do_parse_explicit, #do_parse_explicit_with_tracing, #do_parse_with_tracing, #encode_identifier_octets, #encode_size, encoded_type, #explicit?, #find_type, #get_data, #get_length, #id, #id2octets, #id_value, #implicit?, #indent, #inspect, #inspect_value, #msg_type, #optional?, parse, #parse!, #pc_bit, #primitive?, #private?, #raise_id_error, #raise_on_indefinite_length, #self2name, #set_class, #set_default, #set_optional, #set_tag, #set_value, #specific_initializer, #tag_id_to_integer, #tagged?, #to_der, #trace, #trace_real, #tracer, type, #type, #universal?, #unpack, #unsigned_to_chained_octets, #value, #value=, #value?, #value_size

Methods included from Helpers::Colorize

#begin_colorizer, #brace_surround, #colorize, #colorize_attribute, #colorize_bool, #colorize_class, #colorize_default, #colorize_enum, #colorize_id, #colorize_name, #colorize_nil, #colorize_value, #colorizer, #colorizer=, #end_colorizer, #int_with_hex, #length_specifier, #parens_hex

Constructor Details

#initialize(options = {}) ⇒ Sequence

Returns a new instance of Sequence.

See Also:



33
34
35
36
37
# File 'lib/rasn2/types/sequence.rb', line 33

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

Class Method Details

.start_tracingObject

Patch #der_to_value to add tracing ability



155
156
157
158
# File 'lib/rasn2/tracer.rb', line 155

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_tracingObject

Unpatch #der_to_value to remove tracing ability



162
163
164
# File 'lib/rasn2/tracer.rb', line 162

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

Parameters:

  • idx_or_name (Integer, String, Symbol)

Returns:

  • (Object, nil)


58
59
60
61
62
63
64
65
66
67
# File 'lib/rasn2/types/sequence.rb', line 58

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

Parameters:

  • der (String)
  • ber (::Boolean) (defaults to: false)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rasn2/types/sequence.rb', line 73

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



169
170
171
172
173
# File 'lib/rasn2/tracer.rb', line 169

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

#initialize_copyObject

Deep copy @value



40
41
42
43
44
45
46
47
48
# File 'lib/rasn2/types/sequence.rb', line 40

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

#void_valueArray

Returns:

  • (Array)


51
52
53
# File 'lib/rasn2/types/sequence.rb', line 51

def void_value
  []
end