1 /* crc16-tvb.c
2 * CRC-16 tvb routines
3 *
4 * 2004 Richard van der Hoff <richardv@mxtelecom.com>
5 *
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 *
12 * References:
13 * "A Painless Guide to CRC Error Detection Algorithms", Ross Williams
14 * http://www.repairfaq.org/filipg/LINK/F_crc_v3.html
15 *
16 * ITU-T Recommendation V.42 (2002), "Error-Correcting Procedures for
17 * DCEs using asynchronous-to-synchronous conversion", Para. 8.1.1.6.1
18 */
19
20 #include "config.h"
21
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>
27
28
crc16_ccitt_tvb(tvbuff_t * tvb,guint len)29 guint16 crc16_ccitt_tvb(tvbuff_t *tvb, guint len)
30 {
31 const guint8 *buf;
32
33 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
34 buf = tvb_get_ptr(tvb, 0, len);
35
36 return crc16_ccitt(buf, len);
37 }
38
crc16_x25_ccitt_tvb(tvbuff_t * tvb,guint len)39 guint16 crc16_x25_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 crc16_x25_ccitt_seed(buf, len, 0xFFFF);
47 }
48
crc16_r3_ccitt_tvb(tvbuff_t * tvb,int offset,guint len)49 guint16 crc16_r3_ccitt_tvb(tvbuff_t *tvb, int offset, guint len)
50 {
51 const guint8 *buf;
52
53 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
54 buf = tvb_get_ptr(tvb, offset, len);
55
56 return crc16_x25_ccitt_seed(buf, len, 0);
57 }
58
crc16_ccitt_tvb_offset(tvbuff_t * tvb,guint offset,guint len)59 guint16 crc16_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
60 {
61 const guint8 *buf;
62
63 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
64 buf = tvb_get_ptr(tvb, offset, len);
65
66 return crc16_ccitt(buf, len);
67 }
68
crc16_ccitt_tvb_seed(tvbuff_t * tvb,guint len,guint16 seed)69 guint16 crc16_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint16 seed)
70 {
71 const guint8 *buf;
72
73 tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */
74 buf = tvb_get_ptr(tvb, 0, len);
75
76 return crc16_ccitt_seed(buf, len, seed);
77 }
78
crc16_ccitt_tvb_offset_seed(tvbuff_t * tvb,guint offset,guint len,guint16 seed)79 guint16 crc16_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len, guint16 seed)
80 {
81 const guint8 *buf;
82
83 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
84 buf = tvb_get_ptr(tvb, offset, len);
85
86 return crc16_ccitt_seed(buf, len, seed);
87 }
88
crc16_iso14443a_tvb_offset(tvbuff_t * tvb,guint offset,guint len)89 guint16 crc16_iso14443a_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
90 {
91 const guint8 *buf;
92
93 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
94 buf = tvb_get_ptr(tvb, offset, len);
95
96 return crc16_iso14443a(buf, len);
97 }
98
crc16_usb_tvb_offset(tvbuff_t * tvb,guint offset,guint len)99 guint16 crc16_usb_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
100 {
101 const guint8 *buf;
102
103 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
104 buf = tvb_get_ptr(tvb, offset, len);
105
106 return crc16_usb(buf, len);
107 }
108
crc16_plain_tvb_offset(tvbuff_t * tvb,guint offset,guint len)109 guint16 crc16_plain_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
110 {
111 guint16 crc = crc16_plain_init();
112 const guint8 *buf;
113
114 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
115 buf = tvb_get_ptr(tvb, offset, len);
116
117 crc = crc16_plain_update(crc, buf, len);
118
119 return crc16_plain_finalize(crc);
120 }
121
crc16_plain_tvb_offset_seed(tvbuff_t * tvb,guint offset,guint len,guint16 crc)122 guint16 crc16_plain_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len, guint16 crc)
123 {
124 const guint8 *buf;
125
126 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
127 buf = tvb_get_ptr(tvb, offset, len);
128
129 crc = crc16_plain_update(crc, buf, len);
130
131 return crc16_plain_finalize(crc);
132 }
133
crc16_0x9949_tvb_offset_seed(tvbuff_t * tvb,guint offset,guint len,guint16 seed)134 guint16 crc16_0x9949_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len, guint16 seed)
135 {
136 const guint8 *buf;
137
138 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
139 buf = tvb_get_ptr(tvb, offset, len);
140
141 return crc16_0x9949_seed(buf, len, seed);
142 }
143
crc16_0x3D65_tvb_offset_seed(tvbuff_t * tvb,guint offset,guint len,guint16 seed)144 guint16 crc16_0x3D65_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len, guint16 seed)
145 {
146 const guint8 *buf;
147
148 tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */
149 buf = tvb_get_ptr(tvb, offset, len);
150
151 return crc16_0x3D65_seed(buf, len, seed);
152 }
153
154 /*
155 * Editor modelines - https://www.wireshark.org/tools/modelines.html
156 *
157 * Local variables:
158 * c-basic-offset: 4
159 * tab-width: 8
160 * indent-tabs-mode: nil
161 * End:
162 *
163 * vi: set shiftwidth=4 tabstop=8 expandtab:
164 * :indentSize=4:tabSize=8:noTabs=true:
165 */
166