aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/endian.h
blob: e6805b154fe3dbded477de713bafde80d9c86aa7 (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
/* 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(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(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

#elif defined(HAVE_OPENBSD_ENDIAN)
# define le16toh letoh16
#endif