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.
| 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 * $P4: //depot/projects/trustedbsd/openbsm/compat/endian.h#7 $
| 28 * $P4: //depot/projects/trustedbsd/openbsm/compat/endian.h#8 $
|
29 */ 30 31#ifndef _COMPAT_ENDIAN_H_ 32#define _COMPAT_ENDIAN_H_ 33 34/* 35 * Some systems will have the uint/int types defined here already, others 36 * will need stdint.h. 37 */
| 29 */ 30 31#ifndef _COMPAT_ENDIAN_H_ 32#define _COMPAT_ENDIAN_H_ 33 34/* 35 * Some systems will have the uint/int types defined here already, others 36 * will need stdint.h. 37 */
|
| 38#ifdef HAVE_STDINT_H
|
38#include <stdint.h>
| 39#include <stdint.h>
|
| 40#endif
|
39 40/* 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. 44 */ 45 46static __inline uint16_t 47bswap16(uint16_t int16) 48{ 49 const unsigned char *from; 50 unsigned char *to; 51 uint16_t t; 52 53 from = (const unsigned char *) &int16; 54 to = (unsigned char *) &t; 55 56 to[0] = from[1]; 57 to[1] = from[0]; 58 59 return (t); 60} 61 62static __inline uint32_t 63bswap32(uint32_t int32) 64{ 65 const unsigned char *from; 66 unsigned char *to; 67 uint32_t t; 68 69 from = (const unsigned char *) &int32; 70 to = (unsigned char *) &t; 71 72 to[0] = from[3]; 73 to[1] = from[2]; 74 to[2] = from[1]; 75 to[3] = from[0]; 76 77 return (t); 78} 79 80static __inline uint64_t 81bswap64(uint64_t int64) 82{ 83 const unsigned char *from; 84 unsigned char *to; 85 uint64_t t; 86 87 from = (const unsigned char *) &int64; 88 to = (unsigned char *) &t; 89 90 to[0] = from[7]; 91 to[1] = from[6]; 92 to[2] = from[5]; 93 to[3] = from[4]; 94 to[4] = from[3]; 95 to[5] = from[2]; 96 to[6] = from[1]; 97 to[7] = from[0]; 98 99 return (t); 100} 101 102#if defined(BYTE_ORDER) && !defined(_BYTE_ORDER) 103#define _BYTE_ORDER BYTE_ORDER 104#endif 105#if !defined(_BYTE_ORDER) 106#error "Neither BYTE_ORDER nor _BYTE_ORDER defined" 107#endif 108 109#if defined(BIG_ENDIAN) && !defined(_BIG_ENDIAN) 110#define _BIG_ENDIAN BIG_ENDIAN 111#endif 112 113#if defined(LITTLE_ENDIAN) && !defined(_LITTLE_ENDIAN) 114#define _LITTLE_ENDIAN LITTLE_ENDIAN 115#endif 116 117/* 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). 120 */ 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)) 128 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)) 142 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 */ 150 151/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ 152 153static __inline uint16_t 154be16dec(const void *pp) 155{ 156 unsigned char const *p = (unsigned char const *)pp; 157 158 return ((p[0] << 8) | p[1]); 159} 160 161static __inline uint32_t 162be32dec(const void *pp) 163{ 164 unsigned char const *p = (unsigned char const *)pp; 165 166 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 167} 168 169static __inline uint64_t 170be64dec(const void *pp) 171{ 172 unsigned char const *p = (unsigned char const *)pp; 173 174 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4)); 175} 176 177static __inline uint16_t 178le16dec(const void *pp) 179{ 180 unsigned char const *p = (unsigned char const *)pp; 181 182 return ((p[1] << 8) | p[0]); 183} 184 185static __inline uint32_t 186le32dec(const void *pp) 187{ 188 unsigned char const *p = (unsigned char const *)pp; 189 190 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 191} 192 193static __inline uint64_t 194le64dec(const void *pp) 195{ 196 unsigned char const *p = (unsigned char const *)pp; 197 198 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p)); 199} 200 201static __inline void 202be16enc(void *pp, uint16_t u) 203{ 204 unsigned char *p = (unsigned char *)pp; 205 206 p[0] = (u >> 8) & 0xff; 207 p[1] = u & 0xff; 208} 209 210static __inline void 211be32enc(void *pp, uint32_t u) 212{ 213 unsigned char *p = (unsigned char *)pp; 214 215 p[0] = (u >> 24) & 0xff; 216 p[1] = (u >> 16) & 0xff; 217 p[2] = (u >> 8) & 0xff; 218 p[3] = u & 0xff; 219} 220 221static __inline void 222be64enc(void *pp, uint64_t u) 223{ 224 unsigned char *p = (unsigned char *)pp; 225 226 be32enc(p, u >> 32); 227 be32enc(p + 4, u & 0xffffffff); 228} 229 230static __inline void 231le16enc(void *pp, uint16_t u) 232{ 233 unsigned char *p = (unsigned char *)pp; 234 235 p[0] = u & 0xff; 236 p[1] = (u >> 8) & 0xff; 237} 238 239static __inline void 240le32enc(void *pp, uint32_t u) 241{ 242 unsigned char *p = (unsigned char *)pp; 243 244 p[0] = u & 0xff; 245 p[1] = (u >> 8) & 0xff; 246 p[2] = (u >> 16) & 0xff; 247 p[3] = (u >> 24) & 0xff; 248} 249 250static __inline void 251le64enc(void *pp, uint64_t u) 252{ 253 unsigned char *p = (unsigned char *)pp; 254 255 le32enc(p, u & 0xffffffff); 256 le32enc(p + 4, u >> 32); 257} 258 259#endif /* _COMPAT_ENDIAN_H_ */
| 41 42/* 43 * Some operating systems do not yet have the more recent endian APIs that 44 * permit encoding to and decoding from byte streams. For those systems, we 45 * implement local non-optimized versions. 46 */ 47 48static __inline uint16_t 49bswap16(uint16_t int16) 50{ 51 const unsigned char *from; 52 unsigned char *to; 53 uint16_t t; 54 55 from = (const unsigned char *) &int16; 56 to = (unsigned char *) &t; 57 58 to[0] = from[1]; 59 to[1] = from[0]; 60 61 return (t); 62} 63 64static __inline uint32_t 65bswap32(uint32_t int32) 66{ 67 const unsigned char *from; 68 unsigned char *to; 69 uint32_t t; 70 71 from = (const unsigned char *) &int32; 72 to = (unsigned char *) &t; 73 74 to[0] = from[3]; 75 to[1] = from[2]; 76 to[2] = from[1]; 77 to[3] = from[0]; 78 79 return (t); 80} 81 82static __inline uint64_t 83bswap64(uint64_t int64) 84{ 85 const unsigned char *from; 86 unsigned char *to; 87 uint64_t t; 88 89 from = (const unsigned char *) &int64; 90 to = (unsigned char *) &t; 91 92 to[0] = from[7]; 93 to[1] = from[6]; 94 to[2] = from[5]; 95 to[3] = from[4]; 96 to[4] = from[3]; 97 to[5] = from[2]; 98 to[6] = from[1]; 99 to[7] = from[0]; 100 101 return (t); 102} 103 104#if defined(BYTE_ORDER) && !defined(_BYTE_ORDER) 105#define _BYTE_ORDER BYTE_ORDER 106#endif 107#if !defined(_BYTE_ORDER) 108#error "Neither BYTE_ORDER nor _BYTE_ORDER defined" 109#endif 110 111#if defined(BIG_ENDIAN) && !defined(_BIG_ENDIAN) 112#define _BIG_ENDIAN BIG_ENDIAN 113#endif 114 115#if defined(LITTLE_ENDIAN) && !defined(_LITTLE_ENDIAN) 116#define _LITTLE_ENDIAN LITTLE_ENDIAN 117#endif 118 119/* 120 * Host to big endian, host to little endian, big endian to host, and little 121 * endian to host byte order functions as detailed in byteorder(9). 122 */ 123#if _BYTE_ORDER == _LITTLE_ENDIAN 124#define htobe16(x) bswap16((x)) 125#define htobe32(x) bswap32((x)) 126#define htobe64(x) bswap64((x)) 127#define htole16(x) ((uint16_t)(x)) 128#define htole32(x) ((uint32_t)(x)) 129#define htole64(x) ((uint64_t)(x)) 130 131#define be16toh(x) bswap16((x)) 132#define be32toh(x) bswap32((x)) 133#define be64toh(x) bswap64((x)) 134#define le16toh(x) ((uint16_t)(x)) 135#define le32toh(x) ((uint32_t)(x)) 136#define le64toh(x) ((uint64_t)(x)) 137#else /* _BYTE_ORDER != _LITTLE_ENDIAN */ 138#define htobe16(x) ((uint16_t)(x)) 139#define htobe32(x) ((uint32_t)(x)) 140#define htobe64(x) ((uint64_t)(x)) 141#define htole16(x) bswap16((x)) 142#define htole32(x) bswap32((x)) 143#define htole64(x) bswap64((x)) 144 145#define be16toh(x) ((uint16_t)(x)) 146#define be32toh(x) ((uint32_t)(x)) 147#define be64toh(x) ((uint64_t)(x)) 148#define le16toh(x) bswap16((x)) 149#define le32toh(x) bswap32((x)) 150#define le64toh(x) bswap64((x)) 151#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ 152 153/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ 154 155static __inline uint16_t 156be16dec(const void *pp) 157{ 158 unsigned char const *p = (unsigned char const *)pp; 159 160 return ((p[0] << 8) | p[1]); 161} 162 163static __inline uint32_t 164be32dec(const void *pp) 165{ 166 unsigned char const *p = (unsigned char const *)pp; 167 168 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 169} 170 171static __inline uint64_t 172be64dec(const void *pp) 173{ 174 unsigned char const *p = (unsigned char const *)pp; 175 176 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4)); 177} 178 179static __inline uint16_t 180le16dec(const void *pp) 181{ 182 unsigned char const *p = (unsigned char const *)pp; 183 184 return ((p[1] << 8) | p[0]); 185} 186 187static __inline uint32_t 188le32dec(const void *pp) 189{ 190 unsigned char const *p = (unsigned char const *)pp; 191 192 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 193} 194 195static __inline uint64_t 196le64dec(const void *pp) 197{ 198 unsigned char const *p = (unsigned char const *)pp; 199 200 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p)); 201} 202 203static __inline void 204be16enc(void *pp, uint16_t u) 205{ 206 unsigned char *p = (unsigned char *)pp; 207 208 p[0] = (u >> 8) & 0xff; 209 p[1] = u & 0xff; 210} 211 212static __inline void 213be32enc(void *pp, uint32_t u) 214{ 215 unsigned char *p = (unsigned char *)pp; 216 217 p[0] = (u >> 24) & 0xff; 218 p[1] = (u >> 16) & 0xff; 219 p[2] = (u >> 8) & 0xff; 220 p[3] = u & 0xff; 221} 222 223static __inline void 224be64enc(void *pp, uint64_t u) 225{ 226 unsigned char *p = (unsigned char *)pp; 227 228 be32enc(p, u >> 32); 229 be32enc(p + 4, u & 0xffffffff); 230} 231 232static __inline void 233le16enc(void *pp, uint16_t u) 234{ 235 unsigned char *p = (unsigned char *)pp; 236 237 p[0] = u & 0xff; 238 p[1] = (u >> 8) & 0xff; 239} 240 241static __inline void 242le32enc(void *pp, uint32_t u) 243{ 244 unsigned char *p = (unsigned char *)pp; 245 246 p[0] = u & 0xff; 247 p[1] = (u >> 8) & 0xff; 248 p[2] = (u >> 16) & 0xff; 249 p[3] = (u >> 24) & 0xff; 250} 251 252static __inline void 253le64enc(void *pp, uint64_t u) 254{ 255 unsigned char *p = (unsigned char *)pp; 256 257 le32enc(p, u & 0xffffffff); 258 le32enc(p + 4, u >> 32); 259} 260 261#endif /* _COMPAT_ENDIAN_H_ */
|