Class: GrizzlyBerElement

Inherits:
Object
  • Object
show all
Defined in:
lib/grizzly_ber.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(byte_array = []) ⇒ GrizzlyBerElement

Returns a new instance of GrizzlyBerElement.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/grizzly_ber.rb', line 8

def initialize(byte_array = [])
  raise ArgumentError, "byte_array must be of type Array" unless byte_array.is_a?(Array)
  @tag = +"" # is an uppercase hex string
  @value = nil # is a byte array if this is a data element or a GrizzlyBer if it's a sequence element
  decode_value decode_length decode_tag byte_array
end

Instance Attribute Details

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/grizzly_ber.rb', line 6

def tag
  @tag
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/grizzly_ber.rb', line 6

def value
  @value
end

Instance Method Details

#to_berObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/grizzly_ber.rb', line 26

def to_ber
  ber_array = ""
  ber_array += @tag.upcase
  value_hex_string = @value.pack("C*").unpack("H*").first.upcase if @value.is_a? Array
  value_hex_string = @value.to_ber if @value.is_a? GrizzlyBer
  value_byte_count = value_hex_string.size/2
  if value_byte_count < 0x7F # if the length of the value array is only one byte long and does not have its upper bit set
    ber_array << byte_to_hex(value_byte_count)
  else
    #pack("w") was meant to do this length calc but doesn't work right...
    number_of_bytes_in_byte_count = ((value_byte_count).to_s(16).size/2.0).ceil
    ber_array << byte_to_hex(number_of_bytes_in_byte_count | 0x80)
    ber_array += (value_byte_count).to_s(16).rjust(number_of_bytes_in_byte_count*2,'0').upcase
  end
  ber_array += value_hex_string
end