sys/endian.h

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 2002 Thomas Moestl <[email protected]>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00015  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00017  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00018  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00019  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00020  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00021  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00022  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00023  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00024  * SUCH DAMAGE.
00025  *
00026  * $FreeBSD: src/sys/sys/endian.h,v 1.6 2003/10/15 20:05:57 obrien Exp $
00027  */
00028 
00029 #ifndef _SYS_ENDIAN_H_
00030 #define _SYS_ENDIAN_H_
00031 
00032 #include <sys/cdefs.h>
00033 #include <sys/_types.h>
00034 #include <machine/endian.h>
00035 
00036 #ifndef _UINT16_T_DECLARED
00037 typedef __uint16_t      uint16_t;
00038 #define _UINT16_T_DECLARED
00039 #endif
00040  
00041 #ifndef _UINT32_T_DECLARED
00042 typedef __uint32_t      uint32_t;
00043 #define _UINT32_T_DECLARED
00044 #endif
00045  
00046 #ifndef _UINT64_T_DECLARED
00047 typedef __uint64_t      uint64_t;
00048 #define _UINT64_T_DECLARED
00049 #endif
00050  
00051 /*
00052  * General byte order swapping functions.
00053  */
00054 #define bswap16(x)      __bswap16(x)
00055 #define bswap32(x)      __bswap32(x)
00056 #define bswap64(x)      __bswap64(x)
00057 
00058 /*
00059  * Host to big endian, host to little endian, big endian to host, and little
00060  * endian to host byte order functions as detailed in byteorder(9).
00061  */
00062 #if _BYTE_ORDER == _LITTLE_ENDIAN
00063 #define htobe16(x)      bswap16((x))
00064 #define htobe32(x)      bswap32((x))
00065 #define htobe64(x)      bswap64((x))
00066 #define htole16(x)      ((uint16_t)(x))
00067 #define htole32(x)      ((uint32_t)(x))
00068 #define htole64(x)      ((uint64_t)(x))
00069 
00070 #define be16toh(x)      bswap16((x))
00071 #define be32toh(x)      bswap32((x))
00072 #define be64toh(x)      bswap64((x))
00073 #define le16toh(x)      ((uint16_t)(x))
00074 #define le32toh(x)      ((uint32_t)(x))
00075 #define le64toh(x)      ((uint64_t)(x))
00076 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
00077 #define htobe16(x)      ((uint16_t)(x))
00078 #define htobe32(x)      ((uint32_t)(x))
00079 #define htobe64(x)      ((uint64_t)(x))
00080 #define htole16(x)      bswap16((x))
00081 #define htole32(x)      bswap32((x))
00082 #define htole64(x)      bswap64((x))
00083 
00084 #define be16toh(x)      ((uint16_t)(x))
00085 #define be32toh(x)      ((uint32_t)(x))
00086 #define be64toh(x)      ((uint64_t)(x))
00087 #define le16toh(x)      bswap16((x))
00088 #define le32toh(x)      bswap32((x))
00089 #define le64toh(x)      bswap64((x))
00090 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
00091 
00092 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
00093 
00094 static __inline uint16_t
00095 be16dec(const void *pp)
00096 {
00097         unsigned char const *p = (unsigned char const *)pp;
00098 
00099         return ((p[0] << 8) | p[1]);
00100 }
00101 
00102 static __inline uint32_t
00103 be32dec(const void *pp)
00104 {
00105         unsigned char const *p = (unsigned char const *)pp;
00106 
00107         return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
00108 }
00109 
00110 static __inline uint64_t
00111 be64dec(const void *pp)
00112 {
00113         unsigned char const *p = (unsigned char const *)pp;
00114 
00115         return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
00116 }
00117 
00118 static __inline uint16_t
00119 le16dec(const void *pp)
00120 {
00121         unsigned char const *p = (unsigned char const *)pp;
00122 
00123         return ((p[1] << 8) | p[0]);
00124 }
00125 
00126 static __inline uint32_t
00127 le32dec(const void *pp)
00128 {
00129         unsigned char const *p = (unsigned char const *)pp;
00130 
00131         return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
00132 }
00133 
00134 static __inline uint64_t
00135 le64dec(const void *pp)
00136 {
00137         unsigned char const *p = (unsigned char const *)pp;
00138 
00139         return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
00140 }
00141 
00142 static __inline void
00143 be16enc(void *pp, uint16_t u)
00144 {
00145         unsigned char *p = (unsigned char *)pp;
00146 
00147         p[0] = (u >> 8) & 0xff;
00148         p[1] = u & 0xff;
00149 }
00150 
00151 static __inline void
00152 be32enc(void *pp, uint32_t u)
00153 {
00154         unsigned char *p = (unsigned char *)pp;
00155 
00156         p[0] = (u >> 24) & 0xff;
00157         p[1] = (u >> 16) & 0xff;
00158         p[2] = (u >> 8) & 0xff;
00159         p[3] = u & 0xff;
00160 }
00161 
00162 static __inline void
00163 be64enc(void *pp, uint64_t u)
00164 {
00165         unsigned char *p = (unsigned char *)pp;
00166 
00167         be32enc(p, u >> 32);
00168         be32enc(p + 4, u & 0xffffffff);
00169 }
00170 
00171 static __inline void
00172 le16enc(void *pp, uint16_t u)
00173 {
00174         unsigned char *p = (unsigned char *)pp;
00175 
00176         p[0] = u & 0xff;
00177         p[1] = (u >> 8) & 0xff;
00178 }
00179 
00180 static __inline void
00181 le32enc(void *pp, uint32_t u)
00182 {
00183         unsigned char *p = (unsigned char *)pp;
00184 
00185         p[0] = u & 0xff;
00186         p[1] = (u >> 8) & 0xff;
00187         p[2] = (u >> 16) & 0xff;
00188         p[3] = (u >> 24) & 0xff;
00189 }
00190 
00191 static __inline void
00192 le64enc(void *pp, uint64_t u)
00193 {
00194         unsigned char *p = (unsigned char *)pp;
00195 
00196         le32enc(p, u & 0xffffffff);
00197         le32enc(p + 4, u >> 32);
00198 }
00199 
00200 #endif  /* _SYS_ENDIAN_H_ */

Copyright © Nokia Corporation 2001-2008
Back to top