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 guint32
crc32_ccitt_tvb(tvbuff_t * tvb,guint len)25 crc32_ccitt_tvb(tvbuff_t *tvb, guint len)
26 {
27 const guint8* buf;
28
29 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
30 buf = tvb_get_ptr(tvb, 0, len);
31
32 return ( crc32_ccitt_seed(buf, len, CRC32_CCITT_SEED) );
33 }
34
35 guint32
crc32_ccitt_tvb_offset(tvbuff_t * tvb,guint offset,guint len)36 crc32_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
37 {
38 const guint8* buf;
39
40 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
41 buf = tvb_get_ptr(tvb, offset, len);
42
43 return ( crc32_ccitt(buf, len) );
44 }
45
46 guint32
crc32_ccitt_tvb_seed(tvbuff_t * tvb,guint len,guint32 seed)47 crc32_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
48 {
49 const guint8* buf;
50
51 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
52 buf = tvb_get_ptr(tvb, 0, len);
53
54 return ( crc32_ccitt_seed(buf, len, seed) );
55 }
56
57 guint32
crc32_ccitt_tvb_offset_seed(tvbuff_t * tvb,guint offset,guint len,guint32 seed)58 crc32_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len,
59 guint32 seed)
60 {
61 const guint8* buf;
62
63 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
64 buf = tvb_get_ptr(tvb, offset, len);
65
66 return ( crc32_ccitt_seed(buf, len, seed) );
67 }
68
69 guint32
crc32c_tvb_offset_calculate(tvbuff_t * tvb,guint offset,guint len,guint32 seed)70 crc32c_tvb_offset_calculate(tvbuff_t *tvb, guint offset, guint len, guint32 seed)
71 {
72 const guint8* buf;
73
74 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
75 buf = tvb_get_ptr(tvb, offset, len);
76
77 return ( crc32c_calculate(buf, len, seed) );
78 }
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 guint32
crc32_802_tvb(tvbuff_t * tvb,guint len)90 crc32_802_tvb(tvbuff_t *tvb, guint len)
91 {
92 guint32 c_crc;
93
94 c_crc = crc32_ccitt_tvb(tvb, len);
95
96 /* Byte reverse. */
97 c_crc = GUINT32_SWAP_LE_BE(c_crc);
98
99 return ( c_crc );
100 }
101
102 guint32
crc32_mpeg2_tvb_offset_seed(tvbuff_t * tvb,guint offset,guint len,guint32 seed)103 crc32_mpeg2_tvb_offset_seed(tvbuff_t *tvb, guint offset,
104 guint len, guint32 seed)
105 {
106 const guint8* buf;
107
108 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
109 buf = tvb_get_ptr(tvb, offset, len);
110
111 return ( crc32_mpeg2_seed(buf, len, seed) );
112 }
113
114 guint32
crc32_mpeg2_tvb(tvbuff_t * tvb,guint len)115 crc32_mpeg2_tvb(tvbuff_t *tvb, guint len)
116 {
117 return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, CRC32_MPEG2_SEED) );
118 }
119
120 guint32
crc32_mpeg2_tvb_offset(tvbuff_t * tvb,guint offset,guint len)121 crc32_mpeg2_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
122 {
123 return ( crc32_mpeg2_tvb_offset_seed(tvb, offset, len, CRC32_MPEG2_SEED) );
124 }
125
126 guint32
crc32_mpeg2_tvb_seed(tvbuff_t * tvb,guint len,guint32 seed)127 crc32_mpeg2_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
128 {
129 return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, seed) );
130 }
131
crc32_0x0AA725CF_tvb_offset_seed(tvbuff_t * tvb,guint offset,guint len,guint32 seed)132 guint32 crc32_0x0AA725CF_tvb_offset_seed(tvbuff_t *tvb,
133 guint offset, guint len, guint32 seed)
134 {
135 const guint8 *buf;
136
137 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
138 buf = tvb_get_ptr(tvb, offset, len);
139
140 return crc32_0x0AA725CF_seed(buf, len, seed);
141 }
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 */
155