Class: GrizzlyBer

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

Defined Under Namespace

Classes: ParsingError

Instance Method Summary collapse

Constructor Details

#initialize(hex_string = "", allow_FF_tags: false) ⇒ GrizzlyBer

Returns a new instance of GrizzlyBer.

Raises:

  • (ArgumentError)


111
112
113
114
115
116
# File 'lib/grizzly_ber.rb', line 111

def initialize(hex_string = "", allow_FF_tags: false)
  raise ArgumentError, "hex_string must be a valid hex string" unless hex_string.is_a? String and hex_string.size.even? and hex_string =~ /^[0-9A-F]*$/
  @elements = [] # is an array of GrizzlyBerElement
  @allow_FF_tags = allow_FF_tags
  from_ber_hex_string(hex_string)
end

Instance Method Details

#[](index_or_tag) ⇒ Object



140
141
142
143
# File 'lib/grizzly_ber.rb', line 140

def [](index_or_tag)
  return value_of_first_element_with_tag(index_or_tag) if index_or_tag.is_a? String
  @elements[index_or_tag]
end

#[]=(index_or_tag, value) ⇒ Object



145
146
147
148
149
# File 'lib/grizzly_ber.rb', line 145

def []=(index_or_tag,value)
  return set_value_for_tag(index_or_tag, value) if index_or_tag.is_a? String
  @elements[index].value = value
  @elements[index]
end

#each(&block) ⇒ Object



155
156
157
# File 'lib/grizzly_ber.rb', line 155

def each &block
  @elements.each &block
end

#from_ber(byte_array) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/grizzly_ber.rb', line 123

def from_ber(byte_array)
  raise ArgumentError, "byte_array must be an array of bytes" unless byte_array.is_a? Array and byte_array.all? {|byte| byte.is_a? Integer and byte <= 0xFF}
  while byte_array.any?
    byte_array.shift while byte_array.any? && is_erasure_byte(byte_array[0])
    break if byte_array.empty?
    element = GrizzlyBerElement.new(byte_array)
    raise ParsingError if element.tag.nil? or element.value.nil?
    @elements << element
  end
  raise ParsingError if byte_array.any?
  self
end

#from_ber_hex_string(hex_string) ⇒ Object

Raises:

  • (ArgumentError)


118
119
120
121
# File 'lib/grizzly_ber.rb', line 118

def from_ber_hex_string(hex_string)
  raise ArgumentError, "hex_string must be a valid hex string" unless hex_string.is_a? String and hex_string.size.even? and hex_string =~ /^[0-9A-F]*$/
  self.from_ber [hex_string].pack("H*").unpack("C*")
end

#hex_value_of_first_element_with_tag(tag) ⇒ Object



165
166
167
168
# File 'lib/grizzly_ber.rb', line 165

def hex_value_of_first_element_with_tag(tag)
  first_tagged_element = value_of_first_element_with_tag(tag)
  first_tagged_element &&= first_tagged_element.pack("C*").unpack("H*").first.upcase
end

#remove!(tag) ⇒ Object



186
187
188
189
# File 'lib/grizzly_ber.rb', line 186

def remove!(tag)
  @elements = @elements.select {|element| element.tag != tag.upcase}
  self
end

#set_hex_value_for_tag(tag, value) ⇒ Object



182
183
184
# File 'lib/grizzly_ber.rb', line 182

def set_hex_value_for_tag(tag, value)
  set_value_for_tag(tag, [value].pack("H*").unpack("C*"))
end

#set_value_for_tag(tag, value) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/grizzly_ber.rb', line 170

def set_value_for_tag(tag, value)
  real_tag = GrizzlyTag.named(tag) ? GrizzlyTag.named(tag)[:tag] : tag.upcase
  first_tagged_element = @elements.find {|element| real_tag == element.tag}
  if first_tagged_element.nil?
    first_tagged_element = GrizzlyBerElement.new
    first_tagged_element.tag = real_tag
    @elements << first_tagged_element
  end
  first_tagged_element.value = value
  first_tagged_element
end

#sizeObject



151
152
153
# File 'lib/grizzly_ber.rb', line 151

def size
  @elements.size
end

#to_berObject



136
137
138
# File 'lib/grizzly_ber.rb', line 136

def to_ber
  @elements.reduce("") {|ber_array, element| ber_array += element.to_ber}
end

#to_s(indent_size: 0) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/grizzly_ber.rb', line 191

def to_s(indent_size: 0)
  indent = " " * 3 * indent_size
  @elements.reduce("") do |output, element| 
    info = GrizzlyTag.tagged(element.tag) || {:name => "Unknown Tag", :description => "Unknown"}
    output += "#{indent}#{element.tag}: #{info[:name]}\n"
    output += "#{indent} Description: #{info[:description]}\n"
    if element.value.is_a? GrizzlyBer
      output += element.value.to_s(indent_size: indent_size+1)
    else
      output += "#{indent} Value: #{element.value.pack("C*").unpack("H*").first}"
      if info[:format] == :string
        string_value = ", \"#{element.value.pack("C*")}\""
        output += string_value if string_value.encoding == output.encoding #output is expected to be Encoding::UTF_8 but that default mustn't be forced here.
      end
      output += "\n"
    end
  end
end

#value_of_first_element_with_tag(tag) ⇒ Object



159
160
161
162
163
# File 'lib/grizzly_ber.rb', line 159

def value_of_first_element_with_tag(tag)
  first_tagged_element = @elements.find {|element| tag.upcase == element.tag}
  first_tagged_element ||= @elements.find {|element| element.tag == GrizzlyTag.named(tag)[:tag]} if GrizzlyTag.named(tag)
  first_tagged_element &&= first_tagged_element.value
end