Class: RASN1::Types::Choice
- Defined in:
- lib/rasn1/types/choice.rb,
lib/rasn1/tracer.rb
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::CLASS_MASK, Base::INDEFINITE_LENGTH, Base::MULTI_OCTETS_ID
Instance Attribute Summary collapse
-
#chosen ⇒ Integer
Chosen type.
Attributes inherited from Base
#asn1_class, #default, #name, #options
Class Method Summary collapse
-
.start_tracing ⇒ Object
Patch #parse! to add tracing ability.
-
.stop_tracing ⇒ Object
Unpatch #parse! to remove tracing ability.
Instance Method Summary collapse
-
#chosen_value ⇒ Object
Get chosen value.
-
#der_to_value(der, ber: false) ⇒ void
Make choice value from DER/BER string.
- #do_parse(der, ber: false) ⇒ Object
- #inspect(level = 0) ⇒ String
-
#parse!(der, ber: false) ⇒ Integer
Parse a DER string.
-
#parse_with_tracing(der, ber: false) ⇒ Object
Parse
derwith tracing abillity. -
#set_chosen_value(value) ⇒ Object
Set chosen value.
-
#to_der ⇒ String
DER-formated string.
- #trace ⇒ String
-
#void_value ⇒ Array()
Return empty array.
Methods inherited from Base
#==, #can_build?, constrained?, #constructed?, #do_parse_explicit, #do_parse_explicit_with_tracing, #do_parse_with_tracing, encoded_type, #explicit?, #id, #implicit?, #initialize, #initialize_copy, #optional?, parse, #primitive?, #specific_initializer, #tagged?, #type, type, #value, #value=, #value?, #value_size
Constructor Details
This class inherits a constructor from RASN1::Types::Base
Instance Attribute Details
#chosen ⇒ Integer
Chosen type
43 44 45 |
# File 'lib/rasn1/types/choice.rb', line 43 def chosen @chosen end |
Class Method Details
.start_tracing ⇒ Object
Patch #parse! to add tracing ability
109 110 111 112 |
# File 'lib/rasn1/tracer.rb', line 109 def start_tracing alias_method :parse_without_tracing, :parse! alias_method :parse!, :parse_with_tracing end |
.stop_tracing ⇒ Object
Unpatch #parse! to remove tracing ability
116 117 118 |
# File 'lib/rasn1/tracer.rb', line 116 def stop_tracing alias_method :parse!, :parse_without_tracing end |
Instance Method Details
#chosen_value ⇒ Object
#chosen MUST be set before calling this method
Get chosen value
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rasn1/types/choice.rb', line 59 def chosen_value check_chosen case @value[@chosen] when Base @value[@chosen].value when Model @value[@chosen] when Wrapper @value[@chosen].element end end |
#der_to_value(der, ber: false) ⇒ void
This method returns an undefined value.
Make choice value from DER/BER string.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rasn1/types/choice.rb', line 122 def der_to_value(der, ber: false) @value.each_with_index do |element, i| @chosen = i element.parse!(der, ber: ber) break rescue ASN1Error @chosen = nil next end end |
#do_parse(der, ber: false) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rasn1/types/choice.rb', line 101 def do_parse(der, ber: false) @value.each_with_index do |element, i| @chosen = i return element.do_parse(der, ber: ber) rescue ASN1Error @chosen = nil next end @no_value = true @value = void_value raise ASN1Error, "CHOICE #{@name}: no type matching #{der.inspect}" unless optional? [0, ''.b] end |
#inspect(level = 0) ⇒ String
135 136 137 138 139 140 141 142 |
# File 'lib/rasn1/types/choice.rb', line 135 def inspect(level=0) str = common_inspect(level) str << if defined?(@chosen) && value? "\n#{@value[@chosen].inspect(level + 1)}" else ' not chosen!' end end |
#parse!(der, ber: false) ⇒ Integer
Parse a DER string. This method updates object by setting #chosen and chosen value.
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rasn1/types/choice.rb', line 85 def parse!(der, ber: false) len, data = do_parse(der, ber: ber) return 0 if len.zero? element = @value[@chosen] if element.explicit? element.do_parse_explicit(data) else element.der_to_value(data, ber: ber) end len end |
#parse_with_tracing(der, ber: false) ⇒ Object
Parse der with tracing abillity
124 125 126 127 |
# File 'lib/rasn1/tracer.rb', line 124 def parse_with_tracing(der, ber: false) RASN1.tracer.trace(self.trace) parse_without_tracing(der, ber: ber) end |
#set_chosen_value(value) ⇒ Object
#chosen MUST be set before calling this method
Set chosen value.
50 51 52 53 |
# File 'lib/rasn1/types/choice.rb', line 50 def set_chosen_value(value) # rubocop:disable Naming/AccessorMethodName check_chosen @value[@chosen].value = value end |
#to_der ⇒ String
#chosen MUST be set before calling this method
Returns DER-formated string.
74 75 76 77 |
# File 'lib/rasn1/types/choice.rb', line 74 def to_der check_chosen @value[@chosen].to_der end |
#trace ⇒ String
146 147 148 |
# File 'lib/rasn1/types/choice.rb', line 146 def trace msg_type(no_id: true) end |
#void_value ⇒ Array()
Return empty array
153 154 155 |
# File 'lib/rasn1/types/choice.rb', line 153 def void_value [] end |