Skip to content

Commit bf32f99

Browse files
committed
Defer base 85 overflow check during decoding
Performance gains of up to 8% for a2b_ascii85() and 25% for a2b_base85() and a2b_z85() were observed.
1 parent da165d1 commit bf32f99

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Modules/binascii.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -919,16 +919,17 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
919919
this_digit = 84;
920920
}
921921
if (this_digit < 85) {
922-
if (leftchar > UINT32_MAX / 85
923-
|| (leftchar *= 85) > UINT32_MAX - this_digit)
922+
if (group_pos == 4
923+
&& (leftchar > UINT32_MAX / 85
924+
|| leftchar * 85 > UINT32_MAX - this_digit))
924925
{
925926
state = get_binascii_state(module);
926927
if (state != NULL) {
927928
PyErr_SetString(state->Error, "Ascii85 overflow");
928929
}
929930
goto error;
930931
}
931-
leftchar += this_digit;
932+
leftchar = leftchar * 85 + this_digit;
932933
group_pos++;
933934
}
934935
else if ((this_ch == 'y' && fold_spaces) || this_ch == 'z') {
@@ -1137,8 +1138,9 @@ internal_a2b_base85(PyObject *module, Py_buffer *data, int strict_mode,
11371138
this_digit = 84;
11381139
}
11391140
if (this_digit < 85) {
1140-
if (leftchar > UINT32_MAX / 85
1141-
|| (leftchar *= 85) > UINT32_MAX - this_digit)
1141+
if (group_pos == 4
1142+
&& (leftchar > UINT32_MAX / 85
1143+
|| leftchar * 85 > UINT32_MAX - this_digit))
11421144
{
11431145
state = get_binascii_state(module);
11441146
if (state != NULL) {
@@ -1148,7 +1150,7 @@ internal_a2b_base85(PyObject *module, Py_buffer *data, int strict_mode,
11481150
}
11491151
goto error;
11501152
}
1151-
leftchar += this_digit;
1153+
leftchar = leftchar * 85 + this_digit;
11521154
group_pos++;
11531155
}
11541156
else if (strict_mode) {

0 commit comments

Comments
 (0)