Issue #7703: Add support for the new buffer API to functions of the
binascii module. Backported from py3k by Florent Xicluna, with some additional tests.
This commit is contained in:
parent
647ed91d5c
commit
fd3a60d5ef
|
@ -3,14 +3,19 @@
|
||||||
from test import test_support
|
from test import test_support
|
||||||
import unittest
|
import unittest
|
||||||
import binascii
|
import binascii
|
||||||
|
import array
|
||||||
|
|
||||||
class BinASCIITest(unittest.TestCase):
|
class BinASCIITest(unittest.TestCase):
|
||||||
|
|
||||||
|
type2test = str
|
||||||
# Create binary test data
|
# Create binary test data
|
||||||
data = "The quick brown fox jumps over the lazy dog.\r\n"
|
rawdata = "The quick brown fox jumps over the lazy dog.\r\n"
|
||||||
# Be slow so we don't depend on other modules
|
# Be slow so we don't depend on other modules
|
||||||
data += "".join(map(chr, xrange(256)))
|
rawdata += "".join(map(chr, xrange(256)))
|
||||||
data += "\r\nHello world.\n"
|
rawdata += "\r\nHello world.\n"
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.data = self.type2test(self.rawdata)
|
||||||
|
|
||||||
def test_exceptions(self):
|
def test_exceptions(self):
|
||||||
# Check module exceptions
|
# Check module exceptions
|
||||||
|
@ -26,10 +31,10 @@ class BinASCIITest(unittest.TestCase):
|
||||||
prefixes.extend(["crc_", "rlecode_", "rledecode_"])
|
prefixes.extend(["crc_", "rlecode_", "rledecode_"])
|
||||||
for prefix in prefixes:
|
for prefix in prefixes:
|
||||||
name = prefix + suffix
|
name = prefix + suffix
|
||||||
self.assertTrue(callable(getattr(binascii, name)))
|
self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
|
||||||
self.assertRaises(TypeError, getattr(binascii, name))
|
self.assertRaises(TypeError, getattr(binascii, name))
|
||||||
for name in ("hexlify", "unhexlify"):
|
for name in ("hexlify", "unhexlify"):
|
||||||
self.assertTrue(callable(getattr(binascii, name)))
|
self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
|
||||||
self.assertRaises(TypeError, getattr(binascii, name))
|
self.assertRaises(TypeError, getattr(binascii, name))
|
||||||
|
|
||||||
def test_base64valid(self):
|
def test_base64valid(self):
|
||||||
|
@ -44,7 +49,7 @@ class BinASCIITest(unittest.TestCase):
|
||||||
for line in lines:
|
for line in lines:
|
||||||
b = binascii.a2b_base64(line)
|
b = binascii.a2b_base64(line)
|
||||||
res = res + b
|
res = res + b
|
||||||
self.assertEqual(res, self.data)
|
self.assertEqual(res, self.rawdata)
|
||||||
|
|
||||||
def test_base64invalid(self):
|
def test_base64invalid(self):
|
||||||
# Test base64 with random invalid characters sprinkled throughout
|
# Test base64 with random invalid characters sprinkled throughout
|
||||||
|
@ -77,7 +82,7 @@ class BinASCIITest(unittest.TestCase):
|
||||||
for line in map(addnoise, lines):
|
for line in map(addnoise, lines):
|
||||||
b = binascii.a2b_base64(line)
|
b = binascii.a2b_base64(line)
|
||||||
res += b
|
res += b
|
||||||
self.assertEqual(res, self.data)
|
self.assertEqual(res, self.rawdata)
|
||||||
|
|
||||||
# Test base64 with just invalid characters, which should return
|
# Test base64 with just invalid characters, which should return
|
||||||
# empty strings. TBD: shouldn't it raise an exception instead ?
|
# empty strings. TBD: shouldn't it raise an exception instead ?
|
||||||
|
@ -94,7 +99,7 @@ class BinASCIITest(unittest.TestCase):
|
||||||
for line in lines:
|
for line in lines:
|
||||||
b = binascii.a2b_uu(line)
|
b = binascii.a2b_uu(line)
|
||||||
res += b
|
res += b
|
||||||
self.assertEqual(res, self.data)
|
self.assertEqual(res, self.rawdata)
|
||||||
|
|
||||||
self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31)
|
self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31)
|
||||||
self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32)
|
self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32)
|
||||||
|
@ -167,8 +172,20 @@ class BinASCIITest(unittest.TestCase):
|
||||||
f('')
|
f('')
|
||||||
binascii.crc_hqx('', 0)
|
binascii.crc_hqx('', 0)
|
||||||
|
|
||||||
|
|
||||||
|
class ArrayBinASCIITest(BinASCIITest):
|
||||||
|
def type2test(self, s):
|
||||||
|
return array.array('c', s)
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryviewBinASCIITest(BinASCIITest):
|
||||||
|
type2test = memoryview
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(BinASCIITest)
|
test_support.run_unittest(BinASCIITest,
|
||||||
|
ArrayBinASCIITest,
|
||||||
|
MemoryviewBinASCIITest)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
|
@ -32,6 +32,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #7703: Add support for the new buffer API to functions of the
|
||||||
|
binascii module. Backported from py3k by Florent Xicluna, with some
|
||||||
|
additional tests.
|
||||||
|
|
||||||
- Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
|
- Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
|
||||||
Patch by Brian Curtin.
|
Patch by Brian Curtin.
|
||||||
|
|
||||||
|
|
|
@ -188,6 +188,7 @@ PyDoc_STRVAR(doc_a2b_uu, "(ascii) -> bin. Decode a line of uuencoded data");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_a2b_uu(PyObject *self, PyObject *args)
|
binascii_a2b_uu(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pascii;
|
||||||
unsigned char *ascii_data, *bin_data;
|
unsigned char *ascii_data, *bin_data;
|
||||||
int leftbits = 0;
|
int leftbits = 0;
|
||||||
unsigned char this_ch;
|
unsigned char this_ch;
|
||||||
|
@ -195,8 +196,10 @@ binascii_a2b_uu(PyObject *self, PyObject *args)
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
Py_ssize_t ascii_len, bin_len;
|
Py_ssize_t ascii_len, bin_len;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "t#:a2b_uu", &ascii_data, &ascii_len) )
|
if ( !PyArg_ParseTuple(args, "s*:a2b_uu", &pascii) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
ascii_data = pascii.buf;
|
||||||
|
ascii_len = pascii.len;
|
||||||
|
|
||||||
assert(ascii_len >= 0);
|
assert(ascii_len >= 0);
|
||||||
|
|
||||||
|
@ -205,9 +208,11 @@ binascii_a2b_uu(PyObject *self, PyObject *args)
|
||||||
ascii_len--;
|
ascii_len--;
|
||||||
|
|
||||||
/* Allocate the buffer */
|
/* Allocate the buffer */
|
||||||
if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
|
if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL ) {
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
return NULL;
|
return NULL;
|
||||||
bin_data = (unsigned char *)PyString_AsString(rv);
|
}
|
||||||
|
bin_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
|
|
||||||
for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
|
for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
|
||||||
/* XXX is it really best to add NULs if there's no more data */
|
/* XXX is it really best to add NULs if there's no more data */
|
||||||
|
@ -226,6 +231,7 @@ binascii_a2b_uu(PyObject *self, PyObject *args)
|
||||||
*/
|
*/
|
||||||
if ( this_ch < ' ' || this_ch > (' ' + 64)) {
|
if ( this_ch < ' ' || this_ch > (' ' + 64)) {
|
||||||
PyErr_SetString(Error, "Illegal char");
|
PyErr_SetString(Error, "Illegal char");
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
Py_DECREF(rv);
|
Py_DECREF(rv);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -254,10 +260,12 @@ binascii_a2b_uu(PyObject *self, PyObject *args)
|
||||||
if ( this_ch != ' ' && this_ch != ' '+64 &&
|
if ( this_ch != ' ' && this_ch != ' '+64 &&
|
||||||
this_ch != '\n' && this_ch != '\r' ) {
|
this_ch != '\n' && this_ch != '\r' ) {
|
||||||
PyErr_SetString(Error, "Trailing garbage");
|
PyErr_SetString(Error, "Trailing garbage");
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
Py_DECREF(rv);
|
Py_DECREF(rv);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,6 +274,7 @@ PyDoc_STRVAR(doc_b2a_uu, "(bin) -> ascii. Uuencode line of data");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_b2a_uu(PyObject *self, PyObject *args)
|
binascii_b2a_uu(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pbin;
|
||||||
unsigned char *ascii_data, *bin_data;
|
unsigned char *ascii_data, *bin_data;
|
||||||
int leftbits = 0;
|
int leftbits = 0;
|
||||||
unsigned char this_ch;
|
unsigned char this_ch;
|
||||||
|
@ -273,18 +282,23 @@ binascii_b2a_uu(PyObject *self, PyObject *args)
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
Py_ssize_t bin_len;
|
Py_ssize_t bin_len;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#:b2a_uu", &bin_data, &bin_len) )
|
if ( !PyArg_ParseTuple(args, "s*:b2a_uu", &pbin) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
bin_data = pbin.buf;
|
||||||
|
bin_len = pbin.len;
|
||||||
if ( bin_len > 45 ) {
|
if ( bin_len > 45 ) {
|
||||||
/* The 45 is a limit that appears in all uuencode's */
|
/* The 45 is a limit that appears in all uuencode's */
|
||||||
PyErr_SetString(Error, "At most 45 bytes at once");
|
PyErr_SetString(Error, "At most 45 bytes at once");
|
||||||
|
PyBuffer_Release(&pbin);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We're lazy and allocate to much (fixed up later) */
|
/* We're lazy and allocate to much (fixed up later) */
|
||||||
if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2+2)) == NULL )
|
if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2+2)) == NULL ) {
|
||||||
|
PyBuffer_Release(&pbin);
|
||||||
return NULL;
|
return NULL;
|
||||||
ascii_data = (unsigned char *)PyString_AsString(rv);
|
}
|
||||||
|
ascii_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
|
|
||||||
/* Store the length */
|
/* Store the length */
|
||||||
*ascii_data++ = ' ' + (bin_len & 077);
|
*ascii_data++ = ' ' + (bin_len & 077);
|
||||||
|
@ -306,8 +320,13 @@ binascii_b2a_uu(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
*ascii_data++ = '\n'; /* Append a courtesy newline */
|
*ascii_data++ = '\n'; /* Append a courtesy newline */
|
||||||
|
|
||||||
_PyString_Resize(&rv, (ascii_data -
|
if (_PyString_Resize(&rv,
|
||||||
(unsigned char *)PyString_AsString(rv)));
|
(ascii_data -
|
||||||
|
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
rv = NULL;
|
||||||
|
}
|
||||||
|
PyBuffer_Release(&pbin);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,6 +361,7 @@ PyDoc_STRVAR(doc_a2b_base64, "(ascii) -> bin. Decode a line of base64 data");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_a2b_base64(PyObject *self, PyObject *args)
|
binascii_a2b_base64(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pascii;
|
||||||
unsigned char *ascii_data, *bin_data;
|
unsigned char *ascii_data, *bin_data;
|
||||||
int leftbits = 0;
|
int leftbits = 0;
|
||||||
unsigned char this_ch;
|
unsigned char this_ch;
|
||||||
|
@ -350,20 +370,26 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
|
||||||
Py_ssize_t ascii_len, bin_len;
|
Py_ssize_t ascii_len, bin_len;
|
||||||
int quad_pos = 0;
|
int quad_pos = 0;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "t#:a2b_base64", &ascii_data, &ascii_len) )
|
if ( !PyArg_ParseTuple(args, "s*:a2b_base64", &pascii) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
ascii_data = pascii.buf;
|
||||||
|
ascii_len = pascii.len;
|
||||||
|
|
||||||
assert(ascii_len >= 0);
|
assert(ascii_len >= 0);
|
||||||
|
|
||||||
if (ascii_len > PY_SSIZE_T_MAX - 3)
|
if (ascii_len > PY_SSIZE_T_MAX - 3) {
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
|
}
|
||||||
|
|
||||||
bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
|
bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
|
||||||
|
|
||||||
/* Allocate the buffer */
|
/* Allocate the buffer */
|
||||||
if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
|
if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL ) {
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
return NULL;
|
return NULL;
|
||||||
bin_data = (unsigned char *)PyString_AsString(rv);
|
}
|
||||||
|
bin_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
bin_len = 0;
|
bin_len = 0;
|
||||||
|
|
||||||
for( ; ascii_len > 0; ascii_len--, ascii_data++) {
|
for( ; ascii_len > 0; ascii_len--, ascii_data++) {
|
||||||
|
@ -415,6 +441,7 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leftbits != 0) {
|
if (leftbits != 0) {
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
PyErr_SetString(Error, "Incorrect padding");
|
PyErr_SetString(Error, "Incorrect padding");
|
||||||
Py_DECREF(rv);
|
Py_DECREF(rv);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -424,12 +451,17 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
|
||||||
** (because the input was all invalid) return the shared empty
|
** (because the input was all invalid) return the shared empty
|
||||||
** string instead; _PyString_Resize() won't do this for us.
|
** string instead; _PyString_Resize() won't do this for us.
|
||||||
*/
|
*/
|
||||||
if (bin_len > 0)
|
if (bin_len > 0) {
|
||||||
_PyString_Resize(&rv, bin_len);
|
if (_PyString_Resize(&rv, bin_len) < 0) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
rv = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
Py_DECREF(rv);
|
Py_DECREF(rv);
|
||||||
rv = PyString_FromString("");
|
rv = PyString_FromStringAndSize("", 0);
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&pascii);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -438,6 +470,7 @@ PyDoc_STRVAR(doc_b2a_base64, "(bin) -> ascii. Base64-code line of data");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_b2a_base64(PyObject *self, PyObject *args)
|
binascii_b2a_base64(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pbuf;
|
||||||
unsigned char *ascii_data, *bin_data;
|
unsigned char *ascii_data, *bin_data;
|
||||||
int leftbits = 0;
|
int leftbits = 0;
|
||||||
unsigned char this_ch;
|
unsigned char this_ch;
|
||||||
|
@ -445,22 +478,27 @@ binascii_b2a_base64(PyObject *self, PyObject *args)
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
Py_ssize_t bin_len;
|
Py_ssize_t bin_len;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#:b2a_base64", &bin_data, &bin_len) )
|
if ( !PyArg_ParseTuple(args, "s*:b2a_base64", &pbuf) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
bin_data = pbuf.buf;
|
||||||
|
bin_len = pbuf.len;
|
||||||
|
|
||||||
assert(bin_len >= 0);
|
assert(bin_len >= 0);
|
||||||
|
|
||||||
if ( bin_len > BASE64_MAXBIN ) {
|
if ( bin_len > BASE64_MAXBIN ) {
|
||||||
PyErr_SetString(Error, "Too much data for base64 line");
|
PyErr_SetString(Error, "Too much data for base64 line");
|
||||||
|
PyBuffer_Release(&pbuf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We're lazy and allocate too much (fixed up later).
|
/* We're lazy and allocate too much (fixed up later).
|
||||||
"+3" leaves room for up to two pad characters and a trailing
|
"+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). */
|
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=PyString_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) {
|
||||||
|
PyBuffer_Release(&pbuf);
|
||||||
return NULL;
|
return NULL;
|
||||||
ascii_data = (unsigned char *)PyString_AsString(rv);
|
}
|
||||||
|
ascii_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
|
|
||||||
for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
|
for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
|
||||||
/* Shift the data into our buffer */
|
/* Shift the data into our buffer */
|
||||||
|
@ -484,8 +522,13 @@ binascii_b2a_base64(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
*ascii_data++ = '\n'; /* Append a courtesy newline */
|
*ascii_data++ = '\n'; /* Append a courtesy newline */
|
||||||
|
|
||||||
_PyString_Resize(&rv, (ascii_data -
|
if (_PyString_Resize(&rv,
|
||||||
(unsigned char *)PyString_AsString(rv)));
|
(ascii_data -
|
||||||
|
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
rv = NULL;
|
||||||
|
}
|
||||||
|
PyBuffer_Release(&pbuf);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -515,7 +558,7 @@ binascii_a2b_hqx(PyObject *self, PyObject *args)
|
||||||
would preclude subsequent resizing. */
|
would preclude subsequent resizing. */
|
||||||
if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL )
|
if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL )
|
||||||
return NULL;
|
return NULL;
|
||||||
bin_data = (unsigned char *)PyString_AsString(rv);
|
bin_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
|
|
||||||
for( ; len > 0 ; len--, ascii_data++ ) {
|
for( ; len > 0 ; len--, ascii_data++ ) {
|
||||||
/* Get the byte and look it up */
|
/* Get the byte and look it up */
|
||||||
|
@ -549,8 +592,12 @@ binascii_a2b_hqx(PyObject *self, PyObject *args)
|
||||||
Py_DECREF(rv);
|
Py_DECREF(rv);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
_PyString_Resize(
|
if (_PyString_Resize(&rv,
|
||||||
&rv, (bin_data - (unsigned char *)PyString_AsString(rv)));
|
(bin_data -
|
||||||
|
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
rv = NULL;
|
||||||
|
}
|
||||||
if (rv) {
|
if (rv) {
|
||||||
PyObject *rrv = Py_BuildValue("Oi", rv, done);
|
PyObject *rrv = Py_BuildValue("Oi", rv, done);
|
||||||
Py_DECREF(rv);
|
Py_DECREF(rv);
|
||||||
|
@ -565,23 +612,30 @@ PyDoc_STRVAR(doc_rlecode_hqx, "Binhex RLE-code binary data");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_rlecode_hqx(PyObject *self, PyObject *args)
|
binascii_rlecode_hqx(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pbuf;
|
||||||
unsigned char *in_data, *out_data;
|
unsigned char *in_data, *out_data;
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
unsigned char ch;
|
unsigned char ch;
|
||||||
Py_ssize_t in, inend, len;
|
Py_ssize_t in, inend, len;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#:rlecode_hqx", &in_data, &len) )
|
if ( !PyArg_ParseTuple(args, "s*:rlecode_hqx", &pbuf) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
in_data = pbuf.buf;
|
||||||
|
len = pbuf.len;
|
||||||
|
|
||||||
assert(len >= 0);
|
assert(len >= 0);
|
||||||
|
|
||||||
if (len > PY_SSIZE_T_MAX / 2 - 2)
|
if (len > PY_SSIZE_T_MAX / 2 - 2) {
|
||||||
|
PyBuffer_Release(&pbuf);
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
|
}
|
||||||
|
|
||||||
/* Worst case: output is twice as big as input (fixed later) */
|
/* Worst case: output is twice as big as input (fixed later) */
|
||||||
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
|
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) {
|
||||||
|
PyBuffer_Release(&pbuf);
|
||||||
return NULL;
|
return NULL;
|
||||||
out_data = (unsigned char *)PyString_AsString(rv);
|
}
|
||||||
|
out_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
|
|
||||||
for( in=0; in<len; in++) {
|
for( in=0; in<len; in++) {
|
||||||
ch = in_data[in];
|
ch = in_data[in];
|
||||||
|
@ -607,8 +661,13 @@ binascii_rlecode_hqx(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_PyString_Resize(&rv, (out_data -
|
if (_PyString_Resize(&rv,
|
||||||
(unsigned char *)PyString_AsString(rv)));
|
(out_data -
|
||||||
|
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
rv = NULL;
|
||||||
|
}
|
||||||
|
PyBuffer_Release(&pbuf);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -617,6 +676,7 @@ PyDoc_STRVAR(doc_b2a_hqx, "Encode .hqx data");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_b2a_hqx(PyObject *self, PyObject *args)
|
binascii_b2a_hqx(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pbin;
|
||||||
unsigned char *ascii_data, *bin_data;
|
unsigned char *ascii_data, *bin_data;
|
||||||
int leftbits = 0;
|
int leftbits = 0;
|
||||||
unsigned char this_ch;
|
unsigned char this_ch;
|
||||||
|
@ -624,18 +684,24 @@ binascii_b2a_hqx(PyObject *self, PyObject *args)
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#:b2a_hqx", &bin_data, &len) )
|
if ( !PyArg_ParseTuple(args, "s*:b2a_hqx", &pbin) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
bin_data = pbin.buf;
|
||||||
|
len = pbin.len;
|
||||||
|
|
||||||
assert(len >= 0);
|
assert(len >= 0);
|
||||||
|
|
||||||
if (len > PY_SSIZE_T_MAX / 2 - 2)
|
if (len > PY_SSIZE_T_MAX / 2 - 2) {
|
||||||
|
PyBuffer_Release(&pbin);
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
|
}
|
||||||
|
|
||||||
/* Allocate a buffer that is at least large enough */
|
/* Allocate a buffer that is at least large enough */
|
||||||
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
|
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) {
|
||||||
|
PyBuffer_Release(&pbin);
|
||||||
return NULL;
|
return NULL;
|
||||||
ascii_data = (unsigned char *)PyString_AsString(rv);
|
}
|
||||||
|
ascii_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
|
|
||||||
for( ; len > 0 ; len--, bin_data++ ) {
|
for( ; len > 0 ; len--, bin_data++ ) {
|
||||||
/* Shift into our buffer, and output any 6bits ready */
|
/* Shift into our buffer, and output any 6bits ready */
|
||||||
|
@ -652,8 +718,13 @@ binascii_b2a_hqx(PyObject *self, PyObject *args)
|
||||||
leftchar <<= (6-leftbits);
|
leftchar <<= (6-leftbits);
|
||||||
*ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
|
*ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
|
||||||
}
|
}
|
||||||
_PyString_Resize(&rv, (ascii_data -
|
if (_PyString_Resize(&rv,
|
||||||
(unsigned char *)PyString_AsString(rv)));
|
(ascii_data -
|
||||||
|
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
rv = NULL;
|
||||||
|
}
|
||||||
|
PyBuffer_Release(&pbin);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -662,28 +733,37 @@ PyDoc_STRVAR(doc_rledecode_hqx, "Decode hexbin RLE-coded string");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pin;
|
||||||
unsigned char *in_data, *out_data;
|
unsigned char *in_data, *out_data;
|
||||||
unsigned char in_byte, in_repeat;
|
unsigned char in_byte, in_repeat;
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
Py_ssize_t in_len, out_len, out_len_left;
|
Py_ssize_t in_len, out_len, out_len_left;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#:rledecode_hqx", &in_data, &in_len) )
|
if ( !PyArg_ParseTuple(args, "s*:rledecode_hqx", &pin) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
in_data = pin.buf;
|
||||||
|
in_len = pin.len;
|
||||||
|
|
||||||
assert(in_len >= 0);
|
assert(in_len >= 0);
|
||||||
|
|
||||||
/* Empty string is a special case */
|
/* Empty string is a special case */
|
||||||
if ( in_len == 0 )
|
if ( in_len == 0 ) {
|
||||||
return PyString_FromString("");
|
PyBuffer_Release(&pin);
|
||||||
else if (in_len > PY_SSIZE_T_MAX / 2)
|
return PyString_FromStringAndSize("", 0);
|
||||||
|
}
|
||||||
|
else if (in_len > PY_SSIZE_T_MAX / 2) {
|
||||||
|
PyBuffer_Release(&pin);
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
|
}
|
||||||
|
|
||||||
/* Allocate a buffer of reasonable size. Resized when needed */
|
/* Allocate a buffer of reasonable size. Resized when needed */
|
||||||
out_len = in_len*2;
|
out_len = in_len*2;
|
||||||
if ( (rv=PyString_FromStringAndSize(NULL, out_len)) == NULL )
|
if ( (rv=PyString_FromStringAndSize(NULL, out_len)) == NULL ) {
|
||||||
|
PyBuffer_Release(&pin);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
out_len_left = out_len;
|
out_len_left = out_len;
|
||||||
out_data = (unsigned char *)PyString_AsString(rv);
|
out_data = (unsigned char *)PyString_AS_STRING(rv);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** We need two macros here to get/put bytes and handle
|
** We need two macros here to get/put bytes and handle
|
||||||
|
@ -694,6 +774,7 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
||||||
if ( --in_len < 0 ) { \
|
if ( --in_len < 0 ) { \
|
||||||
PyErr_SetString(Incomplete, ""); \
|
PyErr_SetString(Incomplete, ""); \
|
||||||
Py_DECREF(rv); \
|
Py_DECREF(rv); \
|
||||||
|
PyBuffer_Release(&pin); \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
} \
|
} \
|
||||||
b = *in_data++; \
|
b = *in_data++; \
|
||||||
|
@ -703,9 +784,9 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
||||||
do { \
|
do { \
|
||||||
if ( --out_len_left < 0 ) { \
|
if ( --out_len_left < 0 ) { \
|
||||||
if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
|
if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
|
||||||
_PyString_Resize(&rv, 2*out_len); \
|
if (_PyString_Resize(&rv, 2*out_len) < 0) \
|
||||||
if ( rv == NULL ) return NULL; \
|
{ Py_DECREF(rv); PyBuffer_Release(&pin); return NULL; } \
|
||||||
out_data = (unsigned char *)PyString_AsString(rv) \
|
out_data = (unsigned char *)PyString_AS_STRING(rv) \
|
||||||
+ out_len; \
|
+ out_len; \
|
||||||
out_len_left = out_len-1; \
|
out_len_left = out_len-1; \
|
||||||
out_len = out_len * 2; \
|
out_len = out_len * 2; \
|
||||||
|
@ -726,6 +807,7 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
||||||
** of the string only). This is a programmer error.
|
** of the string only). This is a programmer error.
|
||||||
*/
|
*/
|
||||||
PyErr_SetString(Error, "Orphaned RLE code at start");
|
PyErr_SetString(Error, "Orphaned RLE code at start");
|
||||||
|
PyBuffer_Release(&pin);
|
||||||
Py_DECREF(rv);
|
Py_DECREF(rv);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -753,8 +835,13 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
|
||||||
OUTBYTE(in_byte);
|
OUTBYTE(in_byte);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_PyString_Resize(&rv, (out_data -
|
if (_PyString_Resize(&rv,
|
||||||
(unsigned char *)PyString_AsString(rv)));
|
(out_data -
|
||||||
|
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
rv = NULL;
|
||||||
|
}
|
||||||
|
PyBuffer_Release(&pin);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -764,17 +851,21 @@ PyDoc_STRVAR(doc_crc_hqx,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_crc_hqx(PyObject *self, PyObject *args)
|
binascii_crc_hqx(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer pin;
|
||||||
unsigned char *bin_data;
|
unsigned char *bin_data;
|
||||||
unsigned int crc;
|
unsigned int crc;
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#i:crc_hqx", &bin_data, &len, &crc) )
|
if ( !PyArg_ParseTuple(args, "s*i:crc_hqx", &pin, &crc) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
bin_data = pin.buf;
|
||||||
|
len = pin.len;
|
||||||
|
|
||||||
while(len-- > 0) {
|
while(len-- > 0) {
|
||||||
crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++];
|
crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyBuffer_Release(&pin);
|
||||||
return Py_BuildValue("i", crc);
|
return Py_BuildValue("i", crc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -787,17 +878,21 @@ static PyObject *
|
||||||
binascii_crc32(PyObject *self, PyObject *args)
|
binascii_crc32(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
|
unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
|
||||||
|
Py_buffer pbuf;
|
||||||
Byte *buf;
|
Byte *buf;
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
int signed_val;
|
int signed_val;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
|
if (!PyArg_ParseTuple(args, "s*|I:crc32", &pbuf, &crc32val))
|
||||||
return NULL;
|
return NULL;
|
||||||
/* In Python 2.x we return a signed integer regardless of native platform
|
/* In Python 2.x we return a signed integer regardless of native platform
|
||||||
* long size (the 32bit unsigned long is treated as 32-bit signed and sign
|
* long size (the 32bit unsigned long is treated as 32-bit signed and sign
|
||||||
* extended into a 64-bit long inside the integer object). 3.0 does the
|
* extended into a 64-bit long inside the integer object). 3.0 does the
|
||||||
* right thing and returns unsigned. http://bugs.python.org/issue1202 */
|
* right thing and returns unsigned. http://bugs.python.org/issue1202 */
|
||||||
|
buf = (Byte*)pbuf.buf;
|
||||||
|
len = pbuf.len;
|
||||||
signed_val = crc32(crc32val, buf, len);
|
signed_val = crc32(crc32val, buf, len);
|
||||||
|
PyBuffer_Release(&pbuf);
|
||||||
return PyInt_FromLong(signed_val);
|
return PyInt_FromLong(signed_val);
|
||||||
}
|
}
|
||||||
#else /* USE_ZLIB_CRC32 */
|
#else /* USE_ZLIB_CRC32 */
|
||||||
|
@ -922,13 +1017,16 @@ static unsigned int crc_32_tab[256] = {
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_crc32(PyObject *self, PyObject *args)
|
binascii_crc32(PyObject *self, PyObject *args)
|
||||||
{ /* By Jim Ahlstrom; All rights transferred to CNRI */
|
{ /* By Jim Ahlstrom; All rights transferred to CNRI */
|
||||||
|
Py_buffer pbin;
|
||||||
unsigned char *bin_data;
|
unsigned char *bin_data;
|
||||||
unsigned int crc = 0U; /* initial value of CRC */
|
unsigned int crc = 0U; /* initial value of CRC */
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#|I:crc32", &bin_data, &len, &crc) )
|
if ( !PyArg_ParseTuple(args, "s*|I:crc32", &pbin, &crc) )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
bin_data = pbin.buf;
|
||||||
|
len = pbin.len;
|
||||||
|
|
||||||
crc = ~ crc;
|
crc = ~ crc;
|
||||||
while (len-- > 0)
|
while (len-- > 0)
|
||||||
|
@ -936,6 +1034,7 @@ binascii_crc32(PyObject *self, PyObject *args)
|
||||||
/* Note: (crc >> 8) MUST zero fill on left */
|
/* Note: (crc >> 8) MUST zero fill on left */
|
||||||
|
|
||||||
result = (int)(crc ^ 0xFFFFFFFFU);
|
result = (int)(crc ^ 0xFFFFFFFFU);
|
||||||
|
PyBuffer_Release(&pbin);
|
||||||
return PyInt_FromLong(result);
|
return PyInt_FromLong(result);
|
||||||
}
|
}
|
||||||
#endif /* USE_ZLIB_CRC32 */
|
#endif /* USE_ZLIB_CRC32 */
|
||||||
|
@ -944,25 +1043,30 @@ binascii_crc32(PyObject *self, PyObject *args)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_hexlify(PyObject *self, PyObject *args)
|
binascii_hexlify(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer parg;
|
||||||
char* argbuf;
|
char* argbuf;
|
||||||
Py_ssize_t arglen;
|
Py_ssize_t arglen;
|
||||||
PyObject *retval;
|
PyObject *retval;
|
||||||
char* retbuf;
|
char* retbuf;
|
||||||
Py_ssize_t i, j;
|
Py_ssize_t i, j;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen))
|
if (!PyArg_ParseTuple(args, "s*:b2a_hex", &parg))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
argbuf = parg.buf;
|
||||||
|
arglen = parg.len;
|
||||||
|
|
||||||
assert(arglen >= 0);
|
assert(arglen >= 0);
|
||||||
if (arglen > PY_SSIZE_T_MAX / 2)
|
if (arglen > PY_SSIZE_T_MAX / 2) {
|
||||||
|
PyBuffer_Release(&parg);
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
|
}
|
||||||
|
|
||||||
retval = PyString_FromStringAndSize(NULL, arglen*2);
|
retval = PyString_FromStringAndSize(NULL, arglen*2);
|
||||||
if (!retval)
|
if (!retval) {
|
||||||
|
PyBuffer_Release(&parg);
|
||||||
return NULL;
|
return NULL;
|
||||||
retbuf = PyString_AsString(retval);
|
}
|
||||||
if (!retbuf)
|
retbuf = PyString_AS_STRING(retval);
|
||||||
goto finally;
|
|
||||||
|
|
||||||
/* make hex version of string, taken from shamodule.c */
|
/* make hex version of string, taken from shamodule.c */
|
||||||
for (i=j=0; i < arglen; i++) {
|
for (i=j=0; i < arglen; i++) {
|
||||||
|
@ -974,11 +1078,8 @@ binascii_hexlify(PyObject *self, PyObject *args)
|
||||||
c = (c>9) ? c+'a'-10 : c + '0';
|
c = (c>9) ? c+'a'-10 : c + '0';
|
||||||
retbuf[j++] = c;
|
retbuf[j++] = c;
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&parg);
|
||||||
return retval;
|
return retval;
|
||||||
|
|
||||||
finally:
|
|
||||||
Py_DECREF(retval);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(doc_hexlify,
|
PyDoc_STRVAR(doc_hexlify,
|
||||||
|
@ -1005,14 +1106,17 @@ to_int(int c)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
binascii_unhexlify(PyObject *self, PyObject *args)
|
binascii_unhexlify(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_buffer parg;
|
||||||
char* argbuf;
|
char* argbuf;
|
||||||
Py_ssize_t arglen;
|
Py_ssize_t arglen;
|
||||||
PyObject *retval;
|
PyObject *retval;
|
||||||
char* retbuf;
|
char* retbuf;
|
||||||
Py_ssize_t i, j;
|
Py_ssize_t i, j;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen))
|
if (!PyArg_ParseTuple(args, "s*:a2b_hex", &parg))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
argbuf = parg.buf;
|
||||||
|
arglen = parg.len;
|
||||||
|
|
||||||
assert(arglen >= 0);
|
assert(arglen >= 0);
|
||||||
|
|
||||||
|
@ -1021,16 +1125,17 @@ binascii_unhexlify(PyObject *self, PyObject *args)
|
||||||
* raise an exception.
|
* raise an exception.
|
||||||
*/
|
*/
|
||||||
if (arglen % 2) {
|
if (arglen % 2) {
|
||||||
|
PyBuffer_Release(&parg);
|
||||||
PyErr_SetString(PyExc_TypeError, "Odd-length string");
|
PyErr_SetString(PyExc_TypeError, "Odd-length string");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = PyString_FromStringAndSize(NULL, (arglen/2));
|
retval = PyString_FromStringAndSize(NULL, (arglen/2));
|
||||||
if (!retval)
|
if (!retval) {
|
||||||
|
PyBuffer_Release(&parg);
|
||||||
return NULL;
|
return NULL;
|
||||||
retbuf = PyString_AsString(retval);
|
}
|
||||||
if (!retbuf)
|
retbuf = PyString_AS_STRING(retval);
|
||||||
goto finally;
|
|
||||||
|
|
||||||
for (i=j=0; i < arglen; i += 2) {
|
for (i=j=0; i < arglen; i += 2) {
|
||||||
int top = to_int(Py_CHARMASK(argbuf[i]));
|
int top = to_int(Py_CHARMASK(argbuf[i]));
|
||||||
|
@ -1042,9 +1147,11 @@ binascii_unhexlify(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
retbuf[j++] = (top << 4) + bot;
|
retbuf[j++] = (top << 4) + bot;
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&parg);
|
||||||
return retval;
|
return retval;
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
PyBuffer_Release(&parg);
|
||||||
Py_DECREF(retval);
|
Py_DECREF(retval);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1077,15 +1184,18 @@ binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
Py_ssize_t in, out;
|
Py_ssize_t in, out;
|
||||||
char ch;
|
char ch;
|
||||||
|
Py_buffer pdata;
|
||||||
unsigned char *data, *odata;
|
unsigned char *data, *odata;
|
||||||
Py_ssize_t datalen = 0;
|
Py_ssize_t datalen = 0;
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
static char *kwlist[] = {"data", "header", NULL};
|
static char *kwlist[] = {"data", "header", NULL};
|
||||||
int header = 0;
|
int header = 0;
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i", kwlist, &data,
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|i", kwlist, &pdata,
|
||||||
&datalen, &header))
|
&header))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
data = pdata.buf;
|
||||||
|
datalen = pdata.len;
|
||||||
|
|
||||||
/* We allocate the output same size as input, this is overkill.
|
/* We allocate the output same size as input, this is overkill.
|
||||||
* The previous implementation used calloc() so we'll zero out the
|
* The previous implementation used calloc() so we'll zero out the
|
||||||
|
@ -1093,6 +1203,7 @@ binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
*/
|
*/
|
||||||
odata = (unsigned char *) PyMem_Malloc(datalen);
|
odata = (unsigned char *) PyMem_Malloc(datalen);
|
||||||
if (odata == NULL) {
|
if (odata == NULL) {
|
||||||
|
PyBuffer_Release(&pdata);
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1143,9 +1254,11 @@ binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
|
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
|
||||||
|
PyBuffer_Release(&pdata);
|
||||||
PyMem_Free(odata);
|
PyMem_Free(odata);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&pdata);
|
||||||
PyMem_Free(odata);
|
PyMem_Free(odata);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
@ -1176,6 +1289,7 @@ static PyObject*
|
||||||
binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
|
binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
Py_ssize_t in, out;
|
Py_ssize_t in, out;
|
||||||
|
Py_buffer pdata;
|
||||||
unsigned char *data, *odata;
|
unsigned char *data, *odata;
|
||||||
Py_ssize_t datalen = 0, odatalen = 0;
|
Py_ssize_t datalen = 0, odatalen = 0;
|
||||||
PyObject *rv;
|
PyObject *rv;
|
||||||
|
@ -1189,9 +1303,11 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
int crlf = 0;
|
int crlf = 0;
|
||||||
unsigned char *p;
|
unsigned char *p;
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|iii", kwlist, &data,
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|iii", kwlist, &pdata,
|
||||||
&datalen, "etabs, &istext, &header))
|
"etabs, &istext, &header))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
data = pdata.buf;
|
||||||
|
datalen = pdata.len;
|
||||||
|
|
||||||
/* See if this string is using CRLF line ends */
|
/* See if this string is using CRLF line ends */
|
||||||
/* XXX: this function has the side effect of converting all of
|
/* XXX: this function has the side effect of converting all of
|
||||||
|
@ -1269,6 +1385,7 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
*/
|
*/
|
||||||
odata = (unsigned char *) PyMem_Malloc(odatalen);
|
odata = (unsigned char *) PyMem_Malloc(odatalen);
|
||||||
if (odata == NULL) {
|
if (odata == NULL) {
|
||||||
|
PyBuffer_Release(&pdata);
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1343,9 +1460,11 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
|
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
|
||||||
|
PyBuffer_Release(&pdata);
|
||||||
PyMem_Free(odata);
|
PyMem_Free(odata);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&pdata);
|
||||||
PyMem_Free(odata);
|
PyMem_Free(odata);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue