title: How to decode infinite length ending with two zero bytes
body: <p>I am decoding an array of bytes which are in <code>TLV</code> format (Tag-Length-Value). Sometimes the <code>Length</code> of a <code>Value</code> is infinite. i.e I have to read the succeeding bytes until I get to <strong>two consequent zero bytes</strong> meaning the end of <code>Value</code>. The problem is when the <code>Value</code> of a TLV is itself constructed of another TLV with infinite <code>Length</code>. I am trying to figure out the best way to decode such TLVs with nested infinite values. </p>
  
  <p>In the following example, each square indicates one byte. After reading <code>L1</code>, I realize that the length of <code>V1</code> 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 <code>L1</code> indicate the end of <code>V3</code>, not <code>V1</code>:</p>
  
  <pre><code>                                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 
  </code></pre>
  
  <p>I need a method/algorithm to detect the end of <code>V1</code>. Any advice would be appreciated!</p>