This module defines the Tlv class with type codes for the NDN-TLV wire format.
Bases: object
Bases: object
Create a new TlvDecoder to decode the input using NDN-TLV.
Parameters: | input (An array type with int elements) – The array with the bytes to decode. |
---|
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: | endOffset (int) – The offset of the end of the parent TLV, returned by readNestedTlvsStart. |
---|---|
Raises ValueError: | |
if the TLV length does not equal the total length of the nested TLVs. |
Get the offset into the input buffer, used for the next read.
Returns: | The offset. |
---|---|
Return type: | int |
Return an array of a slice of the input for the given offset range.
Parameters: |
|
---|---|
Returns: | The bytes in the value as a slice on the byte array. This is not necessarily a copy of the bytes in the input buffer. If you need a copy, then you must make a copy of the return value. |
Return type: | memoryview or equivalent |
Decode the type from self’s input starting at offset, and if it is the expectedType, then return True, else False. However, if self’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: |
|
---|---|
Returns: | True if the type of the next TLV is the expectedType, otherwise False. |
Return type: | bool |
Decode the type and length from self’s input starting at offset, expecting the type to be expectedType. Then return an array of the bytes in the value. Update offset.
Parameters: | expectedType (int) – The expected type. |
---|---|
Returns: | The bytes in the value as a slice on the byte array. This is not necessarily a copy of the bytes in the input buffer. If you need a copy, then you must make a copy of the return value. |
Return type: | memoryview or equivalent |
Raises ValueError: | |
if did not get the expected TLV type. |
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 self’s offset is greater than or equal to endOffset, then return False and don’t try to read the type.
Parameters: |
|
---|---|
Returns: | Return True, or else False if the next TLV doesn’t have the expected type. |
Return type: | bool |
A private function to do the work of readVarNumber, given the firstOctet which is >= 253.
Parameters: | firstOctet (int) – The first octet which is >= 253, used to decode the remaining bytes. |
---|---|
Returns: | The decoded VAR-NUMBER. |
Return type: | int |
Decode the type and length from self’s 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: | expectedType (int) – The expected type. |
---|---|
Returns: | The offset of the end of the parent TLV. |
Return type: | int |
Raises ValueError: | |
if did not get the expected TLV type or the TLV length exceeds the buffer length. |
Decode a non-negative integer in NDN-TLV and return it. Update offset by length.
Parameters: | length (int) – The number of bytes in the encoded integer. |
---|---|
Returns: | The integer. |
Return type: | int (or long if a large integer on a 32-bit system) |
Raises ValueError: | |
if length is an invalid length for a TLV non-negative integer. |
Decode the type and length from self’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: | expectedType (int) – The expected type. |
---|---|
Returns: | The integer. |
Return type: | int |
Raises ValueError: | |
if did not get the expected TLV type or can’t decode the value. |
Peek at the next TLV, and if it has the expectedType then call readBlobTlv and return the value. Otherwise, return None. However, if self’s offset is greater than or equal to endOffset, then return None and don’t try to read the type.
Parameters: |
|
---|---|
Returns: | The bytes in the value as a slice on the byte array or None if the next TLV doesn’t have the expected type. This is not necessarily a copy of the bytes in the input buffer. If you need a copy, then you must make a copy of the return value. |
Return type: | memoryview or equivalent |
Peek at the next TLV, and if it has the expectedType then call readNonNegativeIntegerTlv and return the integer. Otherwise, return None. However, if self’s offset is greater than or equal to endOffset, then return None and don’t try to read the type.
Parameters: |
|
---|---|
Returns: | The integer or None if the next TLV doesn’t have the expected type. |
Return type: | int |
Peek at the next TLV, and if it has the expectedType then call readNonNegativeIntegerTlv and return the integer converted to a float. Otherwise, return None. However, if self’s offset is greater than or equal to endOffset, then return None and don’t try to read the type.
Parameters: |
|
---|---|
Returns: | The integer or None if the next TLV doesn’t have the expected type. |
Return type: | float |
Decode the type and length from self’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: | expectedType (int) – The expected type. |
---|---|
Returns: | The length of the TLV. |
Return type: | int |
Raises ValueError: | |
if did not get the expected TLV type or the TLV length exceeds the buffer length. |
Bases: object
Create a new TlvEncoder with an initialCapacity for the encoding buffer.
Parameters: | initialCapacity (int) – (optional) The initial capacity of the encoding buffer. If omitted, use a default value. |
---|
Get a memoryview slice of the encoded bytes.
Returns: | a memoryview backed by the bytearray encoding buffer. |
---|---|
Return type: | memoryview |
Write the type, then the length of the blob then the blob value to self._output just before self._length from the back. Advance self._length.
Parameters: |
|
---|
Write the buffer value to self._output just before self._length from the back. Advance self._length.
Parameters: | buffer – The byte array with the bytes to write. If value is None, then do nothing. |
---|
Encode value as a non-negative integer and write it to self._output just before self._length from the back. Advance self._length.
Parameters: | value (int) – The non-negative integer to encode. |
---|
Write the type, then the length of the encoded value then encode value as a non-negative integer and write it to self._output just before self._length from the back. Advance self._length.
Parameters: |
|
---|
If the byte array is None or zero length then do nothing, otherwise call writeBlobTlv.
Parameters: |
|
---|
If value is negative or None then do nothing, otherwise call writeNonNegativeIntegerTlv.
Parameters: |
|
---|
If value is negative or None then do nothing, otherwise call writeNonNegativeIntegerTlv.
Parameters: |
|
---|
This module defines the TlvStructureDecoder class.
Bases: object
Create and initialize a TlvStructureDecoder.
Continue scanning input starting from self._offset to find the element end. If the end of the element which started at offset 0 is found, this returns True and getOffset() is the length of the element. Otherwise, this returns False which means you should read more into input and call again.
Parameters: | input (An array type with int elements) – The input buffer. You have to pass in input each time because the buffer could be reallocated. |
---|---|
Returns: | True if found the element end, False if not. |
Return type: | bool |