summaryrefslogtreecommitdiffstats
path: root/Modules/binascii.c
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2008-03-20 06:20:09 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2008-03-20 06:20:09 (GMT)
commit2727503bc9c7df0093d8920713fdb5ae4db65a98 (patch)
tree1865acbc48313832f649873366f9279808fb6567 /Modules/binascii.c
parent97797a9c3c69add56faefb8c4e5d029618f5bcb2 (diff)
downloadcpython-2727503bc9c7df0093d8920713fdb5ae4db65a98.zip
cpython-2727503bc9c7df0093d8920713fdb5ae4db65a98.tar.gz
cpython-2727503bc9c7df0093d8920713fdb5ae4db65a98.tar.bz2
crc32 always returns unsigned. cleanup the code a bit and revert r61648 with
the proper fix.
Diffstat (limited to 'Modules/binascii.c')
-rw-r--r--Modules/binascii.c28
1 files changed, 8 insertions, 20 deletions
diff --git a/Modules/binascii.c b/Modules/binascii.c
index 2b4b352..e09255c 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -898,33 +898,21 @@ static PyObject *
binascii_crc32(PyObject *self, PyObject *args)
{ /* By Jim Ahlstrom; All rights transferred to CNRI */
unsigned char *bin_data;
- unsigned long crc = 0UL; /* initial value of CRC */
+ unsigned int crc = 0; /* initial value of CRC */
Py_ssize_t len;
- long result;
+ unsigned int result;
- if ( !PyArg_ParseTuple(args, "s#|k:crc32", &bin_data, &len, &crc) )
+ if ( !PyArg_ParseTuple(args, "s#|I:crc32", &bin_data, &len, &crc) )
return NULL;
crc = ~ crc;
-#if SIZEOF_LONG > 4
- /* only want the trailing 32 bits */
- crc &= 0xFFFFFFFFUL;
-#endif
- while (len--)
- crc = crc_32_tab[(crc ^ *bin_data++) & 0xffUL] ^ (crc >> 8);
+ while (len--) {
+ crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
/* Note: (crc >> 8) MUST zero fill on left */
+ }
- result = (long)(crc ^ 0xFFFFFFFFUL);
-#if SIZEOF_LONG > 4
- /* Extend the sign bit. This is one way to ensure the result is the
- * same across platforms. The other way would be to return an
- * unbounded unsigned long, but the evidence suggests that lots of
- * code outside this treats the result as if it were a signed 4-byte
- * integer.
- */
- result |= -(result & (1L << 31));
-#endif
- return PyLong_FromLong(result);
+ result = (crc ^ 0xFFFFFFFF);
+ return PyLong_FromUnsignedLong(result & 0xffffffff);
}