Merge pull request #3070 from cesanta/hton

remove memcpy from htons/l and test load/save bigendian macros
This commit is contained in:
Sergio R. Caprile authored and GitHub committed 2025-03-08 14:46:46 -03:00
commit fbc231f33e
5 files changed
+71 -48

No files matched your search

-13
View File
@@ -19175,19 +19175,6 @@ char *mg_random_str(char *buf, size_t len) {
return buf;
}
uint32_t mg_ntohl(uint32_t net) {
uint8_t data[4] = {0, 0, 0, 0};
memcpy(&data, &net, sizeof(data));
return (((uint32_t) data[3]) << 0) | (((uint32_t) data[2]) << 8) |
(((uint32_t) data[1]) << 16) | (((uint32_t) data[0]) << 24);
}
uint16_t mg_ntohs(uint16_t net) {
uint8_t data[2] = {0, 0};
memcpy(&data, &net, sizeof(data));
return (uint16_t) ((uint16_t) data[1] | (((uint16_t) data[0]) << 8));
}
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len) {
static const uint32_t crclut[16] = {
// table for polynomial 0xEDB88320 (reflected)
+29 -11
View File
@@ -1104,18 +1104,13 @@ struct mg_str mg_unpacked(const char *path); // Packed file as mg_str
void mg_bzero(volatile unsigned char *buf, size_t len);
bool mg_random(void *buf, size_t len);
char *mg_random_str(char *buf, size_t len);
uint16_t mg_ntohs(uint16_t net);
uint32_t mg_ntohl(uint32_t net);
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
uint64_t mg_millis(void); // Return milliseconds since boot
bool mg_path_is_sane(const struct mg_str path);
#define mg_htons(x) mg_ntohs(x)
#define mg_htonl(x) mg_ntohl(x)
#define MG_U32(a, b, c, d) \
(((uint32_t) ((a) & 255) << 24) | ((uint32_t) ((b) & 255) << 16) | \
((uint32_t) ((c) & 255) << 8) | (uint32_t) ((d) & 255))
#define MG_U32(a, b, c, d) \
(((uint32_t) ((a) &255) << 24) | ((uint32_t) ((b) &255) << 16) | \
((uint32_t) ((c) &255) << 8) | (uint32_t) ((d) &255))
#define MG_IPV4(a, b, c, d) mg_htonl(MG_U32(a, b, c, d))
@@ -1124,9 +1119,15 @@ bool mg_path_is_sane(const struct mg_str path);
#define MG_IPADDR_PARTS(ADDR) \
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
#define MG_LOAD_BE16(p) ((uint16_t) ((MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
#define MG_LOAD_BE24(p) \
((uint32_t) ((MG_U8P(p)[0] << 16U) | (MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
#define MG_LOAD_BE16(p) \
((uint16_t) (((uint16_t) MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
#define MG_LOAD_BE24(p) \
((uint32_t) (((uint32_t) MG_U8P(p)[0] << 16U) | \
((uint32_t) MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
#define MG_LOAD_BE32(p) \
((uint32_t) (((uint32_t) MG_U8P(p)[0] << 24U) | \
((uint32_t) MG_U8P(p)[1] << 16U) | \
((uint32_t) MG_U8P(p)[2] << 8U) | MG_U8P(p)[3]))
#define MG_STORE_BE16(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
@@ -1138,6 +1139,23 @@ bool mg_path_is_sane(const struct mg_str path);
MG_U8P(p)[1] = ((n) >> 8U) & 255; \
MG_U8P(p)[2] = (n) &255; \
} while (0)
#define MG_STORE_BE32(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 24U) & 255; \
MG_U8P(p)[1] = ((n) >> 16U) & 255; \
MG_U8P(p)[2] = ((n) >> 8U) & 255; \
MG_U8P(p)[3] = (n) &255; \
} while (0)
// network format equates big endian order
static inline uint16_t mg_ntohs(uint16_t net) {
return MG_LOAD_BE16(&net);
}
static inline uint32_t mg_ntohl(uint32_t net) {
return MG_LOAD_BE32(&net);
}
#define mg_htons(x) mg_ntohs(x)
#define mg_htonl(x) mg_ntohl(x)
#define MG_REG(x) ((volatile uint32_t *) (x))[0]
#define MG_BIT(x) (((uint32_t) 1U) << (x))
-13
View File
@@ -73,19 +73,6 @@ char *mg_random_str(char *buf, size_t len) {
return buf;
}
uint32_t mg_ntohl(uint32_t net) {
uint8_t data[4] = {0, 0, 0, 0};
memcpy(&data, &net, sizeof(data));
return (((uint32_t) data[3]) << 0) | (((uint32_t) data[2]) << 8) |
(((uint32_t) data[1]) << 16) | (((uint32_t) data[0]) << 24);
}
uint16_t mg_ntohs(uint16_t net) {
uint8_t data[2] = {0, 0};
memcpy(&data, &net, sizeof(data));
return (uint16_t) ((uint16_t) data[1] | (((uint16_t) data[0]) << 8));
}
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len) {
static const uint32_t crclut[16] = {
// table for polynomial 0xEDB88320 (reflected)
+29 -11
View File
@@ -14,18 +14,13 @@
void mg_bzero(volatile unsigned char *buf, size_t len);
bool mg_random(void *buf, size_t len);
char *mg_random_str(char *buf, size_t len);
uint16_t mg_ntohs(uint16_t net);
uint32_t mg_ntohl(uint32_t net);
uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len);
uint64_t mg_millis(void); // Return milliseconds since boot
bool mg_path_is_sane(const struct mg_str path);
#define mg_htons(x) mg_ntohs(x)
#define mg_htonl(x) mg_ntohl(x)
#define MG_U32(a, b, c, d) \
(((uint32_t) ((a) & 255) << 24) | ((uint32_t) ((b) & 255) << 16) | \
((uint32_t) ((c) & 255) << 8) | (uint32_t) ((d) & 255))
#define MG_U32(a, b, c, d) \
(((uint32_t) ((a) &255) << 24) | ((uint32_t) ((b) &255) << 16) | \
((uint32_t) ((c) &255) << 8) | (uint32_t) ((d) &255))
#define MG_IPV4(a, b, c, d) mg_htonl(MG_U32(a, b, c, d))
@@ -34,9 +29,15 @@ bool mg_path_is_sane(const struct mg_str path);
#define MG_IPADDR_PARTS(ADDR) \
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
#define MG_LOAD_BE16(p) ((uint16_t) ((MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
#define MG_LOAD_BE24(p) \
((uint32_t) ((MG_U8P(p)[0] << 16U) | (MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
#define MG_LOAD_BE16(p) \
((uint16_t) (((uint16_t) MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
#define MG_LOAD_BE24(p) \
((uint32_t) (((uint32_t) MG_U8P(p)[0] << 16U) | \
((uint32_t) MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
#define MG_LOAD_BE32(p) \
((uint32_t) (((uint32_t) MG_U8P(p)[0] << 24U) | \
((uint32_t) MG_U8P(p)[1] << 16U) | \
((uint32_t) MG_U8P(p)[2] << 8U) | MG_U8P(p)[3]))
#define MG_STORE_BE16(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
@@ -48,6 +49,23 @@ bool mg_path_is_sane(const struct mg_str path);
MG_U8P(p)[1] = ((n) >> 8U) & 255; \
MG_U8P(p)[2] = (n) &255; \
} while (0)
#define MG_STORE_BE32(p, n) \
do { \
MG_U8P(p)[0] = ((n) >> 24U) & 255; \
MG_U8P(p)[1] = ((n) >> 16U) & 255; \
MG_U8P(p)[2] = ((n) >> 8U) & 255; \
MG_U8P(p)[3] = (n) &255; \
} while (0)
// network format equates big endian order
static inline uint16_t mg_ntohs(uint16_t net) {
return MG_LOAD_BE16(&net);
}
static inline uint32_t mg_ntohl(uint32_t net) {
return MG_LOAD_BE32(&net);
}
#define mg_htons(x) mg_ntohs(x)
#define mg_htonl(x) mg_ntohl(x)
#define MG_REG(x) ((volatile uint32_t *) (x))[0]
#define MG_BIT(x) (((uint32_t) 1U) << (x))
+13
View File
@@ -2412,6 +2412,7 @@ static void test_util(void) {
char buf[100], *s;
struct mg_addr a;
uint32_t ipv4;
uint16_t port;
struct mg_str data;
memset(&a, 0xa5, sizeof(a));
ASSERT(mg_file_printf(&mg_fs_posix, "data.txt", "%s", "hi") == true);
@@ -2429,6 +2430,18 @@ static void test_util(void) {
ASSERT(a.is_ip6 == false);
memcpy(&ipv4, a.ip, sizeof(ipv4));
ASSERT(ipv4 == mg_htonl(0x7f000001));
ASSERT(mg_ntohl(ipv4) == 0x7f000001);
MG_STORE_BE32(&ipv4, 0x5678abcd);
ASSERT(((uint8_t *)&ipv4)[0] == 0x56 && ((uint8_t *)&ipv4)[1] == 0x78 && ((uint8_t *)&ipv4)[2] == 0xab && ((uint8_t *)&ipv4)[3] == 0xcd);
ASSERT(MG_LOAD_BE32(&ipv4) == 0x5678abcd);
MG_STORE_BE16(&port, 0x1234);
ASSERT(((uint8_t *)&port)[0] == 0x12 && ((uint8_t *)&port)[1] == 0x34);
ASSERT(MG_LOAD_BE16(&port) == 0x1234);
ASSERT(port == mg_htons(0x1234));
ASSERT(mg_ntohs(port) == 0x1234);
MG_STORE_BE24(&ipv4, 0xef2345);
ASSERT(((uint8_t *)&ipv4)[0] == 0xef && ((uint8_t *)&ipv4)[1] == 0x23 && ((uint8_t *)&ipv4)[2] == 0x45);
ASSERT(MG_LOAD_BE24(&ipv4) == 0xef2345);
memset(a.ip, 0xa5, sizeof(a.ip));
ASSERT(mg_aton(mg_str("1:2:3:4:5:6:7:8"), &a) == true);