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