From c7dc4e0efcee3628657fa6b274b6a236e84cc6ad Mon Sep 17 00:00:00 2001 From: Thomas Roessler Date: Mon, 28 May 2001 19:14:30 +0000 Subject: [PATCH] Improve checking for 32bit integers. From Brendan Cully and Lars Hecking. --- Makefile.am | 16 +++------ checktypes.c | 92 ------------------------------------------------ configure.in | 22 +++++++++++- imap/Makefile.am | 5 --- md5.h | 27 +++++++------- md5c.c | 36 +++++++++---------- sha1.c | 24 ++++++------- sha1.h | 23 +++++++++--- 8 files changed, 87 insertions(+), 158 deletions(-) delete mode 100644 checktypes.c diff --git a/Makefile.am b/Makefile.am index 18f671bc..66cd7f9f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ ## Use aclocal -I m4; automake --foreign AUTOMAKE_OPTIONS = foreign -EXTRA_PROGRAMS = mutt_dotlock pgpring makedoc checktypes +EXTRA_PROGRAMS = mutt_dotlock pgpring makedoc if BUILD_IMAP IMAP_SUBDIR = imap @@ -17,7 +17,7 @@ else bin_SCRIPTS = muttbug flea endif -BUILT_SOURCES = keymap_defs.h types.h +BUILT_SOURCES = keymap_defs.h bin_PROGRAMS = mutt @DOTLOCK_TARGET@ @PGPAUX_TARGET@ mutt_SOURCES = $(BUILT_SOURCES) \ @@ -43,11 +43,6 @@ makedoc_SOURCES = makedoc.c makedoc_LDADD = makedoc_DEPENDENCIES = -checktypes_SOURCES = checktypes.c -checktypes_LDADD = -checktypes_DEPENDENCIES = - - # $(makedoc_OBJECTS): $(makedoc_SOURCES) # $(HOST_CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) -c $< @@ -86,7 +81,7 @@ EXTRA_DIST = COPYRIGHT GPL OPS OPS.PGP TODO configure acconfig.h account.h \ rfc822.h sha1.h sort.h mime.types VERSION prepare \ _regex.h OPS.MIX README.SECURITY remailer.c remailer.h browser.h \ mbyte.h lib.h extlib.c pgpewrap pgplib.h Muttrc.head Muttrc \ - makedoc.c stamp-doc-rc README.SSL README.UPGRADE checktypes.c \ + makedoc.c stamp-doc-rc README.SSL README.UPGRADE \ muttbug pgppacket.h depcomp ascii.h BEWARE mutt_dotlock_SOURCES = mutt_dotlock.c @@ -101,7 +96,7 @@ mutt_dotlock.c: dotlock.c cp $(srcdir)/dotlock.c mutt_dotlock.c CLEANFILES = mutt_dotlock.c stamp-doc-rc makedoc \ - keymap_alldefs.h keymap_defs.h types.h + keymap_alldefs.h keymap_defs.h ACLOCAL_AMFLAGS = -I m4 @@ -115,9 +110,6 @@ flea: muttbug.sh Makefile: $(BUILT_SOURCES) -types.h: checktypes - ./checktypes > types.h - keymap_defs.h: $(OPS) $(srcdir)/gen_defs $(srcdir)/gen_defs $(OPS) > keymap_defs.h diff --git a/checktypes.c b/checktypes.c deleted file mode 100644 index 5928449e..00000000 --- a/checktypes.c +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) 1999-2000 Thomas Roessler - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -/* Check various types for their respective length, and for Endianness */ - -#include - -#define CHECK_TYPE(a,b) \ - if (!have_UINT##a && (sizeof (b) == a)) \ - { \ - have_UINT##a = 1; \ - puts (" #define UINT" #a " " #b); \ - } - -#define CHECK_ENDIAN(TYPE) \ - if (!have_endian && sizeof (TYPE) == 2) \ - { \ - TYPE end_test = (TYPE) 0x1234; \ - ep = (char *) &end_test; \ - have_endian = 1; \ - if (*ep == 0x34) \ - puts (" #define M_LITTLE_ENDIAN"); \ - else \ - puts (" #define M_BIG_ENDIAN"); \ - } - - -int main (int argc, char *argv[]) -{ - short have_UINT2 = 0; - short have_UINT4 = 0; - short have_endian = 0; - - char *ep; - - puts ("/* This is a generated file. Don't edit! */"); - puts ("#ifndef _TYPES_H"); - puts (" #define _TYPES_H"); - - CHECK_TYPE (2, unsigned short int) - CHECK_TYPE (2, unsigned int) - CHECK_TYPE (4, unsigned short int) - CHECK_TYPE (4, unsigned int) - CHECK_TYPE (4, unsigned long int) - - CHECK_ENDIAN (unsigned short int) - CHECK_ENDIAN (unsigned int) - - puts ("#endif"); - - if (!have_UINT2 || !have_UINT4) - { - fputs ("Can't determine integer types. Please edit " __FILE__ ",\n", stderr); - fputs ("and submit a patch to \n", stderr); - return 1; - } - - return 0; -} diff --git a/configure.in b/configure.in index f25bcee6..0a3cb990 100644 --- a/configure.in +++ b/configure.in @@ -237,7 +237,27 @@ fi AC_DECL_SYS_SIGLIST -dnl need this for DEC alpha +dnl For MD5 and SHA1 on 64-bit systems +AC_C_BIGENDIAN +AC_CHECK_HEADERS(stdint.h) +dnl This is the method autoconf-2.50's new AC_CHECK_TYPE macro uses. +dnl We need to be backwards compatible to autoconf 2.13, though. -lh +AC_MSG_CHECKING(for uint32_t) +AC_TRY_COMPILE([ +#include +#ifdef HAVE_STDINT_H +#include +#endif], +[if ((uint32_t *) 0) + return 0; +if (sizeof (uint32_t)) + return 0; +],[ + AC_DEFINE(HAVE_UINT32_T, 1, [ Define if you have the uint32_t type. ]) + AC_MSG_RESULT(yes) + ], AC_MSG_RESULT(no) +) +AC_CHECK_SIZEOF(int) AC_CHECK_SIZEOF(long) AC_TYPE_PID_T diff --git a/imap/Makefile.am b/imap/Makefile.am index ea16bfb8..4d7db317 100644 --- a/imap/Makefile.am +++ b/imap/Makefile.am @@ -23,8 +23,3 @@ noinst_HEADERS = auth.h imap_private.h message.h libimap_a_SOURCES = auth.c auth_login.c browse.c command.c imap.c imap.h \ message.c utf7.c util.c $(AUTHENTICATORS) $(GSSSOURCES) - -../types.h: ../checktypes - @( cd .. && $(MAKE) types.h ) - -util.o: ../types.h diff --git a/md5.h b/md5.h index e0b70191..b6c5f1d0 100644 --- a/md5.h +++ b/md5.h @@ -26,26 +26,29 @@ documentation and/or software. #ifndef MD5_H #define MD5_H 1 +#include "config.h" + +#ifdef HAVE_STDINT_H +# include +#else +# include +#endif /* POINTER defines a generic pointer type */ typedef unsigned char *POINTER; -#include "types.h" - -#if 0 - -/* UINT2 defines a two byte word */ -typedef unsigned short int UINT2; - -/* UINT4 defines a four byte word */ -typedef unsigned long int UINT4; - +#ifndef HAVE_UINT32_T +# if SIZEOF_INT == 4 +typedef unsigned int uint32_t; +# elif SIZEOF_LONG == 4 +typedef unsigned long int uint32_t; +# endif #endif /* MD5 context. */ typedef struct { - UINT4 state[4]; /* state (ABCD) */ - UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ + uint32_t state[4]; /* state (ABCD) */ + uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ unsigned char buffer[64]; /* input buffer */ } MD5_CTX; diff --git a/md5c.c b/md5c.c index b1e78ce5..2c2c564e 100644 --- a/md5c.c +++ b/md5c.c @@ -45,9 +45,9 @@ documentation and/or software. #define S43 15 #define S44 21 -static void MD5Transform (UINT4 [4], unsigned char [64]); -static void Encode (unsigned char *, UINT4 *, unsigned int); -static void Decode (UINT4 *, unsigned char *, unsigned int); +static void MD5Transform (uint32_t [4], unsigned char [64]); +static void Encode (unsigned char *, uint32_t *, unsigned int); +static void Decode (uint32_t *, unsigned char *, unsigned int); static void MD5_memcpy (POINTER, POINTER, unsigned int); static void MD5_memset (POINTER, int, unsigned int); @@ -72,22 +72,22 @@ static unsigned char PADDING[64] = { Rotation is separate from addition to prevent recomputation. */ #define FF(a, b, c, d, x, s, ac) { \ - (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define GG(a, b, c, d, x, s, ac) { \ - (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define HH(a, b, c, d, x, s, ac) { \ - (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define II(a, b, c, d, x, s, ac) { \ - (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } @@ -121,10 +121,10 @@ unsigned int inputLen; /* length of input block */ index = (unsigned int)((context->count[0] >> 3) & 0x3F); /* Update number of bits */ - if ((context->count[0] += ((UINT4)inputLen << 3)) - < ((UINT4)inputLen << 3)) + if ((context->count[0] += ((uint32_t)inputLen << 3)) + < ((uint32_t)inputLen << 3)) context->count[1]++; - context->count[1] += ((UINT4)inputLen >> 29); + context->count[1] += ((uint32_t)inputLen >> 29); partLen = 64 - index; @@ -181,10 +181,10 @@ MD5_CTX *context; /* context */ /* MD5 basic transformation. Transforms state based on block. */ static void MD5Transform (state, block) -UINT4 state[4]; +uint32_t state[4]; unsigned char block[64]; { - UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16]; Decode (x, block, 64); @@ -269,12 +269,12 @@ unsigned char block[64]; MD5_memset ((POINTER)x, 0, sizeof (x)); } -/* Encodes input (UINT4) into output (unsigned char). Assumes len is +/* Encodes input (uint32_t) into output (unsigned char). Assumes len is a multiple of 4. */ static void Encode (output, input, len) unsigned char *output; -UINT4 *input; +uint32_t *input; unsigned int len; { unsigned int i, j; @@ -287,19 +287,19 @@ unsigned int len; } } -/* Decodes input (unsigned char) into output (UINT4). Assumes len is +/* Decodes input (unsigned char) into output (uint32_t). Assumes len is a multiple of 4. */ static void Decode (output, input, len) -UINT4 *output; +uint32_t *output; unsigned char *input; unsigned int len; { unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | - (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); + output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) | + (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24); } /* Note: Replace "for loop" with standard memcpy if possible. diff --git a/sha1.c b/sha1.c index accd3430..91966649 100644 --- a/sha1.c +++ b/sha1.c @@ -18,20 +18,18 @@ #define SHA1HANDSOFF #include -#include /* for u_int*_t */ #include "sha1.h" -#include "types.h" #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) /* blk0() and blk() perform the initial expand. */ /* I got the idea of expanding during the round function from SSLeay */ -#ifdef M_LITTLE_ENDIAN -# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ - |(rol(block->l[i],8)&0x00FF00FF)) -#else +#ifdef WORDS_BIGENDIAN # define blk0(i) block->l[i] +#else +# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ + |(rol(block->l[i],8)&0x00FF00FF)) #endif #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ @@ -47,12 +45,12 @@ /* Hash a single 512-bit block. This is the core of the algorithm. */ -void SHA1Transform(UINT4 state[5], const unsigned char buffer[64]) +void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) { -UINT4 a, b, c, d, e; +uint32_t a, b, c, d, e; typedef union { unsigned char c[64]; - UINT4 l[16]; + uint32_t l[16]; } CHAR64LONG16; #ifdef SHA1HANDSOFF CHAR64LONG16 block[1]; /* use array to appear as a pointer */ @@ -122,10 +120,10 @@ void SHA1Init(SHA1_CTX* context) /* Run your data through this. */ -void SHA1Update(SHA1_CTX* context, const unsigned char* data, UINT4 len) +void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len) { -UINT4 i; -UINT4 j; +uint32_t i; +uint32_t j; j = context->count[0]; if ((context->count[0] += len << 3) < j) @@ -163,7 +161,7 @@ unsigned char c; for (i = 0; i < 2; i++) { - UINT4 t = context->count[i]; + uint32_t t = context->count[i]; int j; for (j = 0; j < 4; t >>= 8, j++) diff --git a/sha1.h b/sha1.h index 93e1e2f3..24d56841 100644 --- a/sha1.h +++ b/sha1.h @@ -9,17 +9,30 @@ #ifndef _SHA1_H # define _SHA1_H -# include "types.h" +#include "config.h" +#ifdef HAVE_STDINT_H +# include +#else +# include +#endif + +#ifndef HAVE_UINT32_T +# if SIZEOF_INT == 4 +typedef unsigned int uint32_t; +# elif SIZEOF_LONG == 4 +typedef unsigned long uint32_t; +# endif +#endif typedef struct { - UINT4 state[5]; - UINT4 count[2]; + uint32_t state[5]; + uint32_t count[2]; unsigned char buffer[64]; } SHA1_CTX; -void SHA1Transform(UINT4 state[5], const unsigned char buffer[64]); +void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]); void SHA1Init(SHA1_CTX* context); -void SHA1Update(SHA1_CTX* context, const unsigned char* data, UINT4 len); +void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len); void SHA1Final(unsigned char digest[20], SHA1_CTX* context); # define SHA1_Transform SHA1Transform -- 2.40.0