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