Coverage Report

Created: 2026-09-01 07:09

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 <pcap/pcap-inttypes.h>
28
/*
29
 * We use the "receiver-makes-right" approach to byte order;
30
 * because time is at a premium when we are writing the file.
31
 * In other words, the pcap_file_header and pcap_pkthdr,
32
 * records are written in host byte order.
33
 * Note that the bytes of packet data are written out in the order in
34
 * which they were received, so multi-byte fields in packets are not
35
 * written in host byte order, they're written in whatever order the
36
 * sending machine put them in.
37
 *
38
 * We also use this for fixing up packet data headers from a remote
39
 * capture, where the server may have a different byte order from the
40
 * client.
41
 *
42
 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
43
 * machine (if the file was written in little-end order).
44
 *
45
 * These macros are used to generate case labels in switch statements,
46
 * so they can't be done as inline functions, and, when passed a
47
 * compile-time constant argument, must produce a compile-time constant
48
 * result.  This means that they must either use builtin functions that
49
 * are recognized by the compiler as producing constant results when
50
 * passed a constant argument, or must be done with expressions that
51
 * evaluate to compile-time constants when passed a compile-time
52
 * constant.
53
 *
54
 * (In practical terms, to define HAVE_BUILTIN_BSWAPnn it is sufficient to
55
 * verify that respective builtin can be used as a case label in a switch
56
 * statement.)
57
 *
58
 * Newer versions of GCC, Clang, Visual Studio C/C++, and possibly
59
 * some other compilers recognize the expressions as byte-swapping
60
 * idioms and generate optimized machine-specific code for architectures
61
 * that include instructions that can be used to swap bytes.
62
 */
63
#ifdef HAVE_BUILTIN_BSWAP64
64
219
#define PCAP_BSWAP_64(y) __builtin_bswap64((uint64_t)(y))
65
#else
66
#define PCAP_BSWAP_64(y) \
67
    ((uint64_t)(((((uint64_t)(y)) & UINT64_C(0xff00000000000000)) >> 56) | \
68
                ((((uint64_t)(y)) & UINT64_C(0x00ff000000000000)) >> 40) | \
69
                ((((uint64_t)(y)) & UINT64_C(0x0000ff0000000000)) >> 24) | \
70
                ((((uint64_t)(y)) & UINT64_C(0x000000ff00000000)) >> 8)  | \
71
                ((((uint64_t)(y)) & UINT64_C(0x00000000ff000000)) << 8)  | \
72
                ((((uint64_t)(y)) & UINT64_C(0x0000000000ff0000)) << 24) | \
73
                ((((uint64_t)(y)) & UINT64_C(0x000000000000ff00)) << 40) | \
74
                ((((uint64_t)(y)) & UINT64_C(0x00000000000000ff)) << 56)))
75
#endif // 64-bit
76
77
#ifdef HAVE_BUILTIN_BSWAP32
78
851k
#define PCAP_BSWAP_32(y) __builtin_bswap32((uint32_t)(y))
79
#else
80
#define PCAP_BSWAP_32(y) \
81
    ((uint32_t)(((((uint32_t)(y)) & UINT32_C(0xff000000)) >> 24) | \
82
                ((((uint32_t)(y)) & UINT32_C(0x00ff0000)) >> 8)  | \
83
                ((((uint32_t)(y)) & UINT32_C(0x0000ff00)) << 8)  | \
84
                ((((uint32_t)(y)) & UINT32_C(0x000000ff)) << 24)))
85
#endif // 32-bit
86
87
#ifdef HAVE_BUILTIN_BSWAP16
88
24.6k
#define PCAP_BSWAP_16(y) __builtin_bswap16((uint16_t)(y))
89
#else
90
#define PCAP_BSWAP_16(y) \
91
    ((uint16_t)(((((uint16_t)(y)) & UINT16_C(0x00ff)) << 8) | \
92
                ((((uint16_t)(y)) & UINT16_C(0xff00)) >> 8)))
93
#endif // 16-bit
94
95
/*
96
 * Byte-swap a pcap_4_byte_aligned_uint64;
97
 */
98
static inline pcap_4_byte_aligned_uint64 swap_4_byte_aligned_uint64(pcap_4_byte_aligned_uint64 val)
99
7.69k
{
100
7.69k
  return (pcap_4_byte_aligned_uint64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
101
7.69k
}
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
pcap-util.c:swap_4_byte_aligned_uint64
Line
Count
Source
99
7.69k
{
100
7.69k
  return (pcap_4_byte_aligned_uint64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
101
7.69k
}
102
103
/*
104
 * Byte-swap a pcap_4_byte_aligned_int64;
105
 */
106
static inline pcap_4_byte_aligned_int64 swap_4_byte_aligned_int64(pcap_4_byte_aligned_int64 val)
107
6.89k
{
108
6.89k
  return (pcap_4_byte_aligned_int64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
109
6.89k
}
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
pcap-util.c:swap_4_byte_aligned_int64
Line
Count
Source
107
6.89k
{
108
6.89k
  return (pcap_4_byte_aligned_int64){.halves[0] = PCAP_BSWAP_32(val.halves[1]), .halves[1] = PCAP_BSWAP_32(val.halves[0])};
109
6.89k
}
110
111
extern void pcapint_post_process(int linktype, int swapped,
112
    struct pcap_pkthdr *hdr, u_char *data);
113
114
#endif // pcap_util_h