FOUND Coverage Report


src/
File: datafile/encoding.hpp
Date: 2025-11-20 21:57:26
Lines:
36/36
100.0%
Functions:
7/7
100.0%
Branches:
0/0
-%

Line Branch Exec Source
1 #ifndef SRC_DATAFILE_ENCODING_HPP_
2 #define SRC_DATAFILE_ENCODING_HPP_
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
16 namespace found {
17
18 /**
19 * @brief Converts a 16-bit integer from host byte order to network byte order.
20 *
21 * @param v The integer to convert
22 *
23 * @return The integer in network byte order.
24 */
25 inline 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
33 /**
34 * @brief Converts a 16-bit integer from network byte order to host byte order.
35 *
36 * @param v The integer to convert
37 *
38 * @return The integer in host byte order.
39 */
40 inline 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
48 /**
49 * @brief Converts a 32-bit integer from host byte order to network byte order.
50 *
51 * @param v The integer to convert
52 *
53 * @return The integer in network byte order.
54 */
55 96 inline uint32_t htonl(uint32_t v) {
56 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
57 96 return ((v & 0xFF000000) >> 24) |
58 96 ((v & 0x00FF0000) >> 8) |
59 96 ((v & 0x0000FF00) << 8) |
60 96 ((v & 0x000000FF) << 24);
61 #else
62 return v;
63 #endif
64 }
65
66 /**
67 * @brief Converts a 32-bit integer from network byte order to host byte order.
68 *
69 * @param v The integer to convert
70 *
71 * @return The integer in host byte order.
72 */
73 133 inline uint32_t ntohl(uint32_t v) {
74 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
75 133 return ((v & 0xFF000000) >> 24) |
76 133 ((v & 0x00FF0000) >> 8) |
77 133 ((v & 0x0000FF00) << 8) |
78 133 ((v & 0x000000FF) << 24);
79 #else
80 return v;
81 #endif
82 }
83
84 /**
85 * @brief Converts a 64-bit integer from host byte order to network byte order.
86 *
87 * @param v The integer to convert
88 *
89 * @return The integer in network byte order.
90 */
91 403 inline uint64_t htonl(uint64_t v) {
92 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
93 403 return ((v & 0xFF00000000000000ULL) >> 56) |
94 403 ((v & 0x00FF000000000000ULL) >> 40) |
95 403 ((v & 0x0000FF0000000000ULL) >> 24) |
96 403 ((v & 0x000000FF00000000ULL) >> 8) |
97 403 ((v & 0x00000000FF000000ULL) << 8) |
98 403 ((v & 0x0000000000FF0000ULL) << 24) |
99 403 ((v & 0x000000000000FF00ULL) << 40) |
100 403 ((v & 0x00000000000000FFULL) << 56);
101 #else
102 return v;
103 #endif
104 }
105
106 /**
107 * @brief Converts a 64-bit integer from network byte order to host byte order.
108 *
109 * @param v The integer to convert
110 *
111 * @return The integer in host byte order.
112 */
113 20 inline uint64_t ntohl(uint64_t v) {
114 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
115 20 return ((v & 0xFF00000000000000ULL) >> 56) |
116 20 ((v & 0x00FF000000000000ULL) >> 40) |
117 20 ((v & 0x0000FF0000000000ULL) >> 24) |
118 20 ((v & 0x000000FF00000000ULL) >> 8) |
119 20 ((v & 0x00000000FF000000ULL) << 8) |
120 20 ((v & 0x0000000000FF0000ULL) << 24) |
121 20 ((v & 0x000000000000FF00ULL) << 40) |
122 20 ((v & 0x00000000000000FFULL) << 56);
123 #else
124 return v;
125 #endif
126 }
127
128 /**
129 * @brief Union for converting a 32-bit floating point number from host byte order to network byte order and vice versa.
130 */
131 union _f_u_ {
132 /// @brief Floating-point variable used for storing a single-precision value.
133 float f;
134 /// @brief Unsigned 32-bit integer used for encoding or serialization purposes.
135 uint32_t u;
136 };
137
138 /**
139 * @brief Union for converting a 64-bit floating point number from host byte order to network byte order and vice versa.
140 */
141 union _d_u_ {
142 /// @brief A variable to store a double-precision floating-point value.
143 double d;
144 /// @brief Unsigned 64-bit integer used for encoding or serialization purposes.
145 uint64_t u;
146 };
147
148 /**
149 * @brief Converts a float from network byte order to host byte order.
150 *
151 * @param v The float value to convert.
152 * @return The converted float value in host byte order.
153 */
154 inline 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
165 /**
166 * @brief Converts a float from network byte order to host byte order.
167 *
168 * @param v The float value to convert.
169 *
170 * @return The converted float value in host byte order.
171 */
172 inline 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
183 /**
184 * @brief Converts a double from network byte order to host byte order.
185 *
186 * @param v The double value to convert.
187 *
188 * @return The converted double value in host byte order.
189 */
190 inline 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
201 /**
202 * @brief Converts a double from host byte order to network byte order.
203 *
204 * @param v The double value to convert.
205 *
206 * @return The converted double value in network byte order.
207 */
208 381 inline double htond(double v) {
209 #if ENDIANESS == __ORDER_LITTLE_ENDIAN__
210 _d_u_ t;
211 381 t.d = v;
212 381 t.u = htonl(t.u);
213 381 return t.d;
214 #else
215 return v;
216 #endif
217 }
218
219 /**
220 * @brief Converts a decimal from host byte order to network byte order.
221 *
222 * @param v The decimal value to convert.
223 *
224 * @return The converted decimal value in network byte order.
225 */
226 162 inline decimal htondec(decimal v) {
227 #ifdef FOUND_FLOAT_MODE
228 return htonf(v);
229 #else
230 162 return htond(v);
231 #endif
232 }
233
234 /**
235 * @brief Converts a decimal from network byte order to host byte order.
236 *
237 * @param v The decimal value to convert.
238 *
239 * @return The converted decimal value in host byte order.
240 */
241 219 inline decimal ntohdec(decimal v) {
242 #ifdef FOUND_FLOAT_MODE
243 return htonf(v);
244 #else
245 219 return htond(v);
246 #endif
247 }
248
249 /**
250 * @brief Calculates the CRC32 checksum for a given data buffer.
251 *
252 * @param data Pointer to the data buffer.
253 * @param length The size of the data buffer in bytes.
254 *
255 * @return The calculated CRC32 checksum.
256 */
257 uint32_t calculateCRC32(const void* data, size_t length);
258
259 } // namespace found
260
261 #endif // SRC_DATAFILE_ENCODING_HPP_
262