|
| 1 | +.. _tutorial-dyn_byteorder: |
| 2 | + |
| 3 | +Dynamic Byte Order |
| 4 | +================== |
| 5 | + |
| 6 | +In addition to traditional byte order types, *caterpillar* supports a dynamic byte order |
| 7 | +based on the current pack or unpack context. |
| 8 | + |
| 9 | +.. versionadded:: 2.6.4 |
| 10 | + This feature is available in starting from version ``2.6.4`` |
| 11 | + |
| 12 | + |
| 13 | +There are various use-cases that require a struct to handle both big-endian and |
| 14 | +little-endian. In order to reduce the amount of code for these structs, *caterpillar* |
| 15 | +introduces a special byte order type: :class:`~caterpillar.byteorder.DynByteOrder`. It |
| 16 | +supports two different configuration levels: |
| 17 | + |
| 18 | +1. struct-wide: endianess is configured when calling :func:`~caterpillar.model.pack` or |
| 19 | + :func:`~caterpillar.model.unpack`. |
| 20 | +2. field-level: byte order is applied per field |
| 21 | + |
| 22 | +Each of these configuration levels support various methods of selecting the target endian: |
| 23 | + |
| 24 | +* global configuration using an additional keyword argument in :func:`~caterpillar.model.pack` or |
| 25 | + :func:`~caterpillar.model.unpack`. |
| 26 | +* context-key: configuration based on a value within the current context |
| 27 | +* custom function: endian is derived from a custom function |
| 28 | + |
| 29 | +Example: struct-wide dynamic byte order |
| 30 | +--------------------------------------- |
| 31 | + |
| 32 | +Let's consider the following struct definition. The dynamic endian configuration will be applied |
| 33 | +to all fields that haven't got an endian already set. |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | + :linenos: |
| 37 | +
|
| 38 | + from caterpillar.context import CTX_ORDER |
| 39 | +
|
| 40 | + @struct(order=Dynamic) |
| 41 | + class Format: |
| 42 | + a: uint16 # litte endian or big endian is decided using |
| 43 | + b: uint32 # a global context variable |
| 44 | +
|
| 45 | + config = {CTX_ORDER: BigEndian} |
| 46 | + obj = Format(a=0x1234, b=0x56789ABC) |
| 47 | +
|
| 48 | + # pack the object using BigEndian |
| 49 | + pack(obj, **config) |
| 50 | +
|
| 51 | + # now pack with little endian |
| 52 | + config[CTX_ORDER] = LittleEndian |
| 53 | + pack(obj, **config) |
| 54 | +
|
| 55 | +Here we pass an additional global context variable named :attr:`~caterpillar.context.CTX_ORDER` (``"_order"``) |
| 56 | +to the packing and unpacking process. The dynamic endian will automatically infer the order based on this |
| 57 | +global variable. |
| 58 | + |
| 59 | +Example: field-level dynamic byte order |
| 60 | +--------------------------------------- |
| 61 | + |
| 62 | +The same concept as shown above can be applied to single fields too. By default, the endian to use must be |
| 63 | +given as a global context variable as described before. |
| 64 | + |
| 65 | +.. code-block:: python |
| 66 | + :linenos: |
| 67 | +
|
| 68 | + from caterpillar.context import CTX_ORDER |
| 69 | +
|
| 70 | + @struct |
| 71 | + class Format: |
| 72 | + a: uint16 |
| 73 | + b: Dynamic + uint32 # only this field will be affected |
| 74 | +
|
| 75 | + # packing and unpacking is the same as in the previous example |
| 76 | +
|
| 77 | +
|
| 78 | +Example: context key reference |
| 79 | +------------------------------ |
| 80 | + |
| 81 | +Sometimes format specifications use a special field indicating whether all following |
| 82 | +fields are using big or little endian. To implement this kind of endian selection, a |
| 83 | +so called *context key* can be specified, which can take one of the following forms: |
| 84 | + |
| 85 | +* direct reference: just a string reference |
| 86 | + |
| 87 | + .. code-block:: python |
| 88 | +
|
| 89 | + # ... |
| 90 | + spec: uint8 |
| 91 | + number: Dynamic(key="spec") |
| 92 | + # ... |
| 93 | +
|
| 94 | +* context-lambda: a function that takes the current context as its first parameter and |
| 95 | + returns the target endian configuration value. |
| 96 | + |
| 97 | + .. code-block:: python |
| 98 | +
|
| 99 | + # ... |
| 100 | + spec: uint8 |
| 101 | + number: Dynamic(key=this.spec) |
| 102 | + # ... |
| 103 | +
|
| 104 | +The target endianess is decided based on the context value: |
| 105 | + |
| 106 | +* :code:`str` will be applied directly as the format character |
| 107 | +* any object storing a :code:`ch` string value |
| 108 | +* any other object is converted to a :code:`bool` and the following mapping is applied: |
| 109 | + |
| 110 | + * :code:`True`: :attr:`~caterpillar.byteorder.LittleEndian` |
| 111 | + * :code:`False`: :attr:`~caterpillar.byteorder.BigEndian` |
| 112 | + |
| 113 | +As an example, consider the following definition: |
| 114 | + |
| 115 | +.. code-block:: python |
| 116 | + :linenos: |
| 117 | +
|
| 118 | + @struct(order=BigEndian) |
| 119 | + class Format: |
| 120 | + spec: uint8 = 0 |
| 121 | + a: DynByteOrder(key=this.spec) + uint16 |
| 122 | + # alternatively |
| 123 | + # a: Dynamic(this.spec) + uint16 |
| 124 | + b: uint32 |
| 125 | +
|
| 126 | + # packing and unpacking does not require the extra endian value |
| 127 | + obj = Format(spec=0, a=0x1234, b=0x56789ABC) |
| 128 | + # 0 -> False, results in BigEndian |
| 129 | + data_be = pack(obj) |
| 130 | +
|
| 131 | + # 1 -> True, results in LittleEndian |
| 132 | + obj.spec = 1 |
| 133 | + data_le = pack(obj) |
| 134 | +
|
0 commit comments