| Class | Racket::Misc::TLV |
| In: |
lib/racket/misc/tlv.rb
(CVS)
|
| Parent: | Object |
Simple class for your average type, length, value datastructure. Everything after the TLV is stuff into rest
| lbytes | [RW] | |
| length | [RW] | |
| rest | [RW] | |
| tlinclude | [RW] | |
| type | [RW] | |
| value | [RW] |
Create a new TLV which requires ts bytes for the type field and ls bytes for the length field, where (optionally) the value in length is a multiple of lbytes and (optionally) whether or not the length field indicates the total length of the TLV of just that of the value
# File lib/racket/misc/tlv.rb, line 40 def initialize(ts, ls, lbytes=1, tlinclude=false) @ts = ts @ls = ls @lbytes = lbytes @tlinclude = tlinclude end
Given data, return the type, length, value and rest values as dictated by this instance.
# File lib/racket/misc/tlv.rb, line 49 def decode(data) s = "#{punpack_string(@ts)}#{punpack_string(@ls)}" type, length, tmp = data.unpack("#{s}a*") if (type.nil? or length.nil?) nil else elength = (length * lbytes) - (@tlinclude ? (@ls + @ts) : 0) value, rest = tmp.unpack("a#{elength}a*") if (value.empty? and length > 0) nil else [type, length, value, rest] end end end
# File lib/racket/misc/tlv.rb, line 65 def decode!(data) @type, @length, @value, @rest = self.decode(data) end