Class: TlvDecoder

TlvDecoder

new TlvDecoder(input)

Create a new TlvDecoder for decoding the input in the NDN-TLV wire format.
Parameters:
Name Type Description
input Buffer The buffer with the bytes to decode.
Source:

Methods

finishNestedTlvs(endOffset)

Call this after reading all nested TLVs to skip any remaining unrecognized TLVs and to check if the offset after the final nested TLV matches the endOffset returned by readNestedTlvsStart.
Parameters:
Name Type Description
endOffset number The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Source:
Throws:
DecodingException if the TLV length does not equal the total length of the nested TLVs.

getOffset() → {number}

Get the offset into the input, used for the next read.
Source:
Returns:
The offset.
Type
number

getSlice(beginOffset, endOffset) → {Buffer}

Return an array of a slice of the input for the given offset range.
Parameters:
Name Type Description
beginOffset number The offset in the input of the beginning of the slice.
endOffset number The offset in the input of the end of the slice.
Source:
Returns:
The bytes in the value as a slice on the buffer. This is not a copy of the bytes in the input buffer. If you need a copy, then you must make a copy of the return value.
Type
Buffer

peekType(expectedType, endOffset) → {boolean}

Decode the type from this's input starting at offset, and if it is the expectedType, then return true, else false. However, if this's offset is greater than or equal to endOffset, then return false and don't try to read the type. Do not update offset.
Parameters:
Name Type Description
expectedType number The expected type.
endOffset number The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Source:
Returns:
true if the type of the next TLV is the expectedType, otherwise false.
Type
boolean

readBlobTlv(expectedType) → {Buffer}

Decode the type and length from this's input starting at offset, expecting the type to be expectedType. Then return an array of the bytes in the value. Update offset.
Parameters:
Name Type Description
expectedType number The expected type.
Source:
Throws:
DecodingException if did not get the expected TLV type.
Returns:
The bytes in the value as a slice on the buffer. This is not a copy of the bytes in the input buffer. If you need a copy, then you must make a copy of the return value.
Type
Buffer

readBooleanTlv(expectedType, endOffset) → {boolean}

Peek at the next TLV, and if it has the expectedType then read a type and value, ignoring the value, and return true. Otherwise, return false. However, if this's offset is greater than or equal to endOffset, then return false and don't try to read the type.
Parameters:
Name Type Description
expectedType number The expected type.
endOffset number The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Source:
Returns:
true, or else false if the next TLV doesn't have the expected type.
Type
boolean

readExtendedVarNumber(firstOctet) → {number}

A private function to do the work of readVarNumber, given the firstOctet which is >= 253.
Parameters:
Name Type Description
firstOctet number The first octet which is >= 253, used to decode the remaining bytes.
Source:
Returns:
The decoded VAR-NUMBER.
Type
number

readNestedTlvsStart(expectedType) → {number}

Decode the type and length from the input starting at offset, expecting the type to be expectedType. Update offset. Also make sure the decoded length does not exceed the number of bytes remaining in the input. Return the offset of the end of this parent TLV, which is used in decoding optional nested TLVs. After reading all nested TLVs, call finishNestedTlvs.
Parameters:
Name Type Description
expectedType number The expected type.
Source:
Throws:
DecodingException if did not get the expected TLV type or the TLV length exceeds the buffer length.
Returns:
The offset of the end of the parent TLV.
Type
number

readNonNegativeInteger(length) → {number}

Decode a non-negative integer in NDN-TLV and return it. Update offset by length.
Parameters:
Name Type Description
length number The number of bytes in the encoded integer.
Source:
Throws:
DecodingException if length is an invalid length for a TLV non-negative integer.
Returns:
The integer.
Type
number

readNonNegativeIntegerTlv(expectedType) → {number}

Decode the type and length from this's input starting at offset, expecting the type to be expectedType. Then decode a non-negative integer in NDN-TLV and return it. Update offset.
Parameters:
Name Type Description
expectedType number The expected type.
Source:
Throws:
DecodingException if did not get the expected TLV type or can't decode the value.
Returns:
The integer.
Type
number

readOptionalBlobTlv(expectedType, endOffset) → {Buffer}

Peek at the next TLV, and if it has the expectedType then call readBlobTlv and return the value. Otherwise, return null. However, if this's offset is greater than or equal to endOffset, then return null and don't try to read the type.
Parameters:
Name Type Description
expectedType number The expected type.
endOffset number The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Source:
Returns:
The bytes in the value as a slice on the buffer or null if the next TLV doesn't have the expected type. This is not a copy of the bytes in the input buffer. If you need a copy, then you must make a copy of the return value.
Type
Buffer

readOptionalNonNegativeIntegerTlv(expectedType, endOffset) → {number}

Peek at the next TLV, and if it has the expectedType then call readNonNegativeIntegerTlv and return the integer. Otherwise, return null. However, if this's offset is greater than or equal to endOffset, then return null and don't try to read the type.
Parameters:
Name Type Description
expectedType number The expected type.
endOffset number The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Source:
Returns:
The integer or null if the next TLV doesn't have the expected type.
Type
number

readTypeAndLength(expectedType) → {number}

Decode the type and length from this's input starting at offset, expecting the type to be expectedType and return the length. Update offset. Also make sure the decoded length does not exceed the number of bytes remaining in the input.
Parameters:
Name Type Description
expectedType number The expected type.
Source:
Throws:
DecodingException if (did not get the expected TLV type or the TLV length exceeds the buffer length.
Returns:
The length of the TLV.
Type
number

readVarNumber() → {number}

Decode VAR-NUMBER in NDN-TLV and return it. Update offset.
Source:
Returns:
The decoded VAR-NUMBER.
Type
number

seek(offset)

Set the offset into the input, used for the next read.
Parameters:
Name Type Description
offset number The new offset.
Source: