Class: RASN1::Types::BitString

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

Overview

ASN.1 Bit String

A BIT STRING is a primitive ASN.1 type, able to embbed data whose length is not multiple of a byte.

To specify a non byte multiple length, :bit_length option must be used.

Examples:

Simple bit string

# A 4-bit length bit string, with value 1000b
bs = RASN1::Types::BitString.new(value: "\x80", bit_length: 4)
# As DER/BER encode data on bytes, such encodings specify unused part in binary string
bs.to_der   #=> "\x03\x02\x04\x80"

Author:

  • Sylvain Daubert

  • LemonTree55

Constant Summary collapse

ID =

BitString id value

3

Constants inherited from Primitive

Primitive::ASN1_PC

Constants inherited from Base

RASN1::Types::Base::CLASSES, RASN1::Types::Base::CLASS_MASK, RASN1::Types::Base::INDEFINITE_LENGTH, RASN1::Types::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

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

Constructor Details

#initialize(options = {}) ⇒ BitString

Returns a new instance of BitString.

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

  • :bit_length (Object)

    default bit_length value. Should be present if :default is set



34
35
36
37
38
39
40
41
42
# File 'lib/rasn1/types/bit_string.rb', line 34

def initialize(options={})
  super
  if @default
    raise ASN1Error, "#{@name}: default bit length is not defined" if @options[:bit_length].nil?

    @default_bit_length = @options[:bit_length]
  end
  @bit_length = @options[:bit_length]
end

Instance Attribute Details

#bit_lengthObject

Get bit length



45
46
47
48
49
50
51
# File 'lib/rasn1/types/bit_string.rb', line 45

def bit_length
  if value?
    @bit_length
  else
    @default_bit_length
  end
end

Instance Method Details

#can_build?Boolean

Same as RASN1::Types::Base#can_build? but also check bit_length

Returns:

See Also:



63
64
65
# File 'lib/rasn1/types/bit_string.rb', line 63

def can_build?
  super || (!@default.nil? && (@bit_length != @default_bit_length))
end

#der_to_value(der, ber: false) ⇒ void

This method returns an undefined value.

Make value from DER/BER string. Also set #bit_length.

Parameters:

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

See Also:



72
73
74
75
76
77
# File 'lib/rasn1/types/bit_string.rb', line 72

def der_to_value(der, ber: false) # rubocop:disable Lint/UnusedMethodArgument
  unused = der.unpack1('C').to_i
  value = der[1..].to_s
  @bit_length = value.length * 8 - unused
  @value = value
end

#inspect(level = 0) ⇒ String

Parameters:

Returns:

  • (String)


55
56
57
58
# File 'lib/rasn1/types/bit_string.rb', line 55

def inspect(level=0)
  str = common_inspect(level)
  str << " #{value.inspect} (bit length: #{bit_length})"
end