Skip to content

Commit 780517a

Browse files
committed
Separate Z85 from Base85 on the Python API side
This avoids exposing a `z85` bool parameter. Also update documentation. While we're at it, refer to the encoding as "Base85" instead of "base85".
1 parent b9d27bd commit 780517a

File tree

4 files changed

+319
-100
lines changed

4 files changed

+319
-100
lines changed

Doc/library/binascii.rst

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,61 @@ The :mod:`binascii` module defines the following functions:
124124
.. versionadded:: next
125125

126126

127-
.. function:: a2b_base85(string, /, *, strict_mode=False, z85=False)
127+
.. function:: a2b_base85(string, /, *, strict_mode=False)
128128

129-
Convert base85 data back to binary and return the binary data.
129+
Convert Base85 data back to binary and return the binary data.
130130
More than one line may be passed at a time.
131131

132-
If *strict_mode* is true, only valid base85 data will be converted.
133-
Invalid base85 data will raise :exc:`binascii.Error`.
132+
If *strict_mode* is true, only valid Base85 data will be converted.
133+
Invalid Base85 data will raise :exc:`binascii.Error`.
134134

135-
If *z85* is true, the base85 data uses the Z85 alphabet.
136-
See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information.
135+
Valid Base85 data contains characters from the Base85 alphabet in groups
136+
of five (except for the final group, which may have from two to five
137+
characters). Each group encodes 32 bits of binary data in the range from
138+
``0`` to ``2 ** 32 - 1``, inclusive.
139+
140+
.. versionadded:: next
141+
142+
143+
.. function:: b2a_base85(data, /, *, pad=False, newline=True)
144+
145+
Convert binary data to a line of ASCII characters in Base85 coding.
146+
The return value is the converted line.
147+
148+
If *pad* is true, the input is padded to a multiple of 4 before encoding.
137149

138-
Valid base85 data contains characters from the base85 alphabet in groups
150+
If *newline* is true, a newline char is appended to the result.
151+
152+
.. versionadded:: next
153+
154+
155+
.. function:: a2b_z85(string, /, *, strict_mode=False)
156+
157+
Convert Z85 data back to binary and return the binary data.
158+
More than one line may be passed at a time.
159+
160+
If *strict_mode* is true, only valid Z85 data will be converted.
161+
Invalid Z85 data will raise :exc:`binascii.Error`.
162+
163+
Valid Z85 data contains characters from the Z85 alphabet in groups
139164
of five (except for the final group, which may have from two to five
140165
characters). Each group encodes 32 bits of binary data in the range from
141166
``0`` to ``2 ** 32 - 1``, inclusive.
142167

168+
See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information.
169+
143170
.. versionadded:: next
144171

145172

146-
.. function:: b2a_base85(data, /, *, pad=False, newline=True, z85=False)
173+
.. function:: b2a_z85(data, /, *, pad=False, newline=True)
147174

148-
Convert binary data to a line of ASCII characters in base85 coding.
175+
Convert binary data to a line of ASCII characters in Z85 coding.
149176
The return value is the converted line.
150177

151178
If *pad* is true, the input is padded to a multiple of 4 before encoding.
152179

153180
If *newline* is true, a newline char is appended to the result.
154181

155-
If *z85* is true, the Z85 alphabet is used for conversion.
156182
See `Z85 specification <https://rfc.zeromq.org/spec/32/>`_ for more information.
157183

158184
.. versionadded:: next

Lib/base64.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,14 @@ def b85decode(b):
345345

346346
def z85encode(s, pad=False):
347347
"""Encode bytes-like object b in z85 format and return a bytes object."""
348-
return binascii.b2a_base85(s, pad=pad, newline=False, z85=True)
348+
return binascii.b2a_z85(s, pad=pad, newline=False)
349349

350350
def z85decode(s):
351351
"""Decode the z85-encoded bytes-like object or ASCII string b
352352
353353
The result is returned as a bytes object.
354354
"""
355-
return binascii.a2b_base85(s, strict_mode=True, z85=True)
355+
return binascii.a2b_z85(s, strict_mode=True)
356356

