xref: /freebsd/contrib/openbsm/compat/endian.h (revision cc426dd31990b8b50b210efc450e404596548ca1)
1 /*-
2  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3  * Copyright (c) 2005 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Derived from FreeBSD src/sys/sys/endian.h:1.6.
28  */
29 
30 #ifndef _COMPAT_ENDIAN_H_
31 #define _COMPAT_ENDIAN_H_
32 
33 /*
34  * Some systems will have the uint/int types defined here already, others
35  * will need stdint.h.
36  */
37 #ifdef HAVE_STDINT_H
38 #include <stdint.h>
39 #endif
40 
41 /*
42  * Some operating systems do not yet have the more recent endian APIs that
43  * permit encoding to and decoding from byte streams.  For those systems, we
44  * implement local non-optimized versions.
45  */
46 
47 static __inline uint16_t
48 bswap16(uint16_t int16)
49 {
50 	const unsigned char *from;
51 	unsigned char *to;
52 	uint16_t t;
53 
54 	from = (const unsigned char *) &int16;
55 	to = (unsigned char *) &t;
56 
57 	to[0] = from[1];
58 	to[1] = from[0];
59 
60 	return (t);
61 }
62 
63 static __inline uint32_t
64 bswap32(uint32_t int32)
65 {
66 	const unsigned char *from;
67 	unsigned char *to;
68 	uint32_t t;
69 
70 	from = (const unsigned char *) &int32;
71 	to = (unsigned char *) &t;
72 
73 	to[0] = from[3];
74 	to[1] = from[2];
75 	to[2] = from[1];
76 	to[3] = from[0];
77 
78 	return (t);
79 }
80 
81 static __inline uint64_t
82 bswap64(uint64_t int64)
83 {
84 	const unsigned char *from;
85 	unsigned char *to;
86 	uint64_t t;
87 
88 	from = (const unsigned char *) &int64;
89 	to = (unsigned char *) &t;
90 
91 	to[0] = from[7];
92 	to[1] = from[6];
93 	to[2] = from[5];
94 	to[3] = from[4];
95 	to[4] = from[3];
96 	to[5] = from[2];
97 	to[6] = from[1];
98 	to[7] = from[0];
99 
100 	return (t);
101 }
102 
103 #if defined(BYTE_ORDER) && !defined(_BYTE_ORDER)
104 #define	_BYTE_ORDER	BYTE_ORDER
105 #endif
106 #if !defined(_BYTE_ORDER)
107 #error "Neither BYTE_ORDER nor _BYTE_ORDER defined"
108 #endif
109 
110 #if defined(BIG_ENDIAN) && !defined(_BIG_ENDIAN)
111 #define	_BIG_ENDIAN	BIG_ENDIAN
112 #endif
113 
114 #if defined(LITTLE_ENDIAN) && !defined(_LITTLE_ENDIAN)
115 #define	_LITTLE_ENDIAN	LITTLE_ENDIAN
116 #endif
117 
118 /* XXX: Hack. */
119 #ifndef htobe16
120 /*
121  * Host to big endian, host to little endian, big endian to host, and little
122  * endian to host byte order functions as detailed in byteorder(9).
123  */
124 #if _BYTE_ORDER == _LITTLE_ENDIAN
125 #define	htobe16(x)	bswap16((x))
126 #define	htobe32(x)	bswap32((x))
127 #define	htobe64(x)	bswap64((x))
128 #define	htole16(x)	((uint16_t)(x))
129 #define	htole32(x)	((uint32_t)(x))
130 #define	htole64(x)	((uint64_t)(x))
131 
132 #define	be16toh(x)	bswap16((x))
133 #define	be32toh(x)	bswap32((x))
134 #define	be64toh(x)	bswap64((x))
135 #define	le16toh(x)	((uint16_t)(x))
136 #define	le32toh(x)	((uint32_t)(x))
137 #define	le64toh(x)	((uint64_t)(x))
138 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
139 #define	htobe16(x)	((uint16_t)(x))
140 #define	htobe32(x)	((uint32_t)(x))
141 #define	htobe64(x)	((uint64_t)(x))
142 #define	htole16(x)	bswap16((x))
143 #define	htole32(x)	bswap32((x))
144 #define	htole64(x)	bswap64((x))
145 
146 #define	be16toh(x)	((uint16_t)(x))
147 #define	be32toh(x)	((uint32_t)(x))
148 #define	be64toh(x)	((uint64_t)(x))
149 #define	le16toh(x)	bswap16((x))
150 #define	le32toh(x)	bswap32((x))
151 #define	le64toh(x)	bswap64((x))
152 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
153 #endif
154 
155 #endif	/* _COMPAT_ENDIAN_H_ */
156