brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame^] | 1 | #ifndef AOS_COMMON_BYTEORDER_H_ |
| 2 | #define AOS_COMMON_BYTEORDER_H_ |
| 3 | |
| 4 | #ifndef __VXWORKS__ |
| 5 | #include <endian.h> // endian(3) |
| 6 | #endif |
| 7 | |
| 8 | // Contains functions for converting between host and network byte order for |
| 9 | // things other than 16/32 bit integers (those are provided by byteorder(3)). |
| 10 | // Also gives a nice templated interface to these functions. |
| 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | #ifndef __VXWORKS__ |
| 15 | namespace { |
| 16 | |
| 17 | template<typename int_type, typename T, int_type (*function)(int_type)> static inline void copier(const void *in, void *out) { |
| 18 | static_assert(sizeof(T) == sizeof(int_type), "bad template args"); |
| 19 | // Have confirmed by looking at the assembly code that this generates the same |
| 20 | // code as the (undefined by the c++ standard) way of using a union to do it. |
| 21 | // (which is pretty efficient) |
| 22 | int_type temp; |
| 23 | memcpy(&temp, in, sizeof(T)); |
| 24 | temp = function(temp); |
| 25 | memcpy(out, &temp, sizeof(T)); |
| 26 | } |
| 27 | template<typename int_type, typename T, int_type (*function)(int_type)> static inline T memcpier(T in) { |
| 28 | copier<int_type, T, function>(&in, &in); |
| 29 | return in; |
| 30 | } |
| 31 | |
| 32 | // Can't make them static because they have to have external linkage to be used as a |
| 33 | // template parameter. |
| 34 | // Needed because be64toh etc are macros. (not that the manpage says anything...) |
| 35 | // These are used instead of ntohs etc because gcc didn't inline those. |
| 36 | inline uint64_t _be64toh(uint64_t in) { return be64toh(in); } |
| 37 | inline uint64_t _htobe64(uint64_t in) { return htobe64(in); } |
| 38 | inline uint32_t _be32toh(uint32_t in) { return be32toh(in); } |
| 39 | inline uint32_t _htobe32(uint32_t in) { return htobe32(in); } |
| 40 | inline uint16_t _be16toh(uint16_t in) { return be16toh(in); } |
| 41 | inline uint16_t _htobe16(uint16_t in) { return htobe16(in); } |
| 42 | |
| 43 | template<int bytes, typename T> class do_ntoh { |
| 44 | public: |
| 45 | static inline T apply(T net); |
| 46 | static inline void copy(const char *in, T &net); |
| 47 | }; |
| 48 | template<typename T> class do_ntoh<1, T> { |
| 49 | public: |
| 50 | static inline T apply(T net) { return net; } |
| 51 | static inline void copy(const char *in, T *host) { host[0] = in[0]; } |
| 52 | }; |
| 53 | template<typename T> class do_ntoh<2, T> { |
| 54 | public: |
| 55 | static inline T apply(T net) { return memcpier<uint16_t, T, _be16toh>(net); } |
| 56 | static inline void copy(const char *in, T *host) { copier<uint16_t, T, _be16toh>(in, host); } |
| 57 | }; |
| 58 | template<typename T> class do_ntoh<4, T> { |
| 59 | public: |
| 60 | static inline T apply(T net) { return memcpier<uint32_t, T, _be32toh>(net); } |
| 61 | static inline void copy(const char *in, T *host) { copier<uint32_t, T, _be32toh>(in, host); } |
| 62 | }; |
| 63 | template<typename T> class do_ntoh<8, T> { |
| 64 | public: |
| 65 | static inline T apply(T net) { return memcpier<uint64_t, T, _be64toh>(net); } |
| 66 | static inline void copy(const char *in, T *host) { copier<uint64_t, T, _be64toh>(in, host); } |
| 67 | }; |
| 68 | template<int bytes, typename T> class do_hton { |
| 69 | public: |
| 70 | static inline T apply(T host); |
| 71 | static inline void copy(const T *host, char *out); |
| 72 | }; |
| 73 | template<typename T> class do_hton<1, T> { |
| 74 | public: |
| 75 | static inline T apply(T host) { return host; } |
| 76 | static inline void copy(const T *host, char *out) { out[0] = host[0]; } |
| 77 | }; |
| 78 | template<typename T> class do_hton<2, T> { |
| 79 | public: |
| 80 | static inline T apply(T host) { return memcpier<uint16_t, T, _htobe16>(host); } |
| 81 | static inline void copy(const T *host, char *out) { copier<uint16_t, T, _htobe16>(host, out); } |
| 82 | }; |
| 83 | template<typename T> class do_hton<4, T> { |
| 84 | public: |
| 85 | static inline T apply(T host) { return memcpier<uint32_t, T, _htobe32>(host); } |
| 86 | static inline void copy(const T *host, char *out) { copier<uint32_t, T, _htobe32>(host, out); } |
| 87 | }; |
| 88 | template<typename T> class do_hton<8, T> { |
| 89 | public: |
| 90 | static inline T apply(T host) { return memcpier<uint64_t, T, _htobe64>(host); } |
| 91 | static inline void copy(const T *host, char *out) { copier<uint64_t, T, _htobe64>(host, out); } |
| 92 | }; |
| 93 | |
| 94 | } // namespace |
| 95 | #endif // ifndef __VXWORKS__ |
| 96 | |
| 97 | // Converts T from network to host byte order. |
| 98 | template<typename T> static inline T ntoh(T net) { |
| 99 | #ifndef __VXWORKS__ |
| 100 | return do_ntoh<sizeof(net), T>::apply(net); |
| 101 | #else |
| 102 | return net; |
| 103 | #endif |
| 104 | } |
| 105 | // Converts T from host to network byte order. |
| 106 | template<typename T> static inline T hton(T host) { |
| 107 | #ifndef __VXWORKS__ |
| 108 | return do_hton<sizeof(host), T>::apply(host); |
| 109 | #else |
| 110 | return host; |
| 111 | #endif |
| 112 | } |
| 113 | |
| 114 | template<typename T> static inline void to_host(const char *input, T *host) { |
| 115 | #ifndef __VXWORKS__ |
| 116 | do_ntoh<sizeof(*host), T>::copy(input, host); |
| 117 | #else |
| 118 | memcpy(host, input, sizeof(*host)); |
| 119 | #endif |
| 120 | } |
| 121 | template<typename T> static inline void to_network(const T *host, char *output) { |
| 122 | #ifndef __VXWORKS__ |
| 123 | do_hton<sizeof(*host), T>::copy(host, output); |
| 124 | #else |
| 125 | memcpy(output, host, sizeof(*host)); |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | } // namespace aos |
| 130 | |
| 131 | #endif |
| 132 | |