From dcbeca4acb90e463e4af5e12a44dfff2cd161fb9 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 12 Sep 2005 22:05:15 +0000 Subject: [PATCH] Merge glibc and coreutils changes into gnulib, plus a few extra fixes. * md5.c: Use #error rather than a string. (CYCLIC): New macro, from glibc source. Use it instead of rol. * md5.h (__GNUC_PREREQ, __THROW): Define if not defined already. (__attribute__): Define to empty for non recent-GCC. (__md5_buffer, __md5_finish_ctx, __md5_init_ctx, __md5_process_block): (__md5_process_bytes, __md5_read_ctx, __md5_stream): Renamed from their non-__ counterparts, with new macros replacing them if not _LIBC. Add __THROW attribute. (rol): Remove. (struct md5_ctx): Align buffer if using GCC. * sha1.h (struct sha1_ctx): Likewise. * sha1.c (SWAP): Renamed from the NOTSWAP. All uses changed. The old name was backwards. (NOTSWAP): Remove; not used. (rol): New macro, moved here from md5.h. (sha1_process_block): Remove a FIXME that doesn't make sense. --- lib/ChangeLog | 21 ++++++++++++++++ lib/md5.c | 19 +++++++++------ lib/md5.h | 66 +++++++++++++++++++++++++++++++++++++-------------- lib/sha1.c | 38 +++++++++++++---------------- lib/sha1.h | 4 ++-- 5 files changed, 99 insertions(+), 49 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index 4c67bfb9bc..43008aee76 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,24 @@ +2005-09-12 Paul Eggert + + Merge glibc and coreutils changes into gnulib, plus a few + extra fixes. + * md5.c: Use #error rather than a string. + (CYCLIC): New macro, from glibc source. Use it instead of rol. + * md5.h (__GNUC_PREREQ, __THROW): Define if not defined already. + (__attribute__): Define to empty for non recent-GCC. + (__md5_buffer, __md5_finish_ctx, __md5_init_ctx, __md5_process_block): + (__md5_process_bytes, __md5_read_ctx, __md5_stream): + Renamed from their non-__ counterparts, with new macros replacing + them if not _LIBC. Add __THROW attribute. + (rol): Remove. + (struct md5_ctx): Align buffer if using GCC. + * sha1.h (struct sha1_ctx): Likewise. + * sha1.c (SWAP): Renamed from the NOTSWAP. All uses changed. + The old name was backwards. + (NOTSWAP): Remove; not used. + (rol): New macro, moved here from md5.h. + (sha1_process_block): Remove a FIXME that doesn't make sense. + 2005-09-12 Derek Price * gai_strerror.c: Include config.h when available. Include diff --git a/lib/md5.c b/lib/md5.c index 1646676f4f..87db8f956c 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -1,6 +1,6 @@ /* md5.c - 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, 2001, 2003, 2004 Free Software Foundation, Inc. + Copyright (C) 1995, 1996, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. @@ -57,10 +57,8 @@ #endif #define BLOCKSIZE 4096 -/* Ensure that BLOCKSIZE is a multiple of 64. */ #if BLOCKSIZE % 64 != 0 -/* FIXME-someday (soon?): use #error instead of this kludge. */ -"invalid BLOCKSIZE" +# error "invalid BLOCKSIZE" #endif /* This array contains the bytes used to pad the buffer to the next @@ -335,15 +333,22 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) { \ a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ ++words; \ - a = rol (a, s); \ + CYCLIC (a, s); \ a += b; \ } \ while (0) + /* It is unfortunate that C does not provide an operator for + cyclic rotation. Hope the C compiler is smart enough. */ +#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) + /* Before we start, one word to the strange constants. They are defined in RFC 1321 as - T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64, or + T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 + + Here is an equivalent invocation using Perl: + perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' */ @@ -373,7 +378,7 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) do \ { \ a += f (b, c, d) + correct_words[k] + T; \ - a = rol (a, s); \ + CYCLIC (a, s); \ a += b; \ } \ while (0) diff --git a/lib/md5.h b/lib/md5.h index 59f95bce1c..1c29e67b4e 100644 --- a/lib/md5.h +++ b/lib/md5.h @@ -1,8 +1,6 @@ -/* md5.h - Declaration of functions and data types used for MD5 sum - computing library functions. - - Copyright (C) 1995, 1996, 1999, 2000, 2003, 2004 Free Software - Foundation, Inc. +/* Declaration of functions and data types used for MD5 sum computing + library functions. + Copyright (C) 1995-1997,1999-2005 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. @@ -33,6 +31,39 @@ # include #endif +#ifndef __GNUC_PREREQ +# if defined __GNUC__ && defined __GNUC_MINOR__ +# define __GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +# else +# define __GNUC_PREREQ(maj, min) 0 +# endif +#endif + +#ifndef __THROW +# if defined __cplusplus && __GNUC_PREREQ (2,8) +# define __THROW throw () +# else +# define __THROW +# endif +#endif + +#ifndef __attribute__ +# if ! __GNUC_PREREQ (2,8) || __STRICT_ANSI__ +# define __attribute__(x) +# endif +#endif + +#ifndef _LIBC +# define __md5_buffer md5_buffer +# define __md5_finish_ctx md5_finish_ctx +# define __md5_init_ctx md5_init_ctx +# define __md5_process_block md5_process_block +# define __md5_process_bytes md5_process_bytes +# define __md5_read_ctx md5_read_ctx +# define __md5_stream md5_stream +#endif + typedef uint32_t md5_uint32; /* Structure to save state of computation between the single steps. */ @@ -45,7 +76,7 @@ struct md5_ctx md5_uint32 total[2]; md5_uint32 buflen; - char buffer[128]; + char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); }; /* @@ -55,21 +86,21 @@ struct md5_ctx /* Initialize structure containing state of computation. (RFC 1321, 3.3: Step 3) */ -extern void md5_init_ctx (struct md5_ctx *ctx); +extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW; /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is necessary that LEN is a multiple of 64!!! */ -extern void md5_process_block (const void *buffer, size_t len, - struct md5_ctx *ctx); +extern void __md5_process_block (const void *buffer, size_t len, + struct md5_ctx *ctx) __THROW; /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */ -extern void md5_process_bytes (const void *buffer, size_t len, - struct md5_ctx *ctx); +extern void __md5_process_bytes (const void *buffer, size_t len, + struct md5_ctx *ctx) __THROW; /* Process the remaining bytes in the buffer and put result from CTX in first 16 bytes following RESBUF. The result is always in little @@ -78,7 +109,7 @@ extern void md5_process_bytes (const void *buffer, size_t len, IMPORTANT: On some systems it is required that RESBUF be correctly aligned for a 32 bits value. */ -extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); +extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW; /* Put result from CTX in first 16 bytes following RESBUF. The result is @@ -87,20 +118,19 @@ extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 32 bits value. */ -extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf); +extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW; /* Compute MD5 message digest for bytes read from STREAM. The resulting message digest number will be written into the 16 bytes beginning at RESBLOCK. */ -extern int md5_stream (FILE *stream, void *resblock); +extern int __md5_stream (FILE *stream, void *resblock) __THROW; /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *md5_buffer (const char *buffer, size_t len, void *resblock); +extern void *__md5_buffer (const char *buffer, size_t len, + void *resblock) __THROW; -#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) ) - -#endif +#endif /* md5.h */ diff --git a/lib/sha1.c b/lib/sha1.c index ff025af4a4..556d9ca1a4 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -1,7 +1,7 @@ /* sha1.c - Functions to compute SHA1 message digest of files or memory blocks according to the NIST specification FIPS-180-1. - Copyright (C) 2000, 2001, 2003, 2004 Free Software Foundation, Inc. + Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the @@ -35,26 +35,19 @@ # include "unlocked-io.h" #endif -/* - Not-swap is a macro that does an endian swap on architectures that are - big-endian, as SHA1 needs some data in a little-endian format -*/ +/* SWAP does an endian swap on architectures that are little-endian, + as SHA1 needs some data in a big-endian form. */ #ifdef WORDS_BIGENDIAN -# define NOTSWAP(n) (n) -# define SWAP(n) \ - (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) +# define SWAP(n) (n) #else -# define NOTSWAP(n) \ +# define SWAP(n) \ (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) -# define SWAP(n) (n) #endif #define BLOCKSIZE 4096 -/* Ensure that BLOCKSIZE is a multiple of 64. */ #if BLOCKSIZE % 64 != 0 -/* FIXME-someday (soon?): use #error instead of this kludge. */ -"invalid BLOCKSIZE" +# error "invalid BLOCKSIZE" #endif /* This array contains the bytes used to pad the buffer to the next @@ -88,11 +81,11 @@ sha1_init_ctx (struct sha1_ctx *ctx) void * sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf) { - ((md5_uint32 *) resbuf)[0] = NOTSWAP (ctx->A); - ((md5_uint32 *) resbuf)[1] = NOTSWAP (ctx->B); - ((md5_uint32 *) resbuf)[2] = NOTSWAP (ctx->C); - ((md5_uint32 *) resbuf)[3] = NOTSWAP (ctx->D); - ((md5_uint32 *) resbuf)[4] = NOTSWAP (ctx->E); + ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); + ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); + ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); + ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); + ((md5_uint32 *) resbuf)[4] = SWAP (ctx->E); return resbuf; } @@ -118,8 +111,8 @@ sha1_finish_ctx (struct sha1_ctx *ctx, void *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 + 4] = NOTSWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) | + *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3); + *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); /* Process last bytes. */ @@ -317,6 +310,8 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) if (ctx->total[0] < len) ++ctx->total[1]; +#define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) + #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \ ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \ , (x[I&0x0f] = rol(tm, 1)) ) @@ -332,10 +327,9 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) { md5_uint32 tm; int t; - /* FIXME: see sha1.c for a better implementation. */ for (t = 0; t < 16; t++) { - x[t] = NOTSWAP (*words); + x[t] = SWAP (*words); words++; } diff --git a/lib/sha1.h b/lib/sha1.h index b660cd0f3f..bb48855b8d 100644 --- a/lib/sha1.h +++ b/lib/sha1.h @@ -1,6 +1,6 @@ /* Declarations of functions and data types used for SHA1 sum library functions. - Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc. + Copyright (C) 2000, 2001, 2003, 2005 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the @@ -33,7 +33,7 @@ struct sha1_ctx md5_uint32 total[2]; md5_uint32 buflen; - char buffer[128]; + char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); }; -- 2.30.2