Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4897,33 +4897,27 @@ _PyUnicode_EncodeUTF7(PyObject *str,
int base64WhiteSpace,
const char *errors)
{
int kind;
const void *data;
Py_ssize_t len;
PyObject *v;
int inShift = 0;
Py_ssize_t i;
unsigned int base64bits = 0;
unsigned long base64buffer = 0;
char * out;
const char * start;

kind = PyUnicode_KIND(str);
data = PyUnicode_DATA(str);
len = PyUnicode_GET_LENGTH(str);

if (len == 0)
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
if (len == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
int kind = PyUnicode_KIND(str);
const void *data = PyUnicode_DATA(str);

/* It might be possible to tighten this worst case */
if (len > PY_SSIZE_T_MAX / 8)
if (len > PY_SSIZE_T_MAX / 8) {
return PyErr_NoMemory();
v = PyBytes_FromStringAndSize(NULL, len * 8);
if (v == NULL)
}
PyBytesWriter *writer = PyBytesWriter_Create(len * 8);
if (writer == NULL) {
return NULL;
}

start = out = PyBytes_AS_STRING(v);
for (i = 0; i < len; ++i) {
int inShift = 0;
unsigned int base64bits = 0;
unsigned long base64buffer = 0;
char *out = PyBytesWriter_GetData(writer);
for (Py_ssize_t i = 0; i < len; ++i) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);

if (inShift) {
Expand Down Expand Up @@ -4986,9 +4980,7 @@ _PyUnicode_EncodeUTF7(PyObject *str,
*out++= TO_BASE64(base64buffer << (6-base64bits) );
if (inShift)
*out++ = '-';
if (_PyBytes_Resize(&v, out - start) < 0)
return NULL;
return v;
return PyBytesWriter_FinishWithPointer(writer, out);
}

#undef IS_BASE64
Expand Down
Loading