|
Lines 194-199
binascii_a2b_uu(PyObject *self, PyObject
|
Link Here
|
|---|
|
if ( !PyArg_ParseTuple(args, "t#:a2b_uu", &ascii_data, &ascii_len) ) | if ( !PyArg_ParseTuple(args, "t#:a2b_uu", &ascii_data, &ascii_len) ) |
return NULL; | return NULL; |
| |
|
assert(ascii_len >= 0); |
|
|
/* First byte: binary data length (in bytes) */ | /* First byte: binary data length (in bytes) */ |
bin_len = (*ascii_data++ - ' ') & 077; | bin_len = (*ascii_data++ - ' ') & 077; |
ascii_len--; | ascii_len--; |
|
Lines 347-352
binascii_a2b_base64(PyObject *self, PyOb
|
Link Here
|
|---|
|
if ( !PyArg_ParseTuple(args, "t#:a2b_base64", &ascii_data, &ascii_len) ) | if ( !PyArg_ParseTuple(args, "t#:a2b_base64", &ascii_data, &ascii_len) ) |
return NULL; | return NULL; |
| |
|
assert(ascii_len >= 0); |
|
|
|
if (ascii_len > INT_MAX - 3) |
|
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 */ |
|
Lines 436-441
binascii_b2a_base64(PyObject *self, PyOb
|
Link Here
|
|---|
|
| |
if ( !PyArg_ParseTuple(args, "s#:b2a_base64", &bin_data, &bin_len) ) | if ( !PyArg_ParseTuple(args, "s#:b2a_base64", &bin_data, &bin_len) ) |
return NULL; | return NULL; |
|
|
|
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"); |
return NULL; | return NULL; |
|
Lines 491-496
binascii_a2b_hqx(PyObject *self, PyObjec
|
Link Here
|
|---|
|
if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) ) | if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) ) |
return NULL; | return NULL; |
| |
|
assert(len >= 0); |
|
|
|
if (len > INT_MAX - 2) |
|
return PyErr_NoMemory(); |
|
|
/* Allocate a string that is too big (fixed later) | /* Allocate a string that is too big (fixed later) |
Add two to the initial length to prevent interning which | Add two to the initial length to prevent interning which |
would preclude subsequent resizing. */ | would preclude subsequent resizing. */ |
|
Lines 554-559
binascii_rlecode_hqx(PyObject *self, PyO
|
Link Here
|
|---|
|
if ( !PyArg_ParseTuple(args, "s#:rlecode_hqx", &in_data, &len) ) | if ( !PyArg_ParseTuple(args, "s#:rlecode_hqx", &in_data, &len) ) |
return NULL; | return NULL; |
| |
|
assert(len >= 0); |
|
|
|
if (len > INT_MAX / 2 - 2) |
|
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 ) |
return NULL; | return NULL; |
|
Lines 603-608
binascii_b2a_hqx(PyObject *self, PyObjec
|
Link Here
|
|---|
|
if ( !PyArg_ParseTuple(args, "s#:b2a_hqx", &bin_data, &len) ) | if ( !PyArg_ParseTuple(args, "s#:b2a_hqx", &bin_data, &len) ) |
return NULL; | return NULL; |
| |
|
assert(len >= 0); |
|
|
|
if (len > INT_MAX / 2 - 2) |
|
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 ) |
return NULL; | return NULL; |
|
Lines 641-649
binascii_rledecode_hqx(PyObject *self, P
|
Link Here
|
|---|
|
if ( !PyArg_ParseTuple(args, "s#:rledecode_hqx", &in_data, &in_len) ) | if ( !PyArg_ParseTuple(args, "s#:rledecode_hqx", &in_data, &in_len) ) |
return NULL; | return NULL; |
| |
|
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 Py_BuildValue("s", ""); | return Py_BuildValue("s", ""); |
|
else if (in_len > INT_MAX / 2) |
|
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; |
|
Lines 669-674
binascii_rledecode_hqx(PyObject *self, P
|
Link Here
|
|---|
|
#define OUTBYTE(b) \ | #define OUTBYTE(b) \ |
do { \ | do { \ |
if ( --out_len_left < 0 ) { \ | if ( --out_len_left < 0 ) { \ |
|
if ( out_len > INT_MAX / 2) return PyErr_NoMemory(); \ |
_PyString_Resize(&rv, 2*out_len); \ | _PyString_Resize(&rv, 2*out_len); \ |
if ( rv == NULL ) return NULL; \ | if ( rv == NULL ) return NULL; \ |
out_data = (unsigned char *)PyString_AsString(rv) \ | out_data = (unsigned char *)PyString_AsString(rv) \ |
|
Lines 737-743
binascii_crc_hqx(PyObject *self, PyObjec
|
Link Here
|
|---|
|
if ( !PyArg_ParseTuple(args, "s#i:crc_hqx", &bin_data, &len, &crc) ) | if ( !PyArg_ParseTuple(args, "s#i:crc_hqx", &bin_data, &len, &crc) ) |
return NULL; | return NULL; |
| |
while(len--) { |
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++]; |
} | } |
| |
|
Lines 881-887
binascii_crc32(PyObject *self, PyObject
|
Link Here
|
|---|
|
/* only want the trailing 32 bits */ | /* only want the trailing 32 bits */ |
crc &= 0xFFFFFFFFUL; | crc &= 0xFFFFFFFFUL; |
#endif | #endif |
while (len--) |
while (len-- > 0) |
crc = crc_32_tab[(crc ^ *bin_data++) & 0xffUL] ^ (crc >> 8); | crc = crc_32_tab[(crc ^ *bin_data++) & 0xffUL] ^ (crc >> 8); |
/* Note: (crc >> 8) MUST zero fill on left */ | /* Note: (crc >> 8) MUST zero fill on left */ |
| |
|
Lines 911-916
binascii_hexlify(PyObject *self, PyObjec
|
Link Here
|
|---|
|
if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen)) | if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen)) |
return NULL; | return NULL; |
| |
|
assert(arglen >= 0); |
|
if (arglen > INT_MAX / 2) |
|
return PyErr_NoMemory(); |
|
|
retval = PyString_FromStringAndSize(NULL, arglen*2); | retval = PyString_FromStringAndSize(NULL, arglen*2); |
if (!retval) | if (!retval) |
return NULL; | return NULL; |
|
Lines 968-973
binascii_unhexlify(PyObject *self, PyObj
|
Link Here
|
|---|
|
if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen)) | if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen)) |
return NULL; | return NULL; |
| |
|
assert(arglen >= 0); |
|
|
/* XXX What should we do about strings with an odd length? Should | /* XXX What should we do about strings with an odd length? Should |
* we add an implicit leading zero, or a trailing zero? For now, | * we add an implicit leading zero, or a trailing zero? For now, |
* raise an exception. | * raise an exception. |