bpo-45085: Remove the binhex module (GH-28117)

The binhex module, deprecated in Python 3.9, is now removed. The
following binascii functions, deprecated in Python 3.9, are now also
removed:

* a2b_hqx(), b2a_hqx();
* rlecode_hqx(), rledecode_hqx().

The binascii.crc_hqx() function remains available.
This commit is contained in:
Victor Stinner 2021-09-02 12:10:08 +02:00 committed by GitHub
parent d589a7e7eb
commit a806608705
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 30 additions and 1297 deletions

View file

@ -72,69 +72,6 @@ get_binascii_state(PyObject *module)
return (binascii_state *)PyModule_GetState(module);
}
/*
** hqx lookup table, ascii->binary.
*/
#define RUNCHAR 0x90
#define DONE 0x7F
#define SKIP 0x7E
#define FAIL 0x7D
static const unsigned char table_a2b_hqx[256] = {
/* ^@ ^A ^B ^C ^D ^E ^F ^G */
/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/* \b \t \n ^K ^L \r ^N ^O */
/* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
/* ^P ^Q ^R ^S ^T ^U ^V ^W */
/* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
/* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/* ! " # $ % & ' */
/* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
/* ( ) * + , - . / */
/* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
/* 0 1 2 3 4 5 6 7 */
/* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
/* 8 9 : ; < = > ? */
/* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
/* @ A B C D E F G */
/* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
/* H I J K L M N O */
/* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
/* P Q R S T U V W */
/*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
/* X Y Z [ \ ] ^ _ */
/*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
/* ` a b c d e f g */
/*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
/* h i j k l m n o */
/*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
/* p q r s t u v w */
/*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
/* x y z { | } ~ ^? */
/*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
};
static const unsigned char table_b2a_hqx[] =
"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
static const unsigned char table_a2b_base64[] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
@ -165,7 +102,6 @@ static const unsigned char table_b2a_base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const unsigned short crctab_hqx[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
@ -649,356 +585,6 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
return _PyBytesWriter_Finish(&writer, ascii_data);
}
/*[clinic input]
binascii.a2b_hqx
data: ascii_buffer
/
Decode .hqx coding.
[clinic start generated code]*/
static PyObject *
binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=4d6d8c54d54ea1c1 input=0d914c680e0eed55]*/
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"binascii.a2b_hqx() is deprecated", 1) < 0) {
return NULL;
}
const unsigned char *ascii_data;
unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
PyObject *res;
Py_ssize_t len;
int done = 0;
_PyBytesWriter writer;
binascii_state *state;
ascii_data = data->buf;
len = data->len;
_PyBytesWriter_Init(&writer);
assert(len >= 0);
if (len > PY_SSIZE_T_MAX - 2)
return PyErr_NoMemory();
/* Allocate a string that is too big (fixed later)
Add two to the initial length to prevent interning which
would preclude subsequent resizing. */
bin_data = _PyBytesWriter_Alloc(&writer, len + 2);
if (bin_data == NULL)
return NULL;
for( ; len > 0 ; len--, ascii_data++ ) {
/* Get the byte and look it up */
this_ch = table_a2b_hqx[*ascii_data];
if ( this_ch == SKIP )
continue;
if ( this_ch == FAIL ) {
state = get_binascii_state(module);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "Illegal char");
_PyBytesWriter_Dealloc(&writer);
return NULL;
}
if ( this_ch == DONE ) {
/* The terminating colon */
done = 1;
break;
}
/* Shift it into the buffer and see if any bytes are ready */
leftchar = (leftchar << 6) | (this_ch);
leftbits += 6;
if ( leftbits >= 8 ) {
leftbits -= 8;
*bin_data++ = (leftchar >> leftbits) & 0xff;
leftchar &= ((1 << leftbits) - 1);
}
}
if ( leftbits && !done ) {
state = get_binascii_state(module);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Incomplete,
"String has incomplete number of bytes");
_PyBytesWriter_Dealloc(&writer);
return NULL;
}
res = _PyBytesWriter_Finish(&writer, bin_data);
if (res == NULL)
return NULL;
return Py_BuildValue("Ni", res, done);
}
/*[clinic input]
binascii.rlecode_hqx
data: Py_buffer
/
Binhex RLE-code binary data.
[clinic start generated code]*/
static PyObject *
binascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=393d79338f5f5629 input=e1f1712447a82b09]*/
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"binascii.rlecode_hqx() is deprecated", 1) < 0) {
return NULL;
}
const unsigned char *in_data;
unsigned char *out_data;
unsigned char ch;
Py_ssize_t in, inend, len;
_PyBytesWriter writer;
_PyBytesWriter_Init(&writer);
in_data = data->buf;
len = data->len;
assert(len >= 0);
if (len > PY_SSIZE_T_MAX / 2 - 2)
return PyErr_NoMemory();
/* Worst case: output is twice as big as input (fixed later) */
out_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
if (out_data == NULL)
return NULL;
for( in=0; in<len; in++) {
ch = in_data[in];
if ( ch == RUNCHAR ) {
/* RUNCHAR. Escape it. */
*out_data++ = RUNCHAR;
*out_data++ = 0;
} else {
/* Check how many following are the same */
for(inend=in+1;
inend<len && in_data[inend] == ch &&
inend < in+255;
inend++) ;
if ( inend - in > 3 ) {
/* More than 3 in a row. Output RLE. */
*out_data++ = ch;
*out_data++ = RUNCHAR;
*out_data++ = (unsigned char) (inend-in);
in = inend-1;
} else {
/* Less than 3. Output the byte itself */
*out_data++ = ch;
}
}
}
return _PyBytesWriter_Finish(&writer, out_data);
}
/*[clinic input]
binascii.b2a_hqx
data: Py_buffer
/
Encode .hqx data.
[clinic start generated code]*/
static PyObject *
binascii_b2a_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=d0aa5a704bc9f7de input=9596ebe019fe12ba]*/
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"binascii.b2a_hqx() is deprecated", 1) < 0) {
return NULL;
}
unsigned char *ascii_data;
const unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
Py_ssize_t len;
_PyBytesWriter writer;
bin_data = data->buf;
len = data->len;
_PyBytesWriter_Init(&writer);
assert(len >= 0);
if (len > PY_SSIZE_T_MAX / 2 - 2)
return PyErr_NoMemory();
/* Allocate a buffer that is at least large enough */
ascii_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
if (ascii_data == NULL)
return NULL;
for( ; len > 0 ; len--, bin_data++ ) {
/* Shift into our buffer, and output any 6bits ready */
leftchar = (leftchar << 8) | *bin_data;
leftbits += 8;
while ( leftbits >= 6 ) {
this_ch = (leftchar >> (leftbits-6)) & 0x3f;
leftbits -= 6;
*ascii_data++ = table_b2a_hqx[this_ch];
}
}
/* Output a possible runt byte */
if ( leftbits ) {
leftchar <<= (6-leftbits);
*ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
}
return _PyBytesWriter_Finish(&writer, ascii_data);
}
/*[clinic input]
binascii.rledecode_hqx
data: Py_buffer
/
Decode hexbin RLE-coded string.
[clinic start generated code]*/
static PyObject *
binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=9826619565de1c6c input=54cdd49fc014402c]*/
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"binascii.rledecode_hqx() is deprecated", 1) < 0) {
return NULL;
}
const unsigned char *in_data;
unsigned char *out_data;
unsigned char in_byte, in_repeat;
Py_ssize_t in_len;
_PyBytesWriter writer;
in_data = data->buf;
in_len = data->len;
_PyBytesWriter_Init(&writer);
binascii_state *state;
assert(in_len >= 0);
/* Empty string is a special case */
if ( in_len == 0 )
return PyBytes_FromStringAndSize("", 0);
else if (in_len > PY_SSIZE_T_MAX / 2)
return PyErr_NoMemory();
/* Allocate a buffer of reasonable size. Resized when needed */
out_data = _PyBytesWriter_Alloc(&writer, in_len);
if (out_data == NULL)
return NULL;
/* Use overallocation */
writer.overallocate = 1;
/*
** We need two macros here to get/put bytes and handle
** end-of-buffer for input and output strings.
*/
#define INBYTE(b) \
do { \
if ( --in_len < 0 ) { \
state = get_binascii_state(module); \
if (state == NULL) { \
return NULL; \
} \
PyErr_SetString(state->Incomplete, ""); \
goto error; \
} \
b = *in_data++; \
} while(0)
/*
** Handle first byte separately (since we have to get angry
** in case of an orphaned RLE code).
*/
INBYTE(in_byte);
if (in_byte == RUNCHAR) {
INBYTE(in_repeat);
/* only 1 byte will be written, but 2 bytes were preallocated:
subtract 1 byte to prevent overallocation */
writer.min_size--;
if (in_repeat != 0) {
/* Note Error, not Incomplete (which is at the end
** of the string only). This is a programmer error.
*/
state = get_binascii_state(module);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "Orphaned RLE code at start");
goto error;
}
*out_data++ = RUNCHAR;
} else {
*out_data++ = in_byte;
}
while( in_len > 0 ) {
INBYTE(in_byte);
if (in_byte == RUNCHAR) {
INBYTE(in_repeat);
/* only 1 byte will be written, but 2 bytes were preallocated:
subtract 1 byte to prevent overallocation */
writer.min_size--;
if ( in_repeat == 0 ) {
/* Just an escaped RUNCHAR value */
*out_data++ = RUNCHAR;
} else {
/* Pick up value and output a sequence of it */
in_byte = out_data[-1];
/* enlarge the buffer if needed */
if (in_repeat > 1) {
/* -1 because we already preallocated 1 byte */
out_data = _PyBytesWriter_Prepare(&writer, out_data,
in_repeat - 1);
if (out_data == NULL)
goto error;
}
while ( --in_repeat > 0 )
*out_data++ = in_byte;
}
} else {
/* Normal byte */
*out_data++ = in_byte;
}
}
return _PyBytesWriter_Finish(&writer, out_data);
error:
_PyBytesWriter_Dealloc(&writer);
return NULL;
}
/*[clinic input]
binascii.crc_hqx
@ -1629,14 +1215,10 @@ static struct PyMethodDef binascii_module_methods[] = {
BINASCII_B2A_UU_METHODDEF
BINASCII_A2B_BASE64_METHODDEF
BINASCII_B2A_BASE64_METHODDEF
BINASCII_A2B_HQX_METHODDEF
BINASCII_B2A_HQX_METHODDEF
BINASCII_A2B_HEX_METHODDEF
BINASCII_B2A_HEX_METHODDEF
BINASCII_HEXLIFY_METHODDEF
BINASCII_UNHEXLIFY_METHODDEF
BINASCII_RLECODE_HQX_METHODDEF
BINASCII_RLEDECODE_HQX_METHODDEF
BINASCII_CRC_HQX_METHODDEF
BINASCII_CRC32_METHODDEF
BINASCII_A2B_QP_METHODDEF