Coverage Report

Created: 2025-12-27 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/crc32-tvb.c
Line
Count
Source
1
/* crc32-tvb.c
2
 * CRC-32 tvbuff routines
3
 *
4
 * Wireshark - Network traffic analyzer
5
 * By Gerald Combs <gerald@wireshark.org>
6
 * Copyright 1998 Gerald Combs
7
 *
8
 * SPDX-License-Identifier: GPL-2.0-or-later
9
 *
10
 * Credits:
11
 *
12
 * Table from Solomon Peachy
13
 * Routine from Chris Waters
14
 */
15
16
#include "config.h"
17
18
#include <glib.h>
19
#include <epan/tvbuff.h>
20
#include <wsutil/crc32.h>
21
#include <epan/crc32-tvb.h>
22
23
24
uint32_t
25
crc32_ccitt_tvb(tvbuff_t *tvb, unsigned len)
26
28
{
27
28
  const uint8_t* buf;
28
29
28
  tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
30
28
  buf = tvb_get_ptr(tvb, 0, len);
31
32
28
  return ( crc32_ccitt_seed(buf, len, CRC32_CCITT_SEED) );
33
28
}
34
35
uint32_t
36
crc32_ccitt_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len)
37
0
{
38
0
  const uint8_t* buf;
39
40
0
  tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
41
0
  buf = tvb_get_ptr(tvb, offset, len);
42
43
0
  return ( crc32_ccitt(buf, len) );
44
0
}
45
46
uint32_t
47
crc32_ccitt_tvb_seed(tvbuff_t *tvb, unsigned len, uint32_t seed)
48
3.76k
{
49
3.76k
  const uint8_t* buf;
50
51
3.76k
  tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
52
3.76k
  buf = tvb_get_ptr(tvb, 0, len);
53
54
3.76k
  return ( crc32_ccitt_seed(buf, len, seed) );
55
3.76k
}
56
57
uint32_t
58
crc32_ccitt_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len,
59
          uint32_t seed)
60
2
{
61
2
  const uint8_t* buf;
62
63
2
  tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
64
2
  buf = tvb_get_ptr(tvb, offset, len);
65
66
2
  return ( crc32_ccitt_seed(buf, len, seed) );
67
2
}
68
69
uint32_t
70
crc32c_tvb_offset_calculate(tvbuff_t *tvb, unsigned offset, unsigned len, uint32_t seed)
71
161
{
72
161
  const uint8_t* buf;
73
74
161
  tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
75
161
  buf = tvb_get_ptr(tvb, offset, len);
76
77
161
  return ( crc32c_calculate(buf, len, seed) );
78
161
}
79
80
/*
81
 * IEEE 802.x version (Ethernet and 802.11, at least) - byte-swap
82
 * the result of "crc32()".
83
 *
84
 * XXX - does this mean we should fetch the Ethernet and 802.11
85
 * FCS with "tvb_get_letohl()" rather than "tvb_get_ntohl()",
86
 * or is fetching it big-endian and byte-swapping the CRC done
87
 * to cope with 802.x sending stuff out in reverse bit order?
88
 */
89
uint32_t
90
crc32_802_tvb(tvbuff_t *tvb, unsigned len)
91
28
{
92
28
  uint32_t c_crc;
93
94
28
  c_crc = crc32_ccitt_tvb(tvb, len);
95
96
  /* Byte reverse. */
97
28
  c_crc = GUINT32_SWAP_LE_BE(c_crc);
98
99
28
  return ( c_crc );
100
28
}
101
102
uint32_t
103
crc32_mpeg2_tvb_offset_seed(tvbuff_t *tvb, unsigned offset,
104
          unsigned len, uint32_t seed)
105
584
{
106
584
  const uint8_t* buf;
107
108
584
  tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
109
584
  buf = tvb_get_ptr(tvb, offset, len);
110
111
584
  return ( crc32_mpeg2_seed(buf, len, seed) );
112
584
}
113
114
uint32_t
115
crc32_mpeg2_tvb(tvbuff_t *tvb, unsigned len)
116
0
{
117
0
  return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, CRC32_MPEG2_SEED) );
118
0
}
119
120
uint32_t
121
crc32_mpeg2_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len)
122
171
{
123
171
  return ( crc32_mpeg2_tvb_offset_seed(tvb, offset, len, CRC32_MPEG2_SEED) );
124
171
}
125
126
uint32_t
127
crc32_mpeg2_tvb_seed(tvbuff_t *tvb, unsigned len, uint32_t seed)
128
0
{
129
0
  return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, seed) );
130
0
}
131
132
uint32_t crc32_0x0AA725CF_tvb_offset_seed(tvbuff_t *tvb,
133
           unsigned offset, unsigned len, uint32_t seed)
134
0
{
135
0
  const uint8_t *buf;
136
137
0
  tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
138
0
  buf = tvb_get_ptr(tvb, offset, len);
139
140
0
  return crc32_0x0AA725CF_seed(buf, len, seed);
141
0
}
142
143
/*
144
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
145
 *
146
 * Local variables:
147
 * c-basic-offset: 8
148
 * tab-width: 8
149
 * indent-tabs-mode: t
150
 * End:
151
 *
152
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
153
 * :indentSize=8:tabSize=8:noTabs=false:
154
 */