1/*- 2 * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/sys/endian.h,v 1.6 2003/10/15 20:05:57 obrien Exp $ 27 */ 28#ifndef _FBSD_COMPAT_SYS_ENDIAN_H_ 29#define _FBSD_COMPAT_SYS_ENDIAN_H_ 30 31#include <stdint.h> 32#include <posix/endian.h> 33 34#include <support/ByteOrder.h> 35 36#include <sys/cdefs.h> 37#include <sys/_types.h> 38#include <machine/endian.h> 39 40#define _BYTE_ORDER BYTE_ORDER 41#define _LITTLE_ENDIAN LITTLE_ENDIAN 42#define _BIG_ENDIAN BIG_ENDIAN 43 44#define __bswap16(x) __swap_int16(x) 45#define __bswap32(x) __swap_int32(x) 46#define __bswap64(x) __swap_int64(x) 47 48/* 49 * General byte order swapping functions. 50 */ 51#define bswap16(x) __bswap16(x) 52#define bswap32(x) __bswap32(x) 53#define bswap64(x) __bswap64(x) 54 55/* 56 * Host to big endian, host to little endian, big endian to host, and little 57 * endian to host byte order functions as detailed in byteorder(9). 58 */ 59#if _BYTE_ORDER == _LITTLE_ENDIAN 60#define htobe16(x) bswap16((x)) 61#define htobe32(x) bswap32((x)) 62#define htobe64(x) bswap64((x)) 63#define htole16(x) ((uint16_t)(x)) 64#define htole32(x) ((uint32_t)(x)) 65#define htole64(x) ((uint64_t)(x)) 66 67#define be16toh(x) bswap16((x)) 68#define be32toh(x) bswap32((x)) 69#define be64toh(x) bswap64((x)) 70#define le16toh(x) ((uint16_t)(x)) 71#define le32toh(x) ((uint32_t)(x)) 72#define le64toh(x) ((uint64_t)(x)) 73#else /* _BYTE_ORDER != _LITTLE_ENDIAN */ 74#define htobe16(x) ((uint16_t)(x)) 75#define htobe32(x) ((uint32_t)(x)) 76#define htobe64(x) ((uint64_t)(x)) 77#define htole16(x) bswap16((x)) 78#define htole32(x) bswap32((x)) 79#define htole64(x) bswap64((x)) 80 81#define be16toh(x) ((uint16_t)(x)) 82#define be32toh(x) ((uint32_t)(x)) 83#define be64toh(x) ((uint64_t)(x)) 84#define le16toh(x) bswap16((x)) 85#define le32toh(x) bswap32((x)) 86#define le64toh(x) bswap64((x)) 87#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ 88 89/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ 90 91static __inline uint16_t 92be16dec(const void *pp) 93{ 94 unsigned char const *p = (unsigned char const *)pp; 95 96 return ((p[0] << 8) | p[1]); 97} 98 99static __inline uint32_t 100be32dec(const void *pp) 101{ 102 unsigned char const *p = (unsigned char const *)pp; 103 104 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 105} 106 107static __inline uint64_t 108be64dec(const void *pp) 109{ 110 unsigned char const *p = (unsigned char const *)pp; 111 112 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4)); 113} 114 115static __inline uint16_t 116le16dec(const void *pp) 117{ 118 unsigned char const *p = (unsigned char const *)pp; 119 120 return ((p[1] << 8) | p[0]); 121} 122 123static __inline uint32_t 124le32dec(const void *pp) 125{ 126 unsigned char const *p = (unsigned char const *)pp; 127 128 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 129} 130 131static __inline uint64_t 132le64dec(const void *pp) 133{ 134 unsigned char const *p = (unsigned char const *)pp; 135 136 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p)); 137} 138 139static __inline void 140be16enc(void *pp, uint16_t u) 141{ 142 unsigned char *p = (unsigned char *)pp; 143 144 p[0] = (u >> 8) & 0xff; 145 p[1] = u & 0xff; 146} 147 148static __inline void 149be32enc(void *pp, uint32_t u) 150{ 151 unsigned char *p = (unsigned char *)pp; 152 153 p[0] = (u >> 24) & 0xff; 154 p[1] = (u >> 16) & 0xff; 155 p[2] = (u >> 8) & 0xff; 156 p[3] = u & 0xff; 157} 158 159static __inline void 160be64enc(void *pp, uint64_t u) 161{ 162 unsigned char *p = (unsigned char *)pp; 163 164 be32enc(p, u >> 32); 165 be32enc(p + 4, u & 0xffffffff); 166} 167 168static __inline void 169le16enc(void *pp, uint16_t u) 170{ 171 unsigned char *p = (unsigned char *)pp; 172 173 p[0] = u & 0xff; 174 p[1] = (u >> 8) & 0xff; 175} 176 177static __inline void 178le32enc(void *pp, uint32_t u) 179{ 180 unsigned char *p = (unsigned char *)pp; 181 182 p[0] = u & 0xff; 183 p[1] = (u >> 8) & 0xff; 184 p[2] = (u >> 16) & 0xff; 185 p[3] = (u >> 24) & 0xff; 186} 187 188static __inline void 189le64enc(void *pp, uint64_t u) 190{ 191 unsigned char *p = (unsigned char *)pp; 192 193 le32enc(p, u & 0xffffffff); 194 le32enc(p + 4, u >> 32); 195} 196 197#endif /* _SYS_ENDIAN_H_ */ 198