I am decoding an array of bytes which are in TLV format (Tag-Length-Value). Sometimes the Length of a Value is infinite. i.e I have to read the succeeding bytes until I get to two consequent zero bytes meaning the end of Value. The problem is when the Value of a TLV is itself constructed of another TLV with infinite Length. I am trying to figure out the best way to decode such TLVs with nested infinite values.
In the following example, each square indicates one byte. After reading L1, I realize that the length of V1 is infinite, which means that I have to read the succeeding bytes until I get to two zero bytes. But the first two zero bytes after L1 indicate the end of V3, not V1:
V3
.
| end of V2
\ -----------
+----+----+----+----+----+----+----+----+----+----+----+----+----+
| T1 | L1 | T2 | L2 | T3 | L3 | 13 | 0 | 0 | 0 | 0 | 0 | 0 |
+----+----/----+----/----+----/----+----+----+----+----+----+----+
| | | ----------- -----------
| | | end of V3 end of V1
\ | |
V1 Starts here | |
| |
\ |
V2 Starts here |
|
\
V3 starts here
I need a method/algorithm to detect the end of V1. Any advice would be appreciated!
A simple approach is to use a recursive function: each time the function reads until it finds two double zeros, and then returns that position, so that the previous invocation knows where to continue looking for its own double zero terminator.
Alternatively, you could track the nesting level with an integer: increase each time you start reading a new TLV, decrease it when it finds a double zero. The reading is over when the nesting level is zero again.