aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/endian.h
blob: 1ec9d66d7829dbb4b4ab7669fdd7e30f2b05a545 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* See LICENSE file for copyright and license details. */
#include <stdint.h>

#if defined(HAVE_ENDIAN_H)
# include <endian.h>
#elif defined(HAVE_SYS_ENDIAN_H)
# include <sys/endian.h>
#endif

#if !defined(HAVE_ENDIAN_H) && !defined(HAVE_SYS_ENDIAN_H)

# if defined(HOST_ENDIAN_IS_LITTLE_ENDIAN_16)
#  if !defined(htole16)
#   define htole16(x) (x)
#  endif
#  if !defined(htole16)
#   define letoh16(x) (x)
#  endif
# endif
# if defined(HOST_ENDIAN_IS_LITTLE_ENDIAN_32)
#  if !defined(htole32)
#   define htole32(x) (x)
#  endif
#  if !defined(htole32)
#   define letoh32(x) (x)
#  endif
# endif
# if defined(HOST_ENDIAN_IS_LITTLE_ENDIAN_64)
#  if !defined(htole64)
#   define htole16(x) (x)
#  endif
#  if !defined(htole64)
#   define letoh16(x) (x)
#  endif
# endif

# if !defined(htole16)
#  define htole16 blind_htole16
static inline uint16_t
blind_htole16(uint16_t h)
{
	union {
		unsigned char bytes[2];
		uint16_t value;
	} d;
	d.bytes[0] = h;
	d.bytes[1] = h >> 8;
	return d.value;
}
# endif

# if !defined(htole32)
#  define htole32 blind_htole32
static inline uint32_t
blind_htole32(uint32_t h)
{
	union {
		unsigned char bytes[4];
		uint32_t value;
	} d;
	d.bytes[0] = h;
	d.bytes[1] = h >> 8;
	d.bytes[2] = h >> 16;
	d.bytes[3] = h >> 24;
	return d.value;
}
# endif

# if !defined(htole64)
#  define htole64 blind_htole64
static inline uint64_t
blind_htole64(uint64_t h)
{
	union {
		unsigned char bytes[8];
		uint64_t value;
	} d;
	d.bytes[0] = h;
	d.bytes[1] = h >> 8;
	d.bytes[2] = h >> 16;
	d.bytes[3] = h >> 24;
	d.bytes[4] = h >> 32;
	d.bytes[5] = h >> 40;
	d.bytes[6] = h >> 48;
	d.bytes[7] = h >> 56;
	return d.value;
}
# endif

# if !defined(le16toh)
#  if defined(letoh16)
#   define le16toh letoh16
#  else
#   define le16toh blind_le16toh
static inline uint16_t
blind_le16toh(uint16_t le)
{
	unsigned char *bytes = (unsigned char *)&le;
	return ((uint16_t)(bytes[1]) << 8) | (uint16_t)(bytes[0]);
}
#  endif
# endif

# if !defined(le32toh)
#  if defined(letoh32)
#   define le32toh letoh32
#  else
#   define le32toh blind_le32toh
static inline uint32_t
blind_le32toh(uint32_t le)
{
	unsigned char *bytes = (unsigned char *)&le;
	return ((uint32_t)(bytes[3]) << 24) |
	       ((uint32_t)(bytes[2]) << 16) |
	       ((uint32_t)(bytes[1]) << 8) |
		(uint32_t)(bytes[0]);
}
#  endif
# endif

# if !defined(le64toh)
#  if defined(letoh64)
#   define le64toh letoh64
#  else
#   define le64toh blind_le64toh
static inline uint64_t
blind_le64toh(uint64_t le)
{
	unsigned char *bytes = (unsigned char *)&le;
	return ((uint64_t)(bytes[7]) << 56) |
	       ((uint64_t)(bytes[6]) << 48) |
	       ((uint64_t)(bytes[5]) << 40) |
	       ((uint64_t)(bytes[4]) << 32) |
	       ((uint64_t)(bytes[3]) << 24) |
	       ((uint64_t)(bytes[2]) << 16) |
	       ((uint64_t)(bytes[1]) << 8) |
	       (uint64_t)(bytes[0]);
}
#  endif
# endif

#elif defined(HAVE_OPENBSD_ENDIAN)
# define le16toh letoh16
# define le32toh letoh32
# define le64toh letoh64
#endif