Class: Codec::Bertlv

Inherits:
Base
  • Object
show all
Defined in:
lib/codec/tlv.rb

Instance Method Summary collapse

Methods inherited from Base

#add_sub_codec, #get_sub_codecs

Constructor Details

#initializeBertlv

Returns a new instance of Bertlv.



82
83
84
# File 'lib/codec/tlv.rb', line 82

def initialize
  @length = 0
end

Instance Method Details

#decode(buf, msg, length = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/codec/tlv.rb', line 86

def decode(buf,msg,length=nil)
  length ||= buf.length
  buffer = buf.slice!(0...length)
  until buffer.empty?
    sf = Field.new(tag_decode(buffer))
    val = value_decode(buffer, length_decode(buffer))
    sf.set_value(val)
	  msg.add_sub_field(sf)
  end
end

#encode(buf, field) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/codec/tlv.rb', line 97

def encode(buf, field)
  out = ""
  subfields = field.get_value
  unless subfields.kind_of?(Array)
    raise EncodingException, "Invalid field #{field.to_yaml} for BER Tlv encoding"
  end
  
  while subfields.size > 0
    subfield = subfields.shift
    out << tag_encode(subfield.get_id)
    # TODO : Handle value that is not String
    value = value_encode(subfield.get_value)
    out << length_encode(value.length)
    out << value
  end
  buf << out
  return out.length
end