X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Futil%2Fcrypto_crc.c;h=d7f5f7fc8aa13d644df527a4d69bd2d14daf8eda;hb=0945dcf2c250dea65d520ef26f9917e9be3ac4ac;hp=cda6243bee2723d88494dba18397cd187aba4efc;hpb=427dd6f998fb1fde515a3b5c800f9f6d308197b2;p=oweals%2Fgnunet.git diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c index cda6243be..d7f5f7fc8 100644 --- a/src/util/crypto_crc.c +++ b/src/util/crypto_crc.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2001, 2002, 2003, 2004, 2006 Christian Grothoff (and other contributing authors) + Copyright (C) 2001, 2002, 2003, 2004, 2006 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. For the actual CRC-32 code: Copyright abandoned; this code is in the public domain. @@ -28,15 +28,15 @@ * @author Christian Grothoff */ #include "platform.h" -#include "gnunet_util_lib.h" +#include "gnunet_crypto_lib.h" #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__) /* Avoid wasting space on 8-byte longs. */ #if UINT_MAX >= 0xffffffff -typedef unsigned int uLong; +typedef unsigned int GNUNET_uLong; #elif ULONG_MAX >= 0xffffffff -typedef unsigned long uLong; +typedef unsigned long GNUNET_uLong; #else #error This compiler is not ANSI-compliant! #endif @@ -44,8 +44,8 @@ typedef unsigned long uLong; #define Z_NULL 0 -#define POLYNOMIAL (uLong)0xedb88320 -static uLong crc_table[256]; +#define POLYNOMIAL (GNUNET_uLong)0xedb88320 +static GNUNET_uLong crc_table[256]; /* * This routine writes each crc_table entry exactly once, @@ -57,7 +57,7 @@ crc_init () { static int once; unsigned int i, j; - uLong h = 1; + GNUNET_uLong h = 1; if (once) return; @@ -82,8 +82,8 @@ crc_init () * to data in little-endian byte and bit order to preserve the * property of detecting all burst errors of length 32 bits or less. */ -static uLong -crc32 (uLong crc, const char *buf, size_t len) +static GNUNET_uLong +crc32 (GNUNET_uLong crc, const char *buf, size_t len) { crc_init (); GNUNET_assert (crc_table[255] != 0); @@ -104,7 +104,7 @@ crc32 (uLong crc, const char *buf, size_t len) int32_t GNUNET_CRYPTO_crc32_n (const void *buf, size_t len) { - uLong crc; + GNUNET_uLong crc; crc = crc32 (0L, Z_NULL, 0); crc = crc32 (crc, (char *) buf, len); @@ -118,7 +118,7 @@ GNUNET_CRYPTO_crc32_n (const void *buf, size_t len) * @param sum current sum, initially 0 * @param buf buffer to calculate CRC over (must be 16-bit aligned) * @param len number of bytes in hdr, must be multiple of 2 - * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16) + * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish() to get actual crc16) */ uint32_t GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len) @@ -133,7 +133,7 @@ GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len) /** - * Convert results from GNUNET_CRYPTO_crc16_step to final crc16. + * Convert results from #GNUNET_CRYPTO_crc16_step() to final crc16. * * @param sum cummulative sum * @return crc16 value @@ -165,5 +165,35 @@ GNUNET_CRYPTO_crc16_n (const void *buf, size_t len) } +/** + * @ingroup hash + * Calculate the checksum of a buffer in one step. + * + * @param buf buffer to calculate CRC over + * @param len number of bytes in @a buf + * @return crc8 value + */ +uint8_t +GNUNET_CRYPTO_crc8_n (const void *buf, + size_t len) +{ + const uint8_t *data = buf; + unsigned int crc = 0; + int i; + int j; + + for (j = len; 0 != j; j--) + { + crc ^= (*data++ << 8); + for (i = 8; 0 != i; i--) + { + if (0 != (crc & 0x8000)) + crc ^= (0x1070 << 3); + crc <<= 1; + } + } + return (uint8_t) (crc >> 8); +} + /* end of crypto_crc.c */