Skip to content

Commit 33437a5

Browse files
Refactoring: inline static functions for Base85.
1 parent f59fffe commit 33437a5

1 file changed

Lines changed: 58 additions & 70 deletions

File tree

Modules/binascii.c

Lines changed: 58 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,20 +1173,44 @@ binascii_b2a_ascii85_impl(PyObject *module, Py_buffer *data, int foldspaces,
11731173
return PyBytesWriter_FinishWithPointer(writer, ascii_data);
11741174
}
11751175

1176+
/*[clinic input]
1177+
binascii.a2b_base85
1178+
1179+
data: ascii_buffer
1180+
/
1181+
*
1182+
alphabet: PyBytesObject(py_default="BASE85_ALPHABET") = NULL
1183+
1184+
Decode a line of Base85 data.
1185+
[clinic start generated code]*/
1186+
11761187
static PyObject *
1177-
base85_decode_impl(PyObject *module, Py_buffer *data,
1178-
const unsigned char table_a2b[], const char *name)
1188+
binascii_a2b_base85_impl(PyObject *module, Py_buffer *data,
1189+
PyBytesObject *alphabet)
1190+
/*[clinic end generated code: output=3e114af53812e8ff input=81779cd049d44a55]*/
11791191
{
11801192
const unsigned char *ascii_data = data->buf;
11811193
Py_ssize_t ascii_len = data->len;
11821194
binascii_state *state = NULL;
1195+
PyObject *table_obj = NULL;
1196+
const unsigned char *table_a2b = table_a2b_base85;
1197+
1198+
if (alphabet != NULL) {
1199+
state = get_binascii_state(module);
1200+
table_obj = get_reverse_table(state, (PyObject *)alphabet, 85, -1);
1201+
if (table_obj == NULL) {
1202+
return NULL;
1203+
}
1204+
table_a2b = (const unsigned char *)PyBytes_AS_STRING(table_obj);
1205+
}
11831206

11841207
assert(ascii_len >= 0);
11851208

11861209
/* Allocate output buffer. */
11871210
size_t bin_len = ((size_t)ascii_len + 4) / 5 * 4;
11881211
PyBytesWriter *writer = PyBytesWriter_Create(bin_len);
11891212
if (writer == NULL) {
1213+
Py_XDECREF(table_obj);
11901214
return NULL;
11911215
}
11921216
unsigned char *bin_data = PyBytesWriter_GetData(writer);
@@ -1212,8 +1236,8 @@ base85_decode_impl(PyObject *module, Py_buffer *data,
12121236
state = get_binascii_state(module);
12131237
if (state != NULL) {
12141238
PyErr_Format(state->Error,
1215-
"%s overflow in hunk starting at byte %d",
1216-
name, (data->len - ascii_len) / 5 * 5);
1239+
"Base85 overflow in hunk starting at byte %d",
1240+
(data->len - ascii_len) / 5 * 5);
12171241
}
12181242
goto error;
12191243
}
@@ -1223,8 +1247,8 @@ base85_decode_impl(PyObject *module, Py_buffer *data,
12231247
else {
12241248
state = get_binascii_state(module);
12251249
if (state != NULL) {
1226-
PyErr_Format(state->Error, "bad %s character at position %d",
1227-
name, data->len - ascii_len);
1250+
PyErr_Format(state->Error, "bad Base85 character at position %d",
1251+
data->len - ascii_len);
12281252
}
12291253
goto error;
12301254
}
@@ -1244,19 +1268,44 @@ base85_decode_impl(PyObject *module, Py_buffer *data,
12441268
leftchar = 0;
12451269
}
12461270

1271+
Py_XDECREF(table_obj);
12471272
return PyBytesWriter_FinishWithPointer(writer, bin_data);
12481273

12491274
error:
12501275
PyBytesWriter_Discard(writer);
1276+
Py_XDECREF(table_obj);
12511277
return NULL;
12521278
}
12531279

