2 * CRC-32 tvbuff routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Table from Solomon Peachy
25 * Routine from Chris Waters
31 #include <epan/tvbuff.h>
32 #include <wsutil/crc32.h>
33 #include <epan/crc32-tvb.h>
37 crc32_ccitt_tvb(tvbuff_t *tvb, guint len)
41 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
42 buf = tvb_get_ptr(tvb, 0, len);
44 return ( crc32_ccitt_seed(buf, len, CRC32_CCITT_SEED) );
48 crc32_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
52 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
53 buf = tvb_get_ptr(tvb, offset, len);
55 return ( crc32_ccitt(buf, len) );
59 crc32_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
63 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
64 buf = tvb_get_ptr(tvb, 0, len);
66 return ( crc32_ccitt_seed(buf, len, seed) );
70 crc32_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len,
75 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
76 buf = tvb_get_ptr(tvb, offset, len);
78 return ( crc32_ccitt_seed(buf, len, seed) );
82 * IEEE 802.x version (Ethernet and 802.11, at least) - byte-swap
83 * the result of "crc32()".
85 * XXX - does this mean we should fetch the Ethernet and 802.11
86 * FCS with "tvb_get_letohl()" rather than "tvb_get_ntohl()",
87 * or is fetching it big-endian and byte-swapping the CRC done
88 * to cope with 802.x sending stuff out in reverse bit order?
91 crc32_802_tvb(tvbuff_t *tvb, guint len)
95 c_crc = crc32_ccitt_tvb(tvb, len);
98 c_crc = GUINT32_SWAP_LE_BE(c_crc);
104 crc32_mpeg2_tvb_offset_seed(tvbuff_t *tvb, guint offset,
105 guint len, guint32 seed)
109 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
110 buf = tvb_get_ptr(tvb, offset, len);
112 return ( crc32_mpeg2_seed(buf, len, seed) );
116 crc32_mpeg2_tvb(tvbuff_t *tvb, guint len)
118 return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, CRC32_MPEG2_SEED) );
122 crc32_mpeg2_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
124 return ( crc32_mpeg2_tvb_offset_seed(tvb, offset, len, CRC32_MPEG2_SEED) );
128 crc32_mpeg2_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
130 return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, seed) );
133 guint32 crc32_0x0AA725CF_tvb_offset_seed(tvbuff_t *tvb,
134 guint offset, guint len, guint32 seed)
138 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
139 buf = tvb_get_ptr(tvb, offset, len);
141 return crc32_0x0AA725CF_seed(buf, len, seed);