comparison md5.c @ 26:0d82d0f30c98 src

cosmetics: Convert all tabs to spaces.
author diego
date Tue, 23 Sep 2008 09:14:45 +0000
parents 4aa618ae094f
children 98951f8ec89c
comparison
equal deleted inserted replaced
25:17258a7b3239 26:0d82d0f30c98
38 # define WORDS_BIGENDIAN 1 38 # define WORDS_BIGENDIAN 1
39 # endif 39 # endif
40 #endif 40 #endif
41 41
42 #ifdef WORDS_BIGENDIAN 42 #ifdef WORDS_BIGENDIAN
43 # define SWAP(n) \ 43 # define SWAP(n) \
44 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) 44 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
45 #else 45 #else
46 # define SWAP(n) (n) 46 # define SWAP(n) (n)
47 #endif 47 #endif
48 48
108 memcpy (&ctx->buffer[bytes], fillbuf, pad); 108 memcpy (&ctx->buffer[bytes], fillbuf, pad);
109 109
110 /* Put the 64-bit file length in *bits* at the end of the buffer. */ 110 /* Put the 64-bit file length in *bits* at the end of the buffer. */
111 *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); 111 *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
112 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | 112 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
113 (ctx->total[0] >> 29)); 113 (ctx->total[0] >> 29));
114 114
115 /* Process last bytes. */ 115 /* Process last bytes. */
116 md5_process_block (ctx->buffer, bytes + pad + 8, ctx); 116 md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
117 117
118 return md5_read_ctx (ctx, resbuf); 118 return md5_read_ctx (ctx, resbuf);
137 137
138 /* Iterate over full file contents. */ 138 /* Iterate over full file contents. */
139 while (1) 139 while (1)
140 { 140 {
141 /* We read the file in blocks of BLOCKSIZE bytes. One call of the 141 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
142 computation function processes the whole buffer so that with the 142 computation function processes the whole buffer so that with the
143 next round of the loop another block can be read. */ 143 next round of the loop another block can be read. */
144 size_t n; 144 size_t n;
145 sum = 0; 145 sum = 0;
146 146
147 /* Read block. Take care for partial reads. */ 147 /* Read block. Take care for partial reads. */
148 do 148 do
149 { 149 {
150 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); 150 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
151 151
152 sum += n; 152 sum += n;
153 } 153 }
154 while (sum < BLOCKSIZE && n != 0); 154 while (sum < BLOCKSIZE && n != 0);
155 if (n == 0 && ferror (stream)) 155 if (n == 0 && ferror (stream))
156 return 1; 156 return 1;
157 157
158 /* If end of file is reached, end the loop. */ 158 /* If end of file is reached, end the loop. */
159 if (n == 0) 159 if (n == 0)
160 break; 160 break;
161 161
162 /* Process buffer with BLOCKSIZE bytes. Note that 162 /* Process buffer with BLOCKSIZE bytes. Note that
163 BLOCKSIZE % 64 == 0 163 BLOCKSIZE % 64 == 0
164 */ 164 */
165 md5_process_block (buffer, BLOCKSIZE, &ctx); 165 md5_process_block (buffer, BLOCKSIZE, &ctx);
166 } 166 }
167 167
168 /* Add the last bytes if necessary. */ 168 /* Add the last bytes if necessary. */
212 212
213 memcpy (&ctx->buffer[left_over], buffer, add); 213 memcpy (&ctx->buffer[left_over], buffer, add);
214 ctx->buflen += add; 214 ctx->buflen += add;
215 215
216 if (left_over + add > 64) 216 if (left_over + add > 64)
217 { 217 {
218 md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx); 218 md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
219 /* The regions in the following copy operation cannot overlap. */ 219 /* The regions in the following copy operation cannot overlap. */
220 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], 220 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
221 (left_over + add) & 63); 221 (left_over + add) & 63);
222 ctx->buflen = (left_over + add) & 63; 222 ctx->buflen = (left_over + add) & 63;
223 } 223 }
224 224
225 buffer = (const char *) buffer + add; 225 buffer = (const char *) buffer + add;
226 len -= add; 226 len -= add;
227 } 227 }
228 228
286 md5_uint32 B_save = B; 286 md5_uint32 B_save = B;
287 md5_uint32 C_save = C; 287 md5_uint32 C_save = C;
288 md5_uint32 D_save = D; 288 md5_uint32 D_save = D;
289 289
290 /* First round: using the given function, the context and a constant 290 /* First round: using the given function, the context and a constant
291 the next context is computed. Because the algorithms processing 291 the next context is computed. Because the algorithms processing
292 unit is a 32-bit word and it is determined to work on words in 292 unit is a 32-bit word and it is determined to work on words in
293 little endian byte order we perhaps have to change the byte order 293 little endian byte order we perhaps have to change the byte order
294 before the computation. To reduce the work for the next steps 294 before the computation. To reduce the work for the next steps
295 we store the swapped words in the array CORRECT_WORDS. */ 295 we store the swapped words in the array CORRECT_WORDS. */
296 296
297 #define OP(a, b, c, d, s, T) \ 297 #define OP(a, b, c, d, s, T) \
298 do \ 298 do \
299 { \ 299 { \
300 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ 300 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
301 ++words; \ 301 ++words; \
302 a = rol (a, s); \ 302 a = rol (a, s); \
303 a += b; \ 303 a += b; \
304 } \ 304 } \
305 while (0) 305 while (0)
306 306
307 /* Before we start, one word to the strange constants. 307 /* Before we start, one word to the strange constants.
308 They are defined in RFC 1321 as 308 They are defined in RFC 1321 as
309 309
310 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64, or 310 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64, or
311 perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' 311 perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
312 */ 312 */
313 313
314 /* Round 1. */ 314 /* Round 1. */
315 OP (A, B, C, D, 7, 0xd76aa478); 315 OP (A, B, C, D, 7, 0xd76aa478);
316 OP (D, A, B, C, 12, 0xe8c7b756); 316 OP (D, A, B, C, 12, 0xe8c7b756);
328 OP (D, A, B, C, 12, 0xfd987193); 328 OP (D, A, B, C, 12, 0xfd987193);
329 OP (C, D, A, B, 17, 0xa679438e); 329 OP (C, D, A, B, 17, 0xa679438e);
330 OP (B, C, D, A, 22, 0x49b40821); 330 OP (B, C, D, A, 22, 0x49b40821);
331 331
332 /* For the second to fourth round we have the possibly swapped words 332 /* For the second to fourth round we have the possibly swapped words
333 in CORRECT_WORDS. Redefine the macro to take an additional first 333 in CORRECT_WORDS. Redefine the macro to take an additional first
334 argument specifying the function to use. */ 334 argument specifying the function to use. */
335 #undef OP 335 #undef OP
336 #define OP(f, a, b, c, d, k, s, T) \ 336 #define OP(f, a, b, c, d, k, s, T) \
337 do \ 337 do \
338 { \ 338 { \
339 a += f (b, c, d) + correct_words[k] + T; \ 339 a += f (b, c, d) + correct_words[k] + T; \
340 a = rol (a, s); \ 340 a = rol (a, s); \
341 a += b; \ 341 a += b; \
342 } \ 342 } \
343 while (0) 343 while (0)
344 344
345 /* Round 2. */ 345 /* Round 2. */
346 OP (FG, A, B, C, D, 1, 5, 0xf61e2562); 346 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
347 OP (FG, D, A, B, C, 6, 9, 0xc040b340); 347 OP (FG, D, A, B, C, 6, 9, 0xc040b340);