Date: 2005-02-20T07:03:41 Editor: EmmanuelLecharny Wiki: Apache Directory Project Wiki Page: TLVPageInfo URL: http://wiki.apache.org/directory/TLVPageInfo
no comment Change Log: ------------------------------------------------------------------------------ @@ -98,32 +98,25 @@ The last point is that a primitive value has a fixed size, given by the '''Lengh''' part of the '''TLV'''. It will be used for ''constructed values''. ==== Constructed Value ==== -Constructed values are '''TLV''' with inner '''TLVs''' in its '''Value'''. It ha a '''Length''' which is the sum of all its inner '''TLVs'''. A bit is set in the '''Tag''' to distinguish a ''Constructed value'' from a ''Primitive value'' : the 5th bit of the first tag's byte. In the sample, the first '''TLV''' tag is ''30'', which can be read 00__'''1'''__1-0000 binary. The 5th bit (bold) is set : it's a ''constructed value''. +Constructed values are '''TLV''' with inner '''TLVs''' in its '''Value'''. It has a '''Length''' which is the sum of all its inner '''TLVs'''. A bit is set in the '''Tag''' to distinguish a ''Constructed value'' from a ''Primitive value'' : the 5th bit of the first tag's byte. In the sample, the first '''TLV''' tag is ''30'', which can be read 00__'''1'''__1-0000 binary. The 5th bit (bold) is set : it's a ''constructed value''. +=== Decoding TLVs === +For any further information, one should read [http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf] which explain BER encoding, but be aware that you also need to read [http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf]. They are available for free, which is quite cheap compared to sleeping pills ! -(TODO : replace the total BS below ...) - -What about the '''Values'''? '''Length''' was easy, it was totally context-free. Which kind of '''Value''' can we have? How do we know the type of each '''Value'''? - -First, we have seen that some '''Values''' are composed with '''TLVs'''. But we must have some kind of primitive '''Values''', like ''integer'' or ''string''? - -The '''Tag''' element is the key. It give the type of the '''Value'''. In the previous picture, for instance, the second '''TLV''' code for an integer : ''''02'''' means "I contain an integer". One can read ''''02 01 01'''' as "''An integer (02) which is 1 byte long (01) and which value is 1 (01)''". - -So, let's see other '''Tags''' : 04 code for an ''Octet String''. Here, we have two empty strings : ''Octet String'' (04) zero '''Length''' (00) in the two last '''TLVs''' +Decoding a ''Primitive Value'' is somehow easy. Decoding a ''Constructed Value'' is a recursive process. We can't simply write a function that decode a '''TLV''' which call itself recursivly, it's too expensive and does not allow to treat deeply recursive '''TLV''' in a constrained memory environment. -0A (fourth '''TLV''') means ''Enumerated''. This is a way to code a constrained value (i.e something in a set of values). Here, it's a 0 : ''An enumerated (0A) value which is 1 byte long (01) and which value is 0 (00)''. It does not give you a lot of information, as you can see: which kind of value is it suppose to be? +We will use a stack which will store the current '''TLV''' until they are totally decoded (a totally decoded '''TLV''' is either a ''Primitive'', or a ''Constructed'' which value has been read). -So far, so good, we have a kind of way to decode simple '''TLV'''. Let's call them '''Primitive'''. What about '''TLVs''' that contains other '''TLVs'''? We will call them '''Constructed''' +The '''TLV''' we exposed on top of this page could be seen as a tree. To decode it, we need to walk this tree. The next picture show the sample as a tree : -The first '''TLV''' has a '''Tag''' value of 30. This is a ''SEQUENCE'' of '''TLVs'''. A ''SEQUENCE'' is constructed by ordered '''TLVs'''. We can't exchange two '''TLVs''' in a ''SEQUENCE'', there is another '''Tag''' for that : a ''SET''. +attachment:TlvTree.png -The last '''TLV''' has a '''Tag''' value of 61. This is specific of a '''CHOICE''', where you have to choose between different cases, and here it's the first value that has been chosen (we can read 61 has a '''SEQUENCE''' number 1 of the alternative. Accept the explanation, it's quite complicated to give the reason why 61 is a '''SEQUENCE''' while 30 is also a '''SEQUENCE'''). +Each '''TLV''' in this picture has two length : the inner length (''l'', blue) and the outer length (''L'', red). Inner length is the '''Length''' part of a '''TLV'''. Outer length equals length('''Tag''') + length('''Length''') + inner length. We can see that the inner length of a constructed '''TLV''' equals the sum of all the outer length of each of its children. +This drive us to the fact that a constructed '''TLV''' is totally decoded when this sum equals its inner length. -For any further information, one should read [http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf] which explain this encoding, but be aware that you also need to read [http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf]. They are available for free, which is quite cheap compared to sleeping pills ! +The next picture show the stack trace created while decoding the sample '''TLV''' : -=== Decoding TLVs === +attachment:TlvStack.png === Encoding TLVs === - -=== TLVs : Novlang ? ===
