Skip to content

flyingcoconut/xdrparser

Repository files navigation

Not recommended for production!

xdr

External Data Representation (XDR) is a standard data serialization format. It allows data to be transferred between different computer systems.

XDR uses a base unit of 4 bytes, serialized in big-endian order; smaller data types still occupy four bytes each after encoding. Variable-length types such as string and opaque are padded to a total divisible by four bytes. Floating-point numbers are represented in IEEE 754 format.

Wikipedia article

xdrparser

The xdrparser library define multiple data type, support encoding, decoding, and custom filters. This library aim to follow some semantic like the python json library (dump, dumps, load, loads).

Notes

This library aim to support xdr serialisation, not ONC RPC (Sun RPC).
As such their is no support for rpc file parsing (rpcgen).
It will be implemented as another library.

Standards

RFC 1832
RFC 4506

Supported Types

  • Integer
  • Unsigned Integer
  • Boolean
  • Hyper Integer
  • Unsigned Hyper Integer
  • Floating-Point
  • Double-Precision Floating-Point
  • Fixed-Length Opaque Data
  • Variable-Length Opaque Data
  • String
  • Variable-Length String
  • Fixed-Length Array
  • Variable-Length Array
  • Structure
  • Discriminated Union
  • Void
  • Optional
  • Linked List
  • Enum

Unsupported types

  • Quadruple-Precision Floating-Point

Installation

pip3 install .

Examples

Simple serialization

from xdrparser.types import XdrStruct
from xdrparser.types import XdrInt
from xdrparser.types import XdrVariableString

class Message(XdrStruct):
    message_id = XdrInt()
    mfrom = XdrVariableString(encoding='utf-8')
    to = XdrVariableString(encoding='utf-8')
    message = XdrVariableString(encoding='utf-8')

m = Message()
value = m.dumps({"message_id": 45, "mfrom": "Foo", "to": "Bar", "message": "This is a message"})
print(value)

Output

b'\x00\x00\x00-\x00\x00\x00\x03Foo\x00\x00\x00\x00\x03Bar\x00\x00\x00\x00\x11This is a message\x00\x00\x00'

Filters

from xdrparser.types import XdrInt
from xdrparser.filters import XdrFilter

class FooFilter(XdrFilter):
    def __call__(self, obj, dump=False):
        if dump:
            int(obj)
        str(obj)

foo = XdrInt(xdr_filter=FooFilter)

f.dumps("42")

Implementation

xdrparser is built with three main components:

  • Types: These are classes representing the various XDR data types. They define how data is structured and its basic serialization/deserialization logic.

  • Filters: Filters provide a way to transform data during the serialization and deserialization process. When dumping (serializing): data flows through the filter before it's passed to the serializer. When loading (deserializing): data flows through the filter after being processed by the serializer.

  • Serializers: Serializers handle the low-level details of converting objects into bytes and vice-versa. They are responsible for packing and unpacking data according to the XDR standard.

Object flow and serialization/deserialization

Dumping (from object to bytes)
object -> filter -> object -> serializer -> bytes

Loading (from bytes to object)
bytes -> serializer -> object -> filter -> object

About

Python xdr parser

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published