Another stab at SF 576327: zipfile when sizeof(long) == 8

binascii_crc32():  The previous patch forced this to return the same
result across platforms.  This patch deals with that, on a 64-bit box,
the *entry* value may have "unexpected" bits in the high four bytes.

Bugfix candidate.
This commit is contained in:
Tim Peters 2002-07-02 22:24:50 +00:00
parent aab713bdf7
commit 934c1a1c6b
1 changed files with 104 additions and 98 deletions

View File

@ -869,19 +869,25 @@ binascii_crc32(PyObject *self, PyObject *args)
if ( !PyArg_ParseTuple(args, "s#|l:crc32", &bin_data, &len, &crc) )
return NULL;
crc = crc ^ 0xFFFFFFFFUL;
while(len--)
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);
/* Note: (crc >> 8) MUST zero fill on left */
result = (long)(crc ^ 0xFFFFFFFFUL);
/* If long is > 32 bits, 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 long, but the evidence suggests
* that lots of code outside this treats the result as if it were
* a signed 4-byte integer.
#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 PyInt_FromLong(result);
}