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

Methods

decode   decode!   encode   new   to_s   to_str  

Attributes

lbytes  [RW] 
length  [RW] 
rest  [RW] 
tlinclude  [RW] 
type  [RW] 
value  [RW] 

Public Class methods

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

[Source]

# 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

Public Instance methods

Given data, return the type, length, value and rest values as dictated by this instance.

[Source]

# 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

[Source]

# File lib/racket/misc/tlv.rb, line 65
  def decode!(data)
    @type, @length, @value, @rest = self.decode(data)
  end

Return a string suitable for use elswhere.

[Source]

# File lib/racket/misc/tlv.rb, line 70
  def encode
    s = "#{punpack_string(@ts)}#{punpack_string(@ls)}"
    [@type, @length, @value].pack("#{s}a*")
  end

[Source]

# File lib/racket/misc/tlv.rb, line 75
  def to_s
    encode
  end

[Source]

# File lib/racket/misc/tlv.rb, line 79
  def to_str
    encode
  end

[Validate]