2 * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3 * Copyright (c) 2005 Robert N. M. Watson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * Derived from FreeBSD src/sys/sys/endian.h:1.6.
28 * $P4: //depot/projects/trustedbsd/openbsm/compat/endian.h#7 $
31 #ifndef _COMPAT_ENDIAN_H_
32 #define _COMPAT_ENDIAN_H_
35 * Some systems will have the uint/int types defined here already, others
41 * Some operating systems do not yet have the more recent endian APIs that
42 * permit encoding to and decoding from byte streams. For those systems, we
43 * implement local non-optimized versions.
46 static __inline uint16_t
47 bswap16(uint16_t int16)
49 const unsigned char *from;
53 from = (const unsigned char *) &int16;
54 to = (unsigned char *) &t;
62 static __inline uint32_t
63 bswap32(uint32_t int32)
65 const unsigned char *from;
69 from = (const unsigned char *) &int32;
70 to = (unsigned char *) &t;
80 static __inline uint64_t
81 bswap64(uint64_t int64)
83 const unsigned char *from;
87 from = (const unsigned char *) &int64;
88 to = (unsigned char *) &t;
102 #if defined(BYTE_ORDER) && !defined(_BYTE_ORDER)
103 #define _BYTE_ORDER BYTE_ORDER
105 #if !defined(_BYTE_ORDER)
106 #error "Neither BYTE_ORDER nor _BYTE_ORDER defined"
109 #if defined(BIG_ENDIAN) && !defined(_BIG_ENDIAN)
110 #define _BIG_ENDIAN BIG_ENDIAN
113 #if defined(LITTLE_ENDIAN) && !defined(_LITTLE_ENDIAN)
114 #define _LITTLE_ENDIAN LITTLE_ENDIAN
118 * Host to big endian, host to little endian, big endian to host, and little
119 * endian to host byte order functions as detailed in byteorder(9).
121 #if _BYTE_ORDER == _LITTLE_ENDIAN
122 #define htobe16(x) bswap16((x))
123 #define htobe32(x) bswap32((x))
124 #define htobe64(x) bswap64((x))
125 #define htole16(x) ((uint16_t)(x))
126 #define htole32(x) ((uint32_t)(x))
127 #define htole64(x) ((uint64_t)(x))
129 #define be16toh(x) bswap16((x))
130 #define be32toh(x) bswap32((x))
131 #define be64toh(x) bswap64((x))
132 #define le16toh(x) ((uint16_t)(x))
133 #define le32toh(x) ((uint32_t)(x))
134 #define le64toh(x) ((uint64_t)(x))
135 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
136 #define htobe16(x) ((uint16_t)(x))
137 #define htobe32(x) ((uint32_t)(x))
138 #define htobe64(x) ((uint64_t)(x))
139 #define htole16(x) bswap16((x))
140 #define htole32(x) bswap32((x))
141 #define htole64(x) bswap64((x))
143 #define be16toh(x) ((uint16_t)(x))
144 #define be32toh(x) ((uint32_t)(x))
145 #define be64toh(x) ((uint64_t)(x))
146 #define le16toh(x) bswap16((x))
147 #define le32toh(x) bswap32((x))
148 #define le64toh(x) bswap64((x))
149 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
151 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
153 static __inline uint16_t
154 be16dec(const void *pp)
156 unsigned char const *p = (unsigned char const *)pp;
158 return ((p[0] << 8) | p[1]);
161 static __inline uint32_t
162 be32dec(const void *pp)
164 unsigned char const *p = (unsigned char const *)pp;
166 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
169 static __inline uint64_t
170 be64dec(const void *pp)
172 unsigned char const *p = (unsigned char const *)pp;
174 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
177 static __inline uint16_t
178 le16dec(const void *pp)
180 unsigned char const *p = (unsigned char const *)pp;
182 return ((p[1] << 8) | p[0]);
185 static __inline uint32_t
186 le32dec(const void *pp)
188 unsigned char const *p = (unsigned char const *)pp;
190 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
193 static __inline uint64_t
194 le64dec(const void *pp)
196 unsigned char const *p = (unsigned char const *)pp;
198 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
202 be16enc(void *pp, uint16_t u)
204 unsigned char *p = (unsigned char *)pp;
206 p[0] = (u >> 8) & 0xff;
211 be32enc(void *pp, uint32_t u)
213 unsigned char *p = (unsigned char *)pp;
215 p[0] = (u >> 24) & 0xff;
216 p[1] = (u >> 16) & 0xff;
217 p[2] = (u >> 8) & 0xff;
222 be64enc(void *pp, uint64_t u)
224 unsigned char *p = (unsigned char *)pp;
227 be32enc(p + 4, u & 0xffffffff);
231 le16enc(void *pp, uint16_t u)
233 unsigned char *p = (unsigned char *)pp;
236 p[1] = (u >> 8) & 0xff;
240 le32enc(void *pp, uint32_t u)
242 unsigned char *p = (unsigned char *)pp;
245 p[1] = (u >> 8) & 0xff;
246 p[2] = (u >> 16) & 0xff;
247 p[3] = (u >> 24) & 0xff;
251 le64enc(void *pp, uint64_t u)
253 unsigned char *p = (unsigned char *)pp;
255 le32enc(p, u & 0xffffffff);
256 le32enc(p + 4, u >> 32);
259 #endif /* _COMPAT_ENDIAN_H_ */