pyndn.encoding.tlv package

Submodules

pyndn.encoding.tlv.tlv module

This module defines the Tlv class with type codes for the NDN-TLV wire format.

class pyndn.encoding.tlv.tlv.Tlv[source]

Bases: object

Action = 131
Any = 19
ChildSelector = 17
Content = 21
ContentType = 24
ContentType_Default = 0
ContentType_Key = 2
ControlParameters_ControlParameters = 104
ControlParameters_Cost = 106
ControlParameters_ExpirationPeriod = 109
ControlParameters_FaceId = 105
ControlParameters_Flags = 108
ControlParameters_LocalControlFeature = 110
ControlParameters_Origin = 111
ControlParameters_Strategy = 107
ControlParameters_Uri = 114
Data = 6
Encrypt_BlackIntervalList = 142
Encrypt_EncryptedContent = 130
Encrypt_EncryptedPayload = 132
Encrypt_EncryptionAlgorithm = 131
Encrypt_EndDate = 135
Encrypt_InitialVector = 133
Encrypt_IntervalEndHour = 137
Encrypt_IntervalStartHour = 136
Encrypt_NRepeats = 138
Encrypt_RepeatUnit = 139
Encrypt_RepetitiveInterval = 140
Encrypt_Schedule = 143
Encrypt_StartDate = 134
Encrypt_WhiteIntervalList = 141
Exclude = 16
FaceID = 132
FaceInstance = 128
FinalBlockId = 26
ForwardingEntry = 129
ForwardingFlags = 138
FreshnessPeriod = 25
Host = 134
IPProto = 133
Interest = 5
InterestLifetime = 12
KeyLocator = 28
KeyLocatorDigest = 29
LpPacket_CachePolicy = 820
LpPacket_CachePolicyType = 821
LpPacket_FragCount = 83
LpPacket_FragIndex = 82
LpPacket_Fragment = 80
LpPacket_IGNORE_MAX = 959
LpPacket_IGNORE_MIN = 800
LpPacket_IncomingFaceId = 817
LpPacket_LpPacket = 100
LpPacket_Nack = 800
LpPacket_NackReason = 801
LpPacket_NextHopFaceId = 816
LpPacket_Sequence = 81
MaxSuffixComponents = 14
MetaInfo = 20
MinSuffixComponents = 13
MulticastInterface = 136
MulticastTTL = 137
MustBeFresh = 18
Name = 7
NameComponent = 8
NfdCommand_ControlResponse = 101
NfdCommand_StatusCode = 102
NfdCommand_StatusText = 103
Nonce = 10
Port = 135
PublisherPublicKeyLocator = 15
SelectedDelegation = 32
Selectors = 9
SignatureInfo = 22
SignatureType = 27
SignatureType_DigestSha256 = 0
SignatureType_SignatureHmacWithSha256 = 4
SignatureType_SignatureSha256WithEcdsa = 3
SignatureType_SignatureSha256WithRsa = 1
SignatureValue = 23
StatusCode = 139
StatusResponse = 130
StatusText = 140

pyndn.encoding.tlv.tlv_decoder module

class pyndn.encoding.tlv.tlv_decoder.TlvDecoder(input)[source]

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.
finishNestedTlvs(endOffset)[source]

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.
getOffset()[source]

Get the offset into the input buffer, used for the next read.

Returns:The offset.
Return type:int
getSlice(beginOffset, endOffset)[source]

Return an array of a slice of the input for the given offset range.

Parameters:
  • beginOffset (int) – The offset in the input of the beginning of the slice.
  • endOffset (int) – The offset in the input of the end of the slice.
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

peekType(expectedType, endOffset)[source]

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:
  • expectedType (int) – The expected type.
  • endOffset (int) – The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Returns:

True if the type of the next TLV is the expectedType, otherwise False.

Return type:

bool

readBlobTlv(expectedType)[source]

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.
readBooleanTlv(expectedType, endOffset)[source]

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:
  • expectedType (int) – The expected type.
  • endOffset (int) – The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Returns:

Return True, or else False if the next TLV doesn’t have the expected type.

Return type:

bool

