From robionekenobi via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9375 :
[metze/wireshark/wip.git] / epan / crc32-tvb.c
1 /* crc32-tvb.c
2  * CRC-32 tvbuff routines
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  *
24  * Credits:
25  *
26  * Table from Solomon Peachy
27  * Routine from Chris Waters
28  */
29
30 #include "config.h"
31
32 #include <glib.h>
33 #include <epan/tvbuff.h>
34 #include <wsutil/crc32.h>
35 #include <epan/crc32-tvb.h>
36
37
38 guint32
39 crc32_ccitt_tvb(tvbuff_t *tvb, guint len)
40 {
41         const guint8* buf;
42
43         tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
44         buf = tvb_get_ptr(tvb, 0, len);
45
46         return ( crc32_ccitt_seed(buf, len, CRC32_CCITT_SEED) );
47 }
48
49 guint32
50 crc32_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
51 {
52         const guint8* buf;
53
54         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
55         buf = tvb_get_ptr(tvb, offset, len);
56
57         return ( crc32_ccitt(buf, len) );
58 }
59
60 guint32
61 crc32_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
62 {
63         const guint8* buf;
64
65         tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
66         buf = tvb_get_ptr(tvb, 0, len);
67
68         return ( crc32_ccitt_seed(buf, len, seed) );
69 }
70
71 guint32
72 crc32_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len,
73                             guint32 seed)
74 {
75         const guint8* buf;
76
77         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
78         buf = tvb_get_ptr(tvb, offset, len);
79
80         return ( crc32_ccitt_seed(buf, len, seed) );
81 }
82
83 /*
84  * IEEE 802.x version (Ethernet and 802.11, at least) - byte-swap
85  * the result of "crc32()".
86  *
87  * XXX - does this mean we should fetch the Ethernet and 802.11
88  * FCS with "tvb_get_letohl()" rather than "tvb_get_ntohl()",
89  * or is fetching it big-endian and byte-swapping the CRC done
90  * to cope with 802.x sending stuff out in reverse bit order?
91  */
92 guint32
93 crc32_802_tvb(tvbuff_t *tvb, guint len)
94 {
95         guint32 c_crc;
96
97         c_crc = crc32_ccitt_tvb(tvb, len);
98
99         /* Byte reverse. */
100         c_crc = GUINT32_SWAP_LE_BE(c_crc);
101
102         return ( c_crc );
103 }
104
105 guint32
106 crc32_mpeg2_tvb_offset_seed(tvbuff_t *tvb, guint offset,
107                                        guint len, guint32 seed)
108 {
109         const guint8* buf;
110
111         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
112         buf = tvb_get_ptr(tvb, offset, len);
113
114         return ( crc32_mpeg2_seed(buf, len, seed) );
115 }
116
117 guint32
118 crc32_mpeg2_tvb(tvbuff_t *tvb, guint len)
119 {
120     return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, CRC32_MPEG2_SEED) );
121 }
122
123 guint32
124 crc32_mpeg2_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
125 {
126     return ( crc32_mpeg2_tvb_offset_seed(tvb, offset, len, CRC32_MPEG2_SEED) );
127 }
128
129 guint32
130 crc32_mpeg2_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
131 {
132     return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, seed) );
133 }
134
135 guint32 crc32_0x0AA725CF_tvb_offset_seed(tvbuff_t *tvb,
136                                             guint offset, guint len, guint32 seed)
137 {
138     const guint8 *buf;
139
140     tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
141     buf = tvb_get_ptr(tvb, offset, len);
142
143     return crc32_0x0AA725CF_seed(buf, len, seed);
144 }