Class: RASN1::Types::BitString

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

Overview

ASN.1 Bit String

Author:

  • Sylvain Daubert

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):

  • :bit_length (Object)

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

See Also:



24
25
26
27
28
29
30
31
32
# File 'lib/rasn1/types/bit_string.rb', line 24

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



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

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:



53
54
55
# File 'lib/rasn1/types/bit_string.rb', line 53

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:



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

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)


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

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