commit f5970c7804f6bb6f5d99172e048d7a126657b570
parent a50b4d566f089ae46402a6f38096a0ff9c50eae2
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Fri, 19 Apr 2019 22:35:15 +0300
mhd_bithelpers.h: define _MHD_GET_32BIT_LE() and use it in md5.c
Diffstat:
2 files changed, 30 insertions(+), 7 deletions(-)
diff --git a/src/microhttpd/md5.c b/src/microhttpd/md5.c
@@ -96,6 +96,12 @@ MD5Final (void *ctx_,
}
+/**
+ * Number of bytes in single SHA-256 word
+ * used to process data
+ */
+#define MD5_BYTES_IN_WORD (32 / 8)
+
/* The four core functions - F1 is optimized somewhat */
/* #define F1(x, y, z) (x & y | ~x & z) */
@@ -122,14 +128,12 @@ MD5Transform (uint32_t state[4],
#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
const uint32_t *in = (const uint32_t *)block;
#else
- uint32_t in[MD5_BLOCK_SIZE / 4];
- for (a = 0; a < MD5_BLOCK_SIZE / 4; a++)
+ uint32_t in[MD5_BLOCK_SIZE / MD5_BYTES_IN_WORD];
+ int i;
+
+ for (i = 0; i < MD5_BLOCK_SIZE / MD5_BYTES_IN_WORD; i++)
{
- in[a] = (uint32_t)(
- (uint32_t)(block[a * 4 + 0]) |
- (uint32_t)(block[a * 4 + 1]) << 8 |
- (uint32_t)(block[a * 4 + 2]) << 16 |
- (uint32_t)(block[a * 4 + 3]) << 24);
+ in[i] = _MHD_GET_32BIT_LE(block + i * MD5_BYTES_IN_WORD);
}
#endif
diff --git a/src/microhttpd/mhd_bithelpers.h b/src/microhttpd/mhd_bithelpers.h
@@ -101,6 +101,25 @@
} while (0)
#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+/* _MHD_GET_32BIT_LE (addr)
+ * get little-endian 32-bit value storied at addr
+ * and return it in native-endian mode.
+ */
+#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
+#define _MHD_GET_32BIT_LE(addr) \
+ (*(uint32_t*)(addr))
+#elif _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
+#define _MHD_GET_32BIT_LE(addr) \
+ _MHD_BYTES_SWAP32(*(uint32_t*)(addr))
+#else /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+/* Endianess was not detected or non-standard like PDP-endian */
+#define _MHD_GET_32BIT_LE(addr) \
+ ( ( (uint32_t)(((uint8_t*)addr)[0])) | \
+ (((uint32_t)(((uint8_t*)addr)[1])) << 8) | \
+ (((uint32_t)(((uint8_t*)addr)[2])) << 16) | \
+ (((uint32_t)(((uint8_t*)addr)[3])) << 24) )
+#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+
/* _MHD_PUT_64BIT_BE (addr, value64)
* put native-endian 64-bit value64 to addr