Coverage Report

Created: 2026-12-01 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpcap/pcap-util.h
Line
Count
Source
1
/*
2
 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3
 *  The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that: (1) source code distributions
7
 * retain the above copyright notice and this paragraph in its entirety, (2)
8
 * distributions including binary code include the above copyright notice and
9
 * this paragraph in its entirety in the documentation or other materials
10
 * provided with the distribution, and (3) all advertising materials mentioning
11
 * features or use of this software display the following acknowledgement:
12
 * ``This product includes software developed by the University of California,
13
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14
 * the University nor the names of its contributors may be used to endorse
15
 * or promote products derived from this software without specific prior
16
 * written permission.
17
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
 *
21
 * pcap-util.h - common code for various files
22
 */
23
24
#ifndef pcap_util_h
25
#define pcap_util_h
26
27
#include <config.h> // HAVE_xxx
28
29
#include <pcap/compiler-tests.h>
30
#include <pcap/pcap-inttypes.h>
31
/*
32
 * We use the "receiver-makes-right" approach to byte order;
33
 * because time is at a premium when we are writing the file.
34
 * In other words, the pcap_file_header and pcap_pkthdr,
35
 * records are written in host byte order.
36
 * Note that the bytes of packet data are written out in the order in
37
 * which they were received, so multi-byte fields in packets are not
38
 * written in host byte order, they're written in whatever order the
39
 * sending machine put them in.
40
 *
41
 * We also use this for fixing up packet data headers from a remote
42
 * capture, where the server may have a different byte order from the
43
 * client.
44
 *
45
 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
46
 * machine (if the file was written in little-end order).
47
 *
48
 * These macros are used to generate case labels in switch statements,
49
 * so they can't be done as inline functions, and, when passed a
50
 * compile-time constant argument, must produce a compile-time constant
51
 * result.  This means that they must either use builtin functions that
52
 * are recognized by the compiler as producing constant results when
53
 * passed a constant argument, or must be done with expressions that
54
 * evaluate to compile-time constants when passed a compile-time
55
 * constant.
56
 *
57
 * (In practical terms, to define HAVE_BUILTIN_BSWAPnn it is sufficient to
58
 * verify that respective builtin can be used as a case label in a switch
59
 * statement.)
60
 *
61
 * Newer versions of GCC, Clang, Visual Studio C/C++, and possibly
62
 * some other compilers recognize the expressions as byte-swapping
63
 * idioms and generate optimized machine-specific code for architectures
64
 * that include instructions that can be used to swap bytes.
65
 */
66
67
#if defined(__clang__) && ! PCAP_IS_AT_LEAST_CLANG_VERSION(9, 0)
68
/*
69
 * Clang releases before 9.0.0 used "unsigned long long" and "unsigned" to
70
 * implement __builtin_bswap64() and __builtin_bswap32() respectively, which on
71
 * non-mainstream architectures could work incorrectly for these and other
72
 * 64-bit and 32-bit builtins (see LLVM commit 0e525a4) and resulted in a
73
 * -Wformat on at least Linux/AMD64, where PRIx64 is defined to "lx".
74
 *
75
 * Avoid using these builtins in these Clang releases without trying to guess
76
 * whether the architecture is affected or not: this avoids both the warning
77
 * and the additional footprint that would be required in the build systems.
78
 * Do not undefine any macros that config.h might have defined.
79
 */
80
#define BROKEN_BUILTIN_BSWAP64
81
#define BROKEN_BUILTIN_BSWAP32
82
#endif
83
84
#if defined(HAVE_BUILTIN_BSWAP64) && ! defined(BROKEN_BUILTIN_BSWAP64)
85
0
#define PCAP_BSWAP_64(y) __builtin_bswap64((uint64_t)(y))
86
#else
87
#define PCAP_BSWAP_64(y) \
88
    ((uint64_t)(((((uint64_t)(y)) & UINT64_C(0xff00000000000000)) >> 56) | \
89
                ((((uint64_t)(y)) & UINT64_C(0x00ff000000000000)) >> 40) | \
90
                ((((uint64_t)(y)) & UINT64_C(0x0000ff0000000000)) >> 24) | \
91
                ((((uint64_t)(y)) & UINT64_C(0x000000ff00000000)) >> 8)  | \
92
                ((((uint64_t)(y)) & UINT64_C(0x00000000ff000000)) << 8)  | \
93
                ((((uint64_t)(y)) & UINT64_C(0x0000000000ff0000)) << 24) | \
94
                ((((uint64_t)(y)) & UINT64_C(0x000000000000ff00)) << 40) | \
95
                ((((uint64_t)(y)) & UINT64_C(0x00000000000000ff)) << 56)))
96
#endif // 64-bit
97
98
#if defined(HAVE_BUILTIN_BSWAP32) && ! defined(BROKEN_BUILTIN_BSWAP32)
99
0
#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
100
#else
101
#define PCAP_BSWAP_32(y) \
102
    ((uint32_t)(((((uint32_t)(y)) & UINT32_C(0xff000000)) >> 24) | \
103
                ((((uint32_t)(y)) & UINT32_C(0x00ff0000)) >> 8)  | \
104
                ((((uint32_t)(y)) & UINT32_C(0x0000ff00)) << 8)  | \
105
                ((((uint32_t)(y)) & UINT32_C(0x000000ff)) << 24)))
106
#endif // 32-bit
107
108
#ifdef HAVE_BUILTIN_BSWAP16
109
0
#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
110
#else
111
#define PCAP_BSWAP_16(y) \
112
    ((uint16_t)(((((uint16_t)(y)) & UINT16_C(0x00ff)) << 8) | \
113
                ((((uint16_t)(y)) & UINT16_C(0xff00)) >> 8)))
114
#endif // 16-bit
115
116
/*
117
 * Byte-swap a pcap_4_byte_aligned_uint64;
118
 */
119
static inline pcap_4_byte_aligned_uint64 swap_4_byte_aligned_uint64(pcap_4_byte_aligned_uint64 val)
120
0
{
121
0
  return (pcap_4_byte_aligned_uint64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
122
0
}
Unexecuted instantiation: sf-pcapng.c:swap_4_byte_aligned_uint64
Unexecuted instantiation: sf-pcap.c:swap_4_byte_aligned_uint64
Unexecuted instantiation: pcap-linux.c:swap_4_byte_aligned_uint64
Unexecuted instantiation: gencode.c:swap_4_byte_aligned_uint64
Unexecuted instantiation: pcap-util.c:swap_4_byte_aligned_uint64
123
124
/*
125
 * Byte-swap a pcap_4_byte_aligned_int64;
126
 */
127
static inline pcap_4_byte_aligned_int64 swap_4_byte_aligned_int64(pcap_4_byte_aligned_int64 val)
128
0
{
129
0
  return (pcap_4_byte_aligned_int64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
130
0
}
Unexecuted instantiation: sf-pcapng.c:swap_4_byte_aligned_int64
Unexecuted instantiation: sf-pcap.c:swap_4_byte_aligned_int64
Unexecuted instantiation: pcap-linux.c:swap_4_byte_aligned_int64
Unexecuted instantiation: gencode.c:swap_4_byte_aligned_int64
Unexecuted instantiation: pcap-util.c:swap_4_byte_aligned_int64
131
132
extern void pcapint_post_process(int linktype, int swapped,
133
    struct pcap_pkthdr *hdr, u_char *data);
134
135
#endif // pcap_util_h