Class: RASN2::Types::SequenceOf
- Inherits:
-
Constructed
- Object
- Base
- Constructed
- RASN2::Types::SequenceOf
- Defined in:
- lib/rasn2/types/sequence_of.rb,
lib/rasn2/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 = RASN2::Types::SequenceOf.new(RASN2::Types::Integer)
Set integer values
seqof.value = [1, 2, 3, 4].map { |i| RASN2::Types::Integer.new(value: i) } seqof << 5 # infer a RASN2::Types::Integer with given value
Use with Constructed types
SEQUENCE OF may be used to create sequence of composed types. For example: composed_type = RASN2::Types::Sequence.new commposed_type.value = [RASN2::Types::Integer, RASN2::Types::OctetString] seqof = RASN2::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 < RASN2::Model sequence :seq, content: [boolean(:bool), integer(:int)] end
seqof = RASN2::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 # => MyModel seqof[:bool].value # => true seqof[:int].value # => 12
Direct Known Subclasses
Constant Summary collapse
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, #model, #name, #options
Class Method Summary collapse
-
.encoded_type ⇒ 'SEQUENCE'
A SEQUENCE OF is encoded as a SEQUENCE.
-
.start_tracing ⇒ Object
Patch #der_to_value to add tracing ability.
-
.stop_tracing ⇒ Object
Unpatch #der_to_value to remove tracing ability.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
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, color: false) ⇒ String
-
#length ⇒ Integer
Get length of SEQUENCE OF (ie number of elements).
- #void_value ⇒ Array
Methods inherited from Constructed
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, #explicit?, #find_type, #get_data, #get_length, #id, #id2octets, #id_value, #implicit?, #indent, #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(of_type, options = {}) ⇒ SequenceOf
Returns a new instance of SequenceOf.
64 65 66 67 68 |
# File 'lib/rasn2/types/sequence_of.rb', line 64 def initialize(of_type, ={}) super() @of_type = of_type @no_value = false end |
Instance Attribute Details
#of_type ⇒ Class, Base (readonly)
54 55 56 |
# File 'lib/rasn2/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.
58 59 60 |
# File 'lib/rasn2/types/sequence_of.rb', line 58 def self.encoded_type Sequence.encoded_type end |
.start_tracing ⇒ Object
Patch #der_to_value to add tracing ability
180 181 182 183 |
# File 'lib/rasn2/tracer.rb', line 180 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 ⇒ Object
Unpatch #der_to_value to remove tracing ability
187 188 189 |
# File 'lib/rasn2/tracer.rb', line 187 def stop_tracing alias_method :der_to_value, :der_to_value_without_tracing end |
Instance Method Details
#<<(obj) ⇒ Object
- 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
88 89 90 91 92 93 |
# File 'lib/rasn2/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
98 99 100 |
# File 'lib/rasn2/types/sequence_of.rb', line 98 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
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rasn2/types/sequence_of.rb', line 127 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
194 195 196 197 198 |
# File 'lib/rasn2/tracer.rb', line 194 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_copy ⇒ Object
Clone @#of_type and values
71 72 73 74 75 |
# File 'lib/rasn2/types/sequence_of.rb', line 71 def initialize_copy(*) super @of_type = @of_type.dup @value = @value.map(&:dup) end |
#inspect(level = 0, color: false) ⇒ String
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rasn2/types/sequence_of.rb', line 110 def inspect(level=0, color: false) new_level = level.abs + 1 begin_colorizer(color) name = @name ? "#{colorize_name(@name)} " : '' lines = [] lines << "#{" " * level}#{name}SEQUENCE OF:" lines += @value.map do |item| item.inspect(new_level, color: color) end end_colorizer lines.join("\n") end |
#length ⇒ Integer
Get length of SEQUENCE OF (ie number of elements)
104 105 106 |
# File 'lib/rasn2/types/sequence_of.rb', line 104 def length @value.length end |
#void_value ⇒ Array
78 79 80 |
# File 'lib/rasn2/types/sequence_of.rb', line 78 def void_value [] end |