Class: RASN1::Types::SequenceOf

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

Overview

ASN.1 SEQUENCE OF

A SEQUENCE OF is an array of one ASN.1 type.

Use with Primitive types

To encode this ASN.1 example:

Integers ::= SEQUENCE OF INTEGER

do:

# Create a SEQUENCE OF INTEGER
seqof = RASN1::Types::SequenceOf.new(RASN1::Types::Integer)
# Set integer values
seqof.value = [1, 2, 3, 4].map { |i| RASN1::Types::Integer.new(value: i) }
seqof << 5   # infer a RASN1::Types::Integer with given value

Use with Constructed types

SEQUENCE OF may be used to create sequence of composed types. For example:

composed_type = RASN1::Types::Sequence.new
commposed_type.value = [RASN1::Types::Integer,
                        RASN1::Types::OctetString]
seqof = RASN1::Types::SequenceOf.new(composed_type)
seqof << [0, 'data0']
seqof << [1, 'data1']

Use with Model

SEQUENCE OF may also be used with a Model type:

class MyModel < RASN1::Model
  sequence :seq,
           content: [boolean(:bool), integer(:int)]
end

seqof = RASN1::Types::SequenceOf.new(:record, MyModel)
# set values
seqof << { bool: true, int: 12 }
seqof << { bool: false, int: 65535 }
# Generate DER string
der = seqof.to_der    # => String
# parse
seqof.parse! der

After parsing, a SEQUENCE OF may be accessed as an Array:

seqof[0]                # => MyModel
seqof[0][:bool].value   # => true
seqof[0][:int].value    # => 12

Author:

  • Sylvain Daubert

Direct Known Subclasses

SetOf

Constant Summary collapse

ID =

SequenceOf id value

Sequence::ID

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 collapse

Attributes inherited from Base

#asn1_class, #default, #name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Constructed

#can_build?

Methods inherited from Base

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

Constructor Details

#initialize(of_type, options = {}) ⇒ SequenceOf

Returns a new instance of SequenceOf.

Parameters:

  • of_type (Class, Base)

    base type for sequence of

See Also:



64
65
66
67
68
# File 'lib/rasn1/types/sequence_of.rb', line 64

def initialize(of_type, options={})
  super(options)
  @of_type = of_type
  @no_value = false
end

Instance Attribute Details

#of_typeClass, Base (readonly)

Returns:



54
55
56
# File 'lib/rasn1/types/sequence_of.rb', line 54

def of_type
  @of_type
end

Class Method Details

.encoded_type'SEQUENCE'

A SEQUENCE OF is encoded as a SEQUENCE.

Returns:

  • ('SEQUENCE')


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

def self.encoded_type
  Sequence.encoded_type
end

.start_tracingObject

Patch #der_to_value to add tracing ability



153
154
155
156
# File 'lib/rasn1/tracer.rb', line 153

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



160
161
162
# File 'lib/rasn1/tracer.rb', line 160

def stop_tracing
  alias_method :der_to_value, :der_to_value_without_tracing
end

Instance Method Details

#<<(obj) ⇒ Object

Note:
  • if a SEQUENCE OF primitive type, obj is an instance of primitive type or equivalent Ruby type

  • if a SEQUENCE OF composed type, obj is an array of ruby type instances

  • if a SEQUENCE OF model, obj is either a Model or a Hash

Add an item to SEQUENCE OF

Parameters:

  • obj (Object)


88
89
90
91
92
93
# File 'lib/rasn1/types/sequence_of.rb', line 88

def <<(obj)
  return push_primitive(obj) if of_type_class < Primitive
  return push_composed_array(obj) if composed_of_type?

  push_model(obj)
end

#[](idx) ⇒ Base

Get element of index idx

Parameters:

Returns:



98
99
100
# File 'lib/rasn1/types/sequence_of.rb', line 98

def [](idx)
  @value[idx]
end

#der_to_value_with_tracing(der, ber: false) ⇒ Object

der_to_value der with tracing abillity



167
168
169
170
171
# File 'lib/rasn1/tracer.rb', line 167

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

Clone @#of_type and values



71
72
73
74
75
# File 'lib/rasn1/types/sequence_of.rb', line 71

def initialize_copy(*)
  super
  @of_type = @of_type.dup
  @value = @value.map(&:dup)
end

#inspect(level = 0) ⇒ String

Parameters:

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

Returns:

  • (String)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rasn1/types/sequence_of.rb', line 110

def inspect(level=0)
  str = common_inspect(level)
  str << "\n"
  level = level.abs + 1
  @value.each do |item|
    case item
    when Base, Model
      next if item.optional? && item.value.nil?

      str << item.inspect(level)
      str << "\n" unless str.end_with?("\n")
    else
      str << ('  ' * level) << "#{item.inspect}\n"
    end
  end
  str
end

#lengthInteger

Get length of SEQUENCE OF (ie number of elements)

Returns:



104
105
106
# File 'lib/rasn1/types/sequence_of.rb', line 104

def length
  @value.length
end

#void_valueArray

Returns:

  • (Array)


78
79
80
# File 'lib/rasn1/types/sequence_of.rb', line 78

def void_value
  []
end