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 */
203
210static inline uint16_t pntohu16(const void *p)
211{
212 return (uint16_t)*((const uint8_t *)(p)+0)<<8|
213 (uint16_t)*((const uint8_t *)(p)+1)<<0;
214}
215
222static inline uint32_t pntohu32(const void *p)
223{
224 return (uint32_t)*((const uint8_t *)(p)+0)<<24|
225 (uint32_t)*((const uint8_t *)(p)+1)<<16|
226 (uint32_t)*((const uint8_t *)(p)+2)<<8|
227 (uint32_t)*((const uint8_t *)(p)+3)<<0;
228}
229
237static inline uint64_t pntohu64(const void *p)
238{
239 return (uint64_t)*((const uint8_t *)(p)+0)<<56|
240 (uint64_t)*((const uint8_t *)(p)+1)<<48|
241 (uint64_t)*((const uint8_t *)(p)+2)<<40|
242 (uint64_t)*((const uint8_t *)(p)+3)<<32|
243 (uint64_t)*((const uint8_t *)(p)+4)<<24|
244 (uint64_t)*((const uint8_t *)(p)+5)<<16|
245 (uint64_t)*((const uint8_t *)(p)+6)<<8|
246 (uint64_t)*((const uint8_t *)(p)+7)<<0;
247}
248
255static inline uint16_t pletohu16(const void *p)
256{
257 return (uint16_t)*((const uint8_t *)(p)+1)<<8|
258 (uint16_t)*((const uint8_t *)(p)+0)<<0;
259}
260
267static inline uint32_t pletohu32(const void *p)
268{
269 return (uint32_t)*((const uint8_t *)(p)+3)<<24|
270 (uint32_t)*((const uint8_t *)(p)+2)<<16|
271 (uint32_t)*((const uint8_t *)(p)+1)<<8|
272 (uint32_t)*((const uint8_t *)(p)+0)<<0;
273}
274
281static inline uint64_t pletohu64(const void *p)
282{
283 return (uint64_t)*((const uint8_t *)(p)+7)<<56|
284 (uint64_t)*((const uint8_t *)(p)+6)<<48|
285 (uint64_t)*((const uint8_t *)(p)+5)<<40|
286 (uint64_t)*((const uint8_t *)(p)+4)<<32|
287 (uint64_t)*((const uint8_t *)(p)+3)<<24|
288 (uint64_t)*((const uint8_t *)(p)+2)<<16|
289 (uint64_t)*((const uint8_t *)(p)+1)<<8|
290 (uint64_t)*((const uint8_t *)(p)+0)<<0;
291}
292
293/* Pointer routines to put items out in a particular byte order.
294 * These will work regardless of the byte alignment of the pointer.
295 */
296
297static inline void phtonu16(uint8_t *p, uint16_t v)
298{
299 p[0] = (uint8_t)(v >> 8);
300 p[1] = (uint8_t)(v >> 0);
301}
302
309static inline void phtonu32(uint8_t *p, uint32_t v)
310{
311 p[0] = (uint8_t)(v >> 24);
312 p[1] = (uint8_t)(v >> 16);
313 p[2] = (uint8_t)(v >> 8);
314 p[3] = (uint8_t)(v >> 0);
315}
316
323static inline void phtonu64(uint8_t *p, uint64_t v) {
324 p[0] = (uint8_t)(v >> 56);
325 p[1] = (uint8_t)(v >> 48);
326 p[2] = (uint8_t)(v >> 40);
327 p[3] = (uint8_t)(v >> 32);
328 p[4] = (uint8_t)(v >> 24);
329 p[5] = (uint8_t)(v >> 16);
330 p[6] = (uint8_t)(v >> 8);
331 p[7] = (uint8_t)(v >> 0);
332}
333
340static inline void phtoleu16(uint8_t *p, uint16_t v)
341{
342 p[0] = (uint8_t)(v >> 0);
343 p[1] = (uint8_t)(v >> 8);
344}
345
352static inline void phtoleu32(uint8_t *p, uint32_t v) {
353 p[0] = (uint8_t)(v >> 0);
354 p[1] = (uint8_t)(v >> 8);
355 p[2] = (uint8_t)(v >> 16);
356 p[3] = (uint8_t)(v >> 24);
357}
358
365static inline void phtoleu64(uint8_t *p, uint64_t v) {
366 p[0] = (uint8_t)(v >> 0);
367 p[1] = (uint8_t)(v >> 8);
368 p[2] = (uint8_t)(v >> 16);
369 p[3] = (uint8_t)(v >> 24);
370 p[4] = (uint8_t)(v >> 32);
371 p[5] = (uint8_t)(v >> 40);
372 p[6] = (uint8_t)(v >> 48);
373 p[7] = (uint8_t)(v >> 56);
374}
375#endif
376
377/*
378 * Single-byte versions, for completeness.
379*/
380
387static inline uint8_t pntohu8(const void *p)
388{
389 return *((const uint8_t *)(p)+0)<<0;
390}
391
398static inline uint8_t pletohu8(const void *p)
399{
400 return *((const uint8_t *)(p)+0)<<0;
401}
402
409static inline void phtonu8(uint8_t *p, uint8_t v)
410{
411 p[0] = (uint8_t)((v) >> 0);
412}
413
420static inline void phtoleu8(uint8_t *p, uint8_t v)
421{
422 p[0] = (uint8_t)((v) >> 0);
423}
424
425/*
426 * Non-power-of-2 field sizes; do these a byte at a time, and let the
427 * compiler optimize them to combinations of power-of-2 operations if
428 * possible.
429 */
430static inline uint32_t pntohu24(const void *p)
431{
432 return (uint32_t)*((const uint8_t *)(p)+0)<<16|
433 (uint32_t)*((const uint8_t *)(p)+1)<<8|
434 (uint32_t)*((const uint8_t *)(p)+2)<<0;
435}
436
446static inline uint64_t pntohu40(const void *p)
447{
448 return (uint64_t)*((const uint8_t *)(p)+0)<<32|
449 (uint64_t)*((const uint8_t *)(p)+1)<<24|
450 (uint64_t)*((const uint8_t *)(p)+2)<<16|
451 (uint64_t)*((const uint8_t *)(p)+3)<<8|
452 (uint64_t)*((const uint8_t *)(p)+4)<<0;
453}
454
461static inline uint64_t pntohu48(const void *p)
462{
463 return (uint64_t)*((const uint8_t *)(p)+0)<<40|
464 (uint64_t)*((const uint8_t *)(p)+1)<<32|
465 (uint64_t)*((const uint8_t *)(p)+2)<<24|
466 (uint64_t)*((const uint8_t *)(p)+3)<<16|
467 (uint64_t)*((const uint8_t *)(p)+4)<<8|
468 (uint64_t)*((const uint8_t *)(p)+5)<<0;
469}
470
477static inline uint64_t pntohu56(const void *p)
478{
479 return (uint64_t)*((const uint8_t *)(p)+0)<<48|
480 (uint64_t)*((const uint8_t *)(p)+1)<<40|
481 (uint64_t)*((const uint8_t *)(p)+2)<<32|
482 (uint64_t)*((const uint8_t *)(p)+3)<<24|
483 (uint64_t)*((const uint8_t *)(p)+4)<<16|
484 (uint64_t)*((const uint8_t *)(p)+5)<<8|
485 (uint64_t)*((const uint8_t *)(p)+6)<<0;
486}
487
494static inline uint32_t pletohu24(const void *p)
495{
496 return (uint32_t)*((const uint8_t *)(p)+2)<<16|
497 (uint32_t)*((const uint8_t *)(p)+1)<<8|
498 (uint32_t)*((const uint8_t *)(p)+0)<<0;
499}
500
507static inline uint64_t pletohu40(const void *p)
508{
509 return (uint64_t)*((const uint8_t *)(p)+4)<<32|
510 (uint64_t)*((const uint8_t *)(p)+3)<<24|
511 (uint64_t)*((const uint8_t *)(p)+2)<<16|
512 (uint64_t)*((const uint8_t *)(p)+1)<<8|
513 (uint64_t)*((const uint8_t *)(p)+0)<<0;
514}
515
522static inline uint64_t pletohu48(const void *p)
523{
524 return (uint64_t)*((const uint8_t *)(p)+5)<<40|
525 (uint64_t)*((const uint8_t *)(p)+4)<<32|
526 (uint64_t)*((const uint8_t *)(p)+3)<<24|
527 (uint64_t)*((const uint8_t *)(p)+2)<<16|
528 (uint64_t)*((const uint8_t *)(p)+1)<<8|
529 (uint64_t)*((const uint8_t *)(p)+0)<<0;
530}
531
539static inline uint64_t pletohu56(const void *p)
540{
541 return (uint64_t)*((const uint8_t *)(p)+6)<<48|
542 (uint64_t)*((const uint8_t *)(p)+5)<<40|
543 (uint64_t)*((const uint8_t *)(p)+4)<<32|
544 (uint64_t)*((const uint8_t *)(p)+3)<<24|
545 (uint64_t)*((const uint8_t *)(p)+2)<<16|
546 (uint64_t)*((const uint8_t *)(p)+1)<<8|
547 (uint64_t)*((const uint8_t *)(p)+0)<<0;
548}
549
557static inline void phtonu24(uint8_t *p, uint32_t v)
558{
559 p[0] = (uint8_t)((v) >> 16);
560 p[1] = (uint8_t)((v) >> 8);
561 p[2] = (uint8_t)((v) >> 0);
562}
563
570static inline void phtonu40(uint8_t *p, uint64_t v)
571{
572 p[0] = (uint8_t)((v) >> 32);
573 p[1] = (uint8_t)((v) >> 24);
574 p[2] = (uint8_t)((v) >> 16);
575 p[3] = (uint8_t)((v) >> 8);
576 p[4] = (uint8_t)((v) >> 0);
577}
578
585static inline void phtonu48(uint8_t *p, uint64_t v)
586{
587 p[0] = (uint8_t)((v) >> 40);
588 p[1] = (uint8_t)((v) >> 32);
589 p[2] = (uint8_t)((v) >> 24);
590 p[3] = (uint8_t)((v) >> 16);
591 p[4] = (uint8_t)((v) >> 8);
592 p[5] = (uint8_t)((v) >> 0);
593}
594
602static inline void phtonu56(uint8_t *p, uint64_t v)
603{
604 p[0] = (uint8_t)((v) >> 48);
605 p[1] = (uint8_t)((v) >> 40);
606 p[2] = (uint8_t)((v) >> 32);
607 p[3] = (uint8_t)((v) >> 24);
608 p[4] = (uint8_t)((v) >> 16);
609 p[5] = (uint8_t)((v) >> 8);
610 p[6] = (uint8_t)((v) >> 0);
611}
612
619static inline void phtoleu24(uint8_t *p, uint32_t v)
620{
621 p[0] = (uint8_t)((v) >> 0);
622 p[1] = (uint8_t)((v) >> 8);
623 p[2] = (uint8_t)((v) >> 16);
624}
625
632static inline void phtoleu40(uint8_t *p, uint64_t v)
633{
634 p[0] = (uint8_t)((v) >> 0);
635 p[1] = (uint8_t)((v) >> 8);
636 p[2] = (uint8_t)((v) >> 16);
637 p[3] = (uint8_t)((v) >> 24);
638 p[4] = (uint8_t)((v) >> 32);
639}
640
647static inline void phtoleu48(uint8_t *p, uint64_t v)
648{
649 p[0] = (uint8_t)((v) >> 0);
650 p[1] = (uint8_t)((v) >> 8);
651 p[2] = (uint8_t)((v) >> 16);
652 p[3] = (uint8_t)((v) >> 24);
653 p[4] = (uint8_t)((v) >> 32);
654 p[5] = (uint8_t)((v) >> 40);
655}
656
663static inline void phtoleu56(uint8_t *p, uint64_t v)
664{
665 p[0] = (uint8_t)((v) >> 0);
666 p[1] = (uint8_t)((v) >> 8);
667 p[2] = (uint8_t)((v) >> 16);
668 p[3] = (uint8_t)((v) >> 24);
669 p[4] = (uint8_t)((v) >> 32);
670 p[5] = (uint8_t)((v) >> 40);
671 p[6] = (uint8_t)((v) >> 48);
672}
673
674#endif /* PINT_H */
675
676/*
677 * Editor modelines - https://www.wireshark.org/tools/modelines.html
678 *
679 * Local Variables:
680 * c-basic-offset: 4
681 * tab-width: 8
682 * indent-tabs-mode: nil
683 * End:
684 *
685 * ex: set shiftwidth=4 tabstop=8 expandtab:
686 * :indentSize=4:tabSize=8:noTabs=true:
687 */