Class: Racket::Misc::TLV

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/misc/tlv.rb

Overview

Everything after the TLV is stuff into rest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ts, ls, lbytes = 1, tlinclude = false) ⇒ TLV

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



40
41
42
43
44
45
# 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

Instance Attribute Details

#lbytesObject

Returns the value of attribute lbytes.



33
34
35
# File 'lib/racket/misc/tlv.rb', line 33

def lbytes
  @lbytes
end

#lengthObject

Returns the value of attribute length.



33
34
35
# File 'lib/racket/misc/tlv.rb', line 33

def length
  @length
end

#restObject

Returns the value of attribute rest.



33
34
35
# File 'lib/racket/misc/tlv.rb', line 33

def rest
  @rest
end

#tlincludeObject

Returns the value of attribute tlinclude.



33
34
35
# File 'lib/racket/misc/tlv.rb', line 33

def tlinclude
  @tlinclude
end

#typeObject

Returns the value of attribute type.



33
34
35
# File 'lib/racket/misc/tlv.rb', line 33

def type
  @type
end

#valueObject

Returns the value of attribute value.



33
34
35
# File 'lib/racket/misc/tlv.rb', line 33

def value
  @value
end

Instance Method Details

#decode(data) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

#decode!(data) ⇒ Object



65
66
67
# File 'lib/racket/misc/tlv.rb', line 65

def decode!(data)
  @type, @length, @value, @rest = self.decode(data)
end

#encodeObject

Return a string suitable for use elswhere.



70
71
72
73
# File 'lib/racket/misc/tlv.rb', line 70

def encode
  s = "#{punpack_string(@ts)}#{punpack_string(@ls)}"
  [@type, @length, @value].pack("#{s}a*")
end

#to_sObject



75
76
77
# File 'lib/racket/misc/tlv.rb', line 75

def to_s
  encode
end

#to_strObject



79
80
81
# File 'lib/racket/misc/tlv.rb', line 79

def to_str
  encode
end