pyndn.encoding package¶
Submodules¶
pyndn.encoding.element_reader module¶
This module defines the ElementReader class which lets you call onReceivedData multiple times which uses a TlvStructureDecoder to detect the end of a TLV element, and calls elementListener.onReceivedElement(element) with the element. This handles the case where a single call to onReceivedData may contain multiple elements.
-
class
pyndn.encoding.element_reader.
ElementReader
(elementListener)[source]¶ Bases:
object
Create an ElementReader with the elementListener and an initial buffer for saving partial data.
-
onReceivedData
(data)[source]¶ Continue to read data until the end of an element, then call elementListener.onReceivedElement(element). The buffer passed to onReceivedElement is only valid during this call. If you need the data later, you must copy.
Parameters: data (An array type with int elements) – The buffer with the incoming element’s bytes.
-
pyndn.encoding.oid module¶
pyndn.encoding.protobuf_tlv module¶
This module defines the ProtobufTlv class which has static methods to encode and decode an Protobuf Message object as NDN-TLV. The Protobuf tag value is used as the TLV type code. A Protobuf message is encoded/decoded as a nested TLV encoding. Protobuf types uint32, uint64 and enum are encoded/decoded as TLV nonNegativeInteger. (It is an error if an enum value is negative.) Protobuf types bytes and string are encoded/decoded as TLV bytes. The Protobuf type bool is encoded/decoded as a TLV boolean (a zero length value for True, omitted for False). Other Protobuf types are an error.
Protobuf has no “outer” message type, so you need to put your TLV message inside an outer “typeless” message.
-
class
pyndn.encoding.protobuf_tlv.
ProtobufTlv
[source]¶ Bases:
object
-
static
decode
(message, input)[source]¶ Decode the input as NDN-TLV and update the fields of the Protobuf message object.
Parameters: - message (google.protobuf.message) – The Protobuf message object. This does not first clear the object.
- input (An array type with int elements) – The array with the bytes to decode.
-
static
encode
(message)[source]¶ Encode the Protobuf message object as NDN-TLV.
Parameters: message (google.protobuf.message) – The Protobuf message object. This calls message.IsInitialized() to ensure that all required fields are present and raises an exception if not. Returns: The encoded buffer in a Blob object. Return type: Blob
-
static
toName
(componentArray)[source]¶ Return a Name made from the component array in a Protobuf message object, assuming that it was defined with “repeated bytes”. For example: message Name { repeated bytes component = 8; }
Parameters: componentArray (Array) – The array from the Protobuf message object representing the “repeated bytes” component array. Returns: A new Name. Return type: Name
-
static
pyndn.encoding.tlv_0_1_1_wire_format module¶
This module defines the Tlv0_1_1WireFormat class which extends Tlv0_2WireFormat so that it is an alias in case any applications use Tlv0_1_1WireFormat directly. These two wire formats are the same except that Tlv0_2WireFormat adds support for the name component type ImplicitSha256Digest.
-
class
pyndn.encoding.tlv_0_1_1_wire_format.
Tlv0_1_1WireFormat
[source]¶ Bases:
pyndn.encoding.tlv_0_2_wire_format.Tlv0_2WireFormat
-
classmethod
get
()[source]¶ Get a singleton instance of a Tlv0_1_1WireFormat.
Returns: The singleton instance. Return type: Tlv0_1_1WireFormat
-
classmethod
pyndn.encoding.tlv_0_1_wire_format module¶
This module defines the Tlv0_1WireFormat class which extends Tlv0_1_1WireFormat so that it is an alias in case any applications use Tlv0_1WireFormat directly. These two wire formats are the same except that Tlv0_1_1WireFormat adds support for Sha256WithEcdsaSignature.
-
class
pyndn.encoding.tlv_0_1_wire_format.
Tlv0_1WireFormat
[source]¶ Bases:
pyndn.encoding.tlv_0_1_1_wire_format.Tlv0_1_1WireFormat
-
classmethod
get
()[source]¶ Get a singleton instance of a Tlv0_1WireFormat.
Returns: The singleton instance. Return type: Tlv0_1WireFormat
-
classmethod
pyndn.encoding.tlv_0_2_wire_format module¶
-
class
pyndn.encoding.tlv_0_2_wire_format.
Tlv0_2WireFormat
[source]¶ Bases:
pyndn.encoding.wire_format.WireFormat
-
decodeControlParameters
(controlParameters, input, copy=True)[source]¶ Decode input as an NDN-TLV ControlParameters and set the fields of the controlParameters object.
Parameters: - controlParameters (ControlParameters) – The ControlParameters object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
-
decodeControlResponse
(controlResponse, input, copy=True)[source]¶ Decode input as an NDN-TLV ControlResponse and set the fields of the controlResponse object.
Parameters: - controlResponse (ControlResponse) – The ControlResponse object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
-
decodeData
(data, input, copy=True)[source]¶ Decode input as an NDN-TLV data packet, set the fields in the data object, and return the signed offsets.
Parameters: - data (Data) – The Data object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Returns: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion.
Return type: (int, int)
-
decodeDelegationSet
(delegationSet, input, copy=True)[source]¶ Decode input as a DelegationSet in NDN-TLV and set the fields of the delegationSet object. Note that the sequence of Delegation does not have an outer TLV type and length because it is intended to use the type and length of a Data packet’s Content. This ignores any elements after the sequence of Delegation.
Parameters: - delegationSet (DelegationSet) – The DelegationSet object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
-
decodeEncryptedContent
(encryptedContent, input, copy=True)[source]¶ Decode input as an EncryptedContent v1 in NDN-TLV and set the fields of the encryptedContent object.
Parameters: - encryptedContent (EncryptedContent) – The EncryptedContent object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
-
decodeEncryptedContentV2
(encryptedContent, input, copy=True)[source]¶ Decode input as an EncryptedContent v2 (used in Name-based Access Control v2) in NDN-TLV and set the fields of the encryptedContent object. See https://github.com/named-data/name-based-access-control/blob/new/docs/spec.rst .
Parameters: - encryptedContent (EncryptedContent) – The EncryptedContent object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
-
decodeInterest
(interest, input, copy=True)[source]¶ Decode input as an NDN-TLV interest and set the fields of the interest object.
Parameters: - interest (Interest) – The Interest object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Returns: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
Return type: (int, int)
-
decodeLpPacket
(lpPacket, input, copy=True)[source]¶ Decode input as an NDN-TLV LpPacket and set the fields of the lpPacket object.
Parameters: - lpPacket (LpPacket) – The LpPacket object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
-
decodeName
(name, input, copy=True)[source]¶ Decode input as an NDN-TLV name and set the fields of the Name object.
Parameters: - name (Name) – The Name object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
-
decodeSignatureInfoAndValue
(signatureInfo, signatureValue, copy=True)[source]¶ Decode signatureInfo as a signature info and signatureValue as the related SignatureValue, and return a new object which is a subclass of Signature.
Parameters: - signatureInfo (An array type with int elements) – The array with the signature info input buffer to decode.
- signatureValue (An array type with int elements) – The array with the signature value input buffer to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Returns: A new object which is a subclass of Signature.
Return type: a subclass of Signature
-
encodeControlParameters
(controlParameters)[source]¶ Encode controlParameters and return the encoding.
Parameters: controlParameters (ControlParameters) – The ControlParameters object to encode. Returns: A Blob containing the encoding. Return type: Blob
-
encodeControlResponse
(controlResponse)[source]¶ Encode controlResponse and return the encoding.
Parameters: controlResponse (ControlResponse) – The ControlResponse object to encode. Returns: A Blob containing the encoding. Return type: Blob
-
encodeData
(data)[source]¶ Encode data in NDN-TLV and return the encoding and signed offsets.
Parameters: data (Data) – The Data object to encode. Returns: A Tuple of (encoding, signedPortionBeginOffset, signedPortionEndOffset) where encoding is a Blob containing the encoding, signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. Return type: (Blob, int, int)
-
encodeDelegationSet
(delegationSet)[source]¶ Encode the DelegationSet in NDN-TLV and return the encoding. Note that the sequence of Delegation does not have an outer TLV type and length because it is intended to use the type and length of a Data packet’s Content.
Parameters: delegationSet (DelegationSet) – The DelegationSet object to encode. Returns: A Blob containing the encoding. Return type: Blob
-
encodeEncryptedContent
(encryptedContent)[source]¶ Encode the EncryptedContent v1 in NDN-TLV and return the encoding.
Parameters: encryptedContent (EncryptedContent) – The EncryptedContent object to encode. Returns: A Blob containing the encoding. Return type: Blob
-
encodeEncryptedContentV2
(encryptedContent)[source]¶ Encode the EncryptedContent v2 (used in Name-based Access Control v2) in NDN-TLV and return the encoding. See https://github.com/named-data/name-based-access-control/blob/new/docs/spec.rst .
Parameters: encryptedContent (EncryptedContent) – The EncryptedContent object to encode. Returns: A Blob containing the encoding. Return type: Blob
-
encodeInterest
(interest)[source]¶ Encode interest in NDN-TLV and return the encoding.
Parameters: interest (Interest) – The Interest object to encode. Returns: A Tuple of (encoding, signedPortionBeginOffset, signedPortionEndOffset) where encoding is a Blob containing the encoding, signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). Return type: (Blob, int, int)
-
encodeName
(name)[source]¶ Encode name in NDN-TLV and return the encoding.
Parameters: name (Name) – The Name object to encode. Returns: A Blob containing the encoding. Return type: Blob
-
encodeSignatureInfo
(signature)[source]¶ Encode signature as an NDN-TLV SignatureInfo and return the encoding.
Parameters: signature (An object of a subclass of Signature) – An object of a subclass of Signature to encode. Returns: A Blob containing the encoding. Return type: Blob
-
encodeSignatureValue
(signature)[source]¶ Encode the signatureValue in the Signature object as an NDN-TLV SignatureValue (the signature bits) and return the encoding.
Parameters: signature (An object of a subclass of Signature) – An object of a subclass of Signature with the signature value to encode. Returns: A Blob containing the encoding. Return type: Blob
-
static
fromIsoString
(timeString)[source]¶ Convert an ISO time representation with the “T” in the middle to a UNIX timestamp.
Parameters: timeString (str) – The ISO time representation. Returns: The timestamp as milliseconds since Jan 1, 1970 UTC. Return type: float
-
classmethod
get
()[source]¶ Get a singleton instance of a Tlv0_2WireFormat. To always use the preferred version NDN-TLV, you should use TlvWireFormat.get().
Returns: The singleton instance. Return type: Tlv0_2WireFormat
-
pyndn.encoding.tlv_wire_format module¶
-
class
pyndn.encoding.tlv_wire_format.
TlvWireFormat
[source]¶ Bases:
pyndn.encoding.tlv_0_2_wire_format.Tlv0_2WireFormat
-
classmethod
get
()[source]¶ Get a singleton instance of a TlvWireFormat. Assuming that the default wire format was set with WireFormat.setDefaultWireFormat(TlvWireFormat.get()), you can check if this is the default wire encoding with if WireFormat.getDefaultWireFormat() == TlvWireFormat.get().
Returns: The singleton instance. Return type: TlvWireFormat
-
classmethod
pyndn.encoding.wire_format module¶
This module defines the WireFormat class which is an abstract base class for encoding and decoding Interest, Data, etc. with a specific wire format. You should use a derived class such as TlvWireFormat.
-
class
pyndn.encoding.wire_format.
WireFormat
[source]¶ Bases:
object
-
decodeControlParameters
(controlParameters, input, copy=True)[source]¶ Decode input as a controlParameters and set the fields of the controlParameters object. Your derived class should override.
Parameters: - controlParameters (ControlParameters) – The ControlParameters object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeControlResponse
(controlResponse, input, copy=True)[source]¶ Decode input as a controlResponse and set the fields of the controlResponse object. Your derived class should override.
Parameters: - controlResponse (ControlResponse) – The ControlResponse object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeData
(data, input, copy=True)[source]¶ Decode input as a data packet, set the fields in the data object, and return the signed offsets. Your derived class should override.
Parameters: - data (Data) – The Data object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Returns: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion.
Return type: (int, int)
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeDelegationSet
(delegationSet, input, copy=True)[source]¶ Decode input as a DelegationSet and set the fields of the delegationSet object. Your derived class should override.
Parameters: - delegationSet (DelegationSet) – The DelegationSet object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeEncryptedContent
(encryptedContent, input, copy=True)[source]¶ Decode input as an EncryptedContent v1 and set the fields of the encryptedContent object. Your derived class should override.
Parameters: - encryptedContent (EncryptedContent) – The EncryptedContent object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeEncryptedContentV2
(encryptedContent, input, copy=True)[source]¶ Decode input as an EncryptedContent v2 (used in Name-based Access Control v2) and set the fields of the encryptedContent object. See https://github.com/named-data/name-based-access-control/blob/new/docs/spec.rst . Your derived class should override.
Parameters: - encryptedContent (EncryptedContent) – The EncryptedContent object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeInterest
(interest, input, copy=True)[source]¶ Decode input as an interest and set the fields of the interest object. Your derived class should override.
Parameters: - interest (Interest) – The Interest object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Returns: A Tuple of (signedPortionBeginOffset, signedPortionEndOffset) where signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest).
Return type: (int, int)
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeLpPacket
(lpPacket, input, copy=True)[source]¶ Decode input as an LpPacket and set the fields of the lpPacket object. Your derived class should override.
Parameters: - lpPacket (LpPacket) – The LpPacket object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeName
(name, input, copy=True)[source]¶ Decode input as a name and set the fields of the Name object. Your derived class should override.
Parameters: - name (Name) – The Name object whose fields are updated.
- input (An array type with int elements) – The array with the bytes to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
decodeSignatureInfoAndValue
(signatureInfo, signatureValue, copy=True)[source]¶ Decode signatureInfo as a signature info and signatureValue as the related SignatureValue, and return a new object which is a subclass of Signature. Your derived class should override.
Parameters: - signatureInfo (An array type with int elements) – The array with the signature info input buffer to decode.
- signatureValue (An array type with int elements) – The array with the signature value input buffer to decode.
- copy (bool) – (optional) If True, copy from the input when making new Blob values. If False, then Blob values share memory with the input, which must remain unchanged while the Blob values are used. If omitted, use True.
Returns: A new object which is a subclass of Signature.
Return type: a subclass of Signature
Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeControlParameters
(controlParameters)[source]¶ Encode controlParameters and return the encoding. Your derived class should override.
Parameters: controlParameters (ControlParameters) – The ControlParameters object to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeControlResponse
(controlResponse)[source]¶ Encode controlResponse and return the encoding. Your derived class should override.
Parameters: controlResponse (ControlResponse) – The ControlResponse object to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeData
(data)[source]¶ Encode data and return the encoding and signed offsets. Your derived class should override.
Parameters: data (Data) – The Data object to encode. Returns: A Tuple of (encoding, signedPortionBeginOffset, signedPortionEndOffset) where encoding is a Blob containing the encoding, signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. Return type: (Blob, int, int) Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeDelegationSet
(delegationSet)[source]¶ Encode the DelegationSet and return the encoding. Your derived class should override.
Parameters: delegationSet (DelegationSet) – The DelegationSet object to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeEncryptedContent
(encryptedContent)[source]¶ Encode the EncryptedContent v1 and return the encoding. Your derived class should override.
Parameters: encryptedContent (EncryptedContent) – The EncryptedContent object to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeEncryptedContentV2
(encryptedContent)[source]¶ Encode the EncryptedContent v2 (used in Name-based Access Control v2) and return the encoding. See https://github.com/named-data/name-based-access-control/blob/new/docs/spec.rst . Your derived class should override.
Parameters: encryptedContent (EncryptedContent) – The EncryptedContent object to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeInterest
(interest)[source]¶ Encode interest and return the encoding. Your derived class should override.
Parameters: interest (Interest) – The Interest object to encode. Returns: A Tuple of (encoding, signedPortionBeginOffset, signedPortionEndOffset) where encoding is a Blob containing the encoding, signedPortionBeginOffset is the offset in the encoding of the beginning of the signed portion, and signedPortionEndOffset is the offset in the encoding of the end of the signed portion. The signed portion starts from the first name component and ends just before the final name component (which is assumed to be a signature for a signed interest). Return type: (Blob, int, int) Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeName
(name)[source]¶ Encode name and return the encoding. Your derived class should override.
Parameters: name (Name) – The Name object to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeSignatureInfo
(signature)[source]¶ Encode signature as a SignatureInfo and return the encoding. Your derived class should override.
Parameters: signature (An object of a subclass of Signature) – An object of a subclass of Signature to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-
encodeSignatureValue
(signature)[source]¶ Encode the signatureValue in the Signature object as a SignatureValue (the signature bits) and return the encoding. Your derived class should override.
Parameters: signature (An object of a subclass of Signature) – An object of a subclass of Signature with the signature value to encode. Returns: A Blob containing the encoding. Return type: Blob Raises: RuntimeError – for unimplemented if the derived class does not override.
-