mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
Make binascii use byte strings everywhere (in and out).
This commit is contained in:
parent
6dd15d3ab0
commit
0e225aa09b
2 changed files with 116 additions and 89 deletions
|
@ -138,7 +138,7 @@ static char table_a2b_base64[] = {
|
|||
#define BASE64_PAD '='
|
||||
|
||||
/* Max binary chunk size; limited only by available memory */
|
||||
#define BASE64_MAXBIN (INT_MAX/2 - sizeof(PyStringObject) - 3)
|
||||
#define BASE64_MAXBIN ((PY_SSIZE_T_MAX - 3) / 2)
|
||||
|
||||
static unsigned char table_b2a_base64[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
@ -200,9 +200,9 @@ binascii_a2b_uu(PyObject *self, PyObject *args)
|
|||
ascii_len--;
|
||||
|
||||
/* Allocate the buffer */
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
|
||||
return NULL;
|
||||
bin_data = (unsigned char *)PyString_AsString(rv);
|
||||
bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
|
||||
for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
|
||||
/* XXX is it really best to add NULs if there's no more data */
|
||||
|
@ -277,9 +277,9 @@ binascii_b2a_uu(PyObject *self, PyObject *args)
|
|||
}
|
||||
|
||||
/* We're lazy and allocate to much (fixed up later) */
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2+2)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2+2)) == NULL )
|
||||
return NULL;
|
||||
ascii_data = (unsigned char *)PyString_AsString(rv);
|
||||
ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
|
||||
/* Store the length */
|
||||
*ascii_data++ = ' ' + (bin_len & 077);
|
||||
|
@ -301,8 +301,12 @@ binascii_b2a_uu(PyObject *self, PyObject *args)
|
|||
}
|
||||
*ascii_data++ = '\n'; /* Append a courtesy newline */
|
||||
|
||||
_PyString_Resize(&rv, (ascii_data -
|
||||
(unsigned char *)PyString_AsString(rv)));
|
||||
if (PyBytes_Resize(rv,
|
||||
(ascii_data -
|
||||
(unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
|
||||
Py_DECREF(rv);
|
||||
rv = NULL;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
@ -351,9 +355,9 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
|
|||
bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
|
||||
|
||||
/* Allocate the buffer */
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
|
||||
return NULL;
|
||||
bin_data = (unsigned char *)PyString_AsString(rv);
|
||||
bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
bin_len = 0;
|
||||
|
||||
for( ; ascii_len > 0; ascii_len--, ascii_data++) {
|
||||
|
@ -412,13 +416,17 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
|
|||
|
||||
/* And set string size correctly. If the result string is empty
|
||||
** (because the input was all invalid) return the shared empty
|
||||
** string instead; _PyString_Resize() won't do this for us.
|
||||
** string instead; PyBytes_Resize() won't do this for us.
|
||||
*/
|
||||
if (bin_len > 0)
|
||||
_PyString_Resize(&rv, bin_len);
|
||||
if (bin_len > 0) {
|
||||
if (PyBytes_Resize(rv, bin_len) < 0) {
|
||||
Py_DECREF(rv);
|
||||
rv = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Py_DECREF(rv);
|
||||
rv = PyString_FromString("");
|
||||
rv = PyBytes_FromStringAndSize("", 0);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
@ -445,9 +453,9 @@ binascii_b2a_base64(PyObject *self, PyObject *args)
|
|||
/* We're lazy and allocate too much (fixed up later).
|
||||
"+3" leaves room for up to two pad characters and a trailing
|
||||
newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
|
||||
return NULL;
|
||||
ascii_data = (unsigned char *)PyString_AsString(rv);
|
||||
ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
|
||||
for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
|
||||
/* Shift the data into our buffer */
|
||||
|
@ -471,8 +479,12 @@ binascii_b2a_base64(PyObject *self, PyObject *args)
|
|||
}
|
||||
*ascii_data++ = '\n'; /* Append a courtesy newline */
|
||||
|
||||
_PyString_Resize(&rv, (ascii_data -
|
||||
(unsigned char *)PyString_AsString(rv)));
|
||||
if (PyBytes_Resize(rv,
|
||||
(ascii_data -
|
||||
(unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
|
||||
Py_DECREF(rv);
|
||||
rv = NULL;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
@ -495,9 +507,9 @@ binascii_a2b_hqx(PyObject *self, PyObject *args)
|
|||
/* Allocate a string that is too big (fixed later)
|
||||
Add two to the initial length to prevent interning which
|
||||
would preclude subsequent resizing. */
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
|
||||
return NULL;
|
||||
bin_data = (unsigned char *)PyString_AsString(rv);
|
||||
bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
|
||||
for( ; len > 0 ; len--, ascii_data++ ) {
|
||||
/* Get the byte and look it up */
|
||||
|
@ -531,8 +543,12 @@ binascii_a2b_hqx(PyObject *self, PyObject *args)
|
|||
Py_DECREF(rv);
|
||||
return NULL;
|
||||
}
|
||||
_PyString_Resize(
|
||||
&rv, (bin_data - (unsigned char *)PyString_AsString(rv)));
|
||||
if (PyBytes_Resize(rv,
|
||||
(bin_data -
|
||||
(unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
|
||||
Py_DECREF(rv);
|
||||
rv = NULL;
|
||||
}
|
||||
if (rv) {
|
||||
PyObject *rrv = Py_BuildValue("Oi", rv, done);
|
||||
Py_DECREF(rv);
|
||||
|
@ -556,9 +572,9 @@ binascii_rlecode_hqx(PyObject *self, PyObject *args)
|
|||
return NULL;
|
||||
|
||||
/* Worst case: output is twice as big as input (fixed later) */
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
|
||||
return NULL;
|
||||
out_data = (unsigned char *)PyString_AsString(rv);
|
||||
out_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
|
||||
for( in=0; in<len; in++) {
|
||||
ch = in_data[in];
|
||||
|
@ -584,8 +600,12 @@ binascii_rlecode_hqx(PyObject *self, PyObject *args)
|
|||
}
|
||||
}
|
||||
}
|
||||
_PyString_Resize(&rv, (out_data -
|
||||
(unsigned char *)PyString_AsString(rv)));
|
||||
if (PyBytes_Resize(rv,
|
||||
(out_data -
|
||||
(unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
|
||||
Py_DECREF(rv);
|
||||
rv = NULL;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
@ -605,9 +625,9 @@ binascii_b2a_hqx(PyObject *self, PyObject *args)
|
|||
return NULL;
|
||||
|
||||
/* Allocate a buffer that is at least large enough */
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
|
||||
return NULL;
|
||||
ascii_data = (unsigned char *)PyString_AsString(rv);
|
||||
ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
|
||||
for( ; len > 0 ; len--, bin_data++ ) {
|
||||
/* Shift into our buffer, and output any 6bits ready */
|
||||
|
@ -624,8 +644,12 @@ binascii_b2a_hqx(PyObject *self, PyObject *args)
|
|||
leftchar <<= (6-leftbits);
|
||||
*ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
|
||||
}
|
||||
_PyString_Resize(&rv, (ascii_data -
|
||||
(unsigned char *)PyString_AsString(rv)));
|
||||
if (PyBytes_Resize(rv,
|
||||
(ascii_data -
|
||||
(unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
|
||||
Py_DECREF(rv);
|
||||
rv = NULL;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
@ -644,14 +668,14 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
|||
|
||||
/* Empty string is a special case */
|
||||
if ( in_len == 0 )
|
||||
return PyString_FromString("");
|
||||
return PyBytes_FromStringAndSize("", 0);
|
||||
|
||||
/* Allocate a buffer of reasonable size. Resized when needed */
|
||||
out_len = in_len*2;
|
||||
if ( (rv=PyString_FromStringAndSize(NULL, out_len)) == NULL )
|
||||
if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL )
|
||||
return NULL;
|
||||
out_len_left = out_len;
|
||||
out_data = (unsigned char *)PyString_AsString(rv);
|
||||
out_data = (unsigned char *)PyBytes_AS_STRING(rv);
|
||||
|
||||
/*
|
||||
** We need two macros here to get/put bytes and handle
|
||||
|
@ -670,9 +694,9 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
|||
#define OUTBYTE(b) \
|
||||
do { \
|
||||
if ( --out_len_left < 0 ) { \
|
||||
_PyString_Resize(&rv, 2*out_len); \
|
||||
if ( rv == NULL ) return NULL; \
|
||||
out_data = (unsigned char *)PyString_AsString(rv) \
|
||||
if (PyBytes_Resize(rv, 2*out_len) < 0) \
|
||||
{ Py_DECREF(rv); return NULL; } \
|
||||
out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
|
||||
+ out_len; \
|
||||
out_len_left = out_len-1; \
|
||||
out_len = out_len * 2; \
|
||||
|
@ -720,8 +744,12 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
|||
OUTBYTE(in_byte);
|
||||
}
|
||||
}
|
||||
_PyString_Resize(&rv, (out_data -
|
||||
(unsigned char *)PyString_AsString(rv)));
|
||||
if (PyBytes_Resize(rv,
|
||||
(out_data -
|
||||
(unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
|
||||
Py_DECREF(rv);
|
||||
rv = NULL;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
@ -912,12 +940,10 @@ binascii_hexlify(PyObject *self, PyObject *args)
|
|||
if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen))
|
||||
return NULL;
|
||||
|
||||
retval = PyString_FromStringAndSize(NULL, arglen*2);
|
||||
retval = PyBytes_FromStringAndSize(NULL, arglen*2);
|
||||
if (!retval)
|
||||
return NULL;
|
||||
retbuf = PyString_AsString(retval);
|
||||
if (!retbuf)
|
||||
goto finally;
|
||||
retbuf = PyBytes_AS_STRING(retval);
|
||||
|
||||
/* make hex version of string, taken from shamodule.c */
|
||||
for (i=j=0; i < arglen; i++) {
|
||||
|
@ -978,12 +1004,10 @@ binascii_unhexlify(PyObject *self, PyObject *args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
retval = PyString_FromStringAndSize(NULL, (arglen/2));
|
||||
retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
|
||||
if (!retval)
|
||||
return NULL;
|
||||
retbuf = PyString_AsString(retval);
|
||||
if (!retbuf)
|
||||
goto finally;
|
||||
retbuf = PyBytes_AS_STRING(retval);
|
||||
|
||||
for (i=j=0; i < arglen; i += 2) {
|
||||
int top = to_int(Py_CHARMASK(argbuf[i]));
|
||||
|
@ -1095,7 +1119,7 @@ binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
out++;
|
||||
}
|
||||
}
|
||||
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
|
||||
if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
|
||||
PyMem_Free(odata);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1295,7 +1319,7 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
|
|||
}
|
||||
}
|
||||
}
|
||||
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
|
||||
if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
|
||||
PyMem_Free(odata);
|
||||
return NULL;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue