mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #16166: Add PY_LITTLE_ENDIAN and PY_BIG_ENDIAN macros and unified
endianess detection and handling.
This commit is contained in:
parent
1e9af84e2e
commit
743e0cd6b5
15 changed files with 78 additions and 107 deletions
|
@ -21,13 +21,6 @@
|
|||
#include "hashlib.h"
|
||||
|
||||
|
||||
/* Endianness testing and definitions */
|
||||
#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
|
||||
if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
|
||||
|
||||
#define PCT_LITTLE_ENDIAN 1
|
||||
#define PCT_BIG_ENDIAN 0
|
||||
|
||||
/* Some useful types */
|
||||
|
||||
typedef unsigned char SHA_BYTE;
|
||||
|
@ -50,7 +43,6 @@ typedef struct {
|
|||
SHA_INT32 digest[8]; /* Message digest */
|
||||
SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
|
||||
SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
|
||||
int Endianness;
|
||||
int local; /* unprocessed amount in data */
|
||||
int digestsize;
|
||||
} SHAobject;
|
||||
|
@ -58,13 +50,11 @@ typedef struct {
|
|||
/* When run on a little-endian CPU we need to perform byte reversal on an
|
||||
array of longwords. */
|
||||
|
||||
static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
|
||||
#if PY_LITTLE_ENDIAN
|
||||
static void longReverse(SHA_INT32 *buffer, int byteCount)
|
||||
{
|
||||
SHA_INT32 value;
|
||||
|
||||
if ( Endianness == PCT_BIG_ENDIAN )
|
||||
return;
|
||||
|
||||
byteCount /= sizeof(*buffer);
|
||||
while (byteCount--) {
|
||||
value = *buffer;
|
||||
|
@ -73,10 +63,10 @@ static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
|
|||
*buffer++ = ( value << 16 ) | ( value >> 16 );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void SHAcopy(SHAobject *src, SHAobject *dest)
|
||||
{
|
||||
dest->Endianness = src->Endianness;
|
||||
dest->local = src->local;
|
||||
dest->digestsize = src->digestsize;
|
||||
dest->count_lo = src->count_lo;
|
||||
|
@ -131,7 +121,9 @@ sha_transform(SHAobject *sha_info)
|
|||
SHA_INT32 S[8], W[64], t0, t1;
|
||||
|
||||
memcpy(W, sha_info->data, sizeof(sha_info->data));
|
||||
longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
|
||||
#if PY_LITTLE_ENDIAN
|
||||
longReverse(W, (int)sizeof(sha_info->data));
|
||||
#endif
|
||||
|
||||
for (i = 16; i < 64; ++i) {
|
||||
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
|
||||
|
@ -228,7 +220,6 @@ sha_transform(SHAobject *sha_info)
|
|||
static void
|
||||
sha_init(SHAobject *sha_info)
|
||||
{
|
||||
TestEndianness(sha_info->Endianness)
|
||||
sha_info->digest[0] = 0x6A09E667L;
|
||||
sha_info->digest[1] = 0xBB67AE85L;
|
||||
sha_info->digest[2] = 0x3C6EF372L;
|
||||
|
@ -246,7 +237,6 @@ sha_init(SHAobject *sha_info)
|
|||
static void
|
||||
sha224_init(SHAobject *sha_info)
|
||||
{
|
||||
TestEndianness(sha_info->Endianness)
|
||||
sha_info->digest[0] = 0xc1059ed8L;
|
||||
sha_info->digest[1] = 0x367cd507L;
|
||||
sha_info->digest[2] = 0x3070dd17L;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue