PeekLength Method
PeekLength(Int32)
Skip the tagLength
bytes and read the length octets, decode and return
the length without advancing the reader.
public int PeekLength(int tagLength = 1)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | tagLength | The length of the tag to read. If this argument is not given the default length of 1 will be used. |
Returns
The length.
Exceptions
Type | Condition |
---|---|
TlvException | The |
Remarks
For example, if the next byte after the tag is 0x20, this method will return 0x00000020. If the next bytes are 0x81 80, this method will return 0x00000080. For 0x82 01 00, the return is 0x0000000100.
If the next byte after the tag is an invalid or unsupported initial length octet, the method will throw an exception. Invalid initial length octets are those where the most significant bit is set but the value is not 0x81, 0x82, or 0x83. Although 0x84, and 0x85 (and higher) are valid initial length octets, they are not supported by this class. Note that 0x80 is an unsupported initial length octet (it is BER for indefinite length, and this class supports only DER length rules).
If there are not enough bytes left in the encoding to build the
length, this method will throw an exception. For example, if there
are only two bytes left in encoding, and you pass in 2 for the tag
length, this method will throw an exception. If there are two bytes
left in encoding, you pass in 1 as the tagLength
, and the initial
length octet is0x81 (which means there should be one length octet
following the 0x81), the method will throw an exception.
Note that this method does not verify if there are at least length octets available in the encoding after the tag and length, it only returns the length and the number of octets that make up the length. For example, if at the current pointer in the encoding are the octets 01 81 80, and nothing more, the method will return 0x00000080. If you try to decode (i.e. call ReadValue), that will throw an exception. But this method will return the length, even though the encoding is not valid.