Commit 64e898cd authored by Youness Alaoui's avatar Youness Alaoui

Check for endianness at runtime instead of relying on defines that might not...

Check for endianness at runtime instead of relying on defines that might not be defined everywhere...
parent 53455086
......@@ -35,24 +35,27 @@
static void MD5Transform(uint32_t buf[4], uint32_t const in[16]);
static int am_big_endian(void)
{
long one= 1;
return !(*((char *)(&one)));
}
#if defined(_WIN32) || __BYTE_ORDER != __BIG_ENDIAN
#define byteReverse(buf, len) /* Nothing */
#else
/*
* Note: this code is harmless on little-endian machines.
*/
static void byteReverse(unsigned char *buf, unsigned longs)
{
uint32_t t;
do {
t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(uint32_t *) buf = t;
buf += 4;
} while (--longs);
if (am_big_endian ()) {
do {
t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(uint32_t *) buf = t;
buf += 4;
} while (--longs);
}
}
#endif
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
......
......@@ -101,12 +101,8 @@ A million repetitions of "a"
/* blk0() and blk() perform the initial expand. */
/* I got the idea of expanding during the round function from SSLeay */
#if defined(_WIN32) || __BYTE_ORDER != __BIG_ENDIAN
#define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \
(rol(block->l[i], 8) & 0x00FF00FF))
#else
#define blk0(i) block->l[i]
#endif
#define blk0(i) (block->l[i] = blk0_endiansafe (block->l[i]))
#define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ \
block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
......@@ -129,6 +125,19 @@ A million repetitions of "a"
static void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]);
/* Hash a single 512-bit block. This is the core of the algorithm. */
static int am_big_endian(void)
{
long one= 1;
return !(*((char *)(&one)));
}
static uint32_t blk0_endiansafe (uint32_t l)
{
if (am_big_endian ())
return l;
else
return (rol(l, 24) & 0xFF00FF00) | (rol(l, 8) & 0x00FF00FF);
}
static void SHA1Transform(uint32_t state[5], const unsigned char buffer[64])
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment