Class: RASN1::Types::Integer

Inherits:
Primitive show all
Defined in:
lib/rasn1/types/integer.rb

Overview

ASN.1 Integer

An INTEGER is a primitive type to represent integers. There is no limit on integer size.

Examples:

Simple Integers

# Simple integer with a value associated
int1 = RASN1::Types::Integer.new(value: 42)
# Integer without value, but a default one
int2 = RASN1::Types::Integer.new(default: -42)

Enumerated Integer

enum = RASN1::Types::Integer.new(enum: {one: 1, two: 2})
enum.value = :two
enum.value #=> :two
enum.to_i  #=> 2

Author:

  • Sylvain Daubert

  • LemonTree55

Direct Known Subclasses

Enumerated

Constant Summary collapse

ID =

Integer id value

2

Constants inherited from Primitive

Primitive::ASN1_PC

Constants inherited from Base

Base::CLASSES, Base::CLASS_MASK, Base::INDEFINITE_LENGTH, Base::MULTI_OCTETS_ID

Instance Attribute Summary collapse

Attributes inherited from Base

#asn1_class, #default, #name, #options

Instance Method Summary collapse

Methods inherited from Base

#==, #can_build?, constrained?, #constructed?, #do_parse, #do_parse_explicit, #do_parse_explicit_with_tracing, #do_parse_with_tracing, encoded_type, #explicit?, #id, #implicit?, #initialize_copy, #inspect, #optional?, parse, #parse!, #primitive?, #specific_initializer, start_tracing, stop_tracing, #tagged?, #to_der, #trace, #type, type, #value, #value?, #value_size

Constructor Details

#initialize(options = {}) ⇒ Integer

Returns a new instance of Integer.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :class (Symbol)

    ASN.1 class. Default value is :universal. If :explicit or :implicit: is defined, default value is :context.

  • :optional (::Boolean)

    define this tag as optional. Default is false

  • :default (Object)

    default value (ASN.1 DEFAULT)

  • :value (Object)

    value to set

  • :implicit (::Integer)

    define an IMPLICIT tagged type

  • :explicit (::Integer)

    define an EXPLICIT tagged type

  • :constructed (::Boolean)

    if true, set type as constructed. May only be used when :explicit is defined, else it is discarded.

  • :name (::String)

    name for this node

  • :enum (Hash{String=>::Integer})

    enumeration hash. Keys are names, and values are integers.

Raises:

  • (EnumeratedError)

    :default value is unknown when :enum key is present



41
42
43
44
# File 'lib/rasn1/types/integer.rb', line 41

def initialize(options={})
  super
  initialize_enum(@options[:enum])
end

Instance Attribute Details

#enumHash{String=>::Integer}? (readonly)

Associated enumeration

Returns:

  • (Hash{String=>::Integer}, nil)


32
33
34
# File 'lib/rasn1/types/integer.rb', line 32

def enum
  @enum
end

Instance Method Details

#der_to_value(der, ber: false) ⇒ void

This method returns an undefined value.

Make integer value from der string.

Parameters:

  • der (String)
  • ber (::Boolean) (defaults to: false)


92
93
94
95
96
97
98
99
# File 'lib/rasn1/types/integer.rb', line 92

def der_to_value(der, ber: false)
  int_value = der_to_int_value(der, ber: ber)
  @value = if @enum.empty?
             int_value
           else
             int_to_enum(int_value)
           end
end

#to_i::Integer

Integer value

Returns:

  • (::Integer)


78
79
80
81
82
83
84
85
86
# File 'lib/rasn1/types/integer.rb', line 78

def to_i
  val = value? ? @value : @default
  case val
  when String, Symbol
    @enum[val]
  else
    val || 0
  end
end

#value=(val) ⇒ void

Note:

An enumerated INTEGER may be set with an integer value out of enumeration, only when no value is set.

This method returns an undefined value.

Set integer value from a Ruby integer, or a String or a Symbol (only when #enum is defined).

Parameters:

  • val (::Integer, String, Symbol, nil)

Raises:

  • (EnumeratedError)

    string/symbol value: no enum defined or val not in enum

  • (EnumeratedError)

    integer value: value already set and new value not in enum

  • (TypeError)

    unsupported val type

Since:

  • 0.16.2 raise TypeError (was EnumeratedError)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rasn1/types/integer.rb', line 56

def value=(val)
  @no_value = false
  case val
  when String, Symbol
    value_from_string_or_symbol(val)
  when ::Integer
    value_from_integer(val)
  when nil
    @no_value = true
    @value = void_value
  else
    raise TypeError, "#{val.class} not supported"
  end
end

#void_value::Integer

Returns:

  • (::Integer)


72
73
74
# File 'lib/rasn1/types/integer.rb', line 72

def void_value
  0
end