aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1kit/types/relative_oid.rb
blob: 8485010050d902403111b2319296c80e9c82d30a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# coding: ASCII-8BIT

class ASN1Kit::RelativeOID < ASN1Kit::Type
  asn1_tag :IMPLICIT, :UNIVERSAL, 13
  asn1_alias "RELATIVE-OID"

  attr_reader :components

  def initialize(components)
    @components = components
  end

  def dot_notation
    @components.join(".")
  end

  def self.from_dot_notation(string)
    new(string.split(".").map { |s| Integer(s) })
  end

  def to_der
    content = @components.pack("w*")
    der_header(content.bytesize) << content
  end

  private def inspect_inner
    "{ #{@components.join(" ")} }"
  end

  def cast_to(type)
    return type.new(@components) if type <= ASN1Kit::RelativeOID
    super
  end

  def ==(other)
    other.is_a?(ASN1Kit::RelativeOID) && @components == other.components
  end
end

module ASN1Kit::Internal::CompileRelativeOID
  refine ASN1Kit::RelativeOID.singleton_class do
    def _new_internal(arys)
      obj = allocate
      obj.instance_variable_set(:@_compile_arys, arys)
      obj
    end
  end

  refine ASN1Kit::RelativeOID do
    def _compile_fixup(state)
      arys = remove_instance_variable(:@_compile_arys)

      components = []
      arys.each do |ary|
        case ary
        when ASN1Kit::Internal::UnresolvedValue
          rel_oid = ary.unwrap(state)
          components.concat(rel_oid.components)
        else
          components.concat(ary.map { |v| v.unwrap_as_number(state) })
        end
      end

      @components = components
    end
  end
end