blob: 4a0fd8c81050160a1b080233f99ff47b4d49036b [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_BYTEORDER_H_
2#define AOS_BYTEORDER_H_
brians343bc112013-02-10 01:53:46 +00003
4#ifndef __VXWORKS__
5#include <endian.h> // endian(3)
6#endif
Brian Silvermanf665d692013-02-17 22:11:39 -08007#include <string.h>
Brian Silvermanb691f5e2015-08-02 11:37:55 -07008#include <stdint.h>
brians343bc112013-02-10 01:53:46 +00009
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
14namespace aos {
brians343bc112013-02-10 01:53:46 +000015namespace {
16
Brian Silvermanca20b6b2014-03-19 17:52:33 -070017template <typename int_type, int_type (*function)(int_type)>
18static inline void copier(const void *in, void *out) {
brians343bc112013-02-10 01:53:46 +000019 // 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 Silvermanca20b6b2014-03-19 17:52:33 -070023 memcpy(&temp, in, sizeof(int_type));
brians343bc112013-02-10 01:53:46 +000024 temp = function(temp);
Brian Silvermanca20b6b2014-03-19 17:52:33 -070025 memcpy(out, &temp, sizeof(int_type));
brians343bc112013-02-10 01:53:46 +000026}
Brian Silvermanca20b6b2014-03-19 17:52:33 -070027template <typename int_type, typename T, int_type (*function)(int_type)>
28static inline T memcpier(T in) {
29 copier<int_type, function>(&in, &in);
brians343bc112013-02-10 01:53:46 +000030 return in;
31}
32
Brian Silvermanca20b6b2014-03-19 17:52:33 -070033// Needed because be64toh etc are macros (not that the manpage says
34// anything...).
brians343bc112013-02-10 01:53:46 +000035// These are used instead of ntohs etc because gcc didn't inline those.
Brian Silverman5bf41dc2014-04-21 18:31:45 -070036#ifdef __clang__
37// Apparently the macros use "register", and clang doesn't like that.
38#define register
39#endif
Brian Silvermanca20b6b2014-03-19 17:52:33 -070040static inline uint64_t _be64toh(uint64_t in) { return be64toh(in); }
41static inline uint64_t _htobe64(uint64_t in) { return htobe64(in); }
42static inline uint32_t _be32toh(uint32_t in) { return be32toh(in); }
43static inline uint32_t _htobe32(uint32_t in) { return htobe32(in); }
44static inline uint16_t _be16toh(uint16_t in) { return be16toh(in); }
45static inline uint16_t _htobe16(uint16_t in) { return htobe16(in); }
Brian Silverman5bf41dc2014-04-21 18:31:45 -070046#ifdef __clang__
47#undef register
48#endif
brians343bc112013-02-10 01:53:46 +000049
50template<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};
55template<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};
60template<typename T> class do_ntoh<2, T> {
61 public:
62 static inline T apply(T net) { return memcpier<uint16_t, T, _be16toh>(net); }
Brian Silvermanca20b6b2014-03-19 17:52:33 -070063 static inline void copy(const char *in, T *host) {
64 copier<uint16_t, _be16toh>(in, host);
65 }
brians343bc112013-02-10 01:53:46 +000066};
67template<typename T> class do_ntoh<4, T> {
68 public:
69 static inline T apply(T net) { return memcpier<uint32_t, T, _be32toh>(net); }
Brian Silvermanca20b6b2014-03-19 17:52:33 -070070 static inline void copy(const char *in, T *host) {
71 copier<uint32_t, _be32toh>(in, host);
72 }
brians343bc112013-02-10 01:53:46 +000073};
74template<typename T> class do_ntoh<8, T> {
75 public:
76 static inline T apply(T net) { return memcpier<uint64_t, T, _be64toh>(net); }
Brian Silvermanca20b6b2014-03-19 17:52:33 -070077 static inline void copy(const char *in, T *host) {
78 copier<uint64_t, _be64toh>(in, host);
79 }
brians343bc112013-02-10 01:53:46 +000080};
Brian Silvermanca20b6b2014-03-19 17:52:33 -070081
82template <size_t Size>
83struct do_ntoh_int {
84 static inline void copy(const char *in, char *host);
85};
86template <>
87struct do_ntoh_int<1> {
88 static inline void copy(const char *in, char *host) { host[0] = in[0]; }
89};
90template <>
91struct do_ntoh_int<2> {
92 static inline void copy(const char *in, char *host) {
93 copier<uint16_t, _be16toh>(in, host);
94 }
95};
96template <>
97struct do_ntoh_int<4> {
98 static inline void copy(const char *in, char *host) {
99 copier<uint32_t, _be32toh>(in, host);
100 }
101};
102template <>
103struct do_ntoh_int<8> {
104 static inline void copy(const char *in, char *host) {
105 copier<uint64_t, _be64toh>(in, host);
106 }
107};
108
brians343bc112013-02-10 01:53:46 +0000109template<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};
114template<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};
119template<typename T> class do_hton<2, T> {
120 public:
121 static inline T apply(T host) { return memcpier<uint16_t, T, _htobe16>(host); }
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700122 static inline void copy(const T *host, char *out) {
123 copier<uint16_t, _htobe16>(host, out);
124 }
brians343bc112013-02-10 01:53:46 +0000125};
126template<typename T> class do_hton<4, T> {
127 public:
128 static inline T apply(T host) { return memcpier<uint32_t, T, _htobe32>(host); }
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700129 static inline void copy(const T *host, char *out) {
130 copier<uint32_t, _htobe32>(host, out);
131 }
brians343bc112013-02-10 01:53:46 +0000132};
133template<typename T> class do_hton<8, T> {
134 public:
135 static inline T apply(T host) { return memcpier<uint64_t, T, _htobe64>(host); }
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700136 static inline void copy(const T *host, char *out) {
137 copier<uint64_t, _htobe64>(host, out);
138 }
brians343bc112013-02-10 01:53:46 +0000139};
140
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700141template <size_t Size>
142struct do_hton_int {
143 static inline void copy(const char *host, char *out);
144};
145template <>
146struct do_hton_int<1> {
147 static inline void copy(const char *host, char *out) { out[0] = host[0]; }
148};
149template <>
150struct do_hton_int<2> {
151 static inline void copy(const char *host, char *out) {
152 copier<uint16_t, _htobe16>(host, out);
153 }
154};
155template <>
156struct do_hton_int<4> {
157 static inline void copy(const char *host, char *out) {
158 copier<uint32_t, _htobe32>(host, out);
159 }
160};
161template <>
162struct 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
brians343bc112013-02-10 01:53:46 +0000169
170// Converts T from network to host byte order.
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700171template <typename T>
172static inline T ntoh(T net) {
brians343bc112013-02-10 01:53:46 +0000173 return do_ntoh<sizeof(net), T>::apply(net);
brians343bc112013-02-10 01:53:46 +0000174}
175// Converts T from host to network byte order.
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700176template <typename T>
177static inline T hton(T host) {
brians343bc112013-02-10 01:53:46 +0000178 return do_hton<sizeof(host), T>::apply(host);
brians343bc112013-02-10 01:53:46 +0000179}
180
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700181template <typename T>
182static inline void to_host(const char *input, T *host) {
brians343bc112013-02-10 01:53:46 +0000183 do_ntoh<sizeof(*host), T>::copy(input, host);
brians343bc112013-02-10 01:53:46 +0000184}
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700185template <typename T>
186static inline void to_network(const T *host, char *output) {
brians343bc112013-02-10 01:53:46 +0000187 do_hton<sizeof(*host), T>::copy(host, output);
Brian Silvermanca20b6b2014-03-19 17:52:33 -0700188}
189
190template <size_t Size>
191static inline void to_host(const char *input, char *host) {
192 do_ntoh_int<Size>::copy(input, host);
193}
194template <size_t Size>
195static inline void to_network(const char *host, char *output) {
196 do_hton_int<Size>::copy(host, output);
brians343bc112013-02-10 01:53:46 +0000197}
198
199} // namespace aos
200
201#endif