aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_crc.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/util/crypto_crc.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/util/crypto_crc.c')
-rw-r--r--src/util/crypto_crc.c62
1 files changed, 31 insertions, 31 deletions
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c
index e547ef329..acc4a8ab4 100644
--- a/src/util/crypto_crc.c
+++ b/src/util/crypto_crc.c
@@ -30,7 +30,7 @@
30#include "platform.h" 30#include "platform.h"
31#include "gnunet_crypto_lib.h" 31#include "gnunet_crypto_lib.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-crc", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-crc", __VA_ARGS__)
34 34
35/* Avoid wasting space on 8-byte longs. */ 35/* Avoid wasting space on 8-byte longs. */
36#if UINT_MAX >= 0xffffffff 36#if UINT_MAX >= 0xffffffff
@@ -44,7 +44,7 @@ typedef unsigned long GNUNET_uLong;
44#define Z_NULL 0 44#define Z_NULL 0
45 45
46 46
47#define POLYNOMIAL (GNUNET_uLong)0xedb88320 47#define POLYNOMIAL (GNUNET_uLong) 0xedb88320
48static GNUNET_uLong crc_table[256]; 48static GNUNET_uLong crc_table[256];
49 49
50/* 50/*
@@ -53,7 +53,7 @@ static GNUNET_uLong crc_table[256];
53 * even on a table that someone else is using concurrently. 53 * even on a table that someone else is using concurrently.
54 */ 54 */
55static void 55static void
56crc_init() 56crc_init ()
57{ 57{
58 static int once; 58 static int once;
59 unsigned int i, j; 59 unsigned int i, j;
@@ -64,12 +64,12 @@ crc_init()
64 once = 1; 64 once = 1;
65 crc_table[0] = 0; 65 crc_table[0] = 0;
66 for (i = 128; i; i >>= 1) 66 for (i = 128; i; i >>= 1)
67 { 67 {
68 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0); 68 h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
69 /* h is now crc_table[i] */ 69 /* h is now crc_table[i] */
70 for (j = 0; j < 256; j += 2 * i) 70 for (j = 0; j < 256; j += 2 * i)
71 crc_table[i + j] = crc_table[j] ^ h; 71 crc_table[i + j] = crc_table[j] ^ h;
72 } 72 }
73} 73}
74 74
75/* 75/*
@@ -83,10 +83,10 @@ crc_init()
83 * property of detecting all burst errors of length 32 bits or less. 83 * property of detecting all burst errors of length 32 bits or less.
84 */ 84 */
85static GNUNET_uLong 85static GNUNET_uLong
86crc32(GNUNET_uLong crc, const char *buf, size_t len) 86crc32 (GNUNET_uLong crc, const char *buf, size_t len)
87{ 87{
88 crc_init(); 88 crc_init ();
89 GNUNET_assert(crc_table[255] != 0); 89 GNUNET_assert (crc_table[255] != 0);
90 crc ^= 0xffffffff; 90 crc ^= 0xffffffff;
91 while (len--) 91 while (len--)
92 crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff]; 92 crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
@@ -102,12 +102,12 @@ crc32(GNUNET_uLong crc, const char *buf, size_t len)
102 * @return the resulting CRC32 checksum 102 * @return the resulting CRC32 checksum
103 */ 103 */
104int32_t 104int32_t
105GNUNET_CRYPTO_crc32_n(const void *buf, size_t len) 105GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
106{ 106{
107 GNUNET_uLong crc; 107 GNUNET_uLong crc;
108 108
109 crc = crc32(0L, Z_NULL, 0); 109 crc = crc32 (0L, Z_NULL, 0);
110 crc = crc32(crc, (char *)buf, len); 110 crc = crc32 (crc, (char *) buf, len);
111 return crc; 111 return crc;
112} 112}
113 113
@@ -121,14 +121,14 @@ GNUNET_CRYPTO_crc32_n(const void *buf, size_t len)
121 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish() to get actual crc16) 121 * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish() to get actual crc16)
122 */ 122 */
123uint32_t 123uint32_t
124GNUNET_CRYPTO_crc16_step(uint32_t sum, const void *buf, size_t len) 124GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len)
125{ 125{
126 const uint16_t *hdr = buf; 126 const uint16_t *hdr = buf;
127 127
128 for (; len >= 2; len -= 2) 128 for (; len >= 2; len -= 2)
129 sum += *(hdr++); 129 sum += *(hdr++);
130 if (len == 1) 130 if (len == 1)
131 sum += (*hdr) & ntohs(0xFF00); 131 sum += (*hdr) & ntohs (0xFF00);
132 return sum; 132 return sum;
133} 133}
134 134
@@ -140,7 +140,7 @@ GNUNET_CRYPTO_crc16_step(uint32_t sum, const void *buf, size_t len)
140 * @return crc16 value 140 * @return crc16 value
141 */ 141 */
142uint16_t 142uint16_t
143GNUNET_CRYPTO_crc16_finish(uint32_t sum) 143GNUNET_CRYPTO_crc16_finish (uint32_t sum)
144{ 144{
145 sum = (sum >> 16) + (sum & 0xFFFF); 145 sum = (sum >> 16) + (sum & 0xFFFF);
146 sum += (sum >> 16); 146 sum += (sum >> 16);
@@ -157,12 +157,12 @@ GNUNET_CRYPTO_crc16_finish(uint32_t sum)
157 * @return crc16 value 157 * @return crc16 value
158 */ 158 */
159uint16_t 159uint16_t
160GNUNET_CRYPTO_crc16_n(const void *buf, size_t len) 160GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
161{ 161{
162 const uint16_t *hdr = buf; 162 const uint16_t *hdr = buf;
163 uint32_t sum = GNUNET_CRYPTO_crc16_step(0, hdr, len); 163 uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
164 164
165 return GNUNET_CRYPTO_crc16_finish(sum); 165 return GNUNET_CRYPTO_crc16_finish (sum);
166} 166}
167 167
168 168
@@ -175,8 +175,8 @@ GNUNET_CRYPTO_crc16_n(const void *buf, size_t len)
175 * @return crc8 value 175 * @return crc8 value
176 */ 176 */
177uint8_t 177uint8_t
178GNUNET_CRYPTO_crc8_n(const void *buf, 178GNUNET_CRYPTO_crc8_n (const void *buf,
179 size_t len) 179 size_t len)
180{ 180{
181 const uint8_t *data = buf; 181 const uint8_t *data = buf;
182 unsigned int crc = 0; 182 unsigned int crc = 0;
@@ -184,16 +184,16 @@ GNUNET_CRYPTO_crc8_n(const void *buf,
184 int j; 184 int j;
185 185
186 for (j = len; 0 != j; j--) 186 for (j = len; 0 != j; j--)
187 {
188 crc ^= (*data++ << 8);
189 for (i = 8; 0 != i; i--)
187 { 190 {
188 crc ^= (*data++ << 8); 191 if (0 != (crc & 0x8000))
189 for (i = 8; 0 != i; i--) 192 crc ^= (0x1070 << 3);
190 { 193 crc <<= 1;
191 if (0 != (crc & 0x8000))
192 crc ^= (0x1070 << 3);
193 crc <<= 1;
194 }
195 } 194 }
196 return (uint8_t)(crc >> 8); 195 }
196 return (uint8_t) (crc >> 8);
197} 197}
198 198
199 199