Class: RASN1::Types::SequenceOf
- Inherits:
-
Constructed
- Object
- Base
- Constructed
- RASN1::Types::SequenceOf
- 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
Direct Known Subclasses
Constant Summary collapse
- ID =
SequenceOf id value
Sequence::ID
Constants inherited from Constructed
Constants inherited from Base
Base::CLASSES, Base::CLASS_MASK, Base::INDEFINITE_LENGTH, Base::MULTI_OCTETS_ID
Instance Attribute Summary collapse
- #of_type ⇒ Class, Base readonly
Attributes inherited from Base
#asn1_class, #default, #name, #options
Class Method Summary collapse
-
.encoded_type ⇒ String
A SEQUENCE OF is encoded as a SEQUENCE.
-
.start_tracing ⇒ void
Patch #der_to_value to add tracing ability.
-
.stop_tracing ⇒ void
Unpatch #der_to_value to remove tracing ability.
Instance Method Summary collapse
-
#<<(obj) ⇒ self
Add an item to SEQUENCE OF.
-
#[](idx) ⇒ Base, ...
Get element of index
idx. -
#der_to_value(der, ber: false) ⇒ void
Make sequence of value from
derstring. -
#der_to_value_with_tracing(der, ber: false) ⇒ Object
der_to_value
derwith tracing abillity. -
#initialize(of_type, options = {}) ⇒ SequenceOf
constructor
A new instance of SequenceOf.
-
#initialize_copy ⇒ Object
Clone @#of_type and values.
- #inspect(level = 0) ⇒ String
-
#length ⇒ Integer
Get length of SEQUENCE OF (ie number of elements).
- #void_value ⇒ Array
Methods inherited from Constructed
Methods inherited from Base
#==, #can_build?, constrained?, #constructed?, #do_parse, #do_parse_explicit, #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.
71 72 73 74 75 |
# File 'lib/rasn1/types/sequence_of.rb', line 71 def initialize(of_type, ={}) super() @of_type = of_type @no_value = false end |
Instance Attribute Details
#of_type ⇒ Class, Base (readonly)
60 61 62 |
# File 'lib/rasn1/types/sequence_of.rb', line 60 def of_type @of_type end |
Class Method Details
.encoded_type ⇒ String
A SEQUENCE OF is encoded as a SEQUENCE.
64 65 66 |
# File 'lib/rasn1/types/sequence_of.rb', line 64 def self.encoded_type Sequence.encoded_type end |
.start_tracing ⇒ void
This method returns an undefined value.
Patch #der_to_value to add tracing ability
177 178 179 180 |
# File 'lib/rasn1/tracer.rb', line 177 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_tracing ⇒ void
This method returns an undefined value.
Unpatch #der_to_value to remove tracing ability
185 186 187 |
# File 'lib/rasn1/tracer.rb', line 185 def stop_tracing alias_method :der_to_value, :der_to_value_without_tracing end |
Instance Method Details
#<<(obj) ⇒ self
-
if a SEQUENCE OF primitive type,
objis an instance of primitive type or equivalent Ruby type -
if a SEQUENCE OF composed type,
objis an array of ruby type instances -
if a SEQUENCE OF model,
objis either a Model or a Hash
Add an item to SEQUENCE OF
97 98 99 100 101 102 103 |
# File 'lib/rasn1/types/sequence_of.rb', line 97 def <<(obj) return push_primitive(obj) if of_type_class < Primitive return push_composed_array(obj) if composed_of_type? push_model(obj) self end |
#[](idx) ⇒ Base, ...
Get element of index idx
108 109 110 |
# File 'lib/rasn1/types/sequence_of.rb', line 108 def [](idx) @value[idx] end |
#der_to_value(der, ber: false) ⇒ void
This method returns an undefined value.
Make sequence of value from der string
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rasn1/types/sequence_of.rb', line 142 def der_to_value(der, ber: false) # rubocop:disable Lint/UnusedMethodArgument @value = [] nb_bytes = 0 while nb_bytes < der.length type = of_type_class.new nb_bytes += type.parse!(der[nb_bytes, der.length]) @value << type end end |
#der_to_value_with_tracing(der, ber: false) ⇒ Object
der_to_value der with tracing abillity
193 194 195 196 197 |
# File 'lib/rasn1/tracer.rb', line 193 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_copy ⇒ Object
Clone @#of_type and values
78 79 80 81 82 |
# File 'lib/rasn1/types/sequence_of.rb', line 78 def initialize_copy(*) super @of_type = @of_type.dup @value = @value.map(&:dup) end |
#inspect(level = 0) ⇒ String
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rasn1/types/sequence_of.rb', line 120 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 ⇒ Integer
Get length of SEQUENCE OF (ie number of elements)
114 115 116 |
# File 'lib/rasn1/types/sequence_of.rb', line 114 def length @value.length end |
#void_value ⇒ Array
85 86 87 |
# File 'lib/rasn1/types/sequence_of.rb', line 85 def void_value [] end |