2 * CRC-32 tvbuff routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
12 * Table from Solomon Peachy
13 * Routine from Chris Waters
19 #include <epan/tvbuff.h>
20 #include <wsutil/crc32.h>
21 #include <epan/crc32-tvb.h>
25 crc32_ccitt_tvb(tvbuff_t
*tvb
, unsigned len
)
29 tvb_ensure_bytes_exist(tvb
, 0, len
); /* len == -1 not allowed */
30 buf
= tvb_get_ptr(tvb
, 0, len
);
32 return ( crc32_ccitt_seed(buf
, len
, CRC32_CCITT_SEED
) );
36 crc32_ccitt_tvb_offset(tvbuff_t
*tvb
, unsigned offset
, unsigned len
)
40 tvb_ensure_bytes_exist(tvb
, offset
, len
); /* len == -1 not allowed */
41 buf
= tvb_get_ptr(tvb
, offset
, len
);
43 return ( crc32_ccitt(buf
, len
) );
47 crc32_ccitt_tvb_seed(tvbuff_t
*tvb
, unsigned len
, uint32_t seed
)
51 tvb_ensure_bytes_exist(tvb
, 0, len
); /* len == -1 not allowed */
52 buf
= tvb_get_ptr(tvb
, 0, len
);
54 return ( crc32_ccitt_seed(buf
, len
, seed
) );
58 crc32_ccitt_tvb_offset_seed(tvbuff_t
*tvb
, unsigned offset
, unsigned len
,
63 tvb_ensure_bytes_exist(tvb
, offset
, len
); /* len == -1 not allowed */
64 buf
= tvb_get_ptr(tvb
, offset
, len
);
66 return ( crc32_ccitt_seed(buf
, len
, seed
) );
70 crc32c_tvb_offset_calculate(tvbuff_t
*tvb
, unsigned offset
, unsigned len
, uint32_t seed
)
74 tvb_ensure_bytes_exist(tvb
, offset
, len
); /* len == -1 not allowed */
75 buf
= tvb_get_ptr(tvb
, offset
, len
);
77 return ( crc32c_calculate(buf
, len
, seed
) );
81 * IEEE 802.x version (Ethernet and 802.11, at least) - byte-swap
82 * the result of "crc32()".
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?
90 crc32_802_tvb(tvbuff_t
*tvb
, unsigned len
)
94 c_crc
= crc32_ccitt_tvb(tvb
, len
);
97 c_crc
= GUINT32_SWAP_LE_BE(c_crc
);
103 crc32_mpeg2_tvb_offset_seed(tvbuff_t
*tvb
, unsigned offset
,
104 unsigned len
, uint32_t seed
)
108 tvb_ensure_bytes_exist(tvb
, offset
, len
); /* len == -1 not allowed */
109 buf
= tvb_get_ptr(tvb
, offset
, len
);
111 return ( crc32_mpeg2_seed(buf
, len
, seed
) );
115 crc32_mpeg2_tvb(tvbuff_t
*tvb
, unsigned len
)
117 return ( crc32_mpeg2_tvb_offset_seed(tvb
, 0, len
, CRC32_MPEG2_SEED
) );
121 crc32_mpeg2_tvb_offset(tvbuff_t
*tvb
, unsigned offset
, unsigned len
)
123 return ( crc32_mpeg2_tvb_offset_seed(tvb
, offset
, len
, CRC32_MPEG2_SEED
) );
127 crc32_mpeg2_tvb_seed(tvbuff_t
*tvb
, unsigned len
, uint32_t seed
)
129 return ( crc32_mpeg2_tvb_offset_seed(tvb
, 0, len
, seed
) );
132 uint32_t crc32_0x0AA725CF_tvb_offset_seed(tvbuff_t
*tvb
,
133 unsigned offset
, unsigned len
, uint32_t seed
)
137 tvb_ensure_bytes_exist(tvb
, offset
, len
); /* len == -1 not allowed */
138 buf
= tvb_get_ptr(tvb
, offset
, len
);
140 return crc32_0x0AA725CF_seed(buf
, len
, seed
);
144 * Editor modelines - https://www.wireshark.org/tools/modelines.html
149 * indent-tabs-mode: t
152 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
153 * :indentSize=8:tabSize=8:noTabs=false: