| 1 |
About |
| 2 |
===== |
| 3 |
|
| 4 |
This module is intended to have a similar interface as the python |
| 5 |
struct module, but working on bits instead of primitive data types |
| 6 |
(char, int, ...). |
| 7 |
|
| 8 |
Project homepage: https://github.com/eerimoq/bitstruct |
| 9 |
|
| 10 |
Documentation: https://bitstruct.readthedocs.io |
| 11 |
|
| 12 |
Installation |
| 13 |
============ |
| 14 |
|
| 15 |
.. code-block:: python |
| 16 |
|
| 17 |
pip install bitstruct |
| 18 |
|
| 19 |
Performance |
| 20 |
=========== |
| 21 |
|
| 22 |
Parts of this package has been re-implemented in C for faster pack and |
| 23 |
unpack operations. There are two independent C implementations; |
| 24 |
`bitstruct.c`, which is part of this package, and the standalone |
| 25 |
package `cbitstruct`_. These implementations are only available in |
| 26 |
CPython 3, and must be explicitly imported. By default the pure Python |
| 27 |
implementation is used. |
| 28 |
|
| 29 |
To use `bitstruct.c`, do ``import bitstruct.c as bitstruct``. |
| 30 |
|
| 31 |
To use `cbitstruct`_, do ``import cbitstruct as bitstruct``. |
| 32 |
|
| 33 |
`bitstruct.c` has a few limitations compared to the pure Python |
| 34 |
implementation: |
| 35 |
|
| 36 |
- Integers and booleans must be 64 bits or less. |
| 37 |
|
| 38 |
- Text and raw must be a multiple of 8 bits. |
| 39 |
|
| 40 |
- Bit endianness and byte order are not yet supported. |
| 41 |
|
| 42 |
- ``byteswap()`` can only swap 1, 2, 4 and 8 bytes. |
| 43 |
|
| 44 |
See `cbitstruct`_ for its limitations. |
| 45 |
|
| 46 |
MicroPython |
| 47 |
=========== |
| 48 |
|
| 49 |
The C implementation has been ported to `MicroPython`_. See |
| 50 |
`bitstruct-micropython`_ for more details. |
| 51 |
|
| 52 |
Example usage |
| 53 |
============= |
| 54 |
|
| 55 |
A basic example of `packing`_ and `unpacking`_ four integers using the |
| 56 |
format string ``'u1u3u4s16'``: |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
>>> from bitstruct import * |
| 61 |
>>> pack('u1u3u4s16', 1, 2, 3, -4) |
| 62 |
b'\xa3\xff\xfc' |
| 63 |
>>> unpack('u1u3u4s16', b'\xa3\xff\xfc') |
| 64 |
(1, 2, 3, -4) |
| 65 |
>>> calcsize('u1u3u4s16') |
| 66 |
24 |
| 67 |
|
| 68 |
An example `compiling`_ the format string once, and use it to `pack`_ |
| 69 |
and `unpack`_ data: |
| 70 |
|
| 71 |
.. code-block:: python |
| 72 |
|
| 73 |
>>> import bitstruct |
| 74 |
>>> cf = bitstruct.compile('u1u3u4s16') |
| 75 |
>>> cf.pack(1, 2, 3, -4) |
| 76 |
b'\xa3\xff\xfc' |
| 77 |
>>> cf.unpack(b'\xa3\xff\xfc') |
| 78 |
(1, 2, 3, -4) |
| 79 |
|
| 80 |
Use the `pack into`_ and `unpack from`_ functions to pack/unpack |
| 81 |
values at a bit offset into the data, in this example the bit offset |
| 82 |
is 5: |
| 83 |
|
| 84 |
.. code-block:: python |
| 85 |
|
| 86 |
>>> from bitstruct import * |
| 87 |
>>> data = bytearray(b'\x00\x00\x00\x00') |
| 88 |
>>> pack_into('u1u3u4s16', data, 5, 1, 2, 3, -4) |
| 89 |
>>> data |
| 90 |
bytearray(b'\x05\x1f\xff\xe0') |
| 91 |
>>> unpack_from('u1u3u4s16', data, 5) |
| 92 |
(1, 2, 3, -4) |
| 93 |
|
| 94 |
The unpacked values can be named by assigning them to variables or by |
| 95 |
wrapping the result in a named tuple: |
| 96 |
|
| 97 |
.. code-block:: python |
| 98 |
|
| 99 |
>>> from bitstruct import * |
| 100 |
>>> from collections import namedtuple |
| 101 |
>>> MyName = namedtuple('myname', ['a', 'b', 'c', 'd']) |
| 102 |
>>> unpacked = unpack('u1u3u4s16', b'\xa3\xff\xfc') |
| 103 |
>>> myname = MyName(*unpacked) |
| 104 |
>>> myname |
| 105 |
myname(a=1, b=2, c=3, d=-4) |
| 106 |
>>> myname.c |
| 107 |
3 |
| 108 |
|
| 109 |
Use the `pack_dict`_ and `unpack_dict`_ functions to pack/unpack |
| 110 |
values in dictionaries: |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
>>> from bitstruct import * |
| 115 |
>>> names = ['a', 'b', 'c', 'd'] |
| 116 |
>>> pack_dict('u1u3u4s16', names, {'a': 1, 'b': 2, 'c': 3, 'd': -4}) |
| 117 |
b'\xa3\xff\xfc' |
| 118 |
>>> unpack_dict('u1u3u4s16', names, b'\xa3\xff\xfc') |
| 119 |
{'a': 1, 'b': 2, 'c': 3, 'd': -4} |
| 120 |
|
| 121 |
An example of `packing`_ and `unpacking`_ an unsigned integer, a |
| 122 |
signed integer, a float, a boolean, a byte string and a string: |
| 123 |
|
| 124 |
.. code-block:: python |
| 125 |
|
| 126 |
>>> from bitstruct import * |
| 127 |
>>> pack('u5s5f32b1r13t40', 1, -1, 3.75, True, b'\xff\xff', 'hello') |
| 128 |
b'\x0f\xd0\x1c\x00\x00?\xffhello' |
| 129 |
>>> unpack('u5s5f32b1r13t40', b'\x0f\xd0\x1c\x00\x00?\xffhello') |
| 130 |
(1, -1, 3.75, True, b'\xff\xf8', 'hello') |
| 131 |
>>> calcsize('u5s5f32b1r13t40') |
| 132 |
96 |
| 133 |
|
| 134 |
The same format string and values as in the previous example, but |
| 135 |
using LSB (Least Significant Bit) first instead of the default MSB |
| 136 |
(Most Significant Bit) first: |
| 137 |
|
| 138 |
.. code-block:: python |
| 139 |
|
| 140 |
>>> from bitstruct import * |
| 141 |
>>> pack('<u5s5f32b1r13t40', 1, -1, 3.75, True, b'\xff\xff', 'hello') |
| 142 |
b'\x87\xc0\x00\x03\x80\xbf\xff\xf666\xa6\x16' |
| 143 |
>>> unpack('<u5s5f32b1r13t40', b'\x87\xc0\x00\x03\x80\xbf\xff\xf666\xa6\x16') |
| 144 |
(1, -1, 3.75, True, b'\xff\xf8', 'hello') |
| 145 |
>>> calcsize('<u5s5f32b1r13t40') |
| 146 |
96 |
| 147 |
|
| 148 |
An example of `unpacking`_ values from a hexstring and a binary file: |
| 149 |
|
| 150 |
.. code-block:: python |
| 151 |
|
| 152 |
>>> from bitstruct import * |
| 153 |
>>> from binascii import unhexlify |
| 154 |
>>> unpack('s17s13r24', unhexlify('0123456789abcdef')) |
| 155 |
(582, -3751, b'\xe2j\xf3') |
| 156 |
>>> with open("test.bin", "rb") as fin: |
| 157 |
... unpack('s17s13r24', fin.read(8)) |
| 158 |
... |
| 159 |
... |
| 160 |
(582, -3751, b'\xe2j\xf3') |
| 161 |
|
| 162 |
Change endianness of the data with `byteswap`_, and then unpack the |
| 163 |
values: |
| 164 |
|
| 165 |
.. code-block:: python |
| 166 |
|
| 167 |
>>> from bitstruct import * |
| 168 |
>>> packed = pack('u1u3u4s16', 1, 2, 3, 1) |
| 169 |
>>> unpack('u1u3u4s16', byteswap('12', packed)) |
| 170 |
(1, 2, 3, 256) |
| 171 |
|
| 172 |
A basic example of `packing`_ and `unpacking`_ four integers using the |
| 173 |
format string ``'u1u3u4s16'`` using the C implementation: |
| 174 |
|
| 175 |
.. code-block:: python |
| 176 |
|
| 177 |
>>> from bitstruct.c import * |
| 178 |
>>> pack('u1u3u4s16', 1, 2, 3, -4) |
| 179 |
b'\xa3\xff\xfc' |
| 180 |
>>> unpack('u1u3u4s16', b'\xa3\xff\xfc') |
| 181 |
(1, 2, 3, -4) |
| 182 |
|
| 183 |
Contributing |
| 184 |
============ |
| 185 |
|
| 186 |
#. Fork the repository. |
| 187 |
|
| 188 |
#. Install prerequisites. |
| 189 |
|
| 190 |
.. code-block:: text |
| 191 |
|
| 192 |
pip install -r requirements.txt |
| 193 |
|
| 194 |
#. Implement the new feature or bug fix. |
| 195 |
|
| 196 |
#. Implement test case(s) to ensure that future changes do not break |
| 197 |
legacy. |
| 198 |
|
| 199 |
#. Run the tests. |
| 200 |
|
| 201 |
.. code-block:: text |
| 202 |
|
| 203 |
make test |
| 204 |
|
| 205 |
#. Create a pull request. |
| 206 |
|
| 207 |
.. _packing: http://bitstruct.readthedocs.io/en/latest/#bitstruct.pack |
| 208 |
|
| 209 |
.. _unpacking: http://bitstruct.readthedocs.io/en/latest/#bitstruct.unpack |
| 210 |
|
| 211 |
.. _pack: http://bitstruct.readthedocs.io/en/latest/#bitstruct.CompiledFormat.pack |
| 212 |
|
| 213 |
.. _unpack: http://bitstruct.readthedocs.io/en/latest/#bitstruct.CompiledFormat.unpack |
| 214 |
|
| 215 |
.. _pack into: http://bitstruct.readthedocs.io/en/latest/#bitstruct.pack_into |
| 216 |
|
| 217 |
.. _unpack from: http://bitstruct.readthedocs.io/en/latest/#bitstruct.unpack_from |
| 218 |
|
| 219 |
.. _pack_dict: http://bitstruct.readthedocs.io/en/latest/#bitstruct.pack_dict |
| 220 |
|
| 221 |
.. _unpack_dict: http://bitstruct.readthedocs.io/en/latest/#bitstruct.unpack_dict |
| 222 |
|
| 223 |
.. _byteswap: http://bitstruct.readthedocs.io/en/latest/#bitstruct.byteswap |
| 224 |
|
| 225 |
.. _compiling: http://bitstruct.readthedocs.io/en/latest/#bitstruct.compile |
| 226 |
|
| 227 |
.. _cbitstruct: https://github.com/qchateau/cbitstruct |
| 228 |
|
| 229 |
.. _MicroPython: https://github.com/micropython/micropython |
| 230 |
|
| 231 |
.. _bitstruct-micropython: https://github.com/peterzuger/bitstruct-micropython |