1 #ifndef _ENDIAN_H 2 #define _ENDIAN_H 3 4 #include <features.h> 5 6 #define __NEED_uint16_t 7 #define __NEED_uint32_t 8 #define __NEED_uint64_t 9 10 #include <bits/alltypes.h> 11 12 #define __BIG_ENDIAN 4321 13 #define __LITTLE_ENDIAN 1234 14 #define __PDP_ENDIAN 3412 15 16 #define BIG_ENDIAN __BIG_ENDIAN 17 #define LITTLE_ENDIAN __LITTLE_ENDIAN 18 #define PDP_ENDIAN __PDP_ENDIAN 19 #define BYTE_ORDER __BYTE_ORDER 20 21 #include <stdint.h> 22 __bswap16(uint16_t __x)23static __inline uint16_t __bswap16(uint16_t __x) 24 { 25 return __x<<8 | __x>>8; 26 } 27 __bswap32(uint32_t __x)28static __inline uint32_t __bswap32(uint32_t __x) 29 { 30 return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24; 31 } 32 __bswap64(uint64_t __x)33static __inline uint64_t __bswap64(uint64_t __x) 34 { 35 return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32); 36 } 37 38 #if __BYTE_ORDER == __LITTLE_ENDIAN 39 #define htobe16(x) __bswap16(x) 40 #define be16toh(x) __bswap16(x) 41 #define htobe32(x) __bswap32(x) 42 #define be32toh(x) __bswap32(x) 43 #define htobe64(x) __bswap64(x) 44 #define be64toh(x) __bswap64(x) 45 #define htole16(x) (uint16_t)(x) 46 #define le16toh(x) (uint16_t)(x) 47 #define htole32(x) (uint32_t)(x) 48 #define le32toh(x) (uint32_t)(x) 49 #define htole64(x) (uint64_t)(x) 50 #define le64toh(x) (uint64_t)(x) 51 #else 52 #define htobe16(x) (uint16_t)(x) 53 #define be16toh(x) (uint16_t)(x) 54 #define htobe32(x) (uint32_t)(x) 55 #define be32toh(x) (uint32_t)(x) 56 #define htobe64(x) (uint64_t)(x) 57 #define be64toh(x) (uint64_t)(x) 58 #define htole16(x) __bswap16(x) 59 #define le16toh(x) __bswap16(x) 60 #define htole32(x) __bswap32(x) 61 #define le32toh(x) __bswap32(x) 62 #define htole64(x) __bswap64(x) 63 #define le64toh(x) __bswap64(x) 64 #endif 65 66 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 67 #if __BYTE_ORDER == __LITTLE_ENDIAN 68 #define betoh16(x) __bswap16(x) 69 #define betoh32(x) __bswap32(x) 70 #define betoh64(x) __bswap64(x) 71 #define letoh16(x) (uint16_t)(x) 72 #define letoh32(x) (uint32_t)(x) 73 #define letoh64(x) (uint64_t)(x) 74 #else 75 #define betoh16(x) (uint16_t)(x) 76 #define betoh32(x) (uint32_t)(x) 77 #define betoh64(x) (uint64_t)(x) 78 #define letoh16(x) __bswap16(x) 79 #define letoh32(x) __bswap32(x) 80 #define letoh64(x) __bswap64(x) 81 #endif 82 #endif 83 84 static __inline uint32_t be32dec(const void * pp)85be32dec(const void *pp) 86 { 87 uint8_t const *p = (uint8_t const *)pp; 88 89 return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 90 } 91 92 static __inline void be32enc(void * pp,uint32_t u)93be32enc(void *pp, uint32_t u) 94 { 95 uint8_t *p = (uint8_t *)pp; 96 97 p[0] = (u >> 24) & 0xff; 98 p[1] = (u >> 16) & 0xff; 99 p[2] = (u >> 8) & 0xff; 100 p[3] = u & 0xff; 101 } 102 103 static __inline void be64enc(void * pp,uint64_t u)104be64enc(void *pp, uint64_t u) 105 { 106 uint8_t *p = (uint8_t *)pp; 107 108 be32enc(p, (uint32_t)(u >> 32)); 109 be32enc(p + 4, (uint32_t)(u & 0xffffffffU)); 110 } 111 112 #endif 113