2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2006 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
20 For the actual CRC code:
21 Copyright abandoned; this code is in the public domain.
22 Provided to GNUnet by peter@horizon.com
26 * @file monkey/bug_crypto_crc.c
27 * @brief implementation of CRC32 (this code has been copied from GNUnet util source directory, and modified to be Seaspider friendly)
28 * @author Christian Grothoff, Safey A.Halim
39 #define POLYNOMIAL (unsigned long)0xedb88320
40 static unsigned long crc_table[256];
43 * This routine writes each crc_table entry exactly once,
44 * with the ccorrect final value. Thus, it is safe to call
45 * even on a table that someone else is using concurrently.
58 for (i = 128; i; i >>= 1)
60 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
61 /* h is now crc_table[i] */
62 for (j = 0; j < 256; j += 2 * i)
63 crc_table[i + j] = crc_table[j] ^ h;
68 * This computes the standard preset and inverted CRC, as used
69 * by most networking standards. Start by passing in an initial
70 * chaining value of 0, and then pass in the return value from the
71 * previous crc32() call. The final return value is the CRC.
72 * Note that this is a little-endian CRC, which is best used with
73 * data transmitted lsbit-first, and it should, itself, be appended
74 * to data in little-endian byte and bit order to preserve the
75 * property of detecting all burst errors of length 32 bits or less.
78 crc_go (unsigned long crc, const char *buf, size_t len)
81 assert (crc_table[255] != 0);
84 crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
85 return crc ^ 0xffffffff;
90 * Compute the CRC32 checksum for the first len bytes of the buffer.
92 * @param buf the data over which we're taking the CRC
93 * @param len the length of the buffer
94 * @return the resulting CRC32 checksum
97 crc32_n (const void *buf, size_t len)
100 crc = crc_go (0L, Z_NULL, 0);
101 crc = crc_go (crc, (char *) buf, len);
110 for (i = 0; i < 1024; i++)
114 for (i = 0; i < 1024; i++)
116 printf("%d\n", crc32_n (&buf[i], 1024 - i));
121 /* end of bug_crypto_crc.c */