00001
00002
00003
00004 #ifndef __BYTEORDER_H
00005 #define __BYTEORDER_H
00006
00007
00008 #include <arpa/inet.h>
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef htobe16
00029 # define htobe16(x) htons(x)
00030 #endif
00031 #ifndef htobe32
00032 # define htobe32(x) htonl(x)
00033 #endif
00034 #ifndef be16toh
00035 # define be16toh(x) ntohs(x)
00036 #endif
00037 #ifndef be32toh
00038 # define be32toh(x) ntohl(x)
00039 #endif
00040
00041 #define HTOBE16(x) (x) = htobe16(x)
00042 #define HTOBE32(x) (x) = htobe32(x)
00043 #define BE32TOH(x) (x) = be32toh(x)
00044 #define BE16TOH(x) (x) = be16toh(x)
00045
00046
00047 #ifndef htole16
00048 # define htole16(x) swap16(x)
00049 #endif
00050 #ifndef htole32
00051 # define htole32(x) swap32(x)
00052 #endif
00053 #ifndef le16toh
00054 # define le16toh(x) swap16(x)
00055 #endif
00056 #ifndef le32toh
00057 # define le32toh(x) swap32(x)
00058 #endif
00059 #ifndef le64toh
00060 # define le64toh(x) swap64(x)
00061 #endif
00062
00063 #ifndef htobe64
00064 # define htobe64(x) (x)
00065 #endif
00066 #ifndef be64toh
00067 # define be64toh(x) (x)
00068 #endif
00069
00070 #define HTOLE16(x) (x) = htole16(x)
00071 #define HTOLE32(x) (x) = htole32(x)
00072 #define LE16TOH(x) (x) = le16toh(x)
00073 #define LE32TOH(x) (x) = le32toh(x)
00074 #define LE64TOH(x) (x) = le64toh(x)
00075
00076 #define HTOBE64(x) (void) (x)
00077 #define BE64TOH(x) (void) (x)
00078
00079
00080 #include <_stdint.h>
00081
00082
00083
00084
00085
00086
00087
00088
00089 #define be16atoh(x) ((uint16_t)(((x)[0]<<8)|(x)[1]))
00090 #define be32atoh(x) ((uint32_t)(((x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]))
00091 #define be64atoh_x(x,off,shift) (((uint64_t)((x)[off]))<<shift)
00092 #define be64atoh(x) ((uint64_t)(be64atoh_x(x,0,56)|be64atoh_x(x,1,48)|be64atoh_x(x,2,40)| \
00093 be64atoh_x(x,3,32)|be64atoh_x(x,4,24)|be64atoh_x(x,5,16)|be64atoh_x(x,6,8)|((x)[7])))
00094 #define le16atoh(x) ((uint16_t)(((x)[1]<<8)|(x)[0]))
00095 #define le32atoh(x) ((uint32_t)(((x)[3]<<24)|((x)[2]<<16)|((x)[1]<<8)|(x)[0]))
00096 #define le64atoh_x(x,off,shift) (((uint64_t)(x)[off])<<shift)
00097 #define le64atoh(x) ((uint64_t)(le64atoh_x(x,7,56)|le64atoh_x(x,6,48)|le64atoh_x(x,5,40)| \
00098 le64atoh_x(x,4,32)|le64atoh_x(x,3,24)|le64atoh_x(x,2,16)|le64atoh_x(x,1,8)|((x)[0])))
00099
00100 #define htobe16a(a,x) (a)[0]=(uint8_t)((x)>>8), (a)[1]=(uint8_t)(x)
00101 #define htobe32a(a,x) (a)[0]=(uint8_t)((x)>>24), (a)[1]=(uint8_t)((x)>>16), \
00102 (a)[2]=(uint8_t)((x)>>8), (a)[3]=(uint8_t)(x)
00103 #define htobe64a(a,x) (a)[0]=(uint8_t)((x)>>56), (a)[1]=(uint8_t)((x)>>48), \
00104 (a)[2]=(uint8_t)((x)>>40), (a)[3]=(uint8_t)((x)>>32), \
00105 (a)[4]=(uint8_t)((x)>>24), (a)[5]=(uint8_t)((x)>>16), \
00106 (a)[6]=(uint8_t)((x)>>8), (a)[7]=(uint8_t)(x)
00107 #define htole16a(a,x) (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
00108 #define htole32a(a,x) (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
00109 (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
00110 #define htole64a(a,x) (a)[7]=(uint8_t)((x)>>56), (a)[6]=(uint8_t)((x)>>48), \
00111 (a)[5]=(uint8_t)((x)>>40), (a)[4]=(uint8_t)((x)>>32), \
00112 (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
00113 (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
00114
00115 #endif