195533Smike/*- 295533Smike * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org> 395533Smike * All rights reserved. 495533Smike * 595533Smike * Redistribution and use in source and binary forms, with or without 695533Smike * modification, are permitted provided that the following conditions 795533Smike * are met: 895533Smike * 1. Redistributions of source code must retain the above copyright 995533Smike * notice, this list of conditions and the following disclaimer. 1095533Smike * 2. Redistributions in binary form must reproduce the above copyright 1195533Smike * notice, this list of conditions and the following disclaimer in the 1295533Smike * documentation and/or other materials provided with the distribution. 1395533Smike * 1495533Smike * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1595533Smike * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1695533Smike * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1795533Smike * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1895533Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1995533Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2095533Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2195533Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2295533Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2395533Smike * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2495533Smike * SUCH DAMAGE. 2595533Smike * 2695533Smike * $FreeBSD$ 2795533Smike */ 2895533Smike 2995533Smike#ifndef _SYS_ENDIAN_H_ 3095533Smike#define _SYS_ENDIAN_H_ 3195533Smike 3295533Smike#include <sys/cdefs.h> 3395737Smike#include <sys/_types.h> 3495533Smike#include <machine/endian.h> 3595533Smike 36208331Sphk#ifndef _UINT8_T_DECLARED 37208331Sphktypedef __uint8_t uint8_t; 38208331Sphk#define _UINT8_T_DECLARED 39208331Sphk#endif 40208331Sphk 4195737Smike#ifndef _UINT16_T_DECLARED 4295737Smiketypedef __uint16_t uint16_t; 4395737Smike#define _UINT16_T_DECLARED 4495737Smike#endif 4595737Smike 4695737Smike#ifndef _UINT32_T_DECLARED 4795737Smiketypedef __uint32_t uint32_t; 4895737Smike#define _UINT32_T_DECLARED 4995737Smike#endif 5095737Smike 5195737Smike#ifndef _UINT64_T_DECLARED 5295737Smiketypedef __uint64_t uint64_t; 5395737Smike#define _UINT64_T_DECLARED 5495737Smike#endif 5595737Smike 5695533Smike/* 5795533Smike * General byte order swapping functions. 5895533Smike */ 5995533Smike#define bswap16(x) __bswap16(x) 6095533Smike#define bswap32(x) __bswap32(x) 6195533Smike#define bswap64(x) __bswap64(x) 6295533Smike 6395533Smike/* 6495533Smike * Host to big endian, host to little endian, big endian to host, and little 6595533Smike * endian to host byte order functions as detailed in byteorder(9). 6695533Smike */ 6795533Smike#if _BYTE_ORDER == _LITTLE_ENDIAN 6895533Smike#define htobe16(x) bswap16((x)) 6995533Smike#define htobe32(x) bswap32((x)) 7095533Smike#define htobe64(x) bswap64((x)) 7195737Smike#define htole16(x) ((uint16_t)(x)) 7295737Smike#define htole32(x) ((uint32_t)(x)) 7395737Smike#define htole64(x) ((uint64_t)(x)) 7495533Smike 7595533Smike#define be16toh(x) bswap16((x)) 7695533Smike#define be32toh(x) bswap32((x)) 7795533Smike#define be64toh(x) bswap64((x)) 7895737Smike#define le16toh(x) ((uint16_t)(x)) 7995737Smike#define le32toh(x) ((uint32_t)(x)) 8095737Smike#define le64toh(x) ((uint64_t)(x)) 8195533Smike#else /* _BYTE_ORDER != _LITTLE_ENDIAN */ 8295737Smike#define htobe16(x) ((uint16_t)(x)) 8395737Smike#define htobe32(x) ((uint32_t)(x)) 8495737Smike#define htobe64(x) ((uint64_t)(x)) 8595533Smike#define htole16(x) bswap16((x)) 8695533Smike#define htole32(x) bswap32((x)) 8795533Smike#define htole64(x) bswap64((x)) 8895533Smike 8995737Smike#define be16toh(x) ((uint16_t)(x)) 9095737Smike#define be32toh(x) ((uint32_t)(x)) 9195737Smike#define be64toh(x) ((uint64_t)(x)) 9295533Smike#define le16toh(x) bswap16((x)) 9395533Smike#define le32toh(x) bswap32((x)) 9495533Smike#define le64toh(x) bswap64((x)) 9595533Smike#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ 9695533Smike 97113005Sphk/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ 98113005Sphk 99113005Sphkstatic __inline uint16_t 100113005Sphkbe16dec(const void *pp) 101113005Sphk{ 102208331Sphk uint8_t const *p = (uint8_t const *)pp; 103113005Sphk 104113005Sphk return ((p[0] << 8) | p[1]); 105113005Sphk} 106113005Sphk 107113005Sphkstatic __inline uint32_t 108113005Sphkbe32dec(const void *pp) 109113005Sphk{ 110208331Sphk uint8_t const *p = (uint8_t const *)pp; 111113005Sphk 112208331Sphk return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 113113005Sphk} 114113005Sphk 115113005Sphkstatic __inline uint64_t 116113005Sphkbe64dec(const void *pp) 117113005Sphk{ 118208331Sphk uint8_t const *p = (uint8_t const *)pp; 119113005Sphk 120113005Sphk return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4)); 121113005Sphk} 122113005Sphk 123113005Sphkstatic __inline uint16_t 124113005Sphkle16dec(const void *pp) 125113005Sphk{ 126208331Sphk uint8_t const *p = (uint8_t const *)pp; 127113005Sphk 128113005Sphk return ((p[1] << 8) | p[0]); 129113005Sphk} 130113005Sphk 131113005Sphkstatic __inline uint32_t 132113005Sphkle32dec(const void *pp) 133113005Sphk{ 134208331Sphk uint8_t const *p = (uint8_t const *)pp; 135113005Sphk 136208331Sphk return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 137113005Sphk} 138113005Sphk 139113005Sphkstatic __inline uint64_t 140113005Sphkle64dec(const void *pp) 141113005Sphk{ 142208331Sphk uint8_t const *p = (uint8_t const *)pp; 143113005Sphk 144113009Sphk return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p)); 145113005Sphk} 146113005Sphk 147113005Sphkstatic __inline void 148113005Sphkbe16enc(void *pp, uint16_t u) 149113005Sphk{ 150208331Sphk uint8_t *p = (uint8_t *)pp; 151113005Sphk 152113005Sphk p[0] = (u >> 8) & 0xff; 153113005Sphk p[1] = u & 0xff; 154113005Sphk} 155113005Sphk 156113005Sphkstatic __inline void 157113005Sphkbe32enc(void *pp, uint32_t u) 158113005Sphk{ 159208331Sphk uint8_t *p = (uint8_t *)pp; 160113005Sphk 161113005Sphk p[0] = (u >> 24) & 0xff; 162113005Sphk p[1] = (u >> 16) & 0xff; 163113005Sphk p[2] = (u >> 8) & 0xff; 164113005Sphk p[3] = u & 0xff; 165113005Sphk} 166113005Sphk 167113005Sphkstatic __inline void 168113005Sphkbe64enc(void *pp, uint64_t u) 169113005Sphk{ 170208331Sphk uint8_t *p = (uint8_t *)pp; 171113005Sphk 172208331Sphk be32enc(p, (uint32_t)(u >> 32)); 173208331Sphk be32enc(p + 4, (uint32_t)(u & 0xffffffffU)); 174113005Sphk} 175113005Sphk 176113005Sphkstatic __inline void 177113005Sphkle16enc(void *pp, uint16_t u) 178113005Sphk{ 179208331Sphk uint8_t *p = (uint8_t *)pp; 180113005Sphk 181113005Sphk p[0] = u & 0xff; 182113005Sphk p[1] = (u >> 8) & 0xff; 183113005Sphk} 184113005Sphk 185113005Sphkstatic __inline void 186113005Sphkle32enc(void *pp, uint32_t u) 187113005Sphk{ 188208331Sphk uint8_t *p = (uint8_t *)pp; 189113005Sphk 190113005Sphk p[0] = u & 0xff; 191113005Sphk p[1] = (u >> 8) & 0xff; 192113005Sphk p[2] = (u >> 16) & 0xff; 193113005Sphk p[3] = (u >> 24) & 0xff; 194113005Sphk} 195113005Sphk 196113005Sphkstatic __inline void 197113005Sphkle64enc(void *pp, uint64_t u) 198113005Sphk{ 199208331Sphk uint8_t *p = (uint8_t *)pp; 200113005Sphk 201208331Sphk le32enc(p, (uint32_t)(u & 0xffffffffU)); 202208331Sphk le32enc(p + 4, (uint32_t)(u >> 32)); 203113005Sphk} 204113005Sphk 20595533Smike#endif /* _SYS_ENDIAN_H_ */ 206