Class: Racket::Misc::TLV
- Inherits:
-
Object
- Object
- Racket::Misc::TLV
- Defined in:
- lib/racket/misc/tlv.rb
Overview
Everything after the TLV is stuff into rest
Instance Attribute Summary collapse
-
#lbytes ⇒ Object
Returns the value of attribute lbytes.
-
#length ⇒ Object
Returns the value of attribute length.
-
#rest ⇒ Object
Returns the value of attribute rest.
-
#tlinclude ⇒ Object
Returns the value of attribute tlinclude.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#decode(data) ⇒ Object
Given
data, return the type, length, value and rest values as dictated by this instance. - #decode!(data) ⇒ Object
-
#encode ⇒ Object
Return a string suitable for use elswhere.
-
#initialize(ts, ls, lbytes = 1, tlinclude = false) ⇒ TLV
constructor
Create a new TLV which requires
tsbytes for the type field andlsbytes for the length field, where (optionally) the value inlengthis a multiple oflbytesand (optionally) whether or not the length field indicates the total length of the TLV of just that of the value. - #to_s ⇒ Object
- #to_str ⇒ Object
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
#lbytes ⇒ Object
Returns the value of attribute lbytes.
33 34 35 |
# File 'lib/racket/misc/tlv.rb', line 33 def lbytes @lbytes end |
#length ⇒ Object
Returns the value of attribute length.
33 34 35 |
# File 'lib/racket/misc/tlv.rb', line 33 def length @length end |
#rest ⇒ Object
Returns the value of attribute rest.
33 34 35 |
# File 'lib/racket/misc/tlv.rb', line 33 def rest @rest end |
#tlinclude ⇒ Object
Returns the value of attribute tlinclude.
33 34 35 |
# File 'lib/racket/misc/tlv.rb', line 33 def tlinclude @tlinclude end |
#type ⇒ Object
Returns the value of attribute type.
33 34 35 |
# File 'lib/racket/misc/tlv.rb', line 33 def type @type end |
#value ⇒ Object
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 |
#encode ⇒ Object
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_s ⇒ Object
75 76 77 |
# File 'lib/racket/misc/tlv.rb', line 75 def to_s encode end |
#to_str ⇒ Object
79 80 81 |
# File 'lib/racket/misc/tlv.rb', line 79 def to_str encode end |