dcerpc: remove use-after-free (found by clang).
[metze/wireshark/wip.git] / epan / crc32-tvb.c
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  * 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.
12  *
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.
17  *
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.
21  *
22  * Credits:
23  *
24  * Table from Solomon Peachy
25  * Routine from Chris Waters
26  */
27
28 #include "config.h"
29
30 #include <glib.h>
31 #include <epan/tvbuff.h>
32 #include <wsutil/crc32.h>
33 #include <epan/crc32-tvb.h>
34
35
36 guint32
37 crc32_ccitt_tvb(tvbuff_t *tvb, guint len)
38 {
39         const guint8* buf;
40
41         tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
42         buf = tvb_get_ptr(tvb, 0, len);
43
44         return ( crc32_ccitt_seed(buf, len, CRC32_CCITT_SEED) );
45 }
46
47 guint32
48 crc32_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
49 {
50         const guint8* buf;
51
52         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
53         buf = tvb_get_ptr(tvb, offset, len);
54
55         return ( crc32_ccitt(buf, len) );
56 }
57
58 guint32
59 crc32_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
60 {
61         const guint8* buf;
62
63         tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
64         buf = tvb_get_ptr(tvb, 0, len);
65
66         return ( crc32_ccitt_seed(buf, len, seed) );
67 }
68
69 guint32
70 crc32_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len,
71                             guint32 seed)
72 {
73         const guint8* buf;
74
75         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
76         buf = tvb_get_ptr(tvb, offset, len);
77
78         return ( crc32_ccitt_seed(buf, len, seed) );
79 }
80
81 guint32
82 crc32c_tvb_offset_calculate(tvbuff_t *tvb, guint offset, guint len, guint32 seed)
83 {
84         const guint8* buf;
85
86         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
87         buf = tvb_get_ptr(tvb, offset, len);
88
89         return ( crc32c_calculate(buf, len, seed) );
90 }
91
92 /*
93  * IEEE 802.x version (Ethernet and 802.11, at least) - byte-swap
94  * the result of "crc32()".
95  *
96  * XXX - does this mean we should fetch the Ethernet and 802.11
97  * FCS with "tvb_get_letohl()" rather than "tvb_get_ntohl()",
98  * or is fetching it big-endian and byte-swapping the CRC done
99  * to cope with 802.x sending stuff out in reverse bit order?
100  */
101 guint32
102 crc32_802_tvb(tvbuff_t *tvb, guint len)
103 {
104         guint32 c_crc;
105
106         c_crc = crc32_ccitt_tvb(tvb, len);
107
108         /* Byte reverse. */
109         c_crc = GUINT32_SWAP_LE_BE(c_crc);
110
111         return ( c_crc );
112 }
113
114 guint32
115 crc32_mpeg2_tvb_offset_seed(tvbuff_t *tvb, guint offset,
116                             guint len, guint32 seed)
117 {
118         const guint8* buf;
119
120         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
121         buf = tvb_get_ptr(tvb, offset, len);
122
123         return ( crc32_mpeg2_seed(buf, len, seed) );
124 }
125
126 guint32
127 crc32_mpeg2_tvb(tvbuff_t *tvb, guint len)
128 {
129         return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, CRC32_MPEG2_SEED) );
130 }
131
132 guint32
133 crc32_mpeg2_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
134 {
135         return ( crc32_mpeg2_tvb_offset_seed(tvb, offset, len, CRC32_MPEG2_SEED) );
136 }
137
138 guint32
139 crc32_mpeg2_tvb_seed(tvbuff_t *tvb, guint len, guint32 seed)
140 {
141         return ( crc32_mpeg2_tvb_offset_seed(tvb, 0, len, seed) );
142 }
143
144 guint32 crc32_0x0AA725CF_tvb_offset_seed(tvbuff_t *tvb,
145                                          guint offset, guint len, guint32 seed)
146 {
147         const guint8 *buf;
148
149         tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
150         buf = tvb_get_ptr(tvb, offset, len);
151
152         return crc32_0x0AA725CF_seed(buf, len, seed);
153 }
154
155 /*
156  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
157  *
158  * Local variables:
159  * c-basic-offset: 8
160  * tab-width: 8
161  * indent-tabs-mode: t
162  * End:
163  *
164  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
165  * :indentSize=8:tabSize=8:noTabs=false:
166  */