Skip to content

Commit d5bc7cb

Browse files
committed
Improve endian check if macro is not set
1 parent fbea809 commit d5bc7cb

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* The handling of float and double types was rewritten to fix compiler errors
44
and to eliminate the use of volatile.
5+
* Improved endian preprocessor check if `MMDB_LITTLE_ENDIAN` is not set.
56

67
## 1.12.2 - 2025-01-10
78

include/maxminddb.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ extern "C" {
1212
#include <stdio.h>
1313
#include <sys/types.h>
1414

15+
#ifdef __has_include
16+
#if __has_include(<endian.h>)
17+
#include <endian.h>
18+
#elif __has_include(<sys/endian.h>)
19+
#include <sys/endian.h>
20+
#endif
21+
#endif
22+
1523
#ifdef _WIN32
1624
#include <winsock2.h>
1725
#include <ws2tcpip.h>
@@ -28,6 +36,17 @@ extern "C" {
2836
#include <sys/socket.h>
2937
#endif
3038

39+
#if !defined(MMDB_LITTLE_ENDIAN)
40+
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
41+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
42+
#define MMDB_LITTLE_ENDIAN 1
43+
#endif
44+
#elif defined(_WIN32) || defined(_WIN64)
45+
// We assume modern Windows targets are little endian
46+
#define MMDB_LITTLE_ENDIAN 1
47+
#endif
48+
#endif
49+
3150
#define MMDB_DATA_TYPE_EXTENDED (0)
3251
#define MMDB_DATA_TYPE_POINTER (1)
3352
#define MMDB_DATA_TYPE_UTF8_STRING (2)

src/maxminddb.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,9 +1803,7 @@ static float get_ieee754_float(const uint8_t *restrict p) {
18031803

18041804
memcpy(&i, p, sizeof(uint32_t));
18051805

1806-
/* Windows builds don't use autoconf but we can assume they're all
1807-
* little-endian. */
1808-
#if MMDB_LITTLE_ENDIAN || _WIN32
1806+
#if MMDB_LITTLE_ENDIAN
18091807
i = bswap32(i);
18101808
#endif
18111809

@@ -1820,7 +1818,7 @@ static double get_ieee754_double(const uint8_t *restrict p) {
18201818

18211819
memcpy(&i, p, sizeof(uint64_t));
18221820

1823-
#if MMDB_LITTLE_ENDIAN || _WIN32
1821+
#if MMDB_LITTLE_ENDIAN
18241822
i = bswap64(i);
18251823
#endif
18261824

0 commit comments

Comments
 (0)