1155131Srwatson/*- 2155131Srwatson * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org> 3155131Srwatson * Copyright (c) 2005 Robert N. M. Watson 4155131Srwatson * All rights reserved. 5155131Srwatson * 6155131Srwatson * Redistribution and use in source and binary forms, with or without 7155131Srwatson * modification, are permitted provided that the following conditions 8155131Srwatson * are met: 9155131Srwatson * 1. Redistributions of source code must retain the above copyright 10155131Srwatson * notice, this list of conditions and the following disclaimer. 11155131Srwatson * 2. Redistributions in binary form must reproduce the above copyright 12155131Srwatson * notice, this list of conditions and the following disclaimer in the 13155131Srwatson * documentation and/or other materials provided with the distribution. 14155131Srwatson * 15155131Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16155131Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17155131Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18155131Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19155131Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20155131Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21155131Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22155131Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23155131Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24155131Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25155131Srwatson * SUCH DAMAGE. 26155131Srwatson * 27155131Srwatson * Derived from FreeBSD src/sys/sys/endian.h:1.6. 28243750Srwatson * $P4: //depot/projects/trustedbsd/openbsm/compat/endian.h#10 $ 29155131Srwatson */ 30155131Srwatson 31155131Srwatson#ifndef _COMPAT_ENDIAN_H_ 32155131Srwatson#define _COMPAT_ENDIAN_H_ 33155131Srwatson 34155131Srwatson/* 35155131Srwatson * Some systems will have the uint/int types defined here already, others 36155131Srwatson * will need stdint.h. 37155131Srwatson */ 38186647Srwatson#ifdef HAVE_STDINT_H 39155131Srwatson#include <stdint.h> 40186647Srwatson#endif 41155131Srwatson 42155131Srwatson/* 43155131Srwatson * Some operating systems do not yet have the more recent endian APIs that 44155131Srwatson * permit encoding to and decoding from byte streams. For those systems, we 45155131Srwatson * implement local non-optimized versions. 46155131Srwatson */ 47155131Srwatson 48155131Srwatsonstatic __inline uint16_t 49155131Srwatsonbswap16(uint16_t int16) 50155131Srwatson{ 51155131Srwatson const unsigned char *from; 52155131Srwatson unsigned char *to; 53155131Srwatson uint16_t t; 54155131Srwatson 55155131Srwatson from = (const unsigned char *) &int16; 56155131Srwatson to = (unsigned char *) &t; 57155131Srwatson 58155131Srwatson to[0] = from[1]; 59155131Srwatson to[1] = from[0]; 60155131Srwatson 61155131Srwatson return (t); 62155131Srwatson} 63155131Srwatson 64155131Srwatsonstatic __inline uint32_t 65155131Srwatsonbswap32(uint32_t int32) 66155131Srwatson{ 67155131Srwatson const unsigned char *from; 68155131Srwatson unsigned char *to; 69155131Srwatson uint32_t t; 70155131Srwatson 71155131Srwatson from = (const unsigned char *) &int32; 72155131Srwatson to = (unsigned char *) &t; 73155131Srwatson 74155131Srwatson to[0] = from[3]; 75155131Srwatson to[1] = from[2]; 76155131Srwatson to[2] = from[1]; 77155131Srwatson to[3] = from[0]; 78155131Srwatson 79155131Srwatson return (t); 80155131Srwatson} 81155131Srwatson 82155131Srwatsonstatic __inline uint64_t 83155131Srwatsonbswap64(uint64_t int64) 84155131Srwatson{ 85155131Srwatson const unsigned char *from; 86155131Srwatson unsigned char *to; 87155131Srwatson uint64_t t; 88155131Srwatson 89155131Srwatson from = (const unsigned char *) &int64; 90155131Srwatson to = (unsigned char *) &t; 91155131Srwatson 92155131Srwatson to[0] = from[7]; 93155131Srwatson to[1] = from[6]; 94155131Srwatson to[2] = from[5]; 95155131Srwatson to[3] = from[4]; 96155131Srwatson to[4] = from[3]; 97155131Srwatson to[5] = from[2]; 98155131Srwatson to[6] = from[1]; 99155131Srwatson to[7] = from[0]; 100155131Srwatson 101155131Srwatson return (t); 102155131Srwatson} 103155131Srwatson 104155131Srwatson#if defined(BYTE_ORDER) && !defined(_BYTE_ORDER) 105155131Srwatson#define _BYTE_ORDER BYTE_ORDER 106155131Srwatson#endif 107155131Srwatson#if !defined(_BYTE_ORDER) 108155131Srwatson#error "Neither BYTE_ORDER nor _BYTE_ORDER defined" 109155131Srwatson#endif 110155131Srwatson 111155131Srwatson#if defined(BIG_ENDIAN) && !defined(_BIG_ENDIAN) 112155131Srwatson#define _BIG_ENDIAN BIG_ENDIAN 113155131Srwatson#endif 114155131Srwatson 115155131Srwatson#if defined(LITTLE_ENDIAN) && !defined(_LITTLE_ENDIAN) 116155131Srwatson#define _LITTLE_ENDIAN LITTLE_ENDIAN 117155131Srwatson#endif 118155131Srwatson 119243750Srwatson/* XXX: Hack. */ 120243750Srwatson#ifndef htobe16 121155131Srwatson/* 122155131Srwatson * Host to big endian, host to little endian, big endian to host, and little 123155131Srwatson * endian to host byte order functions as detailed in byteorder(9). 124155131Srwatson */ 125155131Srwatson#if _BYTE_ORDER == _LITTLE_ENDIAN 126155131Srwatson#define htobe16(x) bswap16((x)) 127155131Srwatson#define htobe32(x) bswap32((x)) 128155131Srwatson#define htobe64(x) bswap64((x)) 129155131Srwatson#define htole16(x) ((uint16_t)(x)) 130155131Srwatson#define htole32(x) ((uint32_t)(x)) 131155131Srwatson#define htole64(x) ((uint64_t)(x)) 132155131Srwatson 133155131Srwatson#define be16toh(x) bswap16((x)) 134155131Srwatson#define be32toh(x) bswap32((x)) 135155131Srwatson#define be64toh(x) bswap64((x)) 136155131Srwatson#define le16toh(x) ((uint16_t)(x)) 137155131Srwatson#define le32toh(x) ((uint32_t)(x)) 138155131Srwatson#define le64toh(x) ((uint64_t)(x)) 139155131Srwatson#else /* _BYTE_ORDER != _LITTLE_ENDIAN */ 140155131Srwatson#define htobe16(x) ((uint16_t)(x)) 141155131Srwatson#define htobe32(x) ((uint32_t)(x)) 142155131Srwatson#define htobe64(x) ((uint64_t)(x)) 143155131Srwatson#define htole16(x) bswap16((x)) 144155131Srwatson#define htole32(x) bswap32((x)) 145155131Srwatson#define htole64(x) bswap64((x)) 146155131Srwatson 147155131Srwatson#define be16toh(x) ((uint16_t)(x)) 148155131Srwatson#define be32toh(x) ((uint32_t)(x)) 149155131Srwatson#define be64toh(x) ((uint64_t)(x)) 150155131Srwatson#define le16toh(x) bswap16((x)) 151155131Srwatson#define le32toh(x) bswap32((x)) 152155131Srwatson#define le64toh(x) bswap64((x)) 153155131Srwatson#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ 154243750Srwatson#endif 155155131Srwatson 156155131Srwatson#endif /* _COMPAT_ENDIAN_H_ */ 157