diff --git a/Makefile b/Makefile index 921d910..007070e 100644 --- a/Makefile +++ b/Makefile @@ -26,11 +26,13 @@ FILEOFFSET_64BIT = -D_FILE_OFFSET_BITS=64 #OMIT_GETOPT_LONG = -DOMIT_GETOPT_LONG # -# To use the md5sum program for calculating signatures (instead of the -# built in MD5 message digest routines) uncomment the following -# line (try this if you're having trouble with built in code). +# Choose md5 implementation to use. # -#EXTERNAL_MD5 = -DEXTERNAL_MD5=\"md5sum\" +# Openssl +MD5LIB_CFLAGS = -lcrypto -DWITH_OPENSSL +# +# Internal +#MD5LIB_OBJECT_FILE = md5/md5.o ##################################################################### # Developer Configuration Section # @@ -80,8 +82,7 @@ MKDIR = mkdir -p CC ?= gcc COMPILER_OPTIONS = -Wall -O -g -CFLAGS= $(COMPILER_OPTIONS) -I. -DVERSION=\"$(VERSION)\" $(EXTERNAL_MD5) $(OMIT_GETOPT_LONG) $(FILEOFFSET_64BIT) - +CFLAGS= $(COMPILER_OPTIONS) -I. -DVERSION=\"$(VERSION)\" $(OMIT_GETOPT_LONG) $(MD5LIB_CFLAGS) $(FILEOFFSET_64BIT) INSTALL_PROGRAM = $(INSTALL) -c -m 0755 INSTALL_DATA = $(INSTALL) -c -m 0644 @@ -91,7 +92,7 @@ INSTALL_DATA = $(INSTALL) -c -m 0644 # #ADDITIONAL_OBJECTS = getopt.o -OBJECT_FILES = fdupes.o md5/md5.o $(ADDITIONAL_OBJECTS) +OBJECT_FILES = fdupes.o $(MD5LIB_OBJECT_FILE) $(ADDITIONAL_OBJECTS) ##################################################################### # no need to modify anything beyond this point # diff --git a/fdupes.c b/fdupes.c index d33d859..474f40e 100644 --- a/fdupes.c +++ b/fdupes.c @@ -33,7 +33,9 @@ #include #include -#ifndef EXTERNAL_MD5 +#ifdef WITH_OPENSSL +#include +#else #include "md5/md5.h" #endif @@ -345,25 +347,31 @@ int grokdir(char *dir, file_t **filelistp) return filecount; } -#ifndef EXTERNAL_MD5 - -/* If EXTERNAL_MD5 is not defined, use L. Peter Deutsch's MD5 library. - */ char *getcrcsignatureuntil(char *filename, off_t max_read) { int x; off_t fsize; off_t toread; - md5_state_t state; - md5_byte_t digest[16]; - static md5_byte_t chunk[CHUNK_SIZE]; - static char signature[16*2 + 1]; char *sigp; FILE *file; - + +#ifdef WITH_OPENSSL + static char chunk[CHUNK_SIZE]; + static char signature[MD5_DIGEST_LENGTH*2 + 1]; + unsigned char digest[MD5_DIGEST_LENGTH]; + MD5_CTX c; + + MD5_Init(&c); +#else + static md5_byte_t chunk[CHUNK_SIZE]; + static char signature[16*2 + 1]; + md5_state_t state; + md5_byte_t digest[16]; + md5_init(&state); +#endif + - fsize = filesize(filename); if (max_read != 0 && fsize > max_read) @@ -382,11 +390,19 @@ char *getcrcsignatureuntil(char *filename, off_t max_read) fclose(file); return NULL; } +#ifdef WITH_OPENSSL + MD5_Update(&c, chunk, toread); +#else md5_append(&state, chunk, toread); +#endif fsize -= toread; } +#ifdef WITH_OPENSSL + MD5_Final(digest, &c); +#else md5_finish(&state, digest); +#endif sigp = signature; @@ -410,49 +426,6 @@ char *getcrcpartialsignature(char *filename) return getcrcsignatureuntil(filename, PARTIAL_MD5_SIZE); } -#endif /* [#ifndef EXTERNAL_MD5] */ - -#ifdef EXTERNAL_MD5 - -/* If EXTERNAL_MD5 is defined, use md5sum program to calculate signatures. - */ -char *getcrcsignature(char *filename) -{ - static char signature[256]; - char *command; - char *separator; - FILE *result; - - command = (char*) malloc(strlen(filename)+strlen(EXTERNAL_MD5)+2); - if (command == NULL) { - errormsg("out of memory\n"); - exit(1); - } - - sprintf(command, "%s %s", EXTERNAL_MD5, filename); - - result = popen(command, "r"); - if (result == NULL) { - errormsg("error invoking %s\n", EXTERNAL_MD5); - exit(1); - } - - free(command); - - if (fgets(signature, 256, result) == NULL) { - errormsg("error generating signature for %s\n", filename); - return NULL; - } - separator = strchr(signature, ' '); - if (separator) *separator = '\0'; - - pclose(result); - - return signature; -} - -#endif /* [#ifdef EXTERNAL_MD5] */ - void purgetree(filetree_t *checktree) { if (checktree->left != NULL) purgetree(checktree->left);