357357
# Legacy interface. This code could be cleaned up since I don't believe
358358
# binascii has any line length limitations. It just doesn't seem worth it

Modules/binascii.c

Lines changed: 95 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -966,26 +966,9 @@ binascii_b2a_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
966966
return PyBytesWriter_FinishWithPointer(writer, ascii_data);
967967
}
968968

969-
/*[clinic input]
970-
binascii.a2b_base85
971-
972-
data: ascii_buffer
973-
/
974-
*
975-
strict_mode: bool = False
976-
When set to True, bytes that are not in the base85 alphabet
977-
(or the Z85 alphabet, if z85 is True) are not allowed.
978-
z85: bool = False
979-
When set to True, the Z85 alphabet is used instead of the standard
980-
base85 alphabet.
981-
982-
Decode a line of base85 data.
983-
[clinic start generated code]*/
984-
985969
static PyObject *
986-
binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
987-
int z85)
988-
/*[clinic end generated code: output=c5b9118ffe77f1cb input=65c2a532ad64ebd5]*/
970+
internal_a2b_base85(PyObject *module, Py_buffer *data, int strict_mode,
971+
const unsigned char table_a2b[], const char *name)
989972
{
990973
const unsigned char *ascii_data = data->buf;
991974
Py_ssize_t ascii_len = data->len;
@@ -1001,8 +984,6 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
1001984
}
1002985
unsigned char *bin_data = PyBytesWriter_GetData(writer);
1003986

1004-
const unsigned char *table_a2b = z85 ? table_a2b_base85_z85
1005-
: table_a2b_base85;
1006987
uint32_t leftchar = 0;
1007988
int group_pos = 0;
1008989
for (; ascii_len > 0 || group_pos != 0; ascii_len--, ascii_data++) {
@@ -1024,8 +1005,7 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
10241005
if (state != NULL) {
10251006
PyErr_Format(state->Error,
10261007
"%s overflow in hunk starting at byte %d",
1027-
z85 ? "z85" : "base85",
1028-
(data->len - ascii_len) / 5 * 5);
1008+
name, (data->len - ascii_len) / 5 * 5);
10291009
}
10301010
goto error;
10311011
}
@@ -1036,7 +1016,7 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
10361016
state = get_binascii_state(module);
10371017
if (state != NULL) {
10381018
PyErr_Format(state->Error, "bad %s character at position %d",
1039-
z85 ? "z85" : "base85", data->len - ascii_len);
1019+
name, data->len - ascii_len);
10401020
}
10411021
goto error;
10421022
}
@@ -1063,26 +1043,9 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
10631043
return NULL;
10641044
}
10651045

