Coverage Report

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