/src/wireshark/epan/crc16-tvb.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
29 | | uint16_t crc16_ccitt_tvb(tvbuff_t *tvb, unsigned len) |
30 | 2.02k | { |
31 | 2.02k | const uint8_t *buf; |
32 | | |
33 | 2.02k | tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */ |
34 | 2.02k | buf = tvb_get_ptr(tvb, 0, len); |
35 | | |
36 | 2.02k | return crc16_ccitt(buf, len); |
37 | 2.02k | } |
38 | | |
39 | | uint16_t crc16_x25_ccitt_tvb(tvbuff_t *tvb, unsigned len) |
40 | 63 | { |
41 | 63 | const uint8_t *buf; |
42 | | |
43 | 63 | tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */ |
44 | 63 | buf = tvb_get_ptr(tvb, 0, len); |
45 | | |
46 | 63 | return crc16_x25_ccitt_seed(buf, len, 0xFFFF); |
47 | 63 | } |
48 | | |
49 | | uint16_t crc16_r3_ccitt_tvb(tvbuff_t *tvb, int offset, unsigned len) |
50 | 580 | { |
51 | 580 | const uint8_t *buf; |
52 | | |
53 | 580 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
54 | 580 | buf = tvb_get_ptr(tvb, offset, len); |
55 | | |
56 | 580 | return crc16_x25_ccitt_seed(buf, len, 0); |
57 | 580 | } |
58 | | |
59 | | uint16_t crc16_ccitt_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len) |
60 | 0 | { |
61 | 0 | const uint8_t *buf; |
62 | |
|
63 | 0 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
64 | 0 | buf = tvb_get_ptr(tvb, offset, len); |
65 | |
|
66 | 0 | return crc16_ccitt(buf, len); |
67 | 0 | } |
68 | | |
69 | | uint16_t crc16_ccitt_tvb_seed(tvbuff_t *tvb, unsigned len, uint16_t seed) |
70 | 446 | { |
71 | 446 | const uint8_t *buf; |
72 | | |
73 | 446 | tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */ |
74 | 446 | buf = tvb_get_ptr(tvb, 0, len); |
75 | | |
76 | 446 | return crc16_ccitt_seed(buf, len, seed); |
77 | 446 | } |
78 | | |
79 | | uint16_t crc16_ccitt_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t seed) |
80 | 0 | { |
81 | 0 | const uint8_t *buf; |
82 | |
|
83 | 0 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
84 | 0 | buf = tvb_get_ptr(tvb, offset, len); |
85 | |
|
86 | 0 | return crc16_ccitt_seed(buf, len, seed); |
87 | 0 | } |
88 | | |
89 | | uint16_t crc16_iso14443a_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len) |
90 | 0 | { |
91 | 0 | const uint8_t *buf; |
92 | |
|
93 | 0 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
94 | 0 | buf = tvb_get_ptr(tvb, offset, len); |
95 | |
|
96 | 0 | return crc16_iso14443a(buf, len); |
97 | 0 | } |
98 | | |
99 | | uint16_t crc16_usb_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len) |
100 | 0 | { |
101 | 0 | const uint8_t *buf; |
102 | |
|
103 | 0 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
104 | 0 | buf = tvb_get_ptr(tvb, offset, len); |
105 | |
|
106 | 0 | return crc16_usb(buf, len); |
107 | 0 | } |
108 | | |
109 | | uint16_t crc16_plain_tvb_offset(tvbuff_t *tvb, unsigned offset, unsigned len) |
110 | 0 | { |
111 | 0 | uint16_t crc = crc16_plain_init(); |
112 | 0 | const uint8_t *buf; |
113 | |
|
114 | 0 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
115 | 0 | buf = tvb_get_ptr(tvb, offset, len); |
116 | |
|
117 | 0 | crc = crc16_plain_update(crc, buf, len); |
118 | |
|
119 | 0 | return crc16_plain_finalize(crc); |
120 | 0 | } |
121 | | |
122 | | uint16_t crc16_plain_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t crc) |
123 | 0 | { |
124 | 0 | const uint8_t *buf; |
125 | |
|
126 | 0 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
127 | 0 | buf = tvb_get_ptr(tvb, offset, len); |
128 | |
|
129 | 0 | crc = crc16_plain_update(crc, buf, len); |
130 | |
|
131 | 0 | return crc16_plain_finalize(crc); |
132 | 0 | } |
133 | | |
134 | | uint16_t crc16_0x9949_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t seed) |
135 | 0 | { |
136 | 0 | const uint8_t *buf; |
137 | |
|
138 | 0 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
139 | 0 | buf = tvb_get_ptr(tvb, offset, len); |
140 | |
|
141 | 0 | return crc16_0x9949_seed(buf, len, seed); |
142 | 0 | } |
143 | | |
144 | | uint16_t crc16_0x3D65_tvb_offset_seed(tvbuff_t *tvb, unsigned offset, unsigned len, uint16_t seed) |
145 | 7 | { |
146 | 7 | const uint8_t *buf; |
147 | | |
148 | 7 | tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ |
149 | 7 | buf = tvb_get_ptr(tvb, offset, len); |
150 | | |
151 | 7 | return crc16_0x3D65_seed(buf, len, seed); |
152 | 7 | } |
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 | | */ |