summaryrefslogtreecommitdiff
path: root/types.h
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff.email>2026-03-10 09:31:17 +0100
committerJörg Frings-Fürst <debian@jff.email>2026-03-10 09:31:17 +0100
commit22f77b46ab555f57523990094a3e40a55f2c492a (patch)
tree0b2dad175ecf9514c5c9d8fb9473010c2337a3b3 /types.h
parentc9aac994d65f7bcc3659e3219d6729a24c803fcf (diff)
New upstream version 3.7upstream/3.7upstream
Diffstat (limited to 'types.h')
-rw-r--r--types.h97
1 files changed, 61 insertions, 36 deletions
diff --git a/types.h b/types.h
index 51c32d7..28126b1 100644
--- a/types.h
+++ b/types.h
@@ -1,59 +1,84 @@
#ifndef TYPES_H
#define TYPES_H
+#include <string.h>
+
#include "config.h"
+/*
+ * Use the byte-order macros provided by the compiler if available, else
+ * fall back to the ones provided by the C library.
+ */
+#ifdef __BYTE_ORDER__
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define BIGENDIAN
+#endif
+#else /* __BYTE_ORDER__ */
+#include <endian.h>
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define BIGENDIAN
+#endif
+#endif /* __BYTE_ORDER__ */
+
+#ifdef BIGENDIAN
+#ifndef bswap_16
+#include <byteswap.h>
+#endif
+#endif
+
typedef unsigned char u8;
typedef unsigned short u16;
typedef signed short i16;
typedef unsigned int u32;
+typedef unsigned long long int u64;
/*
- * You may use the following defines to adjust the type definitions
- * depending on the architecture:
- * - Define BIGENDIAN on big-endian systems.
- * - Define ALIGNMENT_WORKAROUND if your system doesn't support
- * non-aligned memory access. In this case, we use a slower, but safer,
- * memory access method. This should be done automatically in config.h
- * for architectures which need it.
+ * Per SMBIOS v2.8.0 and later, all structures assume a little-endian
+ * ordering convention.
*/
+static inline u16 WORD(const void *x)
+{
+ u16 ret;
+ memcpy(&ret, x, sizeof(ret));
#ifdef BIGENDIAN
-typedef struct {
- u32 h;
- u32 l;
-} u64;
+#ifdef bswap_16
+ ret = bswap_16(ret);
#else
-typedef struct {
- u32 l;
- u32 h;
-} u64;
+ ret = __builtin_bswap16(ret);
+#endif
#endif
+ return ret;
+}
-#if defined(ALIGNMENT_WORKAROUND) || defined(BIGENDIAN)
-static inline u64 U64(u32 low, u32 high)
+static inline u32 DWORD(const void *x)
{
- u64 self;
-
- self.l = low;
- self.h = high;
-
- return self;
+ u32 ret;
+ memcpy(&ret, x, sizeof(ret));
+#ifdef BIGENDIAN
+#ifdef bswap_32
+ ret = bswap_32(ret);
+#else
+ ret = __builtin_bswap32(ret);
+#endif
+#endif
+ return ret;
}
+
+static inline u64 QWORD(const void *x)
+{
+ u64 ret;
+ memcpy(&ret, x, sizeof(ret));
+#ifdef BIGENDIAN
+#ifdef bswap_64
+ ret = bswap_64(ret);
+#else
+ ret = __builtin_bswap64(ret);
+#endif
#endif
+ return ret;
+}
-/*
- * Per SMBIOS v2.8.0 and later, all structures assume a little-endian
- * ordering convention.
- */
-#if defined(ALIGNMENT_WORKAROUND) || defined(BIGENDIAN)
-#define WORD(x) (u16)((x)[0] + ((x)[1] << 8))
-#define DWORD(x) (u32)((x)[0] + ((x)[1] << 8) + ((x)[2] << 16) + ((x)[3] << 24))
-#define QWORD(x) (U64(DWORD(x), DWORD(x + 4)))
-#else /* ALIGNMENT_WORKAROUND || BIGENDIAN */
-#define WORD(x) (u16)(*(const u16 *)(x))
-#define DWORD(x) (u32)(*(const u32 *)(x))
-#define QWORD(x) (*(const u64 *)(x))
-#endif /* ALIGNMENT_WORKAROUND || BIGENDIAN */
+#undef BIGENDIAN
#endif