diff options
Diffstat (limited to 'src/util/crypto_crc.c')
-rw-r--r-- | src/util/crypto_crc.c | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c index 3d8e23e24..e547ef329 100644 --- a/src/util/crypto_crc.c +++ b/src/util/crypto_crc.c | |||
@@ -11,7 +11,7 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
@@ -20,7 +20,7 @@ | |||
20 | For the actual CRC-32 code: | 20 | For the actual CRC-32 code: |
21 | Copyright abandoned; this code is in the public domain. | 21 | Copyright abandoned; this code is in the public domain. |
22 | Provided to GNUnet by peter@horizon.com | 22 | Provided to GNUnet by peter@horizon.com |
23 | */ | 23 | */ |
24 | 24 | ||
25 | /** | 25 | /** |
26 | * @file util/crypto_crc.c | 26 | * @file 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 |
@@ -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 | */ |
55 | static void | 55 | static void |
56 | crc_init () | 56 | crc_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 | */ |
85 | static GNUNET_uLong | 85 | static GNUNET_uLong |
86 | crc32 (GNUNET_uLong crc, const char *buf, size_t len) | 86 | crc32(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 | */ |
104 | int32_t | 104 | int32_t |
105 | GNUNET_CRYPTO_crc32_n (const void *buf, size_t len) | 105 | GNUNET_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,9 +121,10 @@ 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 | */ |
123 | uint32_t | 123 | uint32_t |
124 | GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len) | 124 | GNUNET_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 | for (; len >= 2; len -= 2) | 128 | for (; len >= 2; len -= 2) |
128 | sum += *(hdr++); | 129 | sum += *(hdr++); |
129 | if (len == 1) | 130 | if (len == 1) |
@@ -139,7 +140,7 @@ GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len) | |||
139 | * @return crc16 value | 140 | * @return crc16 value |
140 | */ | 141 | */ |
141 | uint16_t | 142 | uint16_t |
142 | GNUNET_CRYPTO_crc16_finish (uint32_t sum) | 143 | GNUNET_CRYPTO_crc16_finish(uint32_t sum) |
143 | { | 144 | { |
144 | sum = (sum >> 16) + (sum & 0xFFFF); | 145 | sum = (sum >> 16) + (sum & 0xFFFF); |
145 | sum += (sum >> 16); | 146 | sum += (sum >> 16); |
@@ -156,12 +157,12 @@ GNUNET_CRYPTO_crc16_finish (uint32_t sum) | |||
156 | * @return crc16 value | 157 | * @return crc16 value |
157 | */ | 158 | */ |
158 | uint16_t | 159 | uint16_t |
159 | GNUNET_CRYPTO_crc16_n (const void *buf, size_t len) | 160 | GNUNET_CRYPTO_crc16_n(const void *buf, size_t len) |
160 | { | 161 | { |
161 | const uint16_t *hdr = buf; | 162 | const uint16_t *hdr = buf; |
162 | uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len); | 163 | uint32_t sum = GNUNET_CRYPTO_crc16_step(0, hdr, len); |
163 | 164 | ||
164 | return GNUNET_CRYPTO_crc16_finish (sum); | 165 | return GNUNET_CRYPTO_crc16_finish(sum); |
165 | } | 166 | } |
166 | 167 | ||
167 | 168 | ||
@@ -174,8 +175,8 @@ GNUNET_CRYPTO_crc16_n (const void *buf, size_t len) | |||
174 | * @return crc8 value | 175 | * @return crc8 value |
175 | */ | 176 | */ |
176 | uint8_t | 177 | uint8_t |
177 | GNUNET_CRYPTO_crc8_n (const void *buf, | 178 | GNUNET_CRYPTO_crc8_n(const void *buf, |
178 | size_t len) | 179 | size_t len) |
179 | { | 180 | { |
180 | const uint8_t *data = buf; | 181 | const uint8_t *data = buf; |
181 | unsigned int crc = 0; | 182 | unsigned int crc = 0; |
@@ -183,16 +184,16 @@ GNUNET_CRYPTO_crc8_n (const void *buf, | |||
183 | int j; | 184 | int j; |
184 | 185 | ||
185 | for (j = len; 0 != j; j--) | 186 | for (j = len; 0 != j; j--) |
186 | { | ||
187 | crc ^= (*data++ << 8); | ||
188 | for (i = 8; 0 != i; i--) | ||
189 | { | 187 | { |
190 | if (0 != (crc & 0x8000)) | 188 | crc ^= (*data++ << 8); |
191 | crc ^= (0x1070 << 3); | 189 | for (i = 8; 0 != i; i--) |
192 | crc <<= 1; | 190 | { |
191 | if (0 != (crc & 0x8000)) | ||
192 | crc ^= (0x1070 << 3); | ||
193 | crc <<= 1; | ||
194 | } | ||
193 | } | 195 | } |
194 | } | 196 | return (uint8_t)(crc >> 8); |
195 | return (uint8_t) (crc >> 8); | ||
196 | } | 197 | } |
197 | 198 | ||
198 | 199 | ||