diff --git a/mongoose.c b/mongoose.c index bcf88097..97d5c17e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2802,18 +2802,16 @@ void mg_hexdump(const void *buf, size_t len) { #if defined(MG_ENABLE_MD5) && MG_ENABLE_MD5 static void mg_byte_reverse(unsigned char *buf, unsigned longs) { -/* Forrest: MD5 expect LITTLE_ENDIAN, swap if BIG_ENDIAN */ -#if BYTE_ORDER == BIG_ENDIAN - do { - uint32_t t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | - ((unsigned) buf[1] << 8 | buf[0]); - *(uint32_t *) buf = t; - buf += 4; - } while (--longs); -#else - (void) buf; - (void) longs; -#endif + if (MG_BIG_ENDIAN) { + do { + uint32_t t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | + ((unsigned) buf[1] << 8 | buf[0]); + *(uint32_t *) buf = t; + buf += 4; + } while (--longs); + } else { + (void) buf, (void) longs; // Little endian. Do nothing + } } #define F1(x, y, z) (z ^ (x & (y ^ z))) @@ -3646,11 +3644,11 @@ union char64long16 { #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) static uint32_t blk0(union char64long16 *block, int i) { -/* Forrest: SHA expect BIG_ENDIAN, swap if LITTLE_ENDIAN */ -#if BYTE_ORDER == LITTLE_ENDIAN - block->l[i] = - (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF); -#endif + if (MG_BIG_ENDIAN) { + } else { + block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | + (rol(block->l[i], 8) & 0x00FF00FF); + } return block->l[i]; } diff --git a/mongoose.h b/mongoose.h index 4737b89b..59c2c733 100644 --- a/mongoose.h +++ b/mongoose.h @@ -61,21 +61,6 @@ extern "C" { #define MG_ARCH MG_ARCH_RP2040 #endif -/* - * clang with std=-c99 uses __LITTLE_ENDIAN, by default - * while for ex, RTOS gcc - LITTLE_ENDIAN, by default - * it depends on __USE_BSD, but let's have everything - */ -#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER) -#define BYTE_ORDER __BYTE_ORDER -#ifndef LITTLE_ENDIAN -#define LITTLE_ENDIAN __LITTLE_ENDIAN -#endif /* LITTLE_ENDIAN */ -#ifndef BIG_ENDIAN -#define BIG_ENDIAN __LITTLE_ENDIAN -#endif /* BIG_ENDIAN */ -#endif /* BYTE_ORDER */ - #if !defined(MG_ARCH) #include "mongoose_custom.h" // keep this include #endif @@ -85,6 +70,9 @@ extern "C" { #endif #endif // !defined(MG_ARCH) +// http://esr.ibiblio.org/?p=5095 +#define MG_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100) + @@ -659,7 +647,7 @@ int sscanf(const char *, const char *, ...); #endif #ifndef MG_ENABLE_MD5 -#define MG_ENABLE_MD5 0 +#define MG_ENABLE_MD5 1 #endif // Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) diff --git a/src/arch.h b/src/arch.h index a5b55394..70ae5a48 100644 --- a/src/arch.h +++ b/src/arch.h @@ -34,21 +34,6 @@ #define MG_ARCH MG_ARCH_RP2040 #endif -/* - * clang with std=-c99 uses __LITTLE_ENDIAN, by default - * while for ex, RTOS gcc - LITTLE_ENDIAN, by default - * it depends on __USE_BSD, but let's have everything - */ -#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER) -#define BYTE_ORDER __BYTE_ORDER -#ifndef LITTLE_ENDIAN -#define LITTLE_ENDIAN __LITTLE_ENDIAN -#endif /* LITTLE_ENDIAN */ -#ifndef BIG_ENDIAN -#define BIG_ENDIAN __LITTLE_ENDIAN -#endif /* BIG_ENDIAN */ -#endif /* BYTE_ORDER */ - #if !defined(MG_ARCH) #include "mongoose_custom.h" // keep this include #endif @@ -58,6 +43,9 @@ #endif #endif // !defined(MG_ARCH) +// http://esr.ibiblio.org/?p=5095 +#define MG_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100) + #include "arch_esp32.h" #include "arch_esp8266.h" #include "arch_freertos_lwip.h" diff --git a/src/config.h b/src/config.h index be4a7764..4a0d4967 100644 --- a/src/config.h +++ b/src/config.h @@ -45,7 +45,7 @@ #endif #ifndef MG_ENABLE_MD5 -#define MG_ENABLE_MD5 0 +#define MG_ENABLE_MD5 1 #endif // Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) diff --git a/src/md5.c b/src/md5.c index b8203ca2..5d8dca26 100644 --- a/src/md5.c +++ b/src/md5.c @@ -1,21 +1,19 @@ -#include "md5.h" #include "arch.h" +#include "md5.h" #if defined(MG_ENABLE_MD5) && MG_ENABLE_MD5 static void mg_byte_reverse(unsigned char *buf, unsigned longs) { -/* Forrest: MD5 expect LITTLE_ENDIAN, swap if BIG_ENDIAN */ -#if BYTE_ORDER == BIG_ENDIAN - do { - uint32_t t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | - ((unsigned) buf[1] << 8 | buf[0]); - *(uint32_t *) buf = t; - buf += 4; - } while (--longs); -#else - (void) buf; - (void) longs; -#endif + if (MG_BIG_ENDIAN) { + do { + uint32_t t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | + ((unsigned) buf[1] << 8 | buf[0]); + *(uint32_t *) buf = t; + buf += 4; + } while (--longs); + } else { + (void) buf, (void) longs; // Little endian. Do nothing + } } #define F1(x, y, z) (z ^ (x & (y ^ z))) diff --git a/src/sha1.c b/src/sha1.c index fe974bc6..10be1c74 100644 --- a/src/sha1.c +++ b/src/sha1.c @@ -1,7 +1,7 @@ /* Copyright(c) By Steve Reid */ /* 100% Public Domain */ -#include "sha1.h" #include "arch.h" +#include "sha1.h" union char64long16 { unsigned char c[64]; @@ -11,11 +11,11 @@ union char64long16 { #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) static uint32_t blk0(union char64long16 *block, int i) { -/* Forrest: SHA expect BIG_ENDIAN, swap if LITTLE_ENDIAN */ -#if BYTE_ORDER == LITTLE_ENDIAN - block->l[i] = - (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF); -#endif + if (MG_BIG_ENDIAN) { + } else { + block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | + (rol(block->l[i], 8) & 0x00FF00FF); + } return block->l[i]; } diff --git a/test/unit_test.c b/test/unit_test.c index dc8341aa..be2e9303 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -777,9 +777,7 @@ static void test_http_server(void) { fetch(&mgr, buf, url, "GET /test/ HTTP/1.0\n\n"); ASSERT(fetch(&mgr, buf, url, "GET /test/ HTTP/1.0\n\n") == 200); ASSERT(mg_strstr(mg_str(buf), mg_str(">Index of /test/<")) != NULL); -#if BYTE_ORDER != BIG_ENDIAN ASSERT(mg_strstr(mg_str(buf), mg_str(">fuzz.c<")) != NULL); -#endif { // Credentials @@ -1730,9 +1728,7 @@ static void test_util(void) { ASSERT(mg_aton(mg_str("0.0.0.-1"), &a) == false); ASSERT(mg_aton(mg_str("127.0.0.1"), &a) == true); ASSERT(a.is_ip6 == false); -#if BYTE_ORDER != BIG_ENDIAN - ASSERT(a.ip == 0x100007f); -#endif + ASSERT(a.ip == mg_htonl(0x7f000001)); ASSERT(strcmp(mg_ntoa(&a, buf, sizeof(buf)), "127.0.0.1") == 0); ASSERT(mg_aton(mg_str("1:2:3:4:5:6:7:8"), &a) == true);