/*[clinic end generated code: output=e027f8e0b0598742 input=7cafeaf73df63d1c]*/
{
const unsigned char *ascii_data;
- unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
- PyObject *rv;
Py_ssize_t ascii_len, bin_len;
binascii_state *state;
ascii_len--;
/* Allocate the buffer */
- if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
+ PyBytesWriter *writer = PyBytesWriter_Create(bin_len);
+ if (writer == NULL) {
return NULL;
- bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
+ }
+ unsigned char *bin_data = PyBytesWriter_GetData(writer);
for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
/* XXX is it really best to add NULs if there's no more data */
if ( this_ch < ' ' || this_ch > (' ' + 64)) {
state = get_binascii_state(module);
if (state == NULL) {
- return NULL;
+ goto error;
}
PyErr_SetString(state->Error, "Illegal char");
- Py_DECREF(rv);
- return NULL;
+ goto error;
}
this_ch = (this_ch - ' ') & 077;
}
this_ch != '\n' && this_ch != '\r' ) {
state = get_binascii_state(module);
if (state == NULL) {
- return NULL;
+ goto error;
}
PyErr_SetString(state->Error, "Trailing garbage");
- Py_DECREF(rv);
- return NULL;
+ goto error;
}
}
- return rv;
+ return PyBytesWriter_Finish(writer);
+
+error:
+ PyBytesWriter_Discard(writer);
+ return NULL;
}
/*[clinic input]
binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick)
/*[clinic end generated code: output=b1b99de62d9bbeb8 input=beb27822241095cd]*/
{
- unsigned char *ascii_data;
const unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
binascii_state *state;
- Py_ssize_t bin_len, out_len;
- _PyBytesWriter writer;
+ Py_ssize_t bin_len;
- _PyBytesWriter_Init(&writer);
bin_data = data->buf;
bin_len = data->len;
if ( bin_len > 45 ) {
}
/* We're lazy and allocate to much (fixed up later) */
- out_len = 2 + (bin_len + 2) / 3 * 4;
- ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
- if (ascii_data == NULL)
+ Py_ssize_t out_len = 2 + (bin_len + 2) / 3 * 4;
+ PyBytesWriter *writer = PyBytesWriter_Create(out_len);
+ if (writer == NULL) {
return NULL;
+ }
+ unsigned char *ascii_data = PyBytesWriter_GetData(writer);
/* Store the length */
if (backtick && !bin_len)
}
*ascii_data++ = '\n'; /* Append a courtesy newline */
- return _PyBytesWriter_Finish(&writer, ascii_data);
+ return PyBytesWriter_FinishWithPointer(writer, ascii_data);
}
/*[clinic input]
/* Allocate the buffer */
Py_ssize_t bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
- _PyBytesWriter writer;
- _PyBytesWriter_Init(&writer);
- unsigned char *bin_data = _PyBytesWriter_Alloc(&writer, bin_len);
- if (bin_data == NULL)
+ PyBytesWriter *writer = PyBytesWriter_Create(bin_len);
+ if (writer == NULL) {
return NULL;
- unsigned char *bin_data_start = bin_data;
+ }
+ unsigned char *bin_data = PyBytesWriter_GetData(writer);
if (strict_mode && ascii_len > 0 && ascii_data[0] == '=') {
state = get_binascii_state(module);
state = get_binascii_state(module);
if (state == NULL) {
/* error already set, from get_binascii_state */
+ assert(PyErr_Occurred());
} else if (quad_pos == 1) {
/*
** There is exactly one extra valid, non-padding, base64 character.
** This is an invalid length, as there is no possible input that
** could encoded into such a base64 string.
*/
+ unsigned char *bin_data_start = PyBytesWriter_GetData(writer);
PyErr_Format(state->Error,
"Invalid base64-encoded string: "
"number of data characters (%zd) cannot be 1 more "
} else {
PyErr_SetString(state->Error, "Incorrect padding");
}
- error_end:
- _PyBytesWriter_Dealloc(&writer);
- return NULL;
+ goto error_end;
}
done:
- return _PyBytesWriter_Finish(&writer, bin_data);
+ return PyBytesWriter_FinishWithPointer(writer, bin_data);
+
+error_end:
+ PyBytesWriter_Discard(writer);
+ return NULL;
}
binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
/*[clinic end generated code: output=4ad62c8e8485d3b3 input=0e20ff59c5f2e3e1]*/
{
- unsigned char *ascii_data;
const unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
- Py_ssize_t bin_len, out_len;
- _PyBytesWriter writer;
+ Py_ssize_t bin_len;
binascii_state *state;
bin_data = data->buf;
bin_len = data->len;
- _PyBytesWriter_Init(&writer);
assert(bin_len >= 0);
/* We're lazy and allocate too much (fixed up later).
"+2" leaves room for up to two pad characters.
Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
- out_len = bin_len*2 + 2;
- if (newline)
+ Py_ssize_t out_len = bin_len*2 + 2;
+ if (newline) {
out_len++;
- ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
- if (ascii_data == NULL)
+ }
+ PyBytesWriter *writer = PyBytesWriter_Create(out_len);
+ if (writer == NULL) {
return NULL;
+ }
+ unsigned char *ascii_data = PyBytesWriter_GetData(writer);
for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
/* Shift the data into our buffer */
if (newline)
*ascii_data++ = '\n'; /* Append a courtesy newline */
- return _PyBytesWriter_Finish(&writer, ascii_data);
+ return PyBytesWriter_FinishWithPointer(writer, ascii_data);
}
{
const char* argbuf;
Py_ssize_t arglen;
- PyObject *retval;
- char* retbuf;
Py_ssize_t i, j;
binascii_state *state;
return NULL;
}
- retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
- if (!retval)
+ PyBytesWriter *writer = PyBytesWriter_Create(arglen/2);
+ if (writer == NULL) {
return NULL;
- retbuf = PyBytes_AS_STRING(retval);
+ }
+ char *retbuf = PyBytesWriter_GetData(writer);
for (i=j=0; i < arglen; i += 2) {
unsigned int top = _PyLong_DigitValue[Py_CHARMASK(argbuf[i])];
if (top >= 16 || bot >= 16) {
state = get_binascii_state(module);
if (state == NULL) {
- return NULL;
+ goto error;
}
PyErr_SetString(state->Error,
"Non-hexadecimal digit found");
- goto finally;
+ goto error;
}
retbuf[j++] = (top << 4) + bot;
}
- return retval;
+ return PyBytesWriter_Finish(writer);
- finally:
- Py_DECREF(retval);
+error:
+ PyBytesWriter_Discard(writer);
return NULL;
}