summaryrefslogtreecommitdiffstats
path: root/wsutil/pint.h
blob: 97b6da2355588d85b2105da5fd6b5d0fbbb9e13f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/** @file
 *
 * Definitions for extracting and translating integers safely and portably
 * via pointers.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef __PINT_H__
#define __PINT_H__

#include <inttypes.h>

#include <glib.h>

/* Routines that take a possibly-unaligned pointer to a 16-bit, 24-bit,
 * 32-bit, 40-bit, ... 64-bit integral quantity, in a particular byte
 * order, and fetch the value and return it in host byte order.
 *
 * The pntohN() routines fetch big-endian values; the pletohN() routines
 * fetch little-endian values.
 */

/* On most architectures, accesses of 16, 32, and 64 bit quantities can be
 * heavily optimized. gcc and clang recognize portable versions below and,
 * at -Os and higher, optimize them appropriately (for gcc, that includes
 * for z/Architecture, PPC64, MIPS, etc.). Older versions don't do as good
 * of a job with 16 bit accesses, though.
 *
 * Unfortunately, MSVC and icc (both the "classic" version and the new
 * LLVM-based Intel C Compiler) do not, according to Matt Godbolt's Compiler
 * Explorer (https://godbolt.org) as of the end of 2022. They *do* recognize
 * and optimize a memcpy based approach (which avoids unaligned accesses on,
 * say, ARM32), though that requires byteswapping appropriately.
 */

#if (defined(_MSC_VER) && !defined(__clang__)) || defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
/* MSVC or Intel C Compiler (Classic or new LLVM version), but not
 * clang-cl on Windows.
 */
/* Unfortunately, C23 did not fully accept the N3022 Modern Bit Utilities
 * proposal, so a standard bytereverse function has been deferred for some
 * future version:
 * https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3048.htm
 * https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3022.htm
 *
 * So choose byteswap intrinsics we know we have.
 */
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__INTEL_LLVM_COMPILER) && !defined(__clang__)
/* Intel and clang-cl both define _MSC_VER when compiling on Windows for
 * greater compatiblity (just as they define __GNUC__ on other platforms).
 * However, at least on some versions, while including the MSVC <stdlib.h>
 * provides access to the _byteswap_ intrinsics, they are not actually
 * optimized into a single x86 BSWAP function, unlike the gcc-style intrinsics
 * (which both support.) See: https://stackoverflow.com/q/72327906
 */
#include <stdlib.h> // For MSVC _byteswap intrinsics
#define pint_bswap16(x) _byteswap_ushort(x)
#define pint_bswap32(x) _byteswap_ulong(x)
/* Hopefully MSVC never decides that a long is 64 bit. */
#define pint_bswap64(x) _byteswap_uint64(x)
#elif defined(__INTEL_COMPILER)
/* The (deprecated) Intel C++ Compiler Classic has these byteswap intrinsics.
 * It also has the GCC-style intrinsics, though __builtin_bswap16 wasn't
 * added until some point after icc 13.0 but at least by 16.0, reflecting
 * that it wasn't added to gcc until 4.8.
 */
#define pint_bswap16(x) _bswap16(x)
#define pint_bswap32(x) _bswap32(x)
#define pint_bswap64(x) _bswap64(x)
#else
/* GCC-style _bswap intrinsics */
/* The new LLVM-based Intel C++ Compiler doesn't have the above intrinsics,
 * but it always has all the GCC intrinsics.
 */
/* __builtin_bswap32 and __builtin_bswap64 intrinsics have been supported
 * for a long time on gcc (4.1), and clang (pre 3.0), versions that predate
 * C11 and C+11 support, which we require, so we could assume we have them.
 *
 * __builtin_bswap16 was added a bit later, gcc 4.8, and clang 3.2. While
 * those versions or later are required for full C11 and C++11 support,
 * some earlier versions claim to support C11 and C++11 in ways that might
 * allow them to get past CMake. We don't use this codepath for those
 * compilers because they heavily optimize the portable versions, though.
 */
#define pint_bswap16(x) __builtin_bswap16(x)
#define pint_bswap32(x) __builtin_bswap32(x)
#define pint_bswap64(x) __builtin_bswap64(x)
#endif

static inline uint16_t pntoh16(const void *p)
{
    uint16_t ret;
    memcpy(&ret, p, sizeof(ret));
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    ret = pint_bswap16(ret);
#endif
    return ret;
}

static inline uint32_t pntoh32(const void *p)
{
    uint32_t ret;
    memcpy(&ret, p, sizeof(ret));
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    ret = pint_bswap32(ret);
#endif
    return ret;
}

static inline uint64_t pntoh64(const void *p)
{
    uint64_t ret;
    memcpy(&ret, p, sizeof(ret));
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    ret = pint_bswap64(ret);
#endif
    return ret;
}

