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