FOUND
Loading...
Searching...
No Matches
encoding.hpp
Go to the documentation of this file.
1#ifndef ENCODING_H
2#define ENCODING_H
3
4#include <endian.h>
5#include <stdint.h>
6
7#include "common/decimal.hpp"
8
9#ifdef __BYTE_ORDER__
10 #define ENDIANESS __BYTE_ORDER__
11#else
12 // manual definition
13 #define ENDIANESS __ORDER_LITTLE_ENDIAN__
14#endif
15
16namespace found {
17
25inline uint16_t htons(uint16_t v) {
26#if ENDIANESS == __ORDER_LITTLE_ENDIAN__
27 return (v << 8) | (v >> 8);
28#else
29 return v;
30#endif
31}
32
40inline uint16_t ntohs(uint16_t v) {
41 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
42 return (v << 8) | (v >> 8);
43 #else
44 return v;
45 #endif
46}
47
55inline uint32_t htonl(uint32_t v) {
56 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
57 return ((v & 0xFF000000) >> 24) |
58 ((v & 0x00FF0000) >> 8) |
59 ((v & 0x0000FF00) << 8) |
60 ((v & 0x000000FF) << 24);
61 #else
62 return v;
63 #endif
64}
65
73inline uint32_t ntohl(uint32_t v) {
74 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
75 return ((v & 0xFF000000) >> 24) |
76 ((v & 0x00FF0000) >> 8) |
77 ((v & 0x0000FF00) << 8) |
78 ((v & 0x000000FF) << 24);
79 #else
80 return v;
81 #endif
82}
83
91inline uint64_t htonl(uint64_t v) {
92 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
93 return ((v & 0xFF00000000000000ULL) >> 56) |
94 ((v & 0x00FF000000000000ULL) >> 40) |
95 ((v & 0x0000FF0000000000ULL) >> 24) |
96 ((v & 0x000000FF00000000ULL) >> 8) |
97 ((v & 0x00000000FF000000ULL) << 8) |
98 ((v & 0x0000000000FF0000ULL) << 24) |
99 ((v & 0x000000000000FF00ULL) << 40) |
100 ((v & 0x00000000000000FFULL) << 56);
101 #else
102 return v;
103 #endif
104}
105
113inline uint64_t ntohl(uint64_t v) {
114 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
115 return ((v & 0xFF00000000000000ULL) >> 56) |
116 ((v & 0x00FF000000000000ULL) >> 40) |
117 ((v & 0x0000FF0000000000ULL) >> 24) |
118 ((v & 0x000000FF00000000ULL) >> 8) |
119 ((v & 0x00000000FF000000ULL) << 8) |
120 ((v & 0x0000000000FF0000ULL) << 24) |
121 ((v & 0x000000000000FF00ULL) << 40) |
122 ((v & 0x00000000000000FFULL) << 56);
123 #else
124 return v;
125 #endif
126}
127
131union _f_u_ {
133 float f;
135 uint32_t u;
136};
137
141union _d_u_ {
143 double d;
145 uint64_t u;
146};
147
154inline float htonf(float v) {
155 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
156 _f_u_ t;
157 t.f = v;
158 t.u = htonl(t.u);
159 return t.f;
160 #else
161 return v;
162 #endif
163}
164
172inline float ntohf(float v) {
173 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
174 _f_u_ t;
175 t.f = v;
176 t.u = ntohl(t.u);
177 return t.f;
178 #else
179 return v;
180 #endif
181}
182
190inline double ntohd(double v) {
191 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
192 _d_u_ t;
193 t.d = v;
194 t.u = ntohl(t.u);
195 return t.d;
196 #else
197 return v;
198 #endif
199}
200
208inline double htond(double v) {
209 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
210 _d_u_ t;
211 t.d = v;
212 t.u = htonl(t.u);
213 return t.d;
214 #else
215 return v;
216 #endif
217}
218
227 #ifdef FOUND_FLOAT_MODE
228 return htonf(v);
229 #else
230 return htond(v);
231 #endif
232}
233
242 #ifdef FOUND_FLOAT_MODE
243 return htonf(v);
244 #else
245 return htond(v);
246 #endif
247}
248
257uint32_t calculateCRC32(const void* data, size_t length);
258
259} // namespace found
260
261#endif // SERIALIZATION_H
double decimal
Definition decimal.hpp:15
Definition calibrate.cpp:7
decimal htondec(decimal v)
Converts a decimal from host byte order to network byte order.
Definition encoding.hpp:226
uint16_t htons(uint16_t v)
Converts a 16-bit integer from host byte order to network byte order.
Definition encoding.hpp:25
float htonf(float v)
Converts a float from network byte order to host byte order.
Definition encoding.hpp:154
double htond(double v)
Converts a double from host byte order to network byte order.
Definition encoding.hpp:208
uint32_t ntohl(uint32_t v)
Converts a 32-bit integer from network byte order to host byte order.
Definition encoding.hpp:73
float ntohf(float v)
Converts a float from network byte order to host byte order.
Definition encoding.hpp:172
uint32_t calculateCRC32(const void *data, size_t length)
Calculates the CRC32 checksum for a given data buffer.
Definition serialization.cpp:235
uint32_t htonl(uint32_t v)
Converts a 32-bit integer from host byte order to network byte order.
Definition encoding.hpp:55
decimal ntohdec(decimal v)
Converts a decimal from network byte order to host byte order.
Definition encoding.hpp:241
double ntohd(double v)
Converts a double from network byte order to host byte order.
Definition encoding.hpp:190
uint16_t ntohs(uint16_t v)
Converts a 16-bit integer from network byte order to host byte order.
Definition encoding.hpp:40
Union for converting a 64-bit floating point number from host byte order to network byte order and vi...
Definition encoding.hpp:141
uint64_t u
Unsigned 64-bit integer used for encoding or serialization purposes.
Definition encoding.hpp:145
double d
A variable to store a double-precision floating-point value.
Definition encoding.hpp:143
Union for converting a 32-bit floating point number from host byte order to network byte order and vi...
Definition encoding.hpp:131
uint32_t u
Unsigned 32-bit integer used for encoding or serialization purposes.
Definition encoding.hpp:135
float f
Floating-point variable used for storing a single-precision value.
Definition encoding.hpp:133