| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef LO_ENDIAN_H |
| 18 |
#define LO_ENDIAN_H |
| 19 |
|
| 20 |
#include <sys/types.h> |
| 21 |
|
| 22 |
|
| 23 |
#if defined(_WIN32) && defined(_MSC_VER) |
| 24 |
#define inline __inline |
| 25 |
#define uint64_t unsigned __int64 |
| 26 |
#define uint32_t unsigned __int32 |
| 27 |
#else |
| 28 |
#include <stdint.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifdef _WIN32 |
| 32 |
#include <winsock2.h> |
| 33 |
#include <ws2tcpip.h> |
| 34 |
#else |
| 35 |
#include <netinet/in.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef __cplusplus |
| 39 |
extern "C" { |
| 40 |
#endif |
| 41 |
|
| 42 |
#define lo_swap16(x) htons(x) |
| 43 |
|
| 44 |
#define lo_swap32(x) htonl(x) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#ifndef lo_swap16 |
| 49 |
#define lo_swap16(x) \ |
| 50 |
({ \ |
| 51 |
uint16_t __x = (x); \ |
| 52 |
((uint16_t)( \ |
| 53 |
(((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \ |
| 54 |
(((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \ |
| 55 |
}) |
| 56 |
#warning USING UNOPTIMISED ENDIAN STUFF |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef lo_swap32 |
| 60 |
#define lo_swap32(x) \ |
| 61 |
({ \ |
| 62 |
uint32_t __x = (x); \ |
| 63 |
((uint32_t)( \ |
| 64 |
(((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ |
| 65 |
(((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ |
| 66 |
(((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ |
| 67 |
(((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ |
| 68 |
}) |
| 69 |
#endif |
| 70 |
|
| 71 |
#if 0 |
| 72 |
#ifndef lo_swap64 |
| 73 |
#define lo_swap64(x) \ |
| 74 |
({ \ |
| 75 |
uint64_t __x = (x); \ |
| 76 |
((uint64_t)( \ |
| 77 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
| 78 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
| 79 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
| 80 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
| 81 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
| 82 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
| 83 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
| 84 |
(uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \ |
| 85 |
}) |
| 86 |
#endif |
| 87 |
#else |
| 88 |
|
| 89 |
typedef union { |
| 90 |
uint64_t all; |
| 91 |
struct { |
| 92 |
uint32_t a; |
| 93 |
uint32_t b; |
| 94 |
} part; |
| 95 |
} lo_split64; |
| 96 |
|
| 97 |
static inline uint64_t lo_swap64(uint64_t x) |
| 98 |
{ |
| 99 |
lo_split64 in, out; |
| 100 |
|
| 101 |
in.all = x; |
| 102 |
out.part.a = lo_swap32(in.part.b); |
| 103 |
out.part.b = lo_swap32(in.part.a); |
| 104 |
|
| 105 |
return out.all; |
| 106 |
} |
| 107 |
#endif |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
#if 0 |
| 112 |
#define lo_htoo16(x) (x) |
| 113 |
#define lo_htoo32(x) (x) |
| 114 |
#define lo_htoo64(x) (x) |
| 115 |
#define lo_otoh16(x) (x) |
| 116 |
#define lo_otoh32(x) (x) |
| 117 |
#define lo_otoh64(x) (x) |
| 118 |
#else |
| 119 |
#define lo_htoo16 lo_swap16 |
| 120 |
#define lo_htoo32 lo_swap32 |
| 121 |
#define lo_htoo64 lo_swap64 |
| 122 |
#define lo_otoh16 lo_swap16 |
| 123 |
#define lo_otoh32 lo_swap32 |
| 124 |
#define lo_otoh64 lo_swap64 |
| 125 |
#endif |
| 126 |
|
| 127 |
#ifdef __cplusplus |
| 128 |
} |
| 129 |
#endif |
| 130 |
|
| 131 |
|
| 132 |
#if defined(_WIN32) && defined(_MSC_VER) |
| 133 |
#undef inline |
| 134 |
#undef uint64_t |
| 135 |
#undef uint32_t |
| 136 |
#endif |
| 137 |
|
| 138 |
#endif |
| 139 |
|
| 140 |
|