Lines 194-199
binascii_a2b_uu(PyObject *self, PyObject
Link Here
|
194 |
if ( !PyArg_ParseTuple(args, "t#:a2b_uu", &ascii_data, &ascii_len) ) |
194 |
if ( !PyArg_ParseTuple(args, "t#:a2b_uu", &ascii_data, &ascii_len) ) |
195 |
return NULL; |
195 |
return NULL; |
196 |
|
196 |
|
|
|
197 |
assert(ascii_len >= 0); |
198 |
|
197 |
/* First byte: binary data length (in bytes) */ |
199 |
/* First byte: binary data length (in bytes) */ |
198 |
bin_len = (*ascii_data++ - ' ') & 077; |
200 |
bin_len = (*ascii_data++ - ' ') & 077; |
199 |
ascii_len--; |
201 |
ascii_len--; |
Lines 347-352
binascii_a2b_base64(PyObject *self, PyOb
Link Here
|
347 |
if ( !PyArg_ParseTuple(args, "t#:a2b_base64", &ascii_data, &ascii_len) ) |
349 |
if ( !PyArg_ParseTuple(args, "t#:a2b_base64", &ascii_data, &ascii_len) ) |
348 |
return NULL; |
350 |
return NULL; |
349 |
|
351 |
|
|
|
352 |
assert(ascii_len >= 0); |
353 |
|
354 |
if (ascii_len > INT_MAX - 3) |
355 |
return PyErr_NoMemory(); |
356 |
|
350 |
bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */ |
357 |
bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */ |
351 |
|
358 |
|
352 |
/* Allocate the buffer */ |
359 |
/* Allocate the buffer */ |
Lines 436-441
binascii_b2a_base64(PyObject *self, PyOb
Link Here
|
436 |
|
443 |
|
437 |
if ( !PyArg_ParseTuple(args, "s#:b2a_base64", &bin_data, &bin_len) ) |
444 |
if ( !PyArg_ParseTuple(args, "s#:b2a_base64", &bin_data, &bin_len) ) |
438 |
return NULL; |
445 |
return NULL; |
|
|
446 |
|
447 |
assert(bin_len >= 0); |
448 |
|
439 |
if ( bin_len > BASE64_MAXBIN ) { |
449 |
if ( bin_len > BASE64_MAXBIN ) { |
440 |
PyErr_SetString(Error, "Too much data for base64 line"); |
450 |
PyErr_SetString(Error, "Too much data for base64 line"); |
441 |
return NULL; |
451 |
return NULL; |
Lines 491-496
binascii_a2b_hqx(PyObject *self, PyObjec
Link Here
|
491 |
if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) ) |
501 |
if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) ) |
492 |
return NULL; |
502 |
return NULL; |
493 |
|
503 |
|
|
|
504 |
assert(len >= 0); |
505 |
|
506 |
if (len > INT_MAX - 2) |
507 |
return PyErr_NoMemory(); |
508 |
|
494 |
/* Allocate a string that is too big (fixed later) |
509 |
/* Allocate a string that is too big (fixed later) |
495 |
Add two to the initial length to prevent interning which |
510 |
Add two to the initial length to prevent interning which |
496 |
would preclude subsequent resizing. */ |
511 |
would preclude subsequent resizing. */ |
Lines 554-559
binascii_rlecode_hqx(PyObject *self, PyO
Link Here
|
554 |
if ( !PyArg_ParseTuple(args, "s#:rlecode_hqx", &in_data, &len) ) |
569 |
if ( !PyArg_ParseTuple(args, "s#:rlecode_hqx", &in_data, &len) ) |
555 |
return NULL; |
570 |
return NULL; |
556 |
|
571 |
|
|
|
572 |
assert(len >= 0); |
573 |
|
574 |
if (len > INT_MAX / 2 - 2) |
575 |
return PyErr_NoMemory(); |
576 |
|
557 |
/* Worst case: output is twice as big as input (fixed later) */ |
577 |
/* Worst case: output is twice as big as input (fixed later) */ |
558 |
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) |
578 |
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) |
559 |
return NULL; |
579 |
return NULL; |
Lines 603-608
binascii_b2a_hqx(PyObject *self, PyObjec
Link Here
|
603 |
if ( !PyArg_ParseTuple(args, "s#:b2a_hqx", &bin_data, &len) ) |
623 |
if ( !PyArg_ParseTuple(args, "s#:b2a_hqx", &bin_data, &len) ) |
604 |
return NULL; |
624 |
return NULL; |
605 |
|
625 |
|
|
|
626 |
assert(len >= 0); |
627 |
|
628 |
if (len > INT_MAX / 2 - 2) |
629 |
return PyErr_NoMemory(); |
630 |
|
606 |
/* Allocate a buffer that is at least large enough */ |
631 |
/* Allocate a buffer that is at least large enough */ |
607 |
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) |
632 |
if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL ) |
608 |
return NULL; |
633 |
return NULL; |
Lines 641-649
binascii_rledecode_hqx(PyObject *self, P
Link Here
|
641 |
if ( !PyArg_ParseTuple(args, "s#:rledecode_hqx", &in_data, &in_len) ) |
666 |
if ( !PyArg_ParseTuple(args, "s#:rledecode_hqx", &in_data, &in_len) ) |
642 |
return NULL; |
667 |
return NULL; |
643 |
|
668 |
|
|
|
669 |
assert(in_len >= 0); |
670 |
|
644 |
/* Empty string is a special case */ |
671 |
/* Empty string is a special case */ |
645 |
if ( in_len == 0 ) |
672 |
if ( in_len == 0 ) |
646 |
return Py_BuildValue("s", ""); |
673 |
return Py_BuildValue("s", ""); |
|
|
674 |
else if (in_len > INT_MAX / 2) |
675 |
return PyErr_NoMemory(); |
647 |
|
676 |
|
648 |
/* Allocate a buffer of reasonable size. Resized when needed */ |
677 |
/* Allocate a buffer of reasonable size. Resized when needed */ |
649 |
out_len = in_len*2; |
678 |
out_len = in_len*2; |
Lines 669-674
binascii_rledecode_hqx(PyObject *self, P
Link Here
|
669 |
#define OUTBYTE(b) \ |
698 |
#define OUTBYTE(b) \ |
670 |
do { \ |
699 |
do { \ |
671 |
if ( --out_len_left < 0 ) { \ |
700 |
if ( --out_len_left < 0 ) { \ |
|
|
701 |
if ( out_len > INT_MAX / 2) return PyErr_NoMemory(); \ |
672 |
_PyString_Resize(&rv, 2*out_len); \ |
702 |
_PyString_Resize(&rv, 2*out_len); \ |
673 |
if ( rv == NULL ) return NULL; \ |
703 |
if ( rv == NULL ) return NULL; \ |
674 |
out_data = (unsigned char *)PyString_AsString(rv) \ |
704 |
out_data = (unsigned char *)PyString_AsString(rv) \ |
Lines 737-743
binascii_crc_hqx(PyObject *self, PyObjec
Link Here
|
737 |
if ( !PyArg_ParseTuple(args, "s#i:crc_hqx", &bin_data, &len, &crc) ) |
767 |
if ( !PyArg_ParseTuple(args, "s#i:crc_hqx", &bin_data, &len, &crc) ) |
738 |
return NULL; |
768 |
return NULL; |
739 |
|
769 |
|
740 |
while(len--) { |
770 |
while(len-- > 0) { |
741 |
crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++]; |
771 |
crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++]; |
742 |
} |
772 |
} |
743 |
|
773 |
|
Lines 881-887
binascii_crc32(PyObject *self, PyObject
Link Here
|
881 |
/* only want the trailing 32 bits */ |
911 |
/* only want the trailing 32 bits */ |
882 |
crc &= 0xFFFFFFFFUL; |
912 |
crc &= 0xFFFFFFFFUL; |
883 |
#endif |
913 |
#endif |
884 |
while (len--) |
914 |
while (len-- > 0) |
885 |
crc = crc_32_tab[(crc ^ *bin_data++) & 0xffUL] ^ (crc >> 8); |
915 |
crc = crc_32_tab[(crc ^ *bin_data++) & 0xffUL] ^ (crc >> 8); |
886 |
/* Note: (crc >> 8) MUST zero fill on left */ |
916 |
/* Note: (crc >> 8) MUST zero fill on left */ |
887 |
|
917 |
|
Lines 911-916
binascii_hexlify(PyObject *self, PyObjec
Link Here
|
911 |
if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen)) |
941 |
if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen)) |
912 |
return NULL; |
942 |
return NULL; |
913 |
|
943 |
|
|
|
944 |
assert(arglen >= 0); |
945 |
if (arglen > INT_MAX / 2) |
946 |
return PyErr_NoMemory(); |
947 |
|
914 |
retval = PyString_FromStringAndSize(NULL, arglen*2); |
948 |
retval = PyString_FromStringAndSize(NULL, arglen*2); |
915 |
if (!retval) |
949 |
if (!retval) |
916 |
return NULL; |
950 |
return NULL; |
Lines 968-973
binascii_unhexlify(PyObject *self, PyObj
Link Here
|
968 |
if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen)) |
1002 |
if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen)) |
969 |
return NULL; |
1003 |
return NULL; |
970 |
|
1004 |
|
|
|
1005 |
assert(arglen >= 0); |
1006 |
|
971 |
/* XXX What should we do about strings with an odd length? Should |
1007 |
/* XXX What should we do about strings with an odd length? Should |
972 |
* we add an implicit leading zero, or a trailing zero? For now, |
1008 |
* we add an implicit leading zero, or a trailing zero? For now, |
973 |
* raise an exception. |
1009 |
* raise an exception. |