From d11af4159eb8836696ef29f1e1ac9ad4db348d47 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 10 Jan 2006 17:47:56 +0000 Subject: Sync from gnulib. --- lib/ChangeLog | 9 +++ lib/localcharset.c | 14 +++- lib/md5.c | 195 +++++++++++++++++++++++++++-------------------------- lib/md5.h | 46 ++++++------- lib/savedir.c | 40 ++++++++--- lib/savedir.h | 5 +- lib/sha1.c | 38 +++++------ lib/sha1.h | 20 +++--- lib/sha256.c | 42 ++++++------ lib/sha256.h | 8 +-- lib/sha512.c | 44 ++++++------ lib/sha512.h | 11 ++- lib/strcasecmp.c | 9 ++- lib/strncasecmp.c | 11 ++- lib/verify.h | 47 +++++++------ 15 files changed, 300 insertions(+), 239 deletions(-) (limited to 'lib') diff --git a/lib/ChangeLog b/lib/ChangeLog index 80082355c..588c037b9 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,12 @@ +2006-01-10 Paul Eggert + + * localcharset.c, md5.c, md5.h, savedir.c, savedir.h, sha1.c, sha1.h + * lib/strcasecmp.c, lib/strncasecmp.c, lib/verify.h: + Sync from gnulib. + * sha256.c, sha256.h, sha512.c, sha512.h: + Replace all instances of md5_uint32_t with uint32_t, to accommodate + gnulib change. + 2006-01-10 Jim Meyering Avoid the double-free (first in fts_read, second in fts_close) that diff --git a/lib/localcharset.c b/lib/localcharset.c index 5c257a670..d03ab79d0 100644 --- a/lib/localcharset.c +++ b/lib/localcharset.c @@ -1,6 +1,6 @@ /* Determine a canonical name for the current locale's character encoding. - Copyright (C) 2000-2004 Free Software Foundation, Inc. + Copyright (C) 2000-2006 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 @@ -248,6 +248,7 @@ get_charset_aliases (void) "CP1361" "\0" "JOHAB" "\0" "CP20127" "\0" "ASCII" "\0" "CP20866" "\0" "KOI8-R" "\0" + "CP20936" "\0" "GB2312" "\0" "CP21866" "\0" "KOI8-RU" "\0" "CP28591" "\0" "ISO-8859-1" "\0" "CP28592" "\0" "ISO-8859-2" "\0" @@ -258,7 +259,14 @@ get_charset_aliases (void) "CP28597" "\0" "ISO-8859-7" "\0" "CP28598" "\0" "ISO-8859-8" "\0" "CP28599" "\0" "ISO-8859-9" "\0" - "CP28605" "\0" "ISO-8859-15" "\0"; + "CP28605" "\0" "ISO-8859-15" "\0" + "CP38598" "\0" "ISO-8859-8" "\0" + "CP51932" "\0" "EUC-JP" "\0" + "CP51936" "\0" "GB2312" "\0" + "CP51949" "\0" "EUC-KR" "\0" + "CP51950" "\0" "EUC-TW" "\0" + "CP54936" "\0" "GB18030" "\0" + "CP65001" "\0" "UTF-8" "\0"; # endif #endif @@ -278,7 +286,7 @@ get_charset_aliases (void) STATIC #endif const char * -locale_charset () +locale_charset (void) { const char *codeset; const char *aliases; diff --git a/lib/md5.c b/lib/md5.c index 87db8f956..5e97474e6 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -1,8 +1,8 @@ -/* md5.c - Functions to compute MD5 message digest of files or memory blocks +/* 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, 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. + Copyright (C) 1995,1996,1997,1999,2000,2001,2005 + Free Software Foundation, Inc. + This file is part of the GNU C Library. 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 @@ -27,7 +27,9 @@ #include "md5.h" #include +#include #include +#include #if USE_UNLOCKED_IO # include "unlocked-io.h" @@ -88,10 +90,10 @@ md5_init_ctx (struct md5_ctx *ctx) void * md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) { - ((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); + ((uint32_t *) resbuf)[0] = SWAP (ctx->A); + ((uint32_t *) resbuf)[1] = SWAP (ctx->B); + ((uint32_t *) resbuf)[2] = SWAP (ctx->C); + ((uint32_t *) resbuf)[3] = SWAP (ctx->D); return resbuf; } @@ -105,24 +107,22 @@ void * md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) { /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; - size_t pad; + uint32_t bytes = ctx->buflen; + size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; /* Now count remaining bytes. */ ctx->total[0] += bytes; if (ctx->total[0] < bytes) ++ctx->total[1]; - pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; - 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->total[0] >> 29)); + ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3); + ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + + memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); /* Process last bytes. */ - md5_process_block (ctx->buffer, bytes + pad + 8, ctx); + md5_process_block (ctx->buffer, size * 4, ctx); return md5_read_ctx (ctx, resbuf); } @@ -144,8 +144,8 @@ md5_stream (FILE *stream, void *resblock) while (1) { /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ size_t n; sum = 0; @@ -162,8 +162,8 @@ md5_stream (FILE *stream, void *resblock) if (n == 0) { /* Check for the error flag IFF N == 0, so that we don't - exit the loop after a partial read due to e.g., EAGAIN - or EWOULDBLOCK. */ + exit the loop after a partial read due to e.g., EAGAIN + or EWOULDBLOCK. */ if (ferror (stream)) return 1; goto process_partial_block; @@ -177,12 +177,12 @@ md5_stream (FILE *stream, void *resblock) } /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 + BLOCKSIZE % 64 == 0 */ md5_process_block (buffer, BLOCKSIZE, &ctx); } - process_partial_block:; +process_partial_block: /* Process any remaining bytes. */ if (sum > 0) @@ -223,7 +223,7 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) size_t left_over = ctx->buflen; size_t add = 128 - left_over > len ? len : 128 - left_over; - memcpy (&ctx->buffer[left_over], buffer, add); + memcpy (&((char *) ctx->buffer)[left_over], buffer, add); ctx->buflen += add; if (ctx->buflen > 64) @@ -232,7 +232,8 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) ctx->buflen &= 63; /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], + memcpy (ctx->buffer, + &((char *) ctx->buffer)[(left_over + add) & ~63], ctx->buflen); } @@ -244,8 +245,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) if (len >= 64) { #if !_STRING_ARCH_unaligned -# define alignof(type) offsetof (struct { char c; type x; }, x) -# define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0) +/* To check alignment gcc has an appropriate operator. Other + compilers don't. */ +# if __GNUC__ >= 2 +# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0) +# else +# define alignof(type) offsetof (struct { char c; type x; }, x) +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) +# endif if (UNALIGNED_P (buffer)) while (len > 64) { @@ -267,13 +274,13 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) { size_t left_over = ctx->buflen; - memcpy (&ctx->buffer[left_over], buffer, len); + memcpy (&((char *) ctx->buffer)[left_over], buffer, len); left_over += len; if (left_over >= 64) { md5_process_block (ctx->buffer, 64, ctx); left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[64], left_over); + memcpy (ctx->buffer, &ctx->buffer[16], left_over); } ctx->buflen = left_over; } @@ -295,14 +302,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) void md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) { - md5_uint32 correct_words[16]; - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 A = ctx->A; - md5_uint32 B = ctx->B; - md5_uint32 C = ctx->C; - md5_uint32 D = ctx->D; + uint32_t correct_words[16]; + const uint32_t *words = buffer; + size_t nwords = len / sizeof (uint32_t); + const uint32_t *endp = words + nwords; + uint32_t A = ctx->A; + uint32_t B = ctx->B; + uint32_t C = ctx->C; + uint32_t D = ctx->D; /* First increment the byte count. RFC 1321 specifies the possible length of the file up to 2^64 bits. Here we only compute the @@ -315,18 +322,18 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) the loop. */ while (words < endp) { - md5_uint32 *cwp = correct_words; - md5_uint32 A_save = A; - md5_uint32 B_save = B; - md5_uint32 C_save = C; - md5_uint32 D_save = D; + uint32_t *cwp = correct_words; + uint32_t A_save = A; + uint32_t B_save = B; + uint32_t C_save = C; + uint32_t D_save = D; /* First round: using the given function, the context and a constant - the next context is computed. Because the algorithms processing - unit is a 32-bit word and it is determined to work on words in - little endian byte order we perhaps have to change the byte order - before the computation. To reduce the work for the next steps - we store the swapped words in the array CORRECT_WORDS. */ + the next context is computed. Because the algorithms processing + unit is a 32-bit word and it is determined to work on words in + little endian byte order we perhaps have to change the byte order + before the computation. To reduce the work for the next steps + we store the swapped words in the array CORRECT_WORDS. */ #define OP(a, b, c, d, s, T) \ do \ @@ -339,43 +346,43 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) while (0) /* It is unfortunate that C does not provide an operator for - cyclic rotation. Hope the C compiler is smart enough. */ + 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 + They are defined in RFC 1321 as - T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 + T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 - Here is an equivalent invocation using Perl: + Here is an equivalent invocation using Perl: - perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' + perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' */ /* Round 1. */ - OP (A, B, C, D, 7, 0xd76aa478); + OP (A, B, C, D, 7, 0xd76aa478); OP (D, A, B, C, 12, 0xe8c7b756); OP (C, D, A, B, 17, 0x242070db); OP (B, C, D, A, 22, 0xc1bdceee); - OP (A, B, C, D, 7, 0xf57c0faf); + OP (A, B, C, D, 7, 0xf57c0faf); OP (D, A, B, C, 12, 0x4787c62a); OP (C, D, A, B, 17, 0xa8304613); OP (B, C, D, A, 22, 0xfd469501); - OP (A, B, C, D, 7, 0x698098d8); + OP (A, B, C, D, 7, 0x698098d8); OP (D, A, B, C, 12, 0x8b44f7af); OP (C, D, A, B, 17, 0xffff5bb1); OP (B, C, D, A, 22, 0x895cd7be); - OP (A, B, C, D, 7, 0x6b901122); + OP (A, B, C, D, 7, 0x6b901122); OP (D, A, B, C, 12, 0xfd987193); OP (C, D, A, B, 17, 0xa679438e); OP (B, C, D, A, 22, 0x49b40821); /* For the second to fourth round we have the possibly swapped words - in CORRECT_WORDS. Redefine the macro to take an additional first - argument specifying the function to use. */ + in CORRECT_WORDS. Redefine the macro to take an additional first + argument specifying the function to use. */ #undef OP #define OP(f, a, b, c, d, k, s, T) \ - do \ + do \ { \ a += f (b, c, d) + correct_words[k] + T; \ CYCLIC (a, s); \ @@ -384,58 +391,58 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) while (0) /* Round 2. */ - OP (FG, A, B, C, D, 1, 5, 0xf61e2562); - OP (FG, D, A, B, C, 6, 9, 0xc040b340); + OP (FG, A, B, C, D, 1, 5, 0xf61e2562); + OP (FG, D, A, B, C, 6, 9, 0xc040b340); OP (FG, C, D, A, B, 11, 14, 0x265e5a51); - OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); - OP (FG, A, B, C, D, 5, 5, 0xd62f105d); - OP (FG, D, A, B, C, 10, 9, 0x02441453); + OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); + OP (FG, A, B, C, D, 5, 5, 0xd62f105d); + OP (FG, D, A, B, C, 10, 9, 0x02441453); OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); - OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); - OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); - OP (FG, D, A, B, C, 14, 9, 0xc33707d6); - OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); - OP (FG, B, C, D, A, 8, 20, 0x455a14ed); - OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); - OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); - OP (FG, C, D, A, B, 7, 14, 0x676f02d9); + OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); + OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); + OP (FG, D, A, B, C, 14, 9, 0xc33707d6); + OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); + OP (FG, B, C, D, A, 8, 20, 0x455a14ed); + OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); + OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); + OP (FG, C, D, A, B, 7, 14, 0x676f02d9); OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); /* Round 3. */ - OP (FH, A, B, C, D, 5, 4, 0xfffa3942); - OP (FH, D, A, B, C, 8, 11, 0x8771f681); + OP (FH, A, B, C, D, 5, 4, 0xfffa3942); + OP (FH, D, A, B, C, 8, 11, 0x8771f681); OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); OP (FH, B, C, D, A, 14, 23, 0xfde5380c); - OP (FH, A, B, C, D, 1, 4, 0xa4beea44); - OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); - OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); + OP (FH, A, B, C, D, 1, 4, 0xa4beea44); + OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); + OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); - OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); - OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); - OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); - OP (FH, B, C, D, A, 6, 23, 0x04881d05); - OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); + OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); + OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); + OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); + OP (FH, B, C, D, A, 6, 23, 0x04881d05); + OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); - OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); + OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); /* Round 4. */ - OP (FI, A, B, C, D, 0, 6, 0xf4292244); - OP (FI, D, A, B, C, 7, 10, 0x432aff97); + OP (FI, A, B, C, D, 0, 6, 0xf4292244); + OP (FI, D, A, B, C, 7, 10, 0x432aff97); OP (FI, C, D, A, B, 14, 15, 0xab9423a7); - OP (FI, B, C, D, A, 5, 21, 0xfc93a039); - OP (FI, A, B, C, D, 12, 6, 0x655b59c3); - OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); + OP (FI, B, C, D, A, 5, 21, 0xfc93a039); + OP (FI, A, B, C, D, 12, 6, 0x655b59c3); + OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); OP (FI, C, D, A, B, 10, 15, 0xffeff47d); - OP (FI, B, C, D, A, 1, 21, 0x85845dd1); - OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); + OP (FI, B, C, D, A, 1, 21, 0x85845dd1); + OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); - OP (FI, C, D, A, B, 6, 15, 0xa3014314); + OP (FI, C, D, A, B, 6, 15, 0xa3014314); OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); - OP (FI, A, B, C, D, 4, 6, 0xf7537e82); + OP (FI, A, B, C, D, 4, 6, 0xf7537e82); OP (FI, D, A, B, C, 11, 10, 0xbd3af235); - OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); - OP (FI, B, C, D, A, 9, 21, 0xeb86d391); + OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); + OP (FI, B, C, D, A, 9, 21, 0xeb86d391); /* Add the starting values of the context. */ A += A_save; diff --git a/lib/md5.h b/lib/md5.h index 1c29e67b4..68dfcf89a 100644 --- a/lib/md5.h +++ b/lib/md5.h @@ -1,9 +1,8 @@ /* 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. + Copyright (C) 1995-1997,1999,2000,2001,2004,2005 + Free Software Foundation, Inc. + This file is part of the GNU C Library. 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 @@ -23,18 +22,15 @@ #define _MD5_H 1 #include +#include -#if HAVE_INTTYPES_H -# include -#endif -#if HAVE_STDINT_H || _LIBC -# include -#endif +#define MD5_DIGEST_SIZE 16 +#define MD5_BLOCK_SIZE 64 #ifndef __GNUC_PREREQ # if defined __GNUC__ && defined __GNUC_MINOR__ -# define __GNUC_PREREQ(maj, min) \ - ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +# define __GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) # else # define __GNUC_PREREQ(maj, min) 0 # endif @@ -64,19 +60,17 @@ # define __md5_stream md5_stream #endif -typedef uint32_t md5_uint32; - /* Structure to save state of computation between the single steps. */ struct md5_ctx { - md5_uint32 A; - md5_uint32 B; - md5_uint32 C; - md5_uint32 D; - - md5_uint32 total[2]; - md5_uint32 buflen; - char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; + + uint32_t total[2]; + uint32_t buflen; + uint32_t buffer[32]; }; /* @@ -107,8 +101,8 @@ extern void __md5_process_bytes (const void *buffer, size_t len, endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. - IMPORTANT: On some systems it is required that RESBUF be correctly - aligned for a 32 bits value. */ + IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit + boundary. */ extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW; @@ -116,8 +110,8 @@ extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW; always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ + IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit + boundary. */ extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW; diff --git a/lib/savedir.c b/lib/savedir.c index 86930eb56..b08f32b79 100644 --- a/lib/savedir.c +++ b/lib/savedir.c @@ -55,27 +55,27 @@ #include #include +#include "openat.h" #include "xalloc.h" -/* Return a freshly allocated string containing the file names - in directory DIR, separated by '\0' characters; - the end is marked by two '\0' characters in a row. - Return NULL (setting errno) if DIR cannot be opened, read, or closed. */ - #ifndef NAME_SIZE_DEFAULT # define NAME_SIZE_DEFAULT 512 #endif -char * -savedir (const char *dir) +/* Return a freshly allocated string containing the file names + in directory DIRP, separated by '\0' characters; + the end is marked by two '\0' characters in a row. + Return NULL (setting errno) if DIRP cannot be read or closed. + If DIRP is NULL, return NULL without affecting errno. */ + +static char * +savedirstream (DIR *dirp) { - DIR *dirp; char *name_space; size_t allocated = NAME_SIZE_DEFAULT; size_t used = 0; int save_errno; - dirp = opendir (dir); if (dirp == NULL) return NULL; @@ -127,3 +127,25 @@ savedir (const char *dir) } return name_space; } + +/* Return a freshly allocated string containing the file names + in directory DIR, separated by '\0' characters; + the end is marked by two '\0' characters in a row. + Return NULL (setting errno) if DIR cannot be opened, read, or closed. */ + +char * +savedir (char const *dir) +{ + return savedirstream (opendir (dir)); +} + +/* Return a freshly allocated string containing the file names + in directory FD, separated by '\0' characters; + the end is marked by two '\0' characters in a row. + Return NULL (setting errno) if FD cannot be read or closed. */ + +char * +fdsavedir (int fd) +{ + return savedirstream (fdopendir (fd)); +} diff --git a/lib/savedir.h b/lib/savedir.h index 4828b1bec..7809d2392 100644 --- a/lib/savedir.h +++ b/lib/savedir.h @@ -1,6 +1,6 @@ /* Save the list of files in a directory in a string. - Copyright 1997, 1999, 2001, 2003 Free Software Foundation, Inc. + Copyright 1997, 1999, 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 @@ -21,6 +21,7 @@ #if !defined SAVEDIR_H_ # define SAVEDIR_H_ -char *savedir (const char *dir); +char *savedir (char const *dir); +char *fdsavedir (int fd); #endif diff --git a/lib/sha1.c b/lib/sha1.c index e8949bdcc..09198f334 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -81,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] = 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); + ((uint32_t *) resbuf)[0] = SWAP (ctx->A); + ((uint32_t *) resbuf)[1] = SWAP (ctx->B); + ((uint32_t *) resbuf)[2] = SWAP (ctx->C); + ((uint32_t *) resbuf)[3] = SWAP (ctx->D); + ((uint32_t *) resbuf)[4] = SWAP (ctx->E); return resbuf; } @@ -99,7 +99,7 @@ void * sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf) { /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; + uint32_t bytes = ctx->buflen; size_t pad; /* Now count remaining bytes. */ @@ -111,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] = SWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) | + *(uint32_t *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3); + *(uint32_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); /* Process last bytes. */ @@ -238,7 +238,7 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) { #if !_STRING_ARCH_unaligned # define alignof(type) offsetof (struct { char c; type x; }, x) -# define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0) +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) if (UNALIGNED_P (buffer)) while (len > 64) { @@ -293,15 +293,15 @@ sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) void sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) { - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 x[16]; - md5_uint32 a = ctx->A; - md5_uint32 b = ctx->B; - md5_uint32 c = ctx->C; - md5_uint32 d = ctx->D; - md5_uint32 e = ctx->E; + const uint32_t *words = buffer; + size_t nwords = len / sizeof (uint32_t); + const uint32_t *endp = words + nwords; + uint32_t x[16]; + uint32_t a = ctx->A; + uint32_t b = ctx->B; + uint32_t c = ctx->C; + uint32_t d = ctx->D; + uint32_t e = ctx->E; /* First increment the byte count. RFC 1321 specifies the possible length of the file up to 2^64 bits. Here we only compute the @@ -325,7 +325,7 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) while (words < endp) { - md5_uint32 tm; + uint32_t tm; int t; for (t = 0; t < 16; t++) { diff --git a/lib/sha1.h b/lib/sha1.h index bb48855b8..a3ab132e8 100644 --- a/lib/sha1.h +++ b/lib/sha1.h @@ -20,20 +20,20 @@ # define SHA1_H 1 # include -# include "md5.h" +# include /* Structure to save state of computation between the single steps. */ struct sha1_ctx { - md5_uint32 A; - md5_uint32 B; - md5_uint32 C; - md5_uint32 D; - md5_uint32 E; - - md5_uint32 total[2]; - md5_uint32 buflen; - char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; + uint32_t E; + + uint32_t total[2]; + uint32_t buflen; + char buffer[128] __attribute__ ((__aligned__ (__alignof__ (uint32_t)))); }; diff --git a/lib/sha256.c b/lib/sha256.c index 03c05281a..39ae13a51 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -105,7 +105,7 @@ sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf) int i; for ( i=0 ; i<8 ; i++ ) - ((md5_uint32 *) resbuf)[i] = NOTSWAP (ctx->state[i]); + ((uint32_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); return resbuf; } @@ -116,7 +116,7 @@ sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf) int i; for ( i=0 ; i<7 ; i++ ) - ((md5_uint32 *) resbuf)[i] = NOTSWAP (ctx->state[i]); + ((uint32_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); return resbuf; } @@ -130,7 +130,7 @@ static void sha256_conclude_ctx (struct sha256_ctx *ctx) { /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; + uint32_t bytes = ctx->buflen; size_t pad; /* Now count remaining bytes. */ @@ -142,8 +142,8 @@ sha256_conclude_ctx (struct sha256_ctx *ctx) 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) | + *(uint32_t *) &ctx->buffer[bytes + pad + 4] = NOTSWAP (ctx->total[0] << 3); + *(uint32_t *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); /* Process last bytes. */ @@ -360,7 +360,7 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx) { #if !_STRING_ARCH_unaligned # define alignof(type) offsetof (struct { char c; type x; }, x) -# define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0) +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) if (UNALIGNED_P (buffer)) while (len > 64) { @@ -398,7 +398,7 @@ sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx) /* SHA256 round constants */ #define K(I) sha256_round_constants[I] -static const md5_uint32 sha256_round_constants[64] = { +static const uint32_t sha256_round_constants[64] = { 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, @@ -428,18 +428,18 @@ static const md5_uint32 sha256_round_constants[64] = { void sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) { - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 x[16]; - md5_uint32 a = ctx->state[0]; - md5_uint32 b = ctx->state[1]; - md5_uint32 c = ctx->state[2]; - md5_uint32 d = ctx->state[3]; - md5_uint32 e = ctx->state[4]; - md5_uint32 f = ctx->state[5]; - md5_uint32 g = ctx->state[6]; - md5_uint32 h = ctx->state[7]; + const uint32_t *words = buffer; + size_t nwords = len / sizeof (uint32_t); + const uint32_t *endp = words + nwords; + uint32_t x[16]; + uint32_t a = ctx->state[0]; + uint32_t b = ctx->state[1]; + uint32_t c = ctx->state[2]; + uint32_t d = ctx->state[3]; + uint32_t e = ctx->state[4]; + uint32_t f = ctx->state[5]; + uint32_t g = ctx->state[6]; + uint32_t h = ctx->state[7]; /* First increment the byte count. FIPS PUB 180-2 specifies the possible length of the file up to 2^64 bits. Here we only compute the @@ -468,8 +468,8 @@ sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) while (words < endp) { - md5_uint32 tm; - md5_uint32 t0, t1; + uint32_t tm; + uint32_t t0, t1; int t; /* FIXME: see sha1.c for a better implementation. */ for (t = 0; t < 16; t++) diff --git a/lib/sha256.h b/lib/sha256.h index 34c3756ca..11be3adf1 100644 --- a/lib/sha256.h +++ b/lib/sha256.h @@ -1,6 +1,6 @@ /* Declarations of functions and data types used for SHA256 and SHA224 sum library functions. - Copyright (C) 2005 Free Software Foundation, Inc. + Copyright (C) 2005, 2006 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 @@ -25,10 +25,10 @@ /* Structure to save state of computation between the single steps. */ struct sha256_ctx { - md5_uint32 state[8]; + uint32_t state[8]; - md5_uint32 total[2]; - md5_uint32 buflen; + uint32_t total[2]; + uint32_t buflen; char buffer[128]; }; diff --git a/lib/sha512.c b/lib/sha512.c index 0e0597397..11c3b1d16 100644 --- a/lib/sha512.c +++ b/lib/sha512.c @@ -1,7 +1,7 @@ /* sha512.c - Functions to compute SHA512 and SHA384 message digest of files or memory blocks according to the NIST specification FIPS-180-2. - Copyright (C) 2005 Free Software Foundation, Inc. + Copyright (C) 2005, 2006 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 @@ -107,7 +107,7 @@ sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf) int i; for ( i=0 ; i<8 ; i++ ) - ((sha512_uint64 *) resbuf)[i] = NOTSWAP (ctx->state[i]); + ((uint64_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); return resbuf; } @@ -118,7 +118,7 @@ sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf) int i; for ( i=0 ; i<6 ; i++ ) - ((sha512_uint64 *) resbuf)[i] = NOTSWAP (ctx->state[i]); + ((uint64_t *) resbuf)[i] = NOTSWAP (ctx->state[i]); return resbuf; } @@ -132,7 +132,7 @@ static void sha512_conclude_ctx (struct sha512_ctx *ctx) { /* Take yet unprocessed bytes into account. */ - sha512_uint64 bytes = ctx->buflen; + uint64_t bytes = ctx->buflen; size_t pad; /* Now count remaining bytes. */ @@ -144,8 +144,8 @@ sha512_conclude_ctx (struct sha512_ctx *ctx) memcpy (&ctx->buffer[bytes], fillbuf, pad); /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(sha512_uint64 *) &ctx->buffer[bytes + pad + 8] = NOTSWAP (ctx->total[0] << 3); - *(sha512_uint64 *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) | + *(uint64_t *) &ctx->buffer[bytes + pad + 8] = NOTSWAP (ctx->total[0] << 3); + *(uint64_t *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 61)); /* Process last bytes. */ @@ -362,7 +362,7 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx) { #if !_STRING_ARCH_unaligned # define alignof(type) offsetof (struct { char c; type x; }, x) -# define UNALIGNED_P(p) (((size_t) p) % alignof (sha512_uint64) != 0) +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint64_t) != 0) if (UNALIGNED_P (buffer)) while (len > 128) { @@ -400,7 +400,7 @@ sha512_process_bytes (const void *buffer, size_t len, struct sha512_ctx *ctx) /* SHA512 round constants */ #define K(I) sha512_round_constants[I] -static const sha512_uint64 sha512_round_constants[80] = { +static const uint64_t sha512_round_constants[80] = { 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, @@ -424,18 +424,18 @@ static const sha512_uint64 sha512_round_constants[80] = { void sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx) { - const sha512_uint64 *words = buffer; - size_t nwords = len / sizeof (sha512_uint64); - const sha512_uint64 *endp = words + nwords; - sha512_uint64 x[16]; - sha512_uint64 a = ctx->state[0]; - sha512_uint64 b = ctx->state[1]; - sha512_uint64 c = ctx->state[2]; - sha512_uint64 d = ctx->state[3]; - sha512_uint64 e = ctx->state[4]; - sha512_uint64 f = ctx->state[5]; - sha512_uint64 g = ctx->state[6]; - sha512_uint64 h = ctx->state[7]; + const uint64_t *words = buffer; + size_t nwords = len / sizeof (uint64_t); + const uint64_t *endp = words + nwords; + uint64_t x[16]; + uint64_t a = ctx->state[0]; + uint64_t b = ctx->state[1]; + uint64_t c = ctx->state[2]; + uint64_t d = ctx->state[3]; + uint64_t e = ctx->state[4]; + uint64_t f = ctx->state[5]; + uint64_t g = ctx->state[6]; + uint64_t h = ctx->state[7]; /* First increment the byte count. FIPS PUB 180-2 specifies the possible length of the file up to 2^128 bits. Here we only compute the @@ -463,8 +463,8 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx) while (words < endp) { - sha512_uint64 tm; - sha512_uint64 t0, t1; + uint64_t tm; + uint64_t t0, t1; int t; /* FIXME: see sha1.c for a better implementation. */ for (t = 0; t < 16; t++) diff --git a/lib/sha512.h b/lib/sha512.h index 5a2b5ce35..3740de9c5 100644 --- a/lib/sha512.h +++ b/lib/sha512.h @@ -1,6 +1,6 @@ /* Declarations of functions and data types used for SHA512 and SHA384 sum library functions. - Copyright (C) 2005 Free Software Foundation, Inc. + Copyright (C) 2005, 2006 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,16 +28,13 @@ # include # endif -typedef uint64_t sha512_uint64; - - /* Structure to save state of computation between the single steps. */ struct sha512_ctx { - sha512_uint64 state[8]; + uint64_t state[8]; - sha512_uint64 total[2]; - sha512_uint64 buflen; + uint64_t total[2]; + uint64_t buflen; char buffer[256]; }; diff --git a/lib/strcasecmp.c b/lib/strcasecmp.c index 71f2eca7c..c1bac0a5a 100644 --- a/lib/strcasecmp.c +++ b/lib/strcasecmp.c @@ -25,6 +25,7 @@ #include "strcase.h" #include +#include #if HAVE_MBRTOWC # include "mbuiter.h" @@ -93,6 +94,12 @@ strcasecmp (const char *s1, const char *s2) } while (c1 == c2); - return c1 - c2; + if (UCHAR_MAX <= INT_MAX) + return c1 - c2; + else + /* On machines where 'char' and 'int' are types of the same size, the + difference of two 'unsigned char' values - including the sign bit - + doesn't fit in an 'int'. */ + return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0); } } diff --git a/lib/strncasecmp.c b/lib/strncasecmp.c index 72ead013b..0209c39ea 100644 --- a/lib/strncasecmp.c +++ b/lib/strncasecmp.c @@ -1,5 +1,5 @@ /* strncasecmp.c -- case insensitive string comparator - Copyright (C) 1998, 1999 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 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 @@ -23,6 +23,7 @@ #include "strcase.h" #include +#include #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) @@ -54,5 +55,11 @@ strncasecmp (const char *s1, const char *s2, size_t n) } while (c1 == c2); - return c1 - c2; + if (UCHAR_MAX <= INT_MAX) + return c1 - c2; + else + /* On machines where 'char' and 'int' are types of the same size, the + difference of two 'unsigned char' values - including the sign bit - + doesn't fit in an 'int'. */ + return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0); } diff --git a/lib/verify.h b/lib/verify.h index fbabd82c0..328980fa3 100644 --- a/lib/verify.h +++ b/lib/verify.h @@ -23,24 +23,33 @@ /* Each of these macros verifies that its argument R is a nonzero constant expression. To be portable, R's type must be integer (or - boolean). Unlike assert, there is no run-time overhead. */ - -/* A type that is valid if and only if R is a nonzero constant expression. - The symbols verify_type__ and verify_error_if_negative_size__ are - private to this header file. */ - -# define verify_type__(R) \ - struct { unsigned int verify_error_if_negative_size__ : (R) ? 1 : -1; } - -/* Verify requirement R at compile-time, as a declaration. */ - -# define verify(R) \ - extern int (* verify_function__ (void)) [sizeof (verify_type__ (R))] - -/* Verify requirement R at compile-time, as an expression. - This macro can be used in some contexts where verify cannot, and vice versa. - Return void. */ - -# define verify_expr(R) ((void) ((verify_type__ (R) *) 0)) + boolean). Unlike assert, there is no run-time overhead. + + There are two macros, since no single macro can be used in all + contexts in C. verify_true (R) is for scalar contexts, where it + may be cast to void if need be. verify (R) is for declaration + contexts, e.g., the top level. + + The symbols verify_error_if_negative_size__ and verify_function__ + are private to this header. */ + +/* Verify requirement R at compile-time, as an integer constant expression. + Return true. */ + +# ifdef __cplusplus +template + struct verify_type__ { unsigned int verify_error_if_negative_size__: w; }; +# define verify_true(R) \ + (!!sizeof (verify_type__<(R) ? 1 : -1>)) +# else +# define verify_true(R) \ + (!!sizeof \ + (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; })) +# endif + +/* Verify requirement R at compile-time, as a declaration without a + trailing ';'. */ + +# define verify(R) extern int (* verify_function__ (void)) [verify_true (R)] #endif -- cgit v1.2.3-70-g09d2