Class: RASN1::Types::Choice
Overview
A ASN.1 CHOICE is a choice between different types.
Create a CHOICE
A CHOICE is defined this way:
choice = Choice.new
choice.value = [Integer.new(implicit: 0, class: :context),
Integer.new(implicit: 1, class: :context),
OctetString.new(implicit: 2, class: :context)]
The chosen type may be set this way:
choice.chosen = 0 # choose :int1
The chosen value may be set these ways:
choise.value[choice.chosen].value = 1
choise.set_chosen_value 1
The chosen value may be got these ways:
choise.value[choice.chosen].value # => 1
choice.chosen_value # => 1
Encode a CHOICE
#to_der only encodes the chosen value:
choise.to_der # => "\x80\x01\x01"
Parse a CHOICE
Parsing a CHOICE set #chosen and set value to chosen type. If parsed string does not contain a type from CHOICE, a ASN1Error is raised.
str = "\x04\x03abc"
choice.parse! str
choice.chosen # => 2
choice.chosen_value # => "abc"
Constant Summary
Constants inherited from Base
Base::CLASSES, Base::INDEFINITE_LENGTH, Base::MAX_TAG
Instance Attribute Summary collapse
-
#chosen ⇒ Integer
Chosen type.
Attributes inherited from Base
#asn1_class, #default, #name, #value
Instance Method Summary collapse
-
#chosen_value ⇒ Object
Get chosen value.
- #inspect(level = 0) ⇒ Object
-
#parse!(der, ber: false) ⇒ Integer
Parse a DER string.
-
#set_chosen_value(value) ⇒ Object
Set chosen value.
-
#to_der ⇒ String
DER-formated string.
Methods inherited from Base
#==, #constructed?, #explicit?, #implicit?, #initialize, #initialize_copy, #optional?, parse, #primitive?, #tag, #tagged?, type, #type, #value_size
Constructor Details
This class inherits a constructor from RASN1::Types::Base
Instance Attribute Details
#chosen ⇒ Integer
Chosen type
37 38 39 |
# File 'lib/rasn1/types/choice.rb', line 37 def chosen @chosen end |
Instance Method Details
#chosen_value ⇒ Object
#chosen MUST be set before calling this method
Get chosen value
53 54 55 56 57 58 59 60 61 |
# File 'lib/rasn1/types/choice.rb', line 53 def chosen_value check_chosen case @value[@chosen] when Base @value[@chosen].value when Model @value[@chosen] end end |
#inspect(level = 0) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rasn1/types/choice.rb', line 93 def inspect(level=0) str = '' str << ' ' * level if level > 0 str << "#{name} #{type}: " if @chosen.nil? str << 'not chosen!' else str << @value[@chosen].inspect(level) end end |
#parse!(der, ber: false) ⇒ Integer
Parse a DER string. This method updates object by setting #chosen and chosen value.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rasn1/types/choice.rb', line 77 def parse!(der, ber: false) parsed = false @value.each_with_index do |element, i| begin @chosen = i nb_bytes = element.parse!(der) parsed = true return nb_bytes rescue ASN1Error @chosen = nil next end end raise ASN1Error, "CHOICE #@name: no type matching #{der.inspect}" unless parsed end |
#set_chosen_value(value) ⇒ Object
#chosen MUST be set before calling this method
Set chosen value.
44 45 46 47 |
# File 'lib/rasn1/types/choice.rb', line 44 def set_chosen_value(value) check_chosen @value[@chosen].value = value end |
#to_der ⇒ String
#chosen MUST be set before calling this method
Returns DER-formated string.
66 67 68 69 |
# File 'lib/rasn1/types/choice.rb', line 66 def to_der check_chosen @value[@chosen].to_der end |