From 5359494b1f7fbfa4fce2e98e4d9e579a0fdafff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Bj=C3=B8rlykke?= Date: Wed, 31 Aug 2011 09:00:54 +0000 Subject: Second try to move crc routines to libwsutil. This time keep the tvb routines in epan. Now we can use common crc routines outside epan. svn path=/trunk/; revision=38810 --- epan/crc16-tvb.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 epan/crc16-tvb.c (limited to 'epan/crc16-tvb.c') diff --git a/epan/crc16-tvb.c b/epan/crc16-tvb.c new file mode 100644 index 0000000000..d0271626f7 --- /dev/null +++ b/epan/crc16-tvb.c @@ -0,0 +1,106 @@ +/* crc16-tvb.c + * CRC-16 tvb routines + * + * 2004 Richard van der Hoff + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * References: + * "A Painless Guide to CRC Error Detection Algorithms", Ross Williams + * http://www.repairfaq.org/filipg/LINK/F_crc_v3.html + * + * ITU-T Recommendation V.42 (2002), "Error-Correcting Procedures for + * DCEs using asynchronous-to-synchronous conversion", Para. 8.1.1.6.1 + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include + + +guint16 crc16_ccitt_tvb(tvbuff_t *tvb, guint len) +{ + const guint8 *buf; + + tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */ + buf = tvb_get_ptr(tvb, 0, len); + + return crc16_ccitt(buf, len); +} + +guint16 crc16_x25_ccitt_tvb(tvbuff_t *tvb, guint len) +{ + const guint8 *buf; + + tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */ + buf = tvb_get_ptr(tvb, 0, len); + + return crc16_x25_ccitt(buf, len); +} + +guint16 crc16_ccitt_tvb_offset(tvbuff_t *tvb, guint offset, guint len) +{ + const guint8 *buf; + + tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ + buf = tvb_get_ptr(tvb, offset, len); + + return crc16_ccitt(buf, len); +} + +guint16 crc16_ccitt_tvb_seed(tvbuff_t *tvb, guint len, guint16 seed) +{ + const guint8 *buf; + + tvb_ensure_bytes_exist(tvb, 0, len); /* len == -1 not allowed */ + buf = tvb_get_ptr(tvb, 0, len); + + return crc16_ccitt_seed(buf, len, seed); +} + +guint16 crc16_ccitt_tvb_offset_seed(tvbuff_t *tvb, guint offset, guint len, guint16 seed) +{ + const guint8 *buf; + + tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ + buf = tvb_get_ptr(tvb, offset, len); + + return crc16_ccitt_seed(buf, len, seed); +} + +guint16 crc16_plain_tvb_offset(tvbuff_t *tvb, guint offset, guint len) +{ + guint16 crc = crc16_plain_init(); + const guint8 *buf; + + tvb_ensure_bytes_exist(tvb, offset, len); /* len == -1 not allowed */ + buf = tvb_get_ptr(tvb, offset, len); + + crc = crc16_plain_update(crc, buf, len); + + return crc16_plain_finalize(crc); +} + -- cgit v1.2.3