-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-101178: Add Ascii85, base85, and Z85 support to binascii #102753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
05ae5ad
aa06c5d
6377440
6c0e4a3
ce4773c
4072e3b
bc9217f
2c40ba0
fd9eaf7
4746d18
d075593
6d65fec
a241356
0df9a40
60fbd7c
a070887
167e83e
01df442
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -77,6 +77,79 @@ The :mod:`binascii` module defines the following functions: | |||||||||
| Added the *newline* parameter. | ||||||||||
|
|
||||||||||
|
|
||||||||||
| .. function:: a2b_ascii85(string, /, *, fold_spaces=False, wrap=False, ignore=b"") | ||||||||||
|
|
||||||||||
| Convert Ascii85 data back to binary and return the binary data. | ||||||||||
|
|
||||||||||
| Valid Ascii85 data contains characters from the Ascii85 alphabet in groups | ||||||||||
| of five (except for the final group, which may have from two to five | ||||||||||
| characters). Each group encodes 32 bits of binary data in the range from | ||||||||||
| ``0`` to ``2 ** 32 - 1``, inclusive. The special character ``z`` is | ||||||||||
| accepted as a short form of the group ``!!!!!``, which encodes four | ||||||||||
| consecutive null bytes. | ||||||||||
|
|
||||||||||
| If *fold_spaces* is true, the special character ``y`` is also accepted as a | ||||||||||
| short form of the group ``+<VdL``, which encodes four consecutive spaces. | ||||||||||
| Note that neither short form is permitted if it occurs in the middle of | ||||||||||
| another group. | ||||||||||
|
|
||||||||||
| If *wrap* is true, the input begins with ``<~`` and ends with ``~>``, as in | ||||||||||
| the Adobe Ascii85 format. | ||||||||||
|
|
||||||||||
| *ignore* is an optional bytes-like object that specifies characters to | ||||||||||
| ignore in the input. | ||||||||||
|
|
||||||||||
| Invalid Ascii85 data will raise :exc:`binascii.Error`. | ||||||||||
|
|
||||||||||
|
|
||||||||||
| .. function:: b2a_ascii85(data, /, *, fold_spaces=False, wrap=False, width=0, pad=False) | ||||||||||
|
|
||||||||||
| Convert binary data to a formatted sequence of ASCII characters in Ascii85 | ||||||||||
| coding. The return value is the converted data. | ||||||||||
|
|
||||||||||
| If *fold_spaces* is true, four consecutive spaces are encoded as the | ||||||||||
| special character ``y`` instead of the sequence ``+<VdL``. | ||||||||||
|
|
||||||||||
| If *wrap* is true, the output begins with ``<~`` and ends with ``~>``, as | ||||||||||
| in the Adobe Ascii85 format. | ||||||||||
|
|
||||||||||
| If *width* is provided and greater than 0, the output is split into lines | ||||||||||
| of no more than the specified width separated by the ASCII newline | ||||||||||
| character. | ||||||||||
|
|
||||||||||
| If *pad* is true, the input is padded to a multiple of 4 before encoding. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| .. function:: a2b_base85(string, /, *, strict_mode=False, z85=False) | ||||||||||
|
|
||||||||||
| Convert base85 data back to binary and return the binary data. | ||||||||||
| More than one line may be passed at a time. | ||||||||||
|
|
||||||||||
| If *strict_mode* is true, only valid base85 data will be converted. | ||||||||||
| Invalid base85 data will raise :exc:`binascii.Error`. | ||||||||||
|
|
||||||||||
| If *z85* is true, the base85 data uses the Z85 alphabet. | ||||||||||
| See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information. | ||||||||||
|
|
||||||||||
| Valid base85 data contains characters from the base85 alphabet in groups | ||||||||||
| of five (except for the final group, which may have from two to five | ||||||||||
| characters). Each group encodes 32 bits of binary data in the range from | ||||||||||
| ``0`` to ``2 ** 32 - 1``, inclusive. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| .. function:: b2a_base85(data, /, *, pad=False, newline=True, z85=False) | ||||||||||
|
|
||||||||||
| Convert binary data to a line of ASCII characters in base85 coding. | ||||||||||
| The return value is the converted line. | ||||||||||
|
|
||||||||||
| If *pad* is true, the input is padded to a multiple of 4 before encoding. | ||||||||||
|
|
||||||||||
| If *newline* is true, a newline char is appended to the result. | ||||||||||
|
|
||||||||||
| If *z85* is true, the Z85 alphabet is used for conversion. | ||||||||||
| See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| .. function:: a2b_qp(data, header=False) | ||||||||||
|
|
||||||||||
| Convert a block of quoted-printable data back to binary and return the binary | ||||||||||
|
|
||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||
| """C accelerator wrappers for originally pure-Python parts of base64.""" | ||||||
|
|
||||||
| from binascii import Error, a2b_ascii85, a2b_base85, b2a_ascii85, b2a_base85 | ||||||
|
|
||||||
|
|
||||||
| # Base 85 functions in base64 silently convert input to bytes. | ||||||
| # Copy the conversion logic from base64 to avoid circular imports. | ||||||
|
|
||||||
| bytes_types = (bytes, bytearray) # Types acceptable as binary data | ||||||
|
|
||||||
|
|
||||||
| def _bytes_from_decode_data(s): | ||||||
| if isinstance(s, str): | ||||||
| try: | ||||||
| return s.encode('ascii') | ||||||
| except UnicodeEncodeError: | ||||||
| raise ValueError('string argument should contain only ASCII characters') | ||||||
| if isinstance(s, bytes_types): | ||||||
| return s | ||||||
| try: | ||||||
| return memoryview(s).tobytes() | ||||||
| except TypeError: | ||||||
| raise TypeError("argument should be a bytes-like object or ASCII " | ||||||
| "string, not %r" % s.__class__.__name__) from None | ||||||
|
|
||||||
|
|
||||||
| def _bytes_from_encode_data(b): | ||||||
| return b if isinstance(b, bytes_types) else memoryview(b).tobytes() | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
|
||||||
|
|
||||||
| # Functions in binascii raise binascii.Error instead of ValueError. | ||||||
|
|
||||||
| def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| b = _bytes_from_encode_data(b) | ||||||
| try: | ||||||
| return b2a_ascii85(b, fold_spaces=foldspaces, | ||||||
| wrap=adobe, width=wrapcol, pad=pad) | ||||||
| except Error as e: | ||||||
| raise ValueError(e) from None | ||||||
|
|
||||||
|
|
||||||
| def a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v'): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| b = _bytes_from_decode_data(b) | ||||||
| try: | ||||||
| return a2b_ascii85(b, fold_spaces=foldspaces, | ||||||
| wrap=adobe, ignore=ignorechars) | ||||||
| except Error as e: | ||||||
| raise ValueError(e) from None | ||||||
|
|
||||||
| def b85encode(b, pad=False): | ||||||
| b = _bytes_from_encode_data(b) | ||||||
| try: | ||||||
| return b2a_base85(b, pad=pad, newline=False) | ||||||
| except Error as e: | ||||||
| raise ValueError(e) from None | ||||||
|
|
||||||
|
|
||||||
| def b85decode(b): | ||||||
| b = _bytes_from_decode_data(b) | ||||||
| try: | ||||||
| return a2b_base85(b, strict_mode=True) | ||||||
| except Error as e: | ||||||
| raise ValueError(e) from None | ||||||
|
|
||||||
|
|
||||||
| def z85encode(s, pad=False): | ||||||
| s = _bytes_from_encode_data(s) | ||||||
| try: | ||||||
| return b2a_base85(s, pad=pad, newline=False, z85=True) | ||||||
| except Error as e: | ||||||
| raise ValueError(e) from None | ||||||
|
|
||||||
|
|
||||||
| def z85decode(s): | ||||||
| s = _bytes_from_decode_data(s) | ||||||
| try: | ||||||
| return a2b_base85(s, strict_mode=True, z85=True) | ||||||
| except Error as e: | ||||||
| raise ValueError(e) from None | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.