1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-30 22:43:12 +03:00

Use union to avoid casts in code to store results of hashsum computations

This commit is contained in:
Ulrich Drepper
2011-07-19 16:53:43 -04:00
parent e0e7228480
commit 7dc6bd90c5
7 changed files with 47 additions and 23 deletions

View File

@ -1,3 +1,18 @@
2011-07-19 Ulrich Drepper <drepper@gmail.com>
* crypt/sha512.h (struct sha512_ctx): Move buffer into union and add
buffer64.
* crypt/sha512.c (__sha512_finish_ctx): Use buffer64 for writes instead
of casting of buffer.
* crypt/sha256.h (struct sha256_ctx): Move buffer into union and add
buffer32 and buffer64.
* crypt/sha256.c (__sha256_finish_ctx): Use buffer32 or buffer64 for
writes instead of casting of buffer.
* crypt/md5.h (struct md5_ctx): Move buffer into union and add
buffer32.
* crypt/md5.c (md5_finish_ctx): Use buffer32 for writes instead of
casting of buffer.
2011-07-19 Andreas Schwab <schwab@redhat.com>
* string/strxfrm_l.c (STRXFRM): Fix alloca accounting.

View File

@ -1,6 +1,6 @@
/* Functions to compute MD5 message digest of files or memory blocks.
according to the definition of MD5 in RFC 1321 from April 1992.
Copyright (C) 1995,1996,1997,1999,2000,2001,2005
Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2011
Free Software Foundation, Inc.
This file is part of the GNU C Library.
@ -123,8 +123,8 @@ md5_finish_ctx (ctx, resbuf)
memcpy (&ctx->buffer[bytes], fillbuf, pad);
/* Put the 64-bit file length in *bits* at the end of the buffer. */
*(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
*(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
ctx->buffer32[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
ctx->buffer32[(bytes + pad + 4) / 4] = SWAP ((ctx->total[1] << 3) |
(ctx->total[0] >> 29));
/* Process last bytes. */

View File

@ -1,6 +1,6 @@
/* Declaration of functions and data types used for MD5 sum computing
library functions.
Copyright (C) 1995-1997,1999,2000,2001,2004,2005
Copyright (C) 1995-1997,1999,2000,2001,2004,2005,2011
Free Software Foundation, Inc.
This file is part of the GNU C Library.
@ -88,7 +88,11 @@ struct md5_ctx
md5_uint32 total[2];
md5_uint32 buflen;
char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
union
{
char buffer[128];
md5_uint32 buffer32[32];
};
};
/*

View File

@ -222,12 +222,10 @@ __sha256_finish_ctx (ctx, resbuf)
/* Put the 64-bit file length in *bits* at the end of the buffer. */
#ifdef _STRING_ARCH_unaligned
*(uint64_t *) &ctx->buffer[bytes + pad] = SWAP64 (ctx->total64 << 3);
ctx->buffer64[(bytes + pad) / 8] = SWAP64 (ctx->total64 << 3);
#else
*(uint32_t *) &ctx->buffer[bytes + pad + 4]
= SWAP (ctx->total[TOTAL64_low] << 3);
*(uint32_t *) &ctx->buffer[bytes + pad]
= SWAP ((ctx->total[TOTAL64_high] << 3) |
ctx->buffer32[(bytes + pad + 4) / 4] = SWAP (ctx->total[TOTAL64_low] << 3);
ctx->buffer32[(bytes + pad) / 4] = SWAP ((ctx->total[TOTAL64_high] << 3) |
(ctx->total[TOTAL64_low] >> 29));
#endif

View File

@ -40,7 +40,12 @@ struct sha256_ctx
uint32_t total[2];
};
uint32_t buflen;
char buffer[128] __attribute__ ((__aligned__ (__alignof__ (uint32_t))));
union
{
char buffer[128];
uint32_t buffer32[32];
uint64_t buffer64[16];
};
};
/* Initialize structure containing state of computation.

View File

@ -253,10 +253,8 @@ __sha512_finish_ctx (ctx, resbuf)
memcpy (&ctx->buffer[bytes], fillbuf, pad);
/* Put the 128-bit file length in *bits* at the end of the buffer. */
*(uint64_t *) &ctx->buffer[bytes + pad + 8]
= SWAP (ctx->total[TOTAL128_low] << 3);
*(uint64_t *) &ctx->buffer[bytes + pad]
= SWAP ((ctx->total[TOTAL128_high] << 3) |
ctx->buffer64[(bytes + pad + 8) / 8] = SWAP (ctx->total[TOTAL128_low] << 3);
ctx->buffer64[(bytes + pad) / 8] = SWAP ((ctx->total[TOTAL128_high] << 3) |
(ctx->total[TOTAL128_low] >> 61));
/* Process last bytes. */

View File

@ -44,7 +44,11 @@ struct sha512_ctx
uint64_t total[2];
};
uint64_t buflen;
char buffer[256] __attribute__ ((__aligned__ (__alignof__ (uint64_t))));
union
{
char buffer[256];
uint64_t buffer64[32];
};
};
/* Initialize structure containing state of computation.