diff options
Diffstat (limited to 'lib/asn1kit/types/object_identifier.rb')
-rw-r--r-- | lib/asn1kit/types/object_identifier.rb | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/asn1kit/types/object_identifier.rb b/lib/asn1kit/types/object_identifier.rb new file mode 100644 index 0000000..1c8f69c --- /dev/null +++ b/lib/asn1kit/types/object_identifier.rb @@ -0,0 +1,76 @@ +# coding: ASCII-8BIT + +class ASN1Kit::ObjectIdentifier < ASN1Kit::Type + asn1_tag :IMPLICIT, :UNIVERSAL, 6 + asn1_alias "OBJECT IDENTIFIER" + + attr_reader :components + + def initialize(components) + @components = components + end + + def self.from_dot_notation(string) + new(string.split(".").map { |s| Integer(s) }) + end + + def dot_notation + @components.join(".") + end + + def to_der + first, second, rest = @components + content = [first * 40 + second, *rest].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::ObjectIdentifier + super + end + + def ==(other) + other.is_a?(ASN1Kit::ObjectIdentifier) && @components == other.components + end +end + +module ASN1Kit::Internal::CompileObjectIdentifier + refine ASN1Kit::ObjectIdentifier.singleton_class do + def _new_internal(base, arys) + obj = allocate + obj.instance_variable_set(:@_compile_base, base) + obj.instance_variable_set(:@_compile_arys, arys) + obj + end + end + + refine ASN1Kit::ObjectIdentifier do + def _compile_fixup(state) + base_value = remove_instance_variable(:@_compile_base) + arys = remove_instance_variable(:@_compile_arys) + + if base_value + base = base_value.unwrap(state) + components = base.components.dup + else + components = [] + end + + 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 |