OLD | NEW |
1 /* md5.c - Functions to compute MD5 message digest of files or memory blocks | 1 /* md5.c - Functions to compute MD5 message digest of files or memory blocks |
2 according to the definition of MD5 in RFC 1321 from April 1992. | 2 according to the definition of MD5 in RFC 1321 from April 1992. |
3 Copyright (C) 1995, 1996, 2011 Free Software Foundation, Inc. | 3 Copyright (C) 1995, 1996, 2011 Free Software Foundation, Inc. |
4 | 4 |
5 NOTE: This source is derived from an old version taken from the GNU C | 5 NOTE: This source is derived from an old version taken from the GNU C |
6 Library (glibc). | 6 Library (glibc). |
7 | 7 |
8 This program is free software; you can redistribute it and/or modify it | 8 This program is free software; you can redistribute it and/or modify it |
9 under the terms of the GNU General Public License as published by the | 9 under the terms of the GNU General Public License as published by the |
10 Free Software Foundation; either version 2, or (at your option) any | 10 Free Software Foundation; either version 2, or (at your option) any |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 /* Process the remaining bytes in the internal buffer and the usual | 96 /* Process the remaining bytes in the internal buffer and the usual |
97 prolog according to the standard and write the result to RESBUF. | 97 prolog according to the standard and write the result to RESBUF. |
98 | 98 |
99 IMPORTANT: On some systems it is required that RESBUF is correctly | 99 IMPORTANT: On some systems it is required that RESBUF is correctly |
100 aligned for a 32 bits value. */ | 100 aligned for a 32 bits value. */ |
101 void * | 101 void * |
102 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) | 102 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) |
103 { | 103 { |
104 /* Take yet unprocessed bytes into account. */ | 104 /* Take yet unprocessed bytes into account. */ |
105 md5_uint32 bytes = ctx->buflen; | 105 md5_uint32 bytes = ctx->buflen; |
| 106 md5_uint32 swap_bytes; |
106 size_t pad; | 107 size_t pad; |
107 | 108 |
108 /* Now count remaining bytes. */ | 109 /* Now count remaining bytes. */ |
109 ctx->total[0] += bytes; | 110 ctx->total[0] += bytes; |
110 if (ctx->total[0] < bytes) | 111 if (ctx->total[0] < bytes) |
111 ++ctx->total[1]; | 112 ++ctx->total[1]; |
112 | 113 |
113 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; | 114 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; |
114 memcpy (&ctx->buffer[bytes], fillbuf, pad); | 115 memcpy (&ctx->buffer[bytes], fillbuf, pad); |
115 | 116 |
116 /* Put the 64-bit file length in *bits* at the end of the buffer. */ | 117 /* Put the 64-bit file length in *bits* at the end of the buffer. |
117 *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); | 118 Use memcpy to avoid aliasing problems. On most systems, this |
118 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | | 119 will be optimized away to the same code. */ |
119 » » » » » » » (ctx->total[0] >> 29)); | 120 swap_bytes = SWAP (ctx->total[0] << 3); |
| 121 memcpy (&ctx->buffer[bytes + pad], &swap_bytes, sizeof (swap_bytes)); |
| 122 swap_bytes = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); |
| 123 memcpy (&ctx->buffer[bytes + pad + 4], &swap_bytes, sizeof (swap_bytes)); |
120 | 124 |
121 /* Process last bytes. */ | 125 /* Process last bytes. */ |
122 md5_process_block (ctx->buffer, bytes + pad + 8, ctx); | 126 md5_process_block (ctx->buffer, bytes + pad + 8, ctx); |
123 | 127 |
124 return md5_read_ctx (ctx, resbuf); | 128 return md5_read_ctx (ctx, resbuf); |
125 } | 129 } |
126 | 130 |
127 /* Compute MD5 message digest for bytes read from STREAM. The | 131 /* Compute MD5 message digest for bytes read from STREAM. The |
128 resulting message digest number will be written into the 16 bytes | 132 resulting message digest number will be written into the 16 bytes |
129 beginning at RESBLOCK. */ | 133 beginning at RESBLOCK. */ |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 C += C_save; | 432 C += C_save; |
429 D += D_save; | 433 D += D_save; |
430 } | 434 } |
431 | 435 |
432 /* Put checksum in context given as argument. */ | 436 /* Put checksum in context given as argument. */ |
433 ctx->A = A; | 437 ctx->A = A; |
434 ctx->B = B; | 438 ctx->B = B; |
435 ctx->C = C; | 439 ctx->C = C; |
436 ctx->D = D; | 440 ctx->D = D; |
437 } | 441 } |
OLD | NEW |