1066-
/*[clinic input]
1067-
binascii.b2a_base85
1068-
1069-
data: Py_buffer
1070-
/
1071-
*
1072-
pad: bool = False
1073-
Pad input to a multiple of 4 before encoding.
1074-
newline: bool = True
1075-
Append a newline to the result.
1076-
z85: bool = False
1077-
Use Z85 alphabet instead of standard base85 alphabet.
1078-
1079-
Base85-code line of data.
1080-
[clinic start generated code]*/
1081-
10821046
static PyObject *
1083-
binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
1084-
int newline, int z85)
1085-
/*[clinic end generated code: output=d3740e9a20c8e071 input=e4e07591f7a11ae4]*/
1047+
internal_b2a_base85(PyObject *module, Py_buffer *data, int pad, int newline,
1048+
const unsigned char table_b2a[])
10861049
{
10871050
const unsigned char *bin_data = data->buf;
10881051
Py_ssize_t bin_len = data->len;
@@ -1105,8 +1068,6 @@ binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
11051068
unsigned char *ascii_data = PyBytesWriter_GetData(writer);
11061069

11071070
/* Encode all full-length chunks. */
1108-
const unsigned char *table_b2a = z85 ? table_b2a_base85_z85
1109-
: table_b2a_base85;
11101071
for (; bin_len >= 4; bin_len -= 4, bin_data += 4) {
11111072
uint32_t leftchar = (bin_data[0] << 24) | (bin_data[1] << 16) |
11121073
(bin_data[2] << 8) | bin_data[3];
@@ -1150,6 +1111,93 @@ binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
11501111
return PyBytesWriter_FinishWithPointer(writer, ascii_data);
11511112
}
11521113

1114+
/*[clinic input]
1115+
binascii.a2b_base85
1116+
1117+
data: ascii_buffer
1118+
/
1119+
*
1120+
strict_mode: bool = False
1121+
When set to True, bytes that are not in the Base85 alphabet
1122+
are not allowed.
1123+
1124+
Decode a line of Base85 data.
1125+
[clinic start generated code]*/
1126+
1127+
static PyObject *
1128+
binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode)
1129+
/*[clinic end generated code: output=337b9418636f30f4 input=d19293f194c8cb78]*/
1130+
{
1131+
return internal_a2b_base85(module, data, strict_mode,
1132+
table_a2b_base85, "Base85");
1133+
}
1134+
1135+
/*[clinic input]
1136+
binascii.b2a_base85
1137+
1138+
data: Py_buffer
1139+
/
1140+
*
1141+
pad: bool = False
1142+
Pad input to a multiple of 4 before encoding.
1143+
newline: bool = True
1144+
Append a newline to the result.
1145+
1146+
Base85-code line of data.
1147+
[clinic start generated code]*/
1148+
1149+
static PyObject *
1150+
binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
1151+
int newline)
1152+
/*[clinic end generated code: output=56936eb231e15dc0 input=3899d4f5c3a589a0]*/
1153+
{
1154+
return internal_b2a_base85(module, data, pad, newline, table_b2a_base85);
1155+
}
1156+
1157+
/*[clinic input]
1158+
binascii.a2b_z85
1159+
1160+
data: ascii_buffer
1161+
/
1162+
*
1163+
strict_mode: bool = False
1164+
When set to True, bytes that are not in the Z85 alphabet
1165+
are not allowed.
1166+
1167+
Decode a line of Z85 data.
1168+
[clinic start generated code]*/
1169+
1170+
static PyObject *
1171+
binascii_a2b_z85_impl(PyObject *module, Py_buffer *data, int strict_mode)
1172+
/*[clinic end generated code: output=a2083e8f05d38960 input=a0d5afbf2aebee4d]*/
1173+
{
1174+
return internal_a2b_base85(module, data, strict_mode,
1175+
table_a2b_base85_z85, "Z85");
1176+
}
1177+
1178+
/*[clinic input]
1179+
binascii.b2a_z85
1180+
1181+
data: Py_buffer
1182+
/
1183+
*
1184+
pad: bool = False
1185+
Pad input to a multiple of 4 before encoding.
1186+
newline: bool = True
1187+
Append a newline to the result.
1188+
1189+
Z85-code line of data.
1190+
[clinic start generated code]*/
1191+
1192+
static PyObject *
1193+
binascii_b2a_z85_impl(PyObject *module, Py_buffer *data, int pad,
1194+
int newline)
1195+
/*[clinic end generated code: output=a61636b3f618fc1d input=f71c473209eb8f41]*/
1196+
{
1197+
return internal_b2a_base85(module, data, pad, newline,
1198+
table_b2a_base85_z85);
1199+
}
1200+
11531201
/*[clinic input]
11541202
binascii.crc_hqx
11551203
@@ -1811,6 +1859,8 @@ static struct PyMethodDef binascii_module_methods[] = {
18111859
BINASCII_A2B_ASCII85_METHODDEF
18121860
BINASCII_A2B_BASE85_METHODDEF
18131861
BINASCII_B2A_BASE85_METHODDEF
1862+
BINASCII_A2B_Z85_METHODDEF
1863+
BINASCII_B2A_Z85_METHODDEF
18141864
BINASCII_A2B_HEX_METHODDEF
18151865
BINASCII_B2A_HEX_METHODDEF
18161866
BINASCII_HEXLIFY_METHODDEF

0 commit comments

Comments
 (0)