00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00053
00054 #define bswap16(x) __bswap16(x)
00055 #define bswap32(x) __bswap32(x)
00056 #define bswap64(x) __bswap64(x)
00057
00058
00059
00060
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
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
00091
00092
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