ee77803a3113d4102228e09136da027df5a83e28
[jlayton/wireshark.git] / epan / crc16-tvb.c
1 /* crc16-tvb.c
2  * CRC-16 tvb routines
3  *
4  * 2004 Richard van der Hoff <richardv@mxtelecom.com>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * References:
27  *  "A Painless Guide to CRC Error Detection Algorithms", Ross Williams
28  *      http://www.repairfaq.org/filipg/LINK/F_crc_v3.html
29  *
30  *  ITU-T Recommendation V.42 (2002), "Error-Correcting Procedures for
31  *      DCEs using asynchronous-to-synchronous conversion", Para. 8.1.1.6.1
32  */
33
34 #include "config.h"
35
36 #include <glib.h>
37 #include <epan/tvbuff.h>
38 #include <wsutil/crc16.h>
39 #include <wsutil/crc16-plain.h>
40
41
42 guint16 crc16_ccitt_tvb(tvbuff_t *tvb, guint len)
43 {
44     const guint8 *buf;
45
46     tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
47     buf = tvb_get_ptr(tvb, 0, len);
48
49     return crc16_ccitt(buf, len);
50 }
51
52 guint16 crc16_x25_ccitt_tvb(tvbuff_t *tvb, guint len)
53 {
54     const guint8 *buf;
55
56     tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
57     buf = tvb_get_ptr(tvb, 0, len);
58
59     return crc16_x25_ccitt(buf, len);
60 }
61
62 guint16 crc16_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
63 {
64     const guint8 *buf;
65
66     tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
67     buf = tvb_get_ptr(tvb, offset, len);
68
69     return crc16_ccitt(buf, len);
70 }
71
72 guint16 crc16_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint16 seed)
73 {
74     const guint8 *buf;
75
76     tvb_ensure_bytes_exist(tvb, 0, len);  /* len == -1 not allowed */
77     buf = tvb_get_ptr(tvb, 0, len);
78
79     return crc16_ccitt_seed(buf, len, seed);
80 }
81
82 guint16 crc16_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len, guint16 seed)
83 {
84     const guint8 *buf;
85
86     tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
87     buf = tvb_get_ptr(tvb, offset, len);
88
89     return crc16_ccitt_seed(buf, len, seed);
90 }
91
92 guint16 crc16_plain_tvb_offset(tvbuff_t *tvb, guint offset, guint len)
93 {
94     guint16 crc = crc16_plain_init();
95     const guint8 *buf;
96
97     tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
98     buf = tvb_get_ptr(tvb, offset, len);
99
100     crc = crc16_plain_update(crc, buf, len);
101     
102     return crc16_plain_finalize(crc);
103 }
104
105 guint16 crc16_plain_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len, guint16 crc)
106 {
107     const guint8 *buf;
108
109     tvb_ensure_bytes_exist(tvb, offset, len);  /* len == -1 not allowed */
110     buf = tvb_get_ptr(tvb, offset, len);
111
112     crc = crc16_plain_update(crc, buf, len);
113     
114     return crc16_plain_finalize(crc);
115 }