class RASN1::Types::SequenceOf

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]

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

Constants

ID

SequenceOf id value

Attributes

of_type[R]

@return [Class, Base]

Public Class Methods

encoded_type() click to toggle source

A SEQUENCE OF is encoded as a SEQUENCE. @return ['SEQUENCE']

# File lib/rasn1/types/sequence_of.rb, line 57
def self.encoded_type
  Sequence.encoded_type
end
new(of_type, options={}) click to toggle source

@param [Symbol, String] name name for this tag in grammar @param [Class, Base] of_type base type for sequence of @see Base#initialize

Calls superclass method RASN1::Types::Base::new
# File lib/rasn1/types/sequence_of.rb, line 64
def initialize(of_type, options={})
  super(options)
  @of_type = of_type
  @value = []
end

Public Instance Methods

<<(obj) click to toggle source

Add an item to SEQUENCE OF @param [Array,Hash, Model]

# File lib/rasn1/types/sequence_of.rb, line 78
def <<(obj)
  if of_type_class < Primitive
    raise ASN1Error, 'object to add should be an Array' unless obj.is_a?(Array)

    @value += obj.map { |item| @of_type.new(item) }
  elsif composed_of_type?
    raise ASN1Error, 'object to add should be an Array' unless obj.is_a?(Array)

    new_value = of_type_class.new
    @of_type.value.each_with_index do |type, i|
      type2 = type.dup
      type2.value = obj[i]
      new_value.value << type2
    end
    @value << new_value
  elsif of_type_class < Model
    case obj
    when Hash
      @value << @of_type.new(obj)
    when of_type_class
      @value << obj
    else
      raise ASN1Error, "object to add should be a #{of_type_class} or a Hash"
    end
  end
end
[](idx) click to toggle source

Get element of index idx @param [Integer] idx @return [Base]

# File lib/rasn1/types/sequence_of.rb, line 108
def [](idx)
  @value[idx]
end
initialize_copy(other) click to toggle source
Calls superclass method RASN1::Types::Base#initialize_copy
# File lib/rasn1/types/sequence_of.rb, line 70
def initialize_copy(other)
  super
  @of_type = @of_type.dup
  @value = @value.map(&:dup)
end
inspect(level=0) click to toggle source
# File lib/rasn1/types/sequence_of.rb, line 118
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
length() click to toggle source

Get length of SEQUENCE OF (ie number of elements) @return [Integer]

# File lib/rasn1/types/sequence_of.rb, line 114
def length
  @value.length
end

Private Instance Methods

composed_of_type?() click to toggle source
# File lib/rasn1/types/sequence_of.rb, line 142
def composed_of_type?
  [Sequence, Set].include? of_type_class
end
der_to_value(der, ber: false) click to toggle source
# File lib/rasn1/types/sequence_of.rb, line 150
def der_to_value(der, ber: false)
  @value = []
  nb_bytes = 0

  while nb_bytes < der.length
    type = if composed_of_type?
             @of_type.dup
           elsif of_type_class < Model
             of_type_class.new
           else
             of_type_class.new(:t)
           end
    nb_bytes += type.parse!(der[nb_bytes, der.length])
    @value << type
  end
end
explicit_type() click to toggle source
# File lib/rasn1/types/sequence_of.rb, line 167
def explicit_type
  self.class.new(self.of_type)
end
of_type_class() click to toggle source
# File lib/rasn1/types/sequence_of.rb, line 138
def of_type_class
  @of_type.is_a?(Class) ? @of_type : @of_type.class
end
value_to_der() click to toggle source
# File lib/rasn1/types/sequence_of.rb, line 146
def value_to_der
  @value.map(&:to_der).join
end