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
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
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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();
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
)
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
)
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
)
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
160 * indent-tabs-mode: nil
163 * vi: set shiftwidth=4 tabstop=8 expandtab:
164 * :indentSize=4:tabSize=8:noTabs=true: