Mercurial > hg > CbC > CbC_gcc
comparison libiberty/md5.c @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | a06113de4d67 |
children | 84e7813d76e9 |
comparison
equal
deleted
inserted
replaced
68:561a7518be6b | 111:04ced10e8804 |
---|---|
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 Free Software Foundation, Inc. | 3 Copyright (C) 1995-2017 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 |
74 } | 74 } |
75 | 75 |
76 /* Put result from CTX in first 16 bytes following RESBUF. The result | 76 /* Put result from CTX in first 16 bytes following RESBUF. The result |
77 must be in little endian byte order. | 77 must be in little endian byte order. |
78 | 78 |
79 IMPORTANT: On some systems it is required that RESBUF is correctly | 79 IMPORTANT: RESBUF may not be aligned as strongly as MD5_UNIT32 so we |
80 aligned for a 32 bits value. */ | 80 put things in a local (aligned) buffer first, then memcpy into RESBUF. */ |
81 void * | 81 void * |
82 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) | 82 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) |
83 { | 83 { |
84 ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); | 84 md5_uint32 buffer[4]; |
85 ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); | 85 |
86 ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); | 86 buffer[0] = SWAP (ctx->A); |
87 ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); | 87 buffer[1] = SWAP (ctx->B); |
88 buffer[2] = SWAP (ctx->C); | |
89 buffer[3] = SWAP (ctx->D); | |
90 | |
91 memcpy (resbuf, buffer, 16); | |
88 | 92 |
89 return resbuf; | 93 return resbuf; |
90 } | 94 } |
91 | 95 |
92 /* Process the remaining bytes in the internal buffer and the usual | 96 /* Process the remaining bytes in the internal buffer and the usual |
97 void * | 101 void * |
98 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) | 102 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) |
99 { | 103 { |
100 /* Take yet unprocessed bytes into account. */ | 104 /* Take yet unprocessed bytes into account. */ |
101 md5_uint32 bytes = ctx->buflen; | 105 md5_uint32 bytes = ctx->buflen; |
106 md5_uint32 swap_bytes; | |
102 size_t pad; | 107 size_t pad; |
103 | 108 |
104 /* Now count remaining bytes. */ | 109 /* Now count remaining bytes. */ |
105 ctx->total[0] += bytes; | 110 ctx->total[0] += bytes; |
106 if (ctx->total[0] < bytes) | 111 if (ctx->total[0] < bytes) |
107 ++ctx->total[1]; | 112 ++ctx->total[1]; |
108 | 113 |
109 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; | 114 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; |
110 memcpy (&ctx->buffer[bytes], fillbuf, pad); | 115 memcpy (&ctx->buffer[bytes], fillbuf, pad); |
111 | 116 |
112 /* 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. |
113 *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); | 118 Use memcpy to avoid aliasing problems. On most systems, this |
114 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | | 119 will be optimized away to the same code. */ |
115 (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)); | |
116 | 124 |
117 /* Process last bytes. */ | 125 /* Process last bytes. */ |
118 md5_process_block (ctx->buffer, bytes + pad + 8, ctx); | 126 md5_process_block (ctx->buffer, bytes + pad + 8, ctx); |
119 | 127 |
120 return md5_read_ctx (ctx, resbuf); | 128 return md5_read_ctx (ctx, resbuf); |
239 buffer = (const char *) buffer + 64; | 247 buffer = (const char *) buffer + 64; |
240 len -= 64; | 248 len -= 64; |
241 } | 249 } |
242 else | 250 else |
243 #endif | 251 #endif |
244 md5_process_block (buffer, len & ~63, ctx); | 252 { |
245 buffer = (const void *) ((const char *) buffer + (len & ~63)); | 253 md5_process_block (buffer, len & ~63, ctx); |
246 len &= 63; | 254 buffer = (const void *) ((const char *) buffer + (len & ~63)); |
255 len &= 63; | |
256 } | |
247 } | 257 } |
248 | 258 |
249 /* Move remaining bytes in internal buffer. */ | 259 /* Move remaining bytes in internal buffer. */ |
250 if (len > 0) | 260 if (len > 0) |
251 { | 261 { |
281 | 291 |
282 /* First increment the byte count. RFC 1321 specifies the possible | 292 /* First increment the byte count. RFC 1321 specifies the possible |
283 length of the file up to 2^64 bits. Here we only compute the | 293 length of the file up to 2^64 bits. Here we only compute the |
284 number of bytes. Do a double word increment. */ | 294 number of bytes. Do a double word increment. */ |
285 ctx->total[0] += len; | 295 ctx->total[0] += len; |
286 if (ctx->total[0] < len) | 296 ctx->total[1] += ((len >> 31) >> 1) + (ctx->total[0] < len); |
287 ++ctx->total[1]; | |
288 | 297 |
289 /* Process all bytes in the buffer with 64 bytes in each round of | 298 /* Process all bytes in the buffer with 64 bytes in each round of |
290 the loop. */ | 299 the loop. */ |
291 while (words < endp) | 300 while (words < endp) |
292 { | 301 { |