summaryrefslogtreecommitdiff
path: root/types.h
blob: 28126b1412cd01ed609288726f5408a59a097831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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;

/*
 * 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
#ifdef bswap_16
	ret = bswap_16(ret);
#else
	ret = __builtin_bswap16(ret);
#endif
#endif
	return ret;
}

static inline u32 DWORD(const void *x)
{
	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;
}

#undef BIGENDIAN

#endif