| Wireshark 4.7.0 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
pint.h
Go to the documentation of this file.
1
13#ifndef __PINT_H__
14#define __PINT_H__
15
16#include <inttypes.h>
17
18#include <glib.h>
19
20/* Routines that take a possibly-unaligned pointer to a 16-bit, 24-bit,
21 * 32-bit, 40-bit, ... 64-bit integral quantity, in a particular byte
22 * order, and fetch the value and return it in host byte order.
23 *
24 * The pntohuN() routines fetch big-endian unsigned values; the pletohuN()
25 * routines fetch little-endian unsigned values.
26 */
27
28/* On most architectures, accesses of 16, 32, and 64 bit quantities can be
29 * heavily optimized. gcc and clang recognize portable versions below and,
30 * at -Os and higher, optimize them appropriately (for gcc, that includes
31 * for z/Architecture, PPC64, MIPS, etc.). Older versions don't do as good
32 * of a job with 16 bit accesses, though.
33 *
34 * Unfortunately, MSVC and icc (both the "classic" version and the new
35 * LLVM-based Intel C Compiler) do not, according to Matt Godbolt's Compiler
36 * Explorer (https://godbolt.org) as of the end of 2022. They *do* recognize
37 * and optimize a memcpy based approach (which avoids unaligned accesses on,
38 * say, ARM32), though that requires byteswapping appropriately.
39 */
40
41#if (defined(_MSC_VER) && !defined(__clang__)) || defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
42/* MSVC or Intel C Compiler (Classic or new LLVM version), but not
43 * clang-cl on Windows.
44 */
45/* Unfortunately, C23 did not fully accept the N3022 Modern Bit Utilities
46 * proposal, so a standard bytereverse function has been deferred for some
47 * future version:
48 * https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3048.htm
49 * https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3022.htm
50 *
51 * So choose byteswap intrinsics we know we have.
52 */
53#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__INTEL_LLVM_COMPILER) && !defined(__clang__)
54/* Intel and clang-cl both define _MSC_VER when compiling on Windows for
55 * greater compatibility (just as they define __GNUC__ on other platforms).
56 * However, at least on some versions, while including the MSVC <stdlib.h>
57 * provides access to the _byteswap_ intrinsics, they are not actually
58 * optimized into a single x86 BSWAP function, unlike the gcc-style intrinsics
59 * (which both support.) See: https://stackoverflow.com/q/72327906
60 */
61#include <stdlib.h> // For MSVC _byteswap intrinsics
62#define pint_bswap16(x) _byteswap_ushort(x)
63#define pint_bswap32(x) _byteswap_ulong(x)
64/* Hopefully MSVC never decides that a long is 64 bit. */
65#define pint_bswap64(x) _byteswap_uint64(x)
66#elif defined(__INTEL_COMPILER)
67/* The (deprecated) Intel C++ Compiler Classic has these byteswap intrinsics.
68 * It also has the GCC-style intrinsics, though __builtin_bswap16 wasn't
69 * added until some point after icc 13.0 but at least by 16.0, reflecting
70 * that it wasn't added to gcc until 4.8.
71 */
72#define pint_bswap16(x) _bswap16(x)
73#define pint_bswap32(x) _bswap32(x)
74#define pint_bswap64(x) _bswap64(x)
75#else
76/* GCC-style _bswap intrinsics */
77/* The new LLVM-based Intel C++ Compiler doesn't have the above intrinsics,
78 * but it always has all the GCC intrinsics.
79 */
80/* __builtin_bswap32 and __builtin_bswap64 intrinsics have been supported
81 * for a long time on gcc (4.1), and clang (pre 3.0), versions that predate
82 * C11 and C+11 support, which we require, so we could assume we have them.
83 *
84 * __builtin_bswap16 was added a bit later, gcc 4.8, and clang 3.2. While
85 * those versions or later are required for full C11 and C++11 support,
86 * some earlier versions claim to support C11 and C++11 in ways that might
87 * allow them to get past CMake. We don't use this codepath for those
88 * compilers because they heavily optimize the portable versions, though.
89 */
90#define pint_bswap16(x) __builtin_bswap16(x)
91#define pint_bswap32(x) __builtin_bswap32(x)
92#define pint_bswap64(x) __builtin_bswap64(x)
93#endif
94
95static inline uint16_t pntohu16(const void *p)
96{
97 uint16_t ret;
98 memcpy(&ret, p, sizeof(ret));
99#if G_BYTE_ORDER == G_LITTLE_ENDIAN
100 ret = pint_bswap16(ret);
101#endif
102 return ret;
103}
104
105static inline uint32_t pntohu32(const void *p)
106{
107 uint32_t ret;
108 memcpy(&ret, p, sizeof(ret));
109#if G_BYTE_ORDER == G_LITTLE_ENDIAN
110 ret = pint_bswap32(ret);
111#endif
112 return ret;
113}
114
115static inline uint64_t pntohu64(const void *p)
116{
117 uint64_t ret;
118 memcpy(&ret, p, sizeof(ret));
119#if G_BYTE_ORDER == G_LITTLE_ENDIAN
120 ret = pint_bswap64(ret);
121#endif
122 return ret;
123}
124
125static inline uint16_t pletohu16(const void *p)
126{
127 uint16_t ret;
128 memcpy(&ret, p, sizeof(ret));
129#if G_BYTE_ORDER == G_BIG_ENDIAN
130 ret = pint_bswap16(ret);
131#endif
132 return ret;
133}
134
135static inline uint32_t pletohu32(const void *p)
136{
137 uint32_t ret;
138 memcpy(&ret, p, sizeof(ret));
139#if G_BYTE_ORDER == G_BIG_ENDIAN
140 ret = pint_bswap32(ret);
141#endif
142 return ret;
143}
144
145static inline uint64_t pletohu64(const void *p)
146{
147 uint64_t ret;
148 memcpy(&ret, p, sizeof(ret));
149#if G_BYTE_ORDER == G_BIG_ENDIAN
150 ret = pint_bswap64(ret);
151#endif
152 return ret;
153}
154
155static inline void phtonu16(uint8_t *p, uint16_t v)
156{
157#if G_BYTE_ORDER == G_LITTLE_ENDIAN
158 v = pint_bswap16(v);
159#endif
160 memcpy(p, &v, sizeof(v));
161}
162
163static inline void phtonu32(uint8_t *p, uint32_t v)
164{
165#if G_BYTE_ORDER == G_LITTLE_ENDIAN
166 v = pint_bswap32(v);
167#endif
168 memcpy(p, &v, sizeof(v));
169}
170
171static inline void phtonu64(uint8_t *p, uint64_t v) {
172#if G_BYTE_ORDER == G_LITTLE_ENDIAN
173 v = pint_bswap64(v);
174#endif
175 memcpy(p, &v, sizeof(v));
176}
177
178static inline void phtoleu16(uint8_t *p, uint32_t v)
179{
180#if G_BYTE_ORDER == G_BIG_ENDIAN
181 v = pint_bswap16(v);
182#endif
183 memcpy(p, &v, sizeof(v));
184}
185
186static inline void phtoleu32(uint8_t *p, uint32_t v)
187{
188#if G_BYTE_ORDER == G_BIG_ENDIAN
189 v = pint_bswap32(v);
190#endif
191 memcpy(p, &v, sizeof(v));
192}
193
194static inline void phtoleu64(uint8_t *p, uint64_t v) {
195#if G_BYTE_ORDER == G_BIG_ENDIAN
196 v = pint_bswap64(v);
197#endif
198 memcpy(p, &v, sizeof(v));
199}
200
201#else
202/* Portable functions */
203static inline uint16_t pntohu16(const void *p)
204{
205 return (uint16_t)*((const uint8_t *)(p)+0)<<8|
206 (uint16_t)*((const uint8_t *)(p)+1)<<0;
207}
208
209static inline uint32_t pntohu32(const void *p)
210{
211 return (uint32_t)*((const uint8_t *)(p)+0)<<24|
212 (uint32_t)*((const uint8_t *)(p)+1)<<16|
213 (uint32_t)*((const uint8_t *)(p)+2)<<8|
214 (uint32_t)*((const uint8_t *)(p)+3)<<0;
215}
216
217static inline uint64_t pntohu64(const void *p)
218{
219 return (uint64_t)*((const uint8_t *)(p)+0)<<56|
220 (uint64_t)*((const uint8_t *)(p)+1)<<48|
221 (uint64_t)*((const uint8_t *)(p)+2)<<40|
222 (uint64_t)*((const uint8_t *)(p)+3)<<32|
223 (uint64_t)*((const uint8_t *)(p)+4)<<24|
224 (uint64_t)*((const uint8_t *)(p)+5)<<16|
225 (uint64_t)*((const uint8_t *)(p)+6)<<8|
226 (uint64_t)*((const uint8_t *)(p)+7)<<0;
227}
228
229static inline uint16_t pletohu16(const void *p)
230{
231 return (uint16_t)*((const uint8_t *)(p)+1)<<8|
232 (uint16_t)*((const uint8_t *)(p)+0)<<0;
233}
234
235static inline uint32_t pletohu32(const void *p)
236{
237 return (uint32_t)*((const uint8_t *)(p)+3)<<24|
238 (uint32_t)*((const uint8_t *)(p)+2)<<16|
239 (uint32_t)*((const uint8_t *)(p)+1)<<8|
240 (uint32_t)*((const uint8_t *)(p)+0)<<0;
241}
242
243static inline uint64_t pletohu64(const void *p)
244{
245 return (uint64_t)*((const uint8_t *)(p)+7)<<56|
246 (uint64_t)*((const uint8_t *)(p)+6)<<48|
247 (uint64_t)*((const uint8_t *)(p)+5)<<40|
248 (uint64_t)*((const uint8_t *)(p)+4)<<32|
249 (uint64_t)*((const uint8_t *)(p)+3)<<24|
250 (uint64_t)*((const uint8_t *)(p)+2)<<16|
251 (uint64_t)*((const uint8_t *)(p)+1)<<8|
252 (uint64_t)*((const uint8_t *)(p)+0)<<0;
253}
254
255/* Pointer routines to put items out in a particular byte order.
256 * These will work regardless of the byte alignment of the pointer.
257 */
258
259static inline void phtonu16(uint8_t *p, uint16_t v)
260{
261 p[0] = (uint8_t)(v >> 8);
262 p[1] = (uint8_t)(v >> 0);
263}
264
265static inline void phtonu32(uint8_t *p, uint32_t v)
266{
267 p[0] = (uint8_t)(v >> 24);
268 p[1] = (uint8_t)(v >> 16);
269 p[2] = (uint8_t)(v >> 8);
270 p[3] = (uint8_t)(v >> 0);
271}
272
273static inline void phtonu64(uint8_t *p, uint64_t v) {
274 p[0] = (uint8_t)(v >> 56);
275 p[1] = (uint8_t)(v >> 48);
276 p[2] = (uint8_t)(v >> 40);
277 p[3] = (uint8_t)(v >> 32);
278 p[4] = (uint8_t)(v >> 24);
279 p[5] = (uint8_t)(v >> 16);
280 p[6] = (uint8_t)(v >> 8);
281 p[7] = (uint8_t)(v >> 0);
282}
283
284static inline void phtoleu16(uint8_t *p, uint16_t v)
285{
286 p[0] = (uint8_t)(v >> 0);
287 p[1] = (uint8_t)(v >> 8);
288}
289
290static inline void phtoleu32(uint8_t *p, uint32_t v) {
291 p[0] = (uint8_t)(v >> 0);
292 p[1] = (uint8_t)(v >> 8);
293 p[2] = (uint8_t)(v >> 16);
294 p[3] = (uint8_t)(v >> 24);
295}
296
297static inline void phtoleu64(uint8_t *p, uint64_t v) {
298 p[0] = (uint8_t)(v >> 0);
299 p[1] = (uint8_t)(v >> 8);
300 p[2] = (uint8_t)(v >> 16);
301 p[3] = (uint8_t)(v >> 24);
302 p[4] = (uint8_t)(v >> 32);
303 p[5] = (uint8_t)(v >> 40);
304 p[6] = (uint8_t)(v >> 48);
305 p[7] = (uint8_t)(v >> 56);
306}
307#endif
308
309/*
310 * Single-byte versions, for completeness.
311 */
312static inline uint8_t pntohu8(const void *p)
313{
314 return *((const uint8_t *)(p)+0)<<0;
315}
316
317static inline uint8_t pletohu8(const void *p)
318{
319 return *((const uint8_t *)(p)+0)<<0;
320}
321
322static inline void phtonu8(uint8_t *p, uint8_t v)
323{
324 p[0] = (uint8_t)((v) >> 0);
325}
326
327static inline void phtoleu8(uint8_t *p, uint8_t v)
328{
329 p[0] = (uint8_t)((v) >> 0);
330}
331
332/*
333 * Non-power-of-2 field sizes; do these a byte at a time, and let the
334 * compiler optimize them to combinations of power-of-2 operations if
335 * possible.
336 */
337static inline uint32_t pntohu24(const void *p)
338{
339 return (uint32_t)*((const uint8_t *)(p)+0)<<16|
340 (uint32_t)*((const uint8_t *)(p)+1)<<8|
341 (uint32_t)*((const uint8_t *)(p)+2)<<0;
342}
343
344static inline uint64_t pntohu40(const void *p)
345{
346 return (uint64_t)*((const uint8_t *)(p)+0)<<32|
347 (uint64_t)*((const uint8_t *)(p)+1)<<24|
348 (uint64_t)*((const uint8_t *)(p)+2)<<16|
349 (uint64_t)*((const uint8_t *)(p)+3)<<8|
350 (uint64_t)*((const uint8_t *)(p)+4)<<0;
351}
352
353static inline uint64_t pntohu48(const void *p)
354{
355 return (uint64_t)*((const uint8_t *)(p)+0)<<40|
356 (uint64_t)*((const uint8_t *)(p)+1)<<32|
357 (uint64_t)*((const uint8_t *)(p)+2)<<24|
358 (uint64_t)*((const uint8_t *)(p)+3)<<16|
359 (uint64_t)*((const uint8_t *)(p)+4)<<8|
360 (uint64_t)*((const uint8_t *)(p)+5)<<0;
361}
362
363static inline uint64_t pntohu56(const void *p)
364{
365 return (uint64_t)*((const uint8_t *)(p)+0)<<48|
366 (uint64_t)*((const uint8_t *)(p)+1)<<40|
367 (uint64_t)*((const uint8_t *)(p)+2)<<32|
368 (uint64_t)*((const uint8_t *)(p)+3)<<24|
369 (uint64_t)*((const uint8_t *)(p)+4)<<16|
370 (uint64_t)*((const uint8_t *)(p)+5)<<8|
371 (uint64_t)*((const uint8_t *)(p)+6)<<0;
372}
373
374static inline uint32_t pletohu24(const void *p)
375{
376 return (uint32_t)*((const uint8_t *)(p)+2)<<16|
377 (uint32_t)*((const uint8_t *)(p)+1)<<8|
378 (uint32_t)*((const uint8_t *)(p)+0)<<0;
379}
380
381static inline uint64_t pletohu40(const void *p)
382{
383 return (uint64_t)*((const uint8_t *)(p)+4)<<32|
384 (uint64_t)*((const uint8_t *)(p)+3)<<24|
385 (uint64_t)*((const uint8_t *)(p)+2)<<16|
386 (uint64_t)*((const uint8_t *)(p)+1)<<8|
387 (uint64_t)*((const uint8_t *)(p)+0)<<0;
388}
389
390static inline uint64_t pletohu48(const void *p)
391{
392 return (uint64_t)*((const uint8_t *)(p)+5)<<40|
393 (uint64_t)*((const uint8_t *)(p)+4)<<32|
394 (uint64_t)*((const uint8_t *)(p)+3)<<24|
395 (uint64_t)*((const uint8_t *)(p)+2)<<16|
396 (uint64_t)*((const uint8_t *)(p)+1)<<8|
397 (uint64_t)*((const uint8_t *)(p)+0)<<0;
398}
399
400static inline uint64_t pletohu56(const void *p)
401{
402 return (uint64_t)*((const uint8_t *)(p)+6)<<48|
403 (uint64_t)*((const uint8_t *)(p)+5)<<40|
404 (uint64_t)*((const uint8_t *)(p)+4)<<32|
405 (uint64_t)*((const uint8_t *)(p)+3)<<24|
406 (uint64_t)*((const uint8_t *)(p)+2)<<16|
407 (uint64_t)*((const uint8_t *)(p)+1)<<8|
408 (uint64_t)*((const uint8_t *)(p)+0)<<0;
409}
410
411static inline void phtonu24(uint8_t *p, uint32_t v)
412{
413 p[0] = (uint8_t)((v) >> 16);
414 p[1] = (uint8_t)((v) >> 8);
415 p[2] = (uint8_t)((v) >> 0);
416}
417
418static inline void phtonu40(uint8_t *p, uint64_t v)
419{
420 p[0] = (uint8_t)((v) >> 32);
421 p[1] = (uint8_t)((v) >> 24);
422 p[2] = (uint8_t)((v) >> 16);
423 p[3] = (uint8_t)((v) >> 8);
424 p[4] = (uint8_t)((v) >> 0);
425}
426
427static inline void phtonu48(uint8_t *p, uint64_t v)
428{
429 p[0] = (uint8_t)((v) >> 40);
430 p[1] = (uint8_t)((v) >> 32);
431 p[2] = (uint8_t)((v) >> 24);
432 p[3] = (uint8_t)((v) >> 16);
433 p[4] = (uint8_t)((v) >> 8);
434 p[5] = (uint8_t)((v) >> 0);
435}
436
437static inline void phtonu56(uint8_t *p, uint64_t v)
438{
439 p[0] = (uint8_t)((v) >> 48);
440 p[1] = (uint8_t)((v) >> 40);
441 p[2] = (uint8_t)((v) >> 32);
442 p[3] = (uint8_t)((v) >> 24);
443 p[4] = (uint8_t)((v) >> 16);
444 p[5] = (uint8_t)((v) >> 8);
445 p[6] = (uint8_t)((v) >> 0);
446}
447
448static inline void phtoleu24(uint8_t *p, uint32_t v)
449{
450 p[0] = (uint8_t)((v) >> 0);
451 p[1] = (uint8_t)((v) >> 8);
452 p[2] = (uint8_t)((v) >> 16);
453}
454
455static inline void phtoleu40(uint8_t *p, uint64_t v)
456{
457 p[0] = (uint8_t)((v) >> 0);
458 p[1] = (uint8_t)((v) >> 8);
459 p[2] = (uint8_t)((v) >> 16);
460 p[3] = (uint8_t)((v) >> 24);
461 p[4] = (uint8_t)((v) >> 32);
462}
463
464static inline void phtoleu48(uint8_t *p, uint64_t v)
465{
466 p[0] = (uint8_t)((v) >> 0);
467 p[1] = (uint8_t)((v) >> 8);
468 p[2] = (uint8_t)((v) >> 16);
469 p[3] = (uint8_t)((v) >> 24);
470 p[4] = (uint8_t)((v) >> 32);
471 p[5] = (uint8_t)((v) >> 40);
472}
473
474static inline void phtoleu56(uint8_t *p, uint64_t v)
475{
476 p[0] = (uint8_t)((v) >> 0);
477 p[1] = (uint8_t)((v) >> 8);
478 p[2] = (uint8_t)((v) >> 16);
479 p[3] = (uint8_t)((v) >> 24);
480 p[4] = (uint8_t)((v) >> 32);
481 p[5] = (uint8_t)((v) >> 40);
482 p[6] = (uint8_t)((v) >> 48);
483}
484
485#endif /* PINT_H */
486
487/*
488 * Editor modelines - https://www.wireshark.org/tools/modelines.html
489 *
490 * Local Variables:
491 * c-basic-offset: 4
492 * tab-width: 8
493 * indent-tabs-mode: nil
494 * End:
495 *
496 * ex: set shiftwidth=4 tabstop=8 expandtab:
497 * :indentSize=4:tabSize=8:noTabs=true:
498 */