[PATCH 8/9] switch to gl_BIGENDIAN defining WORD_BIGENDIAN

Michael Haubenwallner michael.haubenwallner at ssi-schaefer.com
Mon Apr 10 11:10:45 UTC 2017


As we already use gnulib, we better use their existing check to get the
current byte order.
---
 configure.ac |  3 ++-
 src/md4.c    |  2 +-
 src/md5.c    |  2 +-
 src/rmd160.c |  7 +------
 src/sha1.c   |  2 +-
 src/sha2.c   | 39 +++------------------------------------
 6 files changed, 9 insertions(+), 46 deletions(-)

diff --git a/configure.ac b/configure.ac
index f238c4b..ff0e56a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -31,12 +31,13 @@ LT_INIT
 # Checks for libraries.
 
 # Checks for header files.
-AC_CHECK_HEADERS([sys/param.h sys/endian.h])
+AC_CHECK_HEADERS([sys/param.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_TYPE_OFF_T
 AC_TYPE_SIZE_T
 AC_TYPE_SSIZE_T
+gl_BIGENDIAN
 
 # Checks for library functions.
 gl_INIT
diff --git a/src/md4.c b/src/md4.c
index caa070a..3e1df9d 100644
--- a/src/md4.c
+++ b/src/md4.c
@@ -160,7 +160,7 @@ MD4Transform(uint32_t state[4], const uint8_t block[MD4_BLOCK_LENGTH])
 {
 	uint32_t a, b, c, d, in[MD4_BLOCK_LENGTH / 4];
 
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
 	memcpy(in, block, sizeof(in));
 #else
 	for (a = 0; a < MD4_BLOCK_LENGTH / 4; a++) {
diff --git a/src/md5.c b/src/md5.c
index ac111c9..2e4b9cb 100644
--- a/src/md5.c
+++ b/src/md5.c
@@ -160,7 +160,7 @@ MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
 {
 	uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
 
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
 	memcpy(in, block, sizeof(in));
 #else
 	for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
diff --git a/src/rmd160.c b/src/rmd160.c
index 62471ef..cdfdd99 100644
--- a/src/rmd160.c
+++ b/src/rmd160.c
@@ -33,11 +33,6 @@
 
 #include <string.h>
 #include <sys/types.h>
-#ifdef HAVE_SYS_ENDIAN_H
-#include <sys/endian.h>
-#else
-#include <endian.h>
-#endif
 #include <rmd160.h>
 
 #define PUT_64BIT_LE(cp, value) do {                                    \
@@ -172,7 +167,7 @@ RMD160Transform(uint32_t state[5], const uint8_t block[RMD160_BLOCK_LENGTH])
 {
 	uint32_t a, b, c, d, e, aa, bb, cc, dd, ee, t, x[16];
 
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
 	memcpy(x, block, RMD160_BLOCK_LENGTH);
 #else
 	int i;
diff --git a/src/sha1.c b/src/sha1.c
index 9520448..dfc728d 100644
--- a/src/sha1.c
+++ b/src/sha1.c
@@ -30,7 +30,7 @@
  * blk0() and blk() perform the initial expand.
  * I got the idea of expanding during the round function from SSLeay
  */
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
 # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
     |(rol(block->l[i],8)&0x00FF00FF))
 #else
diff --git a/src/sha2.c b/src/sha2.c
index 6ce41d3..e048e5a 100644
--- a/src/sha2.c
+++ b/src/sha2.c
@@ -63,39 +63,6 @@
  *
  */
 
-/*** SHA-256/384/512 Machine Architecture Definitions *****************/
-/*
- * BYTE_ORDER NOTE:
- *
- * Please make sure that your system defines BYTE_ORDER.  If your
- * architecture is little-endian, make sure it also defines
- * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
- * equivilent.
- *
- * If your system does not define the above, then you can do so by
- * hand like this:
- *
- *   #define LITTLE_ENDIAN 1234
- *   #define BIG_ENDIAN    4321
- *
- * And for little-endian machines, add:
- *
- *   #define BYTE_ORDER LITTLE_ENDIAN 
- *
- * Or for big-endian machines:
- *
- *   #define BYTE_ORDER BIG_ENDIAN
- *
- * The FreeBSD machine this was written on defines BYTE_ORDER
- * appropriately by including <sys/types.h> (which in turn includes
- * <machine/endian.h> where the appropriate definitions are actually
- * made).
- */
-#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
-#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
-#endif
-
-
 /*** SHA-256/384/512 Various Length Definitions ***********************/
 /* NOTE: Most of these are in sha2.h */
 #define SHA256_SHORT_BLOCK_LENGTH	(SHA256_BLOCK_LENGTH - 8)
@@ -547,7 +514,7 @@ SHA256Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context)
 
 	/* If no digest buffer is passed, we don't bother doing this: */
 	if (digest != NULL) {
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
 		int	i;
 
 		/* Convert TO host byte order */
@@ -829,7 +796,7 @@ SHA512Final(uint8_t digest[SHA512_DIGEST_LENGTH], SHA2_CTX *context)
 
 	/* If no digest buffer is passed, we don't bother doing this: */
 	if (digest != NULL) {
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
 		int	i;
 
 		/* Convert TO host byte order */
@@ -866,7 +833,7 @@ SHA384Final(uint8_t digest[SHA384_DIGEST_LENGTH], SHA2_CTX *context)
 
 	/* If no digest buffer is passed, we don't bother doing this: */
 	if (digest != NULL) {
-#if BYTE_ORDER == LITTLE_ENDIAN
+#ifndef WORDS_BIGENDIAN
 		int	i;
 
 		/* Convert TO host byte order */
-- 
2.10.2



More information about the libbsd mailing list