static inline uint16_t pletoh16(const void *p)
{
    uint16_t ret;
    memcpy(&ret, p, sizeof(ret));
#if G_BYTE_ORDER == G_BIG_ENDIAN
    ret = pint_bswap16(ret);
#endif
    return ret;
}

static inline uint32_t pletoh32(const void *p)
{
    uint32_t ret;
    memcpy(&ret, p, sizeof(ret));
#if G_BYTE_ORDER == G_BIG_ENDIAN
    ret = pint_bswap32(ret);
#endif
    return ret;
}

static inline uint64_t pletoh64(const void *p)
{
    uint64_t ret;
    memcpy(&ret, p, sizeof(ret));
#if G_BYTE_ORDER == G_BIG_ENDIAN
    ret = pint_bswap64(ret);
#endif
    return ret;
}

static inline void phton16(uint8_t *p, uint16_t v)
{
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    v = pint_bswap16(v);
#endif
    memcpy(p, &v, sizeof(v));
}

static inline void phton32(uint8_t *p, uint32_t v)
{
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    v = pint_bswap32(v);
#endif
    memcpy(p, &v, sizeof(v));
}

static inline void phton64(uint8_t *p, uint64_t v) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    v = pint_bswap64(v);
#endif
    memcpy(p, &v, sizeof(v));
}

static inline void phtole32(uint8_t *p, uint32_t v)
{
#if G_BYTE_ORDER == G_BIG_ENDIAN
    v = pint_bswap32(v);
#endif
    memcpy(p, &v, sizeof(v));
}

static inline void phtole64(uint8_t *p, uint64_t v) {
#if G_BYTE_ORDER == G_BIG_ENDIAN
    v = pint_bswap64(v);
#endif
    memcpy(p, &v, sizeof(v));
}

#else
/* Portable functions */
static inline uint16_t pntoh16(const void *p)
{
    return (uint16_t)*((const uint8_t *)(p)+0)<<8|
           (uint16_t)*((const uint8_t *)(p)+1)<<0;
}

static inline uint32_t pntoh32(const void *p)
{
    return (uint32_t)*((const uint8_t *)(p)+0)<<24|
           (uint32_t)*((const uint8_t *)(p)+1)<<16|
           (uint32_t)*((const uint8_t *)(p)+2)<<8|
           (uint32_t)*((const uint8_t *)(p)+3)<<0;
}

static inline uint64_t pntoh64(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+0)<<56|
           (uint64_t)*((const uint8_t *)(p)+1)<<48|
           (uint64_t)*((const uint8_t *)(p)+2)<<40|
           (uint64_t)*((const uint8_t *)(p)+3)<<32|
           (uint64_t)*((const uint8_t *)(p)+4)<<24|
           (uint64_t)*((const uint8_t *)(p)+5)<<16|
           (uint64_t)*((const uint8_t *)(p)+6)<<8|
           (uint64_t)*((const uint8_t *)(p)+7)<<0;
}

static inline uint16_t pletoh16(const void *p)
{
    return (uint16_t)*((const uint8_t *)(p)+1)<<8|
           (uint16_t)*((const uint8_t *)(p)+0)<<0;
}

static inline uint32_t pletoh32(const void *p)
{
    return (uint32_t)*((const uint8_t *)(p)+3)<<24|
           (uint32_t)*((const uint8_t *)(p)+2)<<16|
           (uint32_t)*((const uint8_t *)(p)+1)<<8|
           (uint32_t)*((const uint8_t *)(p)+0)<<0;
}

static inline uint64_t pletoh64(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+7)<<56|
           (uint64_t)*((const uint8_t *)(p)+6)<<48|
           (uint64_t)*((const uint8_t *)(p)+5)<<40|
           (uint64_t)*((const uint8_t *)(p)+4)<<32|
           (uint64_t)*((const uint8_t *)(p)+3)<<24|
           (uint64_t)*((const uint8_t *)(p)+2)<<16|
           (uint64_t)*((const uint8_t *)(p)+1)<<8|
           (uint64_t)*((const uint8_t *)(p)+0)<<0;
}

/* Pointer routines to put items out in a particular byte order.
 * These will work regardless of the byte alignment of the pointer.
 */

static inline void phton16(uint8_t *p, uint16_t v)
{
    p[0] = (uint8_t)(v >> 8);
    p[1] = (uint8_t)(v >> 0);
}

static inline void phton32(uint8_t *p, uint32_t v)
{
    p[0] = (uint8_t)(v >> 24);
    p[1] = (uint8_t)(v >> 16);
    p[2] = (uint8_t)(v >> 8);
    p[3] = (uint8_t)(v >> 0);
}

