Fix endianness issues in unit test

This commit is contained in:
cpq 2022-09-28 12:42:20 +01:00
parent b5355307a3
commit 88097f1829
7 changed files with 41 additions and 73 deletions

View File

@ -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];
}

View File

@ -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)

View File

@ -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"

View File

@ -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)

View File

@ -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)))

View File

@ -1,7 +1,7 @@
/* Copyright(c) By Steve Reid <steve@edmweb.com> */
/* 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];
}

View File

@ -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);