readExtendedVarNumber(firstOctet)[source]

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
readNestedTlvsStart(expectedType)[source]

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.
readNonNegativeInteger(length)[source]

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.
readNonNegativeIntegerTlv(expectedType)[source]

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.
readOptionalBlobTlv(expectedType, endOffset)[source]

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:
  • expectedType (int) – The expected type.
  • endOffset (int) – The offset of the end of the parent TLV, returned by readNestedTlvsStart.
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

readOptionalNonNegativeIntegerTlv(expectedType, endOffset)[source]

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:
  • expectedType (int) – The expected type.
  • endOffset (int) – The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Returns:

The integer or None if the next TLV doesn’t have the expected type.

Return type:

int

readOptionalNonNegativeIntegerTlvAsFloat(expectedType, endOffset)[source]

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:
  • expectedType (int) – The expected type.
  • endOffset (int) – The offset of the end of the parent TLV, returned by readNestedTlvsStart.
Returns:

The integer or None if the next TLV doesn’t have the expected type.

Return type:

float

readTypeAndLength(expectedType)[source]

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.
readVarNumber()[source]

Decode VAR-NUMBER in NDN-TLV and return it. Update offset.

Returns:The decoded VAR-NUMBER.
Return type:int
seek(offset)[source]

Set the offset into the input, used for the next read.

Parameters:offset (int) – The new offset.

pyndn.encoding.tlv.tlv_encoder module

class pyndn.encoding.tlv.tlv_encoder.TlvEncoder(initialCapacity=16)[source]

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.
getOutput()[source]

Get a memoryview slice of the encoded bytes.

Returns:a memoryview backed by the bytearray encoding buffer.
Return type:memoryview
writeBlobTlv(type, value)[source]

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:
  • type (int) – The type of the TLV.
  • value (bytearray or memoryview) – The byte array with the bytes of the blob. If value is None, then just write the type and length 0.
writeBuffer(buffer)[source]

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.
writeNonNegativeInteger(value)[source]

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.
writeNonNegativeIntegerTlv(type, value)[source]

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:
  • type (int) – The type of the TLV.
  • value (int) – The non-negative integer to encode.
writeOptionalBlobTlv(type, value)[source]

If the byte array is None or zero length then do nothing, otherwise call writeBlobTlv.

Parameters:
  • type (int) – The type of the TLV.
  • value (bytearray or memoryview) – If None or zero length do nothing, otherwise the byte array with the bytes of the blob.
writeOptionalNonNegativeIntegerTlv(type, value)[source]

If value is negative or None then do nothing, otherwise call writeNonNegativeIntegerTlv.

Parameters:
  • type (int) – The type of the TLV.
  • value (int) – If negative or None do nothing, otherwise the integer to encode.
writeOptionalNonNegativeIntegerTlvFromFloat(type, value)[source]

If value is negative or None then do nothing, otherwise call writeNonNegativeIntegerTlv.

Parameters:
  • type (int) – The type of the TLV.
  • value (float) – If negative or None do nothing, otherwise use int(round(value)).
writeTypeAndLength(type, length)[source]

Encode the type and length as VAR-NUMBER and write to self._output just before self._length from the back. Advance self._length.

Parameters:
  • type (int) – The type of the TLV.
  • length (int) – The non-negative length of the TLV.
writeVarNumber(varNumber)[source]

Encode varNumber as a VAR-NUMBER in NDN-TLV and write it to self._output just before self._length from the back. Advance self._length.

Parameters:varNumber (int) – The non-negative number to encode.

pyndn.encoding.tlv.tlv_structure_decoder module

This module defines the TlvStructureDecoder class.

class pyndn.encoding.tlv.tlv_structure_decoder.TlvStructureDecoder[source]

Bases: object

Create and initialize a TlvStructureDecoder.

READ_LENGTH = 2
READ_LENGTH_BYTES = 3
READ_TYPE = 0
READ_TYPE_BYTES = 1
READ_VALUE_BYTES = 4
findElementEnd(input)[source]

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
getOffset()[source]

Get the current offset into the input buffer.

Returns:The offset.
Return type:int
seek(offset)[source]

Set the offset into the input, used for the next read.

Parameters:offset (int) – The new offset.

Module contents