static inline void phton64(uint8_t *p, uint64_t v) {
    p[0] = (uint8_t)(v >> 56);
    p[1] = (uint8_t)(v >> 48);
    p[2] = (uint8_t)(v >> 40);
    p[3] = (uint8_t)(v >> 32);
    p[4] = (uint8_t)(v >> 24);
    p[5] = (uint8_t)(v >> 16);
    p[6] = (uint8_t)(v >> 8);
    p[7] = (uint8_t)(v >> 0);
}

static inline void phtole32(uint8_t *p, uint32_t v) {
    p[0] = (uint8_t)(v >> 0);
    p[1] = (uint8_t)(v >> 8);
    p[2] = (uint8_t)(v >> 16);
    p[3] = (uint8_t)(v >> 24);
}

static inline void phtole64(uint8_t *p, uint64_t v) {
    p[0] = (uint8_t)(v >> 0);
    p[1] = (uint8_t)(v >> 8);
    p[2] = (uint8_t)(v >> 16);
    p[3] = (uint8_t)(v >> 24);
    p[4] = (uint8_t)(v >> 32);
    p[5] = (uint8_t)(v >> 40);
    p[6] = (uint8_t)(v >> 48);
    p[7] = (uint8_t)(v >> 56);
}
#endif

static inline uint32_t pntoh24(const void *p)
{
    return (uint32_t)*((const uint8_t *)(p)+0)<<16|
           (uint32_t)*((const uint8_t *)(p)+1)<<8|
           (uint32_t)*((const uint8_t *)(p)+2)<<0;
}

static inline uint64_t pntoh40(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+0)<<32|
           (uint64_t)*((const uint8_t *)(p)+1)<<24|
           (uint64_t)*((const uint8_t *)(p)+2)<<16|
           (uint64_t)*((const uint8_t *)(p)+3)<<8|
           (uint64_t)*((const uint8_t *)(p)+4)<<0;
}

static inline uint64_t pntoh48(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+0)<<40|
           (uint64_t)*((const uint8_t *)(p)+1)<<32|
           (uint64_t)*((const uint8_t *)(p)+2)<<24|
           (uint64_t)*((const uint8_t *)(p)+3)<<16|
           (uint64_t)*((const uint8_t *)(p)+4)<<8|
           (uint64_t)*((const uint8_t *)(p)+5)<<0;
}

static inline uint64_t pntoh56(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+0)<<48|
           (uint64_t)*((const uint8_t *)(p)+1)<<40|
           (uint64_t)*((const uint8_t *)(p)+2)<<32|
           (uint64_t)*((const uint8_t *)(p)+3)<<24|
           (uint64_t)*((const uint8_t *)(p)+4)<<16|
           (uint64_t)*((const uint8_t *)(p)+5)<<8|
           (uint64_t)*((const uint8_t *)(p)+6)<<0;
}

static inline uint32_t pletoh24(const void *p)
{
    return (uint32_t)*((const uint8_t *)(p)+2)<<16|
           (uint32_t)*((const uint8_t *)(p)+1)<<8|
           (uint32_t)*((const uint8_t *)(p)+0)<<0;
}

static inline uint64_t pletoh40(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+4)<<32|
           (uint64_t)*((const uint8_t *)(p)+3)<<24|
           (uint64_t)*((const uint8_t *)(p)+2)<<16|
           (uint64_t)*((const uint8_t *)(p)+1)<<8|
           (uint64_t)*((const uint8_t *)(p)+0)<<0;
}

static inline uint64_t pletoh48(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+5)<<40|
           (uint64_t)*((const uint8_t *)(p)+4)<<32|
           (uint64_t)*((const uint8_t *)(p)+3)<<24|
           (uint64_t)*((const uint8_t *)(p)+2)<<16|
           (uint64_t)*((const uint8_t *)(p)+1)<<8|
           (uint64_t)*((const uint8_t *)(p)+0)<<0;
}

static inline uint64_t pletoh56(const void *p)
{
    return (uint64_t)*((const uint8_t *)(p)+6)<<48|
           (uint64_t)*((const uint8_t *)(p)+5)<<40|
           (uint64_t)*((const uint8_t *)(p)+4)<<32|
           (uint64_t)*((const uint8_t *)(p)+3)<<24|
           (uint64_t)*((const uint8_t *)(p)+2)<<16|
           (uint64_t)*((const uint8_t *)(p)+1)<<8|
           (uint64_t)*((const uint8_t *)(p)+0)<<0;
}

/* Subtract two guint32s with respect to wraparound */
#define guint32_wraparound_diff(higher, lower) ((higher>lower)?(higher-lower):(higher+0xffffffff-lower+1))

#endif /* PINT_H */

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */