more decompress
[wireshark-sm.git] / epan / crc16-tvb.c
blobaec5115527ea5e7387e4e908dce6daa3f766e561
1 /* crc16-tvb.c
2 * CRC-16 tvb routines
4 * 2004 Richard van der Hoff <richardv@mxtelecom.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
12 * References:
13 * "A Painless Guide to CRC Error Detection Algorithms", Ross Williams
14 * http://www.repairfaq.org/filipg/LINK/F_crc_v3.html
16 * ITU-T Recommendation V.42 (2002), "Error-Correcting Procedures for
17 * DCEs using asynchronous-to-synchronous conversion", Para. 8.1.1.6.1
20 #include "config.h"
22 #include <glib.h>
23 #include <epan/tvbuff.h>
24 #include <wsutil/crc16.h>
25 #include <epan/crc16-tvb.h>
26 #include <wsutil/crc16-plain.h>
29 uint16_t crc16_ccitt_tvb(tvbuff_t *tvb, unsigned len)
31 const uint8_t *buf;
33 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
34 buf = tvb_get_ptr(tvb, 0, len);
36 return crc16_ccitt(buf, len);
39 uint16_t crc16_x25_ccitt_tvb(tvbuff_t *tvb, unsigned len)
41 const uint8_t *buf;
43 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
44 buf = tvb_get_ptr(tvb, 0, len);
46 return crc16_x25_ccitt_seed(buf, len, 0xFFFF);
49 uint16_t crc16_r3_ccitt_tvb(tvbuff_t *tvb, int offset, unsigned len)
51 const uint8_t *buf;
53 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
54 buf = tvb_get_ptr(tvb, offset, len);
56 return crc16_x25_ccitt_seed(buf, len, 0);
59 uint16_t crc16_ccitt_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len)
61 const uint8_t *buf;
63 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
64 buf = tvb_get_ptr(tvb, offset, len);
66 return crc16_ccitt(buf, len);
69 uint16_t crc16_ccitt_tvb_seed(tvbuff_t *tvb, unsigned len, uint16_t seed)
71 const uint8_t *buf;
73 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
74 buf = tvb_get_ptr(tvb, 0, len);
76 return crc16_ccitt_seed(buf, len, seed);
79 uint16_t crc16_ccitt_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t seed)
81 const uint8_t *buf;
83 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
84 buf = tvb_get_ptr(tvb, offset, len);
86 return crc16_ccitt_seed(buf, len, seed);
89 uint16_t crc16_iso14443a_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len)
91 const uint8_t *buf;
93 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
94 buf = tvb_get_ptr(tvb, offset, len);
96 return crc16_iso14443a(buf, len);
99 uint16_t crc16_usb_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len)
101 const uint8_t *buf;
103 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
104 buf = tvb_get_ptr(tvb, offset, len);
106 return crc16_usb(buf, len);
109 uint16_t crc16_plain_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len)
111 uint16_t crc = crc16_plain_init();
112 const uint8_t *buf;
114 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
115 buf = tvb_get_ptr(tvb, offset, len);
117 crc = crc16_plain_update(crc, buf, len);
119 return crc16_plain_finalize(crc);
122 uint16_t crc16_plain_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t crc)
124 const uint8_t *buf;
126 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
127 buf = tvb_get_ptr(tvb, offset, len);
129 crc = crc16_plain_update(crc, buf, len);
131 return crc16_plain_finalize(crc);
134 uint16_t crc16_0x9949_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t seed)
136 const uint8_t *buf;
138 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
139 buf = tvb_get_ptr(tvb, offset, len);
141 return crc16_0x9949_seed(buf, len, seed);
144 uint16_t crc16_0x3D65_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t seed)
146 const uint8_t *buf;
148 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
149 buf = tvb_get_ptr(tvb, offset, len);
151 return crc16_0x3D65_seed(buf, len, seed);
155 * Editor modelines - https://www.wireshark.org/tools/modelines.html
157 * Local variables:
158 * c-basic-offset: 4
159 * tab-width: 8
160 * indent-tabs-mode: nil
161 * End:
163 * vi: set shiftwidth=4 tabstop=8 expandtab:
164 * :indentSize=4:tabSize=8:noTabs=true: