1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/include/sys/endian.h Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,200 @@
1.4 +/*-
1.5 + * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
1.6 + * All rights reserved.
1.7 + *
1.8 + * Redistribution and use in source and binary forms, with or without
1.9 + * modification, are permitted provided that the following conditions
1.10 + * are met:
1.11 + * 1. Redistributions of source code must retain the above copyright
1.12 + * notice, this list of conditions and the following disclaimer.
1.13 + * 2. Redistributions in binary form must reproduce the above copyright
1.14 + * notice, this list of conditions and the following disclaimer in the
1.15 + * documentation and/or other materials provided with the distribution.
1.16 + *
1.17 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1.18 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.20 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.21 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.22 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.23 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.24 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.25 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.26 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.27 + * SUCH DAMAGE.
1.28 + *
1.29 + * $FreeBSD: src/sys/sys/endian.h,v 1.6 2003/10/15 20:05:57 obrien Exp $
1.30 + */
1.31 +
1.32 +#ifndef _SYS_ENDIAN_H_
1.33 +#define _SYS_ENDIAN_H_
1.34 +
1.35 +#include <sys/cdefs.h>
1.36 +#include <sys/_types.h>
1.37 +#include <stdapis/machine/endian.h>
1.38 +
1.39 +#ifndef _UINT16_T_DECLARED
1.40 +typedef __uint16_t uint16_t;
1.41 +#define _UINT16_T_DECLARED
1.42 +#endif
1.43 +
1.44 +#ifndef _UINT32_T_DECLARED
1.45 +typedef __uint32_t uint32_t;
1.46 +#define _UINT32_T_DECLARED
1.47 +#endif
1.48 +
1.49 +#ifndef _UINT64_T_DECLARED
1.50 +typedef __uint64_t uint64_t;
1.51 +#define _UINT64_T_DECLARED
1.52 +#endif
1.53 +
1.54 +/*
1.55 + * General byte order swapping functions.
1.56 + */
1.57 +#define bswap16(x) __bswap16(x)
1.58 +#define bswap32(x) __bswap32(x)
1.59 +#define bswap64(x) __bswap64(x)
1.60 +
1.61 +/*
1.62 + * Host to big endian, host to little endian, big endian to host, and little
1.63 + * endian to host byte order functions as detailed in byteorder(9).
1.64 + */
1.65 +#if _BYTE_ORDER == _LITTLE_ENDIAN
1.66 +#define htobe16(x) bswap16((x))
1.67 +#define htobe32(x) bswap32((x))
1.68 +#define htobe64(x) bswap64((x))
1.69 +#define htole16(x) ((uint16_t)(x))
1.70 +#define htole32(x) ((uint32_t)(x))
1.71 +#define htole64(x) ((uint64_t)(x))
1.72 +
1.73 +#define be16toh(x) bswap16((x))
1.74 +#define be32toh(x) bswap32((x))
1.75 +#define be64toh(x) bswap64((x))
1.76 +#define le16toh(x) ((uint16_t)(x))
1.77 +#define le32toh(x) ((uint32_t)(x))
1.78 +#define le64toh(x) ((uint64_t)(x))
1.79 +#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
1.80 +#define htobe16(x) ((uint16_t)(x))
1.81 +#define htobe32(x) ((uint32_t)(x))
1.82 +#define htobe64(x) ((uint64_t)(x))
1.83 +#define htole16(x) bswap16((x))
1.84 +#define htole32(x) bswap32((x))
1.85 +#define htole64(x) bswap64((x))
1.86 +
1.87 +#define be16toh(x) ((uint16_t)(x))
1.88 +#define be32toh(x) ((uint32_t)(x))
1.89 +#define be64toh(x) ((uint64_t)(x))
1.90 +#define le16toh(x) bswap16((x))
1.91 +#define le32toh(x) bswap32((x))
1.92 +#define le64toh(x) bswap64((x))
1.93 +#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
1.94 +
1.95 +/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
1.96 +
1.97 +static __inline uint16_t
1.98 +be16dec(const void *pp)
1.99 +{
1.100 + unsigned char const *p = (unsigned char const *)pp;
1.101 +
1.102 + return ((p[0] << 8) | p[1]);
1.103 +}
1.104 +
1.105 +static __inline uint32_t
1.106 +be32dec(const void *pp)
1.107 +{
1.108 + unsigned char const *p = (unsigned char const *)pp;
1.109 +
1.110 + return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1.111 +}
1.112 +
1.113 +static __inline uint64_t
1.114 +be64dec(const void *pp)
1.115 +{
1.116 + unsigned char const *p = (unsigned char const *)pp;
1.117 +
1.118 + return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
1.119 +}
1.120 +
1.121 +static __inline uint16_t
1.122 +le16dec(const void *pp)
1.123 +{
1.124 + unsigned char const *p = (unsigned char const *)pp;
1.125 +
1.126 + return ((p[1] << 8) | p[0]);
1.127 +}
1.128 +
1.129 +static __inline uint32_t
1.130 +le32dec(const void *pp)
1.131 +{
1.132 + unsigned char const *p = (unsigned char const *)pp;
1.133 +
1.134 + return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
1.135 +}
1.136 +
1.137 +static __inline uint64_t
1.138 +le64dec(const void *pp)
1.139 +{
1.140 + unsigned char const *p = (unsigned char const *)pp;
1.141 +
1.142 + return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
1.143 +}
1.144 +
1.145 +static __inline void
1.146 +be16enc(void *pp, uint16_t u)
1.147 +{
1.148 + unsigned char *p = (unsigned char *)pp;
1.149 +
1.150 + p[0] = (u >> 8) & 0xff;
1.151 + p[1] = u & 0xff;
1.152 +}
1.153 +
1.154 +static __inline void
1.155 +be32enc(void *pp, uint32_t u)
1.156 +{
1.157 + unsigned char *p = (unsigned char *)pp;
1.158 +
1.159 + p[0] = (u >> 24) & 0xff;
1.160 + p[1] = (u >> 16) & 0xff;
1.161 + p[2] = (u >> 8) & 0xff;
1.162 + p[3] = u & 0xff;
1.163 +}
1.164 +
1.165 +static __inline void
1.166 +be64enc(void *pp, uint64_t u)
1.167 +{
1.168 + unsigned char *p = (unsigned char *)pp;
1.169 +
1.170 + be32enc(p, u >> 32);
1.171 + be32enc(p + 4, u & 0xffffffff);
1.172 +}
1.173 +
1.174 +static __inline void
1.175 +le16enc(void *pp, uint16_t u)
1.176 +{
1.177 + unsigned char *p = (unsigned char *)pp;
1.178 +
1.179 + p[0] = u & 0xff;
1.180 + p[1] = (u >> 8) & 0xff;
1.181 +}
1.182 +
1.183 +static __inline void
1.184 +le32enc(void *pp, uint32_t u)
1.185 +{
1.186 + unsigned char *p = (unsigned char *)pp;
1.187 +
1.188 + p[0] = u & 0xff;
1.189 + p[1] = (u >> 8) & 0xff;
1.190 + p[2] = (u >> 16) & 0xff;
1.191 + p[3] = (u >> 24) & 0xff;
1.192 +}
1.193 +
1.194 +static __inline void
1.195 +le64enc(void *pp, uint64_t u)
1.196 +{
1.197 + unsigned char *p = (unsigned char *)pp;
1.198 +
1.199 + le32enc(p, u & 0xffffffff);
1.200 + le32enc(p + 4, u >> 32);
1.201 +}
1.202 +
1.203 +#endif /* _SYS_ENDIAN_H_ */