Skip to content

Commit 05ae5ad

Browse files
committed
Add Ascii85, base85, and Z85 support to binascii
Add Ascii85, base85, and Z85 encoders and decoders to `binascii`, replacing the existing pure Python implementations in `base64`. No API or documentation changes are necessary with respect to `base64.a85encode()`, `b85encode()`, etc., and all existing unit tests for those functions continue to pass without modification. Note that attempting to decode Ascii85 or base85 data of length 1 mod 5 (after accounting for Ascii85 quirks) now produces an error, as no encoder would emit such data. This should be the only significant externally visible difference compared to the old implementation. Resolves: gh-101178
1 parent 78cfee6 commit 05ae5ad

10 files changed

+1412
-174
lines changed

Doc/library/binascii.rst

+73
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,79 @@ The :mod:`binascii` module defines the following functions:
7777
Added the *newline* parameter.
7878

7979

80+
.. function:: a2b_ascii85(string, /, *, fold_spaces=False, wrap=False, ignore=b"")
81+
82+
Convert Ascii85 data back to binary and return the binary data.
83+
84+
Valid Ascii85 data contains characters from the Ascii85 alphabet in groups
85+
of five (except for the final group, which may have from two to five
86+
characters). Each group encodes 32 bits of binary data in the range from
87+
``0`` to ``2 ** 32 - 1``, inclusive. The special character ``z`` is
88+
accepted as a short form of the group ``!!!!!``, which encodes four
89+
consecutive null bytes.
90+
91+
If *fold_spaces* is true, the special character ``y`` is also accepted as a
92+
short form of the group ``+<VdL``, which encodes four consecutive spaces.
93+
Note that neither short form is permitted if it occurs in the middle of
94+
another group.
95+
96+
If *wrap* is true, the input begins with ``<~`` and ends with ``~>``, as in
97+
the Adobe Ascii85 format.
98+
99+
*ignore* is an optional bytes-like object that specifies characters to
100+
ignore in the input.
101+
102+
Invalid Ascii85 data will raise :exc:`binascii.Error`.
103+
104+
105+
.. function:: b2a_ascii85(data, /, *, fold_spaces=False, wrap=False, width=0, pad=False)
106+
107+
Convert binary data to a formatted sequence of ASCII characters in Ascii85
108+
coding. The return value is the converted data.
109+
110+
If *fold_spaces* is true, four consecutive spaces are encoded as the
111+
special character ``y`` instead of the sequence ``+<VdL``.
112+
113+
If *wrap* is true, the output begins with ``<~`` and ends with ``~>``, as
114+
in the Adobe Ascii85 format.
115+
116+
If *width* is provided and greater than 0, the output is split into lines
117+
of no more than the specified width separated by the ASCII newline
118+
character.
119+
120+
If *pad* is true, the input is padded to a multiple of 4 before encoding.
121+
122+
123+
.. function:: a2b_base85(string, /, *, strict_mode=False, z85=False)
124+
125+
Convert base85 data back to binary and return the binary data.
126+
More than one line may be passed at a time.
127+
128+
If *strict_mode* is true, only valid base85 data will be converted.
129+
Invalid base85 data will raise :exc:`binascii.Error`.
130+
131+
If *z85* is true, the base85 data uses the Z85 alphabet.
132+
See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information.
133+
134+
Valid base85 data contains characters from the base85 alphabet in groups
135+
of five (except for the final group, which may have from two to five
136+
characters). Each group encodes 32 bits of binary data in the range from
137+
``0`` to ``2 ** 32 - 1``, inclusive.
138+
139+
140+
.. function:: b2a_base85(data, /, *, pad=False, newline=True, z85=False)
141+
142+
Convert binary data to a line of ASCII characters in base85 coding.
143+
The return value is the converted line.
144+
145+
If *pad* is true, the input is padded to a multiple of 4 before encoding.
146+
147+
If *newline* is true, a newline char is appended to the result.
148+
149+
If *z85* is true, the Z85 alphabet is used for conversion.
150+
See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information.
151+
152+
80153
.. function:: a2b_qp(data, header=False)
81154

82155
Convert a block of quoted-printable data back to binary and return the binary

Include/internal/pycore_global_objects_fini_generated.h

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

+5
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ struct _Py_global_strings {
454454
STRUCT_FOR_ID(flags)
455455
STRUCT_FOR_ID(flush)
456456
STRUCT_FOR_ID(fold)
457+
STRUCT_FOR_ID(fold_spaces)
457458
STRUCT_FOR_ID(follow_symlinks)
458459
STRUCT_FOR_ID(format)
459460
STRUCT_FOR_ID(format_spec)
@@ -636,6 +637,7 @@ struct _Py_global_strings {
636637
STRUCT_FOR_ID(outpath)
637638
STRUCT_FOR_ID(overlapped)
638639
STRUCT_FOR_ID(owner)
640+
STRUCT_FOR_ID(pad)
639641
STRUCT_FOR_ID(pages)
640642
STRUCT_FOR_ID(parent)
641643
STRUCT_FOR_ID(password)
@@ -792,11 +794,14 @@ struct _Py_global_strings {
792794
STRUCT_FOR_ID(weekday)
793795
STRUCT_FOR_ID(which)
794796
STRUCT_FOR_ID(who)
797+
STRUCT_FOR_ID(width)
795798
STRUCT_FOR_ID(withdata)
799+
STRUCT_FOR_ID(wrap)
796800
STRUCT_FOR_ID(writable)
797801
STRUCT_FOR_ID(write)
798802
STRUCT_FOR_ID(write_through)
799803
STRUCT_FOR_ID(year)
804+
STRUCT_FOR_ID(z85)
800805
STRUCT_FOR_ID(zdict)
801806
} identifiers;
802807
struct {

Include/internal/pycore_runtime_init_generated.h

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)