Continuing in our series of integer-arithmetic cleanups for coreutils, here is a patch to make md5.c and sha1.c portable to hosts (like the AS/400) which do not let you cast pointers to integers in any meaningful way. This also modernizes the integer terminology a bit (using C99 names) and fixes the Autoconf macros to match the code.
2004-07-28 Paul Eggert <[EMAIL PROTECTED]> * lib/md5.h: Include <stdint.h> if HAVE_STDINT_H || _LIBC, not ifdef _LIBC. (md5_uint32): Use uint32_t if available. Simplify fallback ifdefs. * lib/md5.c: Don't include <sys/types.h> or <stdlib.h>; <stddef.h> suffices with C89 or better. (alignof): New macro, portable to all C89 hosts. (UNALIGNED): Use it. Use uintptr_t if available, and assume everything is unaligned otherwise; this is more portable than assuming 'unsigned long int' will always work. * lib/sha1.c: Likewise. * m4/md5.m4 (gl_MD5): Do not require AC_C_INLINE, since it doesn't use inline any more. Require AC_C_BIGENDIAN, though. * m4/sha.m4 (gl_SHA): Require AC_C_BIGENDIAN. Index: lib/md5.h =================================================================== RCS file: /home/eggert/coreutils/cu/lib/md5.h,v retrieving revision 1.15 diff -p -u -r1.15 md5.h --- lib/md5.h 2 Dec 2003 08:24:31 -0000 1.15 +++ lib/md5.h 28 Jul 2004 19:55:32 -0000 @@ -1,6 +1,9 @@ /* md5.h - Declaration of functions and data types used for MD5 sum computing library functions. - Copyright (C) 1995, 1996, 1999, 2000, 2003 Free Software Foundation, Inc. + + Copyright (C) 1995, 1996, 1999, 2000, 2003, 2004 Free Software + Foundation, Inc. + NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to [EMAIL PROTECTED] @@ -25,37 +28,27 @@ #include <limits.h> /* The following contortions are an attempt to use the C preprocessor - to determine an unsigned integral type that is 32 bits wide. An - alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but - doing that would require that the configure script compile and *run* - the resulting executable. Locally running cross-compiled executables - is usually not possible. */ + to determine an unsigned integral type that is exactly 32 bits wide. */ -#ifdef _LIBC +#if HAVE_STDINT_H || _LIBC # include <stdint.h> +#endif + +#ifdef UINT32_MAX typedef uint32_t md5_uint32; -typedef uintptr_t md5_uintptr; #else # define UINT_MAX_32_BITS 4294967295U - # if UINT_MAX == UINT_MAX_32_BITS typedef unsigned int md5_uint32; +# elif USHRT_MAX == UINT_MAX_32_BITS + typedef unsigned short int md5_uint32; +# elif ULONG_MAX == UINT_MAX_32_BITS + typedef unsigned long md5_uint32; # else -# if USHRT_MAX == UINT_MAX_32_BITS - typedef unsigned short md5_uint32; -# else -# if ULONG_MAX == UINT_MAX_32_BITS - typedef unsigned long md5_uint32; -# else - /* The following line is intended to evoke an error. - Using #error is not portable enough. */ - "Cannot determine unsigned 32-bit data type." -# endif -# endif + /* The following line is intended to evoke an error. + Using #error is not portable enough. */ + "Cannot determine unsigned 32-bit data type." # endif -/* We have to make a guess about the integer type equivalent in size - to pointers which should always be correct. */ -typedef unsigned long int md5_uintptr; #endif /* Structure to save state of computation between the single steps. */ Index: lib/md5.c =================================================================== RCS file: /home/eggert/coreutils/cu/lib/md5.c,v retrieving revision 1.18 diff -p -u -r1.18 md5.c --- lib/md5.c 10 Sep 2003 08:54:26 -0000 1.18 +++ lib/md5.c 28 Jul 2004 19:55:19 -0000 @@ -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 Free Software Foundation, Inc. + Copyright (C) 1995, 1996, 2001, 2003, 2004 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to [EMAIL PROTECTED] @@ -26,9 +26,7 @@ #include "md5.h" -#include <sys/types.h> - -#include <stdlib.h> +#include <stddef.h> #include <string.h> #include "unlocked-io.h" @@ -246,12 +244,11 @@ md5_process_bytes (const void *buffer, s if (len >= 64) { #if !_STRING_ARCH_unaligned -/* To check alignment gcc has an appropriate operator. Other - compilers don't. */ -# if __GNUC__ >= 2 -# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) +# define alignof(type) offsetof (struct { char c; type x; }, x) +# ifdef UINTPTR_MAX +# define UNALIGNED_P(p) (((uintptr_t) p) % alignof (md5_uint32) != 0) # else -# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) +# define UNALIGNED_P(p) 1 # endif if (UNALIGNED_P (buffer)) while (len > 64) Index: lib/sha1.c =================================================================== RCS file: /home/eggert/coreutils/cu/lib/sha1.c,v retrieving revision 1.1 diff -p -u -r1.1 sha1.c --- lib/sha1.c 2 Dec 2003 09:05:40 -0000 1.1 +++ lib/sha1.c 28 Jul 2004 19:56:04 -0000 @@ -1,7 +1,7 @@ /* sha.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 Free Software Foundation, Inc. + Copyright (C) 2000, 2001, 2003, 2004 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 @@ -28,9 +28,7 @@ #include "sha1.h" -#include <sys/types.h> - -#include <stdlib.h> +#include <stddef.h> #include <string.h> #include "unlocked-io.h" @@ -244,12 +242,11 @@ sha_process_bytes (const void *buffer, s if (len >= 64) { #if !_STRING_ARCH_unaligned -/* To check alignment gcc has an appropriate operator. Other - compilers don't. */ -# if __GNUC__ >= 2 -# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) +# define alignof(type) offsetof (struct { char c; type x; }, x) +# ifdef UINTPTR_MAX +# define UNALIGNED_P(p) (((uintptr_t) p) % alignof (md5_uint32) != 0) # else -# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) +# define UNALIGNED_P(p) 1 # endif if (UNALIGNED_P (buffer)) while (len > 64) Index: m4/md5.m4 =================================================================== RCS file: /home/eggert/coreutils/cu/m4/md5.m4,v retrieving revision 1.1 diff -p -u -r1.1 md5.m4 --- m4/md5.m4 12 Sep 2003 21:11:30 -0000 1.1 +++ m4/md5.m4 28 Jul 2004 19:55:03 -0000 @@ -1,5 +1,5 @@ -# md5.m4 serial 3 -dnl Copyright (C) 2002-2003 Free Software Foundation, Inc. +# md5.m4 serial 4 +dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. dnl This file is free software, distributed under the terms of the GNU dnl General Public License. As a special exception to the GNU General dnl Public License, this file may be distributed as part of a program @@ -8,8 +8,9 @@ dnl the same distribution terms as the r AC_DEFUN([gl_MD5], [ - dnl Prerequisites of lib/md5.h. - AC_REQUIRE([AC_C_INLINE]) + dnl No prerequisites of lib/md5.h. - dnl No prerequisites of lib/md5.c. + dnl Prerequisites of lib/md5.c. + AC_REQUIRE([AC_C_BIGENDIAN]) + : ]) Index: m4/sha.m4 =================================================================== RCS file: /home/eggert/coreutils/cu/m4/sha.m4,v retrieving revision 1.2 diff -p -u -r1.2 sha.m4 --- m4/sha.m4 12 Sep 2003 21:08:06 -0000 1.2 +++ m4/sha.m4 28 Jul 2004 19:54:52 -0000 @@ -1,5 +1,5 @@ -# sha.m4 serial 2 -dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc. +# sha.m4 serial 3 +dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. dnl This file is free software, distributed under the terms of the GNU dnl General Public License. As a special exception to the GNU General dnl Public License, this file may be distributed as part of a program @@ -8,6 +8,7 @@ dnl the same distribution terms as the r AC_DEFUN([gl_SHA], [ - dnl Prerequisites of lib/sha.c. + dnl Prerequisites of lib/sha1.c. + AC_REQUIRE([AC_C_BIGENDIAN]) : ]) _______________________________________________ Bug-coreutils mailing list [EMAIL PROTECTED] http://lists.gnu.org/mailman/listinfo/bug-coreutils