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1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,
162+ -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,
163+ -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,
164+ -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,
165+ -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 , -1 ,-1 ,-1 ,-1 ,
166+ };
167+
148168static const unsigned char table_b2a_base85 [] =
149169 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
150170 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" ;
@@ -153,6 +173,10 @@ static const unsigned char table_b2a_base85_a85[] =
153173 "!\"#$%&\'()*+,-./0123456789:;<=>?@" \
154174 "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu" ;
155175
176+ static const unsigned char table_b2a_base85_z85 [] =
177+ "0123456789abcdefghijklmnopqrstuvwxyz" \
178+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/\x2a?&<>()[]{}@%$#" ; /* clinic doesn't like '/' followed by '*' */
179+
156180#define BASE85_A85_PREFIX '<'
157181#define BASE85_A85_AFFIX '~'
158182#define BASE85_A85_SUFFIX '>'
@@ -911,17 +935,21 @@ binascii.a2b_base85
911935 /
912936 *
913937 strict_mode: bool = False
914- When set to True, bytes that are not part of the base85 standard
915- are not allowed.
938+ When set to True, bytes that are not in the base85 alphabet
939+ (or the Z85 alphabet, if z85 is True) are not allowed.
940+ z85: bool = False
941+ When set to True, the Z85 alphabet is used instead of the standard
942+ base85 alphabet.
916943
917944Decode a line of base85 data.
918945[clinic start generated code]*/
919946
920947static PyObject *
921- binascii_a2b_base85_impl (PyObject * module , Py_buffer * data , int strict_mode )
922- /*[clinic end generated code: output=337b9418636f30f4 input=a7555d0e33783562]*/
948+ binascii_a2b_base85_impl (PyObject * module , Py_buffer * data , int strict_mode ,
949+ int z85 )
950+ /*[clinic end generated code: output=c5b9118ffe77f1cb input=65c2a532ad64ebd5]*/
923951{
924- const unsigned char * ascii_data ;
952+ const unsigned char * ascii_data , * table_a2b ;
925953 unsigned char * bin_data ;
926954 int group_pos = 0 ;
927955 unsigned char this_ch , this_digit ;
@@ -930,6 +958,7 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode)
930958 _PyBytesWriter writer ;
931959 binascii_state * state ;
932960
961+ table_a2b = z85 ? table_a2b_base85_z85 : table_a2b_base85 ;
933962 ascii_data = data -> buf ;
934963 ascii_len = data -> len ;
935964
@@ -948,7 +977,7 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode)
948977 /* Shift (in radix-85) data or padding into our buffer. */
949978 if (ascii_len > 0 ) {
950979 this_ch = * ascii_data ;
951- this_digit = table_a2b_base85 [this_ch ];
980+ this_digit = table_a2b [this_ch ];
952981 } else {
953982 /* Pad with largest radix-85 digit when decoding. */
954983 this_digit = 84 ;
@@ -960,7 +989,8 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode)
960989 if (state == NULL ) {
961990 goto error_end ;
962991 }
963- PyErr_SetString (state -> Error , "base85 overflow" );
992+ PyErr_SetString (state -> Error ,
993+ z85 ? "z85 overflow" : "base85 overflow" );
964994 goto error_end ;
965995 }
966996 leftchar += this_digit ;
@@ -970,7 +1000,8 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode)
9701000 if (state == NULL ) {
9711001 goto error_end ;
9721002 }
973- PyErr_Format (state -> Error , "'%c' invalid in base85" , this_ch );
1003+ PyErr_Format (state -> Error , "'%c' %s" , this_ch ,
1004+ z85 ? "invalid in z85" : "invalid in base85" );
9741005 goto error_end ;
9751006 }
9761007
@@ -985,7 +1016,8 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode)
9851016 if (state == NULL ) {
9861017 goto error_end ;
9871018 }
988- PyErr_SetString (state -> Error , "base85 data has invalid length" );
1019+ PyErr_Format (state -> Error , "%s data has invalid length" ,
1020+ z85 ? "z85" : "base85" );
9891021 goto error_end ;
9901022 }
9911023
@@ -1016,21 +1048,24 @@ binascii.b2a_base85
10161048 Pad input to a multiple of 4 before encoding.
10171049 newline: bool = True
10181050 Append a newline to the result.
1051+ z85: bool = False
1052+ Use Z85 alphabet instead of standard base85 alphabet.
10191053
10201054Base85-code line of data.
10211055[clinic start generated code]*/
10221056
10231057static PyObject *
10241058binascii_b2a_base85_impl (PyObject * module , Py_buffer * data , int pad ,
1025- int newline )
1026- /*[clinic end generated code: output=56936eb231e15dc0 input=3899d4f5c3a589a0 ]*/
1059+ int newline , int z85 )
1060+ /*[clinic end generated code: output=d3740e9a20c8e071 input=e4e07591f7a11ae4 ]*/
10271061{
10281062 unsigned char * ascii_data ;
1029- const unsigned char * bin_data ;
1063+ const unsigned char * bin_data , * table_b2a ;
10301064 uint32_t leftchar = 0 ;
10311065 Py_ssize_t bin_len , group_len , out_len ;
10321066 _PyBytesWriter writer ;
10331067
1068+ table_b2a = z85 ? table_b2a_base85_z85 : table_b2a_base85 ;
10341069 bin_data = data -> buf ;
10351070 bin_len = data -> len ;
10361071
@@ -1052,15 +1087,15 @@ binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
10521087 leftchar = (bin_data [0 ] << 24 ) | (bin_data [1 ] << 16 ) |
10531088 (bin_data [2 ] << 8 ) | bin_data [3 ];
10541089
1055- ascii_data [4 ] = table_b2a_base85 [leftchar % 85 ];
1090+ ascii_data [4 ] = table_b2a [leftchar % 85 ];
10561091 leftchar /= 85 ;
1057- ascii_data [3 ] = table_b2a_base85 [leftchar % 85 ];
1092+ ascii_data [3 ] = table_b2a [leftchar % 85 ];
10581093 leftchar /= 85 ;
1059- ascii_data [2 ] = table_b2a_base85 [leftchar % 85 ];
1094+ ascii_data [2 ] = table_b2a [leftchar % 85 ];
10601095 leftchar /= 85 ;
1061- ascii_data [1 ] = table_b2a_base85 [leftchar % 85 ];
1096+ ascii_data [1 ] = table_b2a [leftchar % 85 ];
10621097 leftchar /= 85 ;
1063- ascii_data [0 ] = table_b2a_base85 [leftchar ];
1098+ ascii_data [0 ] = table_b2a [leftchar ];
10641099
10651100 ascii_data += 5 ;
10661101 }
@@ -1076,7 +1111,7 @@ binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
10761111 group_len = pad ? 5 : bin_len + 1 ;
10771112 for (Py_ssize_t i = 4 ; i >= 0 ; i -- ) {
10781113 if (i < group_len ) {
1079- ascii_data [i ] = table_b2a_base85 [leftchar % 85 ];
1114+ ascii_data [i ] = table_b2a [leftchar % 85 ];
10801115 }
10811116 leftchar /= 85 ;
10821117 }
0 commit comments