|
| 1 | +.. _librt-base64: |
| 2 | + |
| 3 | +librt.base64 |
| 4 | +============ |
| 5 | + |
| 6 | +The ``librt.base64`` module is part of the ``librt`` package on PyPI, and it includes |
| 7 | +base64 encoding and decoding functions that use SIMD (Single Instruction, Multiple Data) |
| 8 | +for high efficiency. It is a wrapper around |
| 9 | +`Alfred Klomp's base64 library <https://github.com/aklomp/base64>`_. |
| 10 | + |
| 11 | +These functions are mostly compatible with the corresponding functions in the |
| 12 | +Python standard library ``base64`` module but are significantly faster, |
| 13 | +especially in code compiled with mypyc. For larger inputs, these are much |
| 14 | +faster than the standard library alternatives even when used from interpreted code. |
| 15 | + |
| 16 | +.. note:: |
| 17 | + |
| 18 | + The decode functions don't behave identically when data is malformed. |
| 19 | + Only commonly used functionality is provided. The supported arguments may be |
| 20 | + restricted compared to the stdlib functions: the optional ``altchars`` and |
| 21 | + ``validate`` arguments are not supported. |
| 22 | + |
| 23 | +Functions |
| 24 | +--------- |
| 25 | + |
| 26 | +.. function:: b64encode(s: bytes) -> bytes |
| 27 | + |
| 28 | + Encode a bytes object using Base64 and return the encoded bytes. |
| 29 | + |
| 30 | + This is equivalent to ``base64.b64encode(s)`` in the standard library. |
| 31 | + |
| 32 | +.. function:: b64decode(s: bytes | str) -> bytes |
| 33 | + |
| 34 | + Decode a Base64 encoded bytes object or ASCII string and return |
| 35 | + the decoded bytes. |
| 36 | + |
| 37 | + Non-base64-alphabet characters are ignored. This is compatible |
| 38 | + with the default behavior of standard library ``base64.b64decode``. |
| 39 | + |
| 40 | + Raise ``ValueError`` if the padding is incorrect. The standard |
| 41 | + library raises ``binascii.Error`` (a subclass of ``ValueError``) |
| 42 | + instead. |
| 43 | + |
| 44 | +.. function:: urlsafe_b64encode(s: bytes) -> bytes |
| 45 | + |
| 46 | + Encode a bytes object using the URL and filesystem safe Base64 |
| 47 | + alphabet (using ``-`` instead of ``+`` and ``_`` instead of ``/``), |
| 48 | + and return the encoded bytes. |
| 49 | + |
| 50 | + This is equivalent to ``base64.urlsafe_b64encode(s)`` in the |
| 51 | + standard library. |
| 52 | + |
| 53 | +.. function:: urlsafe_b64decode(s: bytes | str) -> bytes |
| 54 | + |
| 55 | + Decode a bytes object or ASCII string using the URL and filesystem |
| 56 | + safe Base64 alphabet (using ``-`` instead of ``+`` and ``_`` instead |
| 57 | + of ``/``), and return the decoded bytes. |
| 58 | + |
| 59 | + This is an alternative to ``base64.urlsafe_b64decode(s)`` in the |
| 60 | + standard library. |
| 61 | + |
| 62 | + Raise ``ValueError`` if the padding is incorrect. The standard |
| 63 | + library raises ``binascii.Error`` (a subclass of ``ValueError``) |
| 64 | + instead. |
0 commit comments