1280+
/*[clinic input]
1281+
binascii.b2a_base85
1282+
1283+
data: Py_buffer
1284+
/
1285+
*
1286+
pad: bool = False
1287+
Pad input to a multiple of 4 before encoding.
1288+
alphabet: Py_buffer(py_default="BASE85_ALPHABET") = None
1289+
1290+
Base85-code line of data.
1291+
[clinic start generated code]*/
1292+
12541293
static PyObject *
1255-
base85_encode_impl(PyObject *module, Py_buffer *data, int pad,
1256-
const unsigned char table_b2a[], const char *name)
1294+
binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
1295+
Py_buffer *alphabet)
1296+
/*[clinic end generated code: output=a59f4f2ff6f0e69f input=cde4ebe8abfaa982]*/
12571297
{
12581298
const unsigned char *bin_data = data->buf;
12591299
Py_ssize_t bin_len = data->len;
1300+
const unsigned char *table_b2a = table_b2a_base85;
1301+
1302+
if (alphabet->buf != NULL) {
1303+
if (alphabet->len != 85) {
1304+
PyErr_SetString(PyExc_ValueError, "alphabet must have length 85");
1305+
return NULL;
1306+
}
1307+
table_b2a = alphabet->buf;
1308+
}
12601309

12611310
assert(bin_len >= 0);
12621311

@@ -1270,7 +1319,7 @@ base85_encode_impl(PyObject *module, Py_buffer *data, int pad,
12701319
if (state == NULL) {
12711320
return NULL;
12721321
}
1273-
PyErr_Format(state->Error, "Too much data for %s", name);
1322+
PyErr_SetString(state->Error, "Too much data for Base85");
12741323
return NULL;
12751324
}
12761325

@@ -1320,67 +1369,6 @@ base85_encode_impl(PyObject *module, Py_buffer *data, int pad,
13201369
return PyBytesWriter_FinishWithPointer(writer, ascii_data);
13211370
}
13221371

1323-
/*[clinic input]
1324-
binascii.a2b_base85
1325-
1326-
data: ascii_buffer
1327-
/
1328-
*
1329-
alphabet: PyBytesObject(py_default="BASE85_ALPHABET") = NULL
1330-
1331-
Decode a line of Base85 data.
1332-
[clinic start generated code]*/
1333-
1334-
static PyObject *
1335-
binascii_a2b_base85_impl(PyObject *module, Py_buffer *data,
1336-
PyBytesObject *alphabet)
1337-
/*[clinic end generated code: output=3e114af53812e8ff input=81779cd049d44a55]*/
1338-
{
1339-
const unsigned char *table_a2b = table_a2b_base85;
1340-
PyObject *table_obj = NULL;
1341-
binascii_state *state;
1342-
if (alphabet != NULL) {
1343-
state = get_binascii_state(module);
1344-
table_obj = get_reverse_table(state, (PyObject *)alphabet, 85, -1);
1345-
if (table_obj == NULL) {
1346-
return NULL;
1347-
}
1348-
table_a2b = (const unsigned char *)PyBytes_AS_STRING(table_obj);
1349-
}
1350-
PyObject *result = base85_decode_impl(module, data, table_a2b, "Base85");
1351-
Py_XDECREF(table_obj);
1352-
return result;
1353-
}
1354-
1355-
/*[clinic input]
1356-
binascii.b2a_base85
1357-
1358-
data: Py_buffer
1359-
/
1360-
*
1361-
pad: bool = False
1362-
Pad input to a multiple of 4 before encoding.
1363-
alphabet: Py_buffer(py_default="BASE85_ALPHABET") = None
1364-
1365-
Base85-code line of data.
1366-
[clinic start generated code]*/
1367-
1368-
static PyObject *
1369-
binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
1370-
Py_buffer *alphabet)
1371-
/*[clinic end generated code: output=a59f4f2ff6f0e69f input=cde4ebe8abfaa982]*/
1372-
{
1373-
const unsigned char *table_b2a = table_b2a_base85;
1374-
if (alphabet->buf != NULL) {
1375-
if (alphabet->len != 85) {
1376-
PyErr_SetString(PyExc_ValueError, "alphabet must have length 85");
1377-
return NULL;
1378-
}
1379-
table_b2a = alphabet->buf;
1380-
}
1381-
return base85_encode_impl(module, data, pad, table_b2a, "Base85");
1382-
}
1383-
13841372
/*[clinic input]
13851373
binascii.crc_hqx
13861374

0 commit comments

Comments
 (0)