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 { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 14 | namespace { |
| 15 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 16 | template <typename int_type, int_type (*function)(int_type)> |
| 17 | static inline void copier(const void *in, void *out) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 18 | // Have confirmed by looking at the assembly code that this generates the same |
| 19 | // code as the (undefined by the c++ standard) way of using a union to do it. |
| 20 | // (which is pretty efficient) |
| 21 | int_type temp; |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 22 | memcpy(&temp, in, sizeof(int_type)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | temp = function(temp); |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 24 | memcpy(out, &temp, sizeof(int_type)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 26 | template <typename int_type, typename T, int_type (*function)(int_type)> |
| 27 | static inline T memcpier(T in) { |
| 28 | copier<int_type, function>(&in, &in); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 29 | return in; |
| 30 | } |
| 31 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 32 | // Needed because be64toh etc are macros (not that the manpage says |
| 33 | // anything...). |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 34 | // These are used instead of ntohs etc because gcc didn't inline those. |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 35 | static inline uint64_t _be64toh(uint64_t in) { return be64toh(in); } |
| 36 | static inline uint64_t _htobe64(uint64_t in) { return htobe64(in); } |
| 37 | static inline uint32_t _be32toh(uint32_t in) { return be32toh(in); } |
| 38 | static inline uint32_t _htobe32(uint32_t in) { return htobe32(in); } |
| 39 | static inline uint16_t _be16toh(uint16_t in) { return be16toh(in); } |
| 40 | static inline uint16_t _htobe16(uint16_t in) { return htobe16(in); } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 41 | |
| 42 | template<int bytes, typename T> class do_ntoh { |
| 43 | public: |
| 44 | static inline T apply(T net); |
| 45 | static inline void copy(const char *in, T &net); |
| 46 | }; |
| 47 | template<typename T> class do_ntoh<1, T> { |
| 48 | public: |
| 49 | static inline T apply(T net) { return net; } |
| 50 | static inline void copy(const char *in, T *host) { host[0] = in[0]; } |
| 51 | }; |
| 52 | template<typename T> class do_ntoh<2, T> { |
| 53 | public: |
| 54 | static inline T apply(T net) { return memcpier<uint16_t, T, _be16toh>(net); } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 55 | static inline void copy(const char *in, T *host) { |
| 56 | copier<uint16_t, _be16toh>(in, host); |
| 57 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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); } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 62 | static inline void copy(const char *in, T *host) { |
| 63 | copier<uint32_t, _be32toh>(in, host); |
| 64 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 65 | }; |
| 66 | template<typename T> class do_ntoh<8, T> { |
| 67 | public: |
| 68 | static inline T apply(T net) { return memcpier<uint64_t, T, _be64toh>(net); } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 69 | static inline void copy(const char *in, T *host) { |
| 70 | copier<uint64_t, _be64toh>(in, host); |
| 71 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 72 | }; |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 73 | |
| 74 | template <size_t Size> |
| 75 | struct do_ntoh_int { |
| 76 | static inline void copy(const char *in, char *host); |
| 77 | }; |
| 78 | template <> |
| 79 | struct do_ntoh_int<1> { |
| 80 | static inline void copy(const char *in, char *host) { host[0] = in[0]; } |
| 81 | }; |
| 82 | template <> |
| 83 | struct do_ntoh_int<2> { |
| 84 | static inline void copy(const char *in, char *host) { |
| 85 | copier<uint16_t, _be16toh>(in, host); |
| 86 | } |
| 87 | }; |
| 88 | template <> |
| 89 | struct do_ntoh_int<4> { |
| 90 | static inline void copy(const char *in, char *host) { |
| 91 | copier<uint32_t, _be32toh>(in, host); |
| 92 | } |
| 93 | }; |
| 94 | template <> |
| 95 | struct do_ntoh_int<8> { |
| 96 | static inline void copy(const char *in, char *host) { |
| 97 | copier<uint64_t, _be64toh>(in, host); |
| 98 | } |
| 99 | }; |
| 100 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 101 | template<int bytes, typename T> class do_hton { |
| 102 | public: |
| 103 | static inline T apply(T host); |
| 104 | static inline void copy(const T *host, char *out); |
| 105 | }; |
| 106 | template<typename T> class do_hton<1, T> { |
| 107 | public: |
| 108 | static inline T apply(T host) { return host; } |
| 109 | static inline void copy(const T *host, char *out) { out[0] = host[0]; } |
| 110 | }; |
| 111 | template<typename T> class do_hton<2, T> { |
| 112 | public: |
| 113 | static inline T apply(T host) { return memcpier<uint16_t, T, _htobe16>(host); } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 114 | static inline void copy(const T *host, char *out) { |
| 115 | copier<uint16_t, _htobe16>(host, out); |
| 116 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | }; |
| 118 | template<typename T> class do_hton<4, T> { |
| 119 | public: |
| 120 | static inline T apply(T host) { return memcpier<uint32_t, T, _htobe32>(host); } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 121 | static inline void copy(const T *host, char *out) { |
| 122 | copier<uint32_t, _htobe32>(host, out); |
| 123 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 124 | }; |
| 125 | template<typename T> class do_hton<8, T> { |
| 126 | public: |
| 127 | static inline T apply(T host) { return memcpier<uint64_t, T, _htobe64>(host); } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 128 | static inline void copy(const T *host, char *out) { |
| 129 | copier<uint64_t, _htobe64>(host, out); |
| 130 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 133 | template <size_t Size> |
| 134 | struct do_hton_int { |
| 135 | static inline void copy(const char *host, char *out); |
| 136 | }; |
| 137 | template <> |
| 138 | struct do_hton_int<1> { |
| 139 | static inline void copy(const char *host, char *out) { out[0] = host[0]; } |
| 140 | }; |
| 141 | template <> |
| 142 | struct do_hton_int<2> { |
| 143 | static inline void copy(const char *host, char *out) { |
| 144 | copier<uint16_t, _htobe16>(host, out); |
| 145 | } |
| 146 | }; |
| 147 | template <> |
| 148 | struct do_hton_int<4> { |
| 149 | static inline void copy(const char *host, char *out) { |
| 150 | copier<uint32_t, _htobe32>(host, out); |
| 151 | } |
| 152 | }; |
| 153 | template <> |
| 154 | struct do_hton_int<8> { |
| 155 | static inline void copy(const char *host, char *out) { |
| 156 | copier<uint64_t, _htobe64>(host, out); |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 161 | |
| 162 | // Converts T from network to host byte order. |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 163 | template <typename T> |
| 164 | static inline T ntoh(T net) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 165 | return do_ntoh<sizeof(net), T>::apply(net); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 166 | } |
| 167 | // Converts T from host to network byte order. |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 168 | template <typename T> |
| 169 | static inline T hton(T host) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 170 | return do_hton<sizeof(host), T>::apply(host); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 173 | template <typename T> |
| 174 | static inline void to_host(const char *input, T *host) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 175 | do_ntoh<sizeof(*host), T>::copy(input, host); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 176 | } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 177 | template <typename T> |
| 178 | static inline void to_network(const T *host, char *output) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 179 | do_hton<sizeof(*host), T>::copy(host, output); |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | template <size_t Size> |
| 183 | static inline void to_host(const char *input, char *host) { |
| 184 | do_ntoh_int<Size>::copy(input, host); |
| 185 | } |
| 186 | template <size_t Size> |
| 187 | static inline void to_network(const char *host, char *output) { |
| 188 | do_hton_int<Size>::copy(host, output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } // namespace aos |
| 192 | |
| 193 | #endif |