@@ -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-
985969static 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-
10821046static 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]
11541202binascii.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