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> |
Brian Silverman | b691f5e | 2015-08-02 11:37:55 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
| 10 | // Contains functions for converting between host and network byte order for |
| 11 | // things other than 16/32 bit integers (those are provided by byteorder(3)). |
| 12 | // Also gives a nice templated interface to these functions. |
| 13 | |
| 14 | namespace aos { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 17 | template <typename int_type, int_type (*function)(int_type)> |
| 18 | static inline void copier(const void *in, void *out) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 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; |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 23 | memcpy(&temp, in, sizeof(int_type)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 24 | temp = function(temp); |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 25 | memcpy(out, &temp, sizeof(int_type)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 27 | template <typename int_type, typename T, int_type (*function)(int_type)> |
| 28 | static inline T memcpier(T in) { |
| 29 | copier<int_type, function>(&in, &in); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 30 | return in; |
| 31 | } |
| 32 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 33 | // Needed because be64toh etc are macros (not that the manpage says |
| 34 | // anything...). |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 35 | // These are used instead of ntohs etc because gcc didn't inline those. |
Brian Silverman | 5bf41dc | 2014-04-21 18:31:45 -0700 | [diff] [blame] | 36 | #ifdef __clang__ |
| 37 | // Apparently the macros use "register", and clang doesn't like that. |
| 38 | #define register |
| 39 | #endif |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 40 | static inline uint64_t _be64toh(uint64_t in) { return be64toh(in); } |
| 41 | static inline uint64_t _htobe64(uint64_t in) { return htobe64(in); } |
| 42 | static inline uint32_t _be32toh(uint32_t in) { return be32toh(in); } |
| 43 | static inline uint32_t _htobe32(uint32_t in) { return htobe32(in); } |
| 44 | static inline uint16_t _be16toh(uint16_t in) { return be16toh(in); } |
| 45 | static inline uint16_t _htobe16(uint16_t in) { return htobe16(in); } |
Brian Silverman | 5bf41dc | 2014-04-21 18:31:45 -0700 | [diff] [blame] | 46 | #ifdef __clang__ |
| 47 | #undef register |
| 48 | #endif |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 49 | |
| 50 | template<int bytes, typename T> class do_ntoh { |
| 51 | public: |
| 52 | static inline T apply(T net); |
| 53 | static inline void copy(const char *in, T &net); |
| 54 | }; |
| 55 | template<typename T> class do_ntoh<1, T> { |
| 56 | public: |
| 57 | static inline T apply(T net) { return net; } |
| 58 | static inline void copy(const char *in, T *host) { host[0] = in[0]; } |
| 59 | }; |
| 60 | template<typename T> class do_ntoh<2, T> { |
| 61 | public: |
| 62 | 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] | 63 | static inline void copy(const char *in, T *host) { |
| 64 | copier<uint16_t, _be16toh>(in, host); |
| 65 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 66 | }; |
| 67 | template<typename T> class do_ntoh<4, T> { |
| 68 | public: |
| 69 | 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] | 70 | static inline void copy(const char *in, T *host) { |
| 71 | copier<uint32_t, _be32toh>(in, host); |
| 72 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | }; |
| 74 | template<typename T> class do_ntoh<8, T> { |
| 75 | public: |
| 76 | 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] | 77 | static inline void copy(const char *in, T *host) { |
| 78 | copier<uint64_t, _be64toh>(in, host); |
| 79 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 80 | }; |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 81 | |
| 82 | template <size_t Size> |
| 83 | struct do_ntoh_int { |
| 84 | static inline void copy(const char *in, char *host); |
| 85 | }; |
| 86 | template <> |
| 87 | struct do_ntoh_int<1> { |
| 88 | static inline void copy(const char *in, char *host) { host[0] = in[0]; } |
| 89 | }; |
| 90 | template <> |
| 91 | struct do_ntoh_int<2> { |
| 92 | static inline void copy(const char *in, char *host) { |
| 93 | copier<uint16_t, _be16toh>(in, host); |
| 94 | } |
| 95 | }; |
| 96 | template <> |
| 97 | struct do_ntoh_int<4> { |
| 98 | static inline void copy(const char *in, char *host) { |
| 99 | copier<uint32_t, _be32toh>(in, host); |
| 100 | } |
| 101 | }; |
| 102 | template <> |
| 103 | struct do_ntoh_int<8> { |
| 104 | static inline void copy(const char *in, char *host) { |
| 105 | copier<uint64_t, _be64toh>(in, host); |
| 106 | } |
| 107 | }; |
| 108 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 109 | template<int bytes, typename T> class do_hton { |
| 110 | public: |
| 111 | static inline T apply(T host); |
| 112 | static inline void copy(const T *host, char *out); |
| 113 | }; |
| 114 | template<typename T> class do_hton<1, T> { |
| 115 | public: |
| 116 | static inline T apply(T host) { return host; } |
| 117 | static inline void copy(const T *host, char *out) { out[0] = host[0]; } |
| 118 | }; |
| 119 | template<typename T> class do_hton<2, T> { |
| 120 | public: |
| 121 | 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] | 122 | static inline void copy(const T *host, char *out) { |
| 123 | copier<uint16_t, _htobe16>(host, out); |
| 124 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 125 | }; |
| 126 | template<typename T> class do_hton<4, T> { |
| 127 | public: |
| 128 | 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] | 129 | static inline void copy(const T *host, char *out) { |
| 130 | copier<uint32_t, _htobe32>(host, out); |
| 131 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 132 | }; |
| 133 | template<typename T> class do_hton<8, T> { |
| 134 | public: |
| 135 | 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] | 136 | static inline void copy(const T *host, char *out) { |
| 137 | copier<uint64_t, _htobe64>(host, out); |
| 138 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 141 | template <size_t Size> |
| 142 | struct do_hton_int { |
| 143 | static inline void copy(const char *host, char *out); |
| 144 | }; |
| 145 | template <> |
| 146 | struct do_hton_int<1> { |
| 147 | static inline void copy(const char *host, char *out) { out[0] = host[0]; } |
| 148 | }; |
| 149 | template <> |
| 150 | struct do_hton_int<2> { |
| 151 | static inline void copy(const char *host, char *out) { |
| 152 | copier<uint16_t, _htobe16>(host, out); |
| 153 | } |
| 154 | }; |
| 155 | template <> |
| 156 | struct do_hton_int<4> { |
| 157 | static inline void copy(const char *host, char *out) { |
| 158 | copier<uint32_t, _htobe32>(host, out); |
| 159 | } |
| 160 | }; |
| 161 | template <> |
| 162 | struct do_hton_int<8> { |
| 163 | static inline void copy(const char *host, char *out) { |
| 164 | copier<uint64_t, _htobe64>(host, out); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | } // namespace |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 169 | |
| 170 | // Converts T from network to host byte order. |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 171 | template <typename T> |
| 172 | static inline T ntoh(T net) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 173 | return do_ntoh<sizeof(net), T>::apply(net); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 174 | } |
| 175 | // Converts T from host to network byte order. |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 176 | template <typename T> |
| 177 | static inline T hton(T host) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 178 | return do_hton<sizeof(host), T>::apply(host); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 181 | template <typename T> |
| 182 | static inline void to_host(const char *input, T *host) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 183 | do_ntoh<sizeof(*host), T>::copy(input, host); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 184 | } |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 185 | template <typename T> |
| 186 | static inline void to_network(const T *host, char *output) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 187 | do_hton<sizeof(*host), T>::copy(host, output); |
Brian Silverman | ca20b6b | 2014-03-19 17:52:33 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | template <size_t Size> |
| 191 | static inline void to_host(const char *input, char *host) { |
| 192 | do_ntoh_int<Size>::copy(input, host); |
| 193 | } |
| 194 | template <size_t Size> |
| 195 | static inline void to_network(const char *host, char *output) { |
| 196 | do_hton_int<Size>::copy(host, output); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | } // namespace aos |
| 200 | |
| 201 | #endif |