2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2006 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 For the actual CRC-32 code:
19 Copyright abandoned; this code is in the public domain.
20 Provided to GNUnet by peter@horizon.com
24 * @file util/crypto_crc.c
25 * @brief implementation of CRC16 and CRC32
26 * @author Christian Grothoff
29 #include "gnunet_crypto_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-crc", __VA_ARGS__)
33 /* Avoid wasting space on 8-byte longs. */
34 #if UINT_MAX >= 0xffffffff
35 typedef unsigned int GNUNET_uLong;
36 #elif ULONG_MAX >= 0xffffffff
37 typedef unsigned long GNUNET_uLong;
39 #error This compiler is not ANSI-compliant!
45 #define POLYNOMIAL (GNUNET_uLong)0xedb88320
46 static GNUNET_uLong crc_table[256];
49 * This routine writes each crc_table entry exactly once,
50 * with the correct final value. Thus, it is safe to call
51 * even on a table that someone else is using concurrently.
64 for (i = 128; i; i >>= 1)
66 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
67 /* h is now crc_table[i] */
68 for (j = 0; j < 256; j += 2 * i)
69 crc_table[i + j] = crc_table[j] ^ h;
74 * This computes the standard preset and inverted CRC, as used
75 * by most networking standards. Start by passing in an initial
76 * chaining value of 0, and then pass in the return value from the
77 * previous crc32() call. The final return value is the CRC.
78 * Note that this is a little-endian CRC, which is best used with
79 * data transmitted lsbit-first, and it should, itself, be appended
80 * to data in little-endian byte and bit order to preserve the
81 * property of detecting all burst errors of length 32 bits or less.
84 crc32 (GNUNET_uLong crc, const char *buf, size_t len)
87 GNUNET_assert (crc_table[255] != 0);
90 crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
91 return crc ^ 0xffffffff;
96 * Compute the CRC32 checksum for the first len bytes of the buffer.
98 * @param buf the data over which we're taking the CRC
99 * @param len the length of the buffer
100 * @return the resulting CRC32 checksum
103 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
107 crc = crc32 (0L, Z_NULL, 0);
108 crc = crc32 (crc, (char *) buf, len);
114 * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
116 * @param sum current sum, initially 0
117 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
118 * @param len number of bytes in hdr, must be multiple of 2
119 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish() to get actual crc16)
122 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len)
124 const uint16_t *hdr = buf;
125 for (; len >= 2; len -= 2)
128 sum += (*hdr) & ntohs(0xFF00);
134 * Convert results from #GNUNET_CRYPTO_crc16_step() to final crc16.
136 * @param sum cummulative sum
137 * @return crc16 value
140 GNUNET_CRYPTO_crc16_finish (uint32_t sum)
142 sum = (sum >> 16) + (sum & 0xFFFF);
150 * Calculate the checksum of a buffer in one step.
152 * @param buf buffer to calculate CRC over (must be 16-bit aligned)
153 * @param len number of bytes in hdr, must be multiple of 2
154 * @return crc16 value
157 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
159 const uint16_t *hdr = buf;
160 uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
162 return GNUNET_CRYPTO_crc16_finish (sum);
168 * Calculate the checksum of a buffer in one step.
170 * @param buf buffer to calculate CRC over
171 * @param len number of bytes in @a buf
175 GNUNET_CRYPTO_crc8_n (const void *buf,
178 const uint8_t *data = buf;
179 unsigned int crc = 0;
183 for (j = len; 0 != j; j--)
185 crc ^= (*data++ << 8);
186 for (i = 8; 0 != i; i--)
188 if (0 != (crc & 0x8000))
189 crc ^= (0x1070 << 3);
193 return (uint8_t) (crc >> 8);
197 /* end of crypto_crc.c */