33

What is TLV?

To put hooks (functions) in the code instead of if/else?

If I have one piece of code running on different platforms, at different places in code, I can put TLV function hooks to identify what platform I am on and do accordingly? Or something like that?

Benefits can be cleaner code? Easy to maintain? When a new platform is added, only TLV code needs to change and not the source code?

I may be completely wrong here.

informatik01
  • 16,038
  • 10
  • 74
  • 104
hari
  • 9,439
  • 27
  • 76
  • 110
  • 2
    It has nothing to do with code and everything to do with data. – Karl Knechtel Dec 11 '10 at 00:02
  • 1
    @karl: ( I know this is an old thread, but somehow I landed here today again :) ) Are you sure that this has nothing to do with code? Because I guess its other way around - all about code and nothing about data. :D – hari Apr 15 '11 at 18:26

6 Answers6

41

TLV is Tag-length-value encoding. Often it is better referred to by it's original name, type-length-value.

The first field is the "type" of data being processed, the second field specifies the "length" of the value, the third field contains a "length" amount of data representing the value for the "type".

Multiple pieces of data can be transmitted in the same message by appending more triplets to a previously existing message.

There's a page on wikipedia covering it in just a little more detail. Don't get confused though, each triplet is a "top level" description, there is typically no nesting of items in TLV (although you could come up with a way to do so by encoding TLV triplets in the V of another tag).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Thanks Edwin. I was looking for exact need and pros/cons of it. If you know of any example, let me know. – hari Dec 11 '10 at 07:44
  • The pros are that it's a dead simple format (easy to parse, you can skip new added fields in old programs). The cons are that it's a dead simple format (no built-in hierarchy support, encodes binary poorly, data type must be known to both sides beforehand or transmitted in some custom-encoded way, very poor support for when the data changes over time are not additive but are substitutions, etc). Some of these cons are fixable, like you can uuencode binary to transmit it, but that's just more work to de-uuencode on the other end. – Edwin Buck Dec 13 '10 at 15:20
  • 3
    Actually you *can* put more TLVs inside other TLVs: the 6th bit of the 1st byte of _type_ field in the TLV structure, if set to 1, means _constructed_, which indicates that the _v_ part of the TLV structure is actually a set of other TLVs. – Fabio A. Sep 24 '13 at 13:36
  • 4
    Fabio, yes you can embed TLVs in TLVs, as my original post mentioned; however, there is not a standard for which byte needs to be set unless you go with something a bit more specific than TLV, like X.690 BER encoding, or something. With the mentioned encoding, it is the 6th byte of the class sub-field of the tag that flags a constructed value, not the first byte of the type field. So obviously there is not an accepted constructed tag flag, without selecting amongst many different "more specific" TLV like protocols. – Edwin Buck Sep 24 '13 at 18:07
  • Wouldn't browser work faster by using TLV instead of HTML? – ar2015 Feb 15 '20 at 23:09
  • @ar2015 As TLV doesn't typically store trees of elements, but lists of elements, I imagine it would depend heavily on the implementation / approach. TLV isn't a drop-in replacement for HTML, and even x.690 BER would require some rework, as the tree structures which are possible are very different. – Edwin Buck Feb 16 '20 at 23:20
  • I am not why you say that. A TLV can have a list of payloads which themselves embed some other TLVs. This way, they support trees. – ar2015 Feb 18 '20 at 08:49
  • @ar2015 But TLV alone doesn't even support payloads that are TLVs, that's what encodings like X.690 BER add to TLVs. In addition, the HTML structure makes a nested encoding of text and tags less obvious, and without a new question in StackOverflow, it would be hard to describe the pros and cons of the new approach. Please consider asking a new question, as I think your original question of "Wouldn't a browser work faster by using TLV instead of HTML" is a great StackOverflow style question. – Edwin Buck Feb 19 '20 at 03:55
18

TLV is a way of storing data to facilitate quick parsing of that data.

Typically, you read the type(tag), length and value and then send those datum to a processor function. This processor functions only function will be to process type X. Then, you read the next type, it's length and value and send it to the appropriate processor.

It's typically used as an easy way to process data without a lot of extra overhead.

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
12

We still use TLV for data formatting. And if we want to send data to receiver, we prepare a TLV package that is contain Tag-Length-Value data.

For example:

Data Tag = DF 82 0A   
Data Length = 03  
Data Value =  30 31 32

When we want to send it we concatenate this 3 row data like DF 82 0A 03 30 31 32. Data packages can contain lots of data like that.

When receiver get it, parsing package is very easy and receiver can parse all of data smoothly.

Data Parsing:

Data : DF 82 0A 03 30 31 32 DF 82 0B 04 01 12 22 33

DF 82 0A and DF 82 0B are pre-defined (value type BCD, HEX, ASCII) tags. When data arrived, in a loop, firstly parser will look for tag (i.e. DF 82 0A) and one more byte (data length). It will also read data bytes up to length.

informatik01
  • 16,038
  • 10
  • 74
  • 104
ceyun
  • 351
  • 4
  • 14
5

TLV refers to encoding values in Type-Length-Value trio's, and this more general form is documented on WikiPedia.

In some context's (such as EMV) TLV refers to the more specific X.690 which is also documented on WikiPedia.

TLV has the following advantages:

  • Relatively compact encoding format
  • Relatively simple to parse (I wrote a basic X.690 parser in a couple of hours)
  • The X.690 TLV has support for nested types (this part is slightly more complicated to parse, but from what I can tell is not required for EMV)

TLV's biggest disadvantage is that it is not directly human readable. Note however if the data is converted to hex it is only moderately difficult to read.

David Cravey
  • 151
  • 1
  • 4
3

TLV : tag - length - value

EXAMPLE : 045002124354

If we took as example that in the configuration : 045 is the meaning of the
winning number in a TV Show so : the value of the winning number is : 12

  • TAG : Search of the tag which is : 045
  • LENGTH : length for example is on 3 position so : 002
  • VALUE : Now the value is : 12 ( on 2 position )
3

I think what you are referring to is called Type Length Value, and there is a wikipedia page for it. Hope that helps.

nategoose
  • 12,054
  • 27
  • 42