glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / crypto_crc.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2004, 2006 GNUnet e.V.
4
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.
9
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.
14
15      For the actual CRC-32 code:
16      Copyright abandoned; this code is in the public domain.
17      Provided to GNUnet by peter@horizon.com
18 */
19
20 /**
21  * @file util/crypto_crc.c
22  * @brief implementation of CRC16 and CRC32
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_crypto_lib.h"
27
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-crc", __VA_ARGS__)
29
30 /* Avoid wasting space on 8-byte longs. */
31 #if UINT_MAX >= 0xffffffff
32 typedef unsigned int GNUNET_uLong;
33 #elif ULONG_MAX >= 0xffffffff
34 typedef unsigned long GNUNET_uLong;
35 #else
36 #error This compiler is not ANSI-compliant!
37 #endif
38
39 #define Z_NULL  0
40
41
42 #define POLYNOMIAL (GNUNET_uLong)0xedb88320
43 static GNUNET_uLong crc_table[256];
44
45 /*
46  * This routine writes each crc_table entry exactly once,
47  * with the correct final value.  Thus, it is safe to call
48  * even on a table that someone else is using concurrently.
49  */
50 static void
51 crc_init ()
52 {
53   static int once;
54   unsigned int i, j;
55   GNUNET_uLong h = 1;
56
57   if (once)
58     return;
59   once = 1;
60   crc_table[0] = 0;
61   for (i = 128; i; i >>= 1)
62   {
63     h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
64     /* h is now crc_table[i] */
65     for (j = 0; j < 256; j += 2 * i)
66       crc_table[i + j] = crc_table[j] ^ h;
67   }
68 }
69
70 /*
71  * This computes the standard preset and inverted CRC, as used
72  * by most networking standards.  Start by passing in an initial
73  * chaining value of 0, and then pass in the return value from the
74  * previous crc32() call.  The final return value is the CRC.
75  * Note that this is a little-endian CRC, which is best used with
76  * data transmitted lsbit-first, and it should, itself, be appended
77  * to data in little-endian byte and bit order to preserve the
78  * property of detecting all burst errors of length 32 bits or less.
79  */
80 static GNUNET_uLong
81 crc32 (GNUNET_uLong crc, const char *buf, size_t len)
82 {
83   crc_init ();
84   GNUNET_assert (crc_table[255] != 0);
85   crc ^= 0xffffffff;
86   while (len--)
87     crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
88   return crc ^ 0xffffffff;
89 }
90
91
92 /**
93  * Compute the CRC32 checksum for the first len bytes of the buffer.
94  *
95  * @param buf the data over which we're taking the CRC
96  * @param len the length of the buffer
97  * @return the resulting CRC32 checksum
98  */
99 int32_t
100 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
101 {
102   GNUNET_uLong crc;
103
104   crc = crc32 (0L, Z_NULL, 0);
105   crc = crc32 (crc, (char *) buf, len);
106   return crc;
107 }
108
109
110 /**
111  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
112  *
113  * @param sum current sum, initially 0
114  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
115  * @param len number of bytes in hdr, must be multiple of 2
116  * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish() to get actual crc16)
117  */
118 uint32_t
119 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len)
120 {
121   const uint16_t *hdr = buf;
122   for (; len >= 2; len -= 2)
123     sum += *(hdr++);
124   if (len == 1)
125     sum += (*hdr) & ntohs(0xFF00);
126   return sum;
127 }
128
129
130 /**
131  * Convert results from #GNUNET_CRYPTO_crc16_step() to final crc16.
132  *
133  * @param sum cummulative sum
134  * @return crc16 value
135  */
136 uint16_t
137 GNUNET_CRYPTO_crc16_finish (uint32_t sum)
138 {
139   sum = (sum >> 16) + (sum & 0xFFFF);
140   sum += (sum >> 16);
141
142   return ~sum;
143 }
144
145
146 /**
147  * Calculate the checksum of a buffer in one step.
148  *
149  * @param buf buffer to  calculate CRC over (must be 16-bit aligned)
150  * @param len number of bytes in hdr, must be multiple of 2
151  * @return crc16 value
152  */
153 uint16_t
154 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
155 {
156   const uint16_t *hdr = buf;
157   uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
158
159   return GNUNET_CRYPTO_crc16_finish (sum);
160 }
161
162
163 /**
164  * @ingroup hash
165  * Calculate the checksum of a buffer in one step.
166  *
167  * @param buf buffer to calculate CRC over
168  * @param len number of bytes in @a buf
169  * @return crc8 value
170  */
171 uint8_t
172 GNUNET_CRYPTO_crc8_n (const void *buf,
173                       size_t len)
174 {
175   const uint8_t *data = buf;
176   unsigned int crc = 0;
177   int i;
178   int j;
179
180   for (j = len; 0 != j; j--)
181   {
182     crc ^= (*data++ << 8);
183     for (i = 8; 0 != i; i--)
184     {
185       if (0 != (crc & 0x8000))
186         crc ^= (0x1070 << 3);
187       crc <<= 1;
188     }
189   }
190   return (uint8_t) (crc >> 8);
191 }
192
193
194 /* end of crypto_crc.c */