diff options
Diffstat (limited to 'src/util/crypto_crc.c')
-rw-r--r-- | src/util/crypto_crc.c | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c index 698d8d450..c26541457 100644 --- a/src/util/crypto_crc.c +++ b/src/util/crypto_crc.c | |||
@@ -17,14 +17,14 @@ | |||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | 19 | ||
20 | For the actual CRC 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 |
27 | * @brief implementation of CRC32 | 27 | * @brief implementation of CRC16 and CRC32 |
28 | * @author Christian Grothoff | 28 | * @author Christian Grothoff |
29 | */ | 29 | */ |
30 | 30 | ||
@@ -113,4 +113,58 @@ GNUNET_CRYPTO_crc32_n (const void *buf, size_t len) | |||
113 | return crc; | 113 | return crc; |
114 | } | 114 | } |
115 | 115 | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Perform an incremental step in a CRC16 (for TCP/IP) calculation. | ||
120 | * | ||
121 | * @param sum current sum, initially 0 | ||
122 | * @param hdr buffer to calculate CRC over (must be 16-bit aligned) | ||
123 | * @param len number of bytes in hdr, must be multiple of 2 | ||
124 | * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16) | ||
125 | */ | ||
126 | uint32_t | ||
127 | GNUNET_CRYPTO_crc16_step (uint32_t sum, uint16_t * hdr, size_t len) | ||
128 | { | ||
129 | GNUNET_assert (0 == (len & 1)); | ||
130 | for (; len >= 2; len -= 2) | ||
131 | sum += *(hdr++); | ||
132 | if (len == 1) | ||
133 | sum += *((unsigned char *) hdr); | ||
134 | return sum; | ||
135 | } | ||
136 | |||
137 | /** | ||
138 | * Convert results from GNUNET_CRYPTO_crc16_step to final crc16. | ||
139 | * | ||
140 | * @param sum cummulative sum | ||
141 | * @return crc16 value | ||
142 | */ | ||
143 | uint16_t | ||
144 | GNUNET_CRYPTO_crc16_finish (uint32_t sum) | ||
145 | { | ||
146 | sum = (sum >> 16) + (sum & 0xFFFF); | ||
147 | sum += (sum >> 16); | ||
148 | |||
149 | return ~sum; | ||
150 | } | ||
151 | |||
152 | |||
153 | /** | ||
154 | * Calculate the checksum of a buffer in one step. | ||
155 | * | ||
156 | * @param hdr buffer to calculate CRC over (must be 16-bit aligned) | ||
157 | * @param len number of bytes in hdr, must be multiple of 2 | ||
158 | * @return crc16 value | ||
159 | */ | ||
160 | uint16_t | ||
161 | GNUNET_CRYPTO_crc16_n (uint16_t *hdr, size_t len) | ||
162 | { | ||
163 | uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len); | ||
164 | |||
165 | return GNUNET_CRYPTO_crc16_finish (sum); | ||
166 | } | ||
167 | |||
168 | |||
169 | |||
116 | /* end of crypto_crc.c */ | 170 | /* end of crypto_crc.c */ |