From 2b250e8d2d74461bf855683b04d2de9085964ccf Mon Sep 17 00:00:00 2001 From: Ivan Diorditsa Date: Mon, 19 Jan 2015 06:21:14 +0200 Subject: [PATCH 1/2] Remove the dead and broken EXTERNAL_MD5. It was broken in 54c920e06ea5c6550b8fade9bfee8f3cb60a389f (fdupes-1.50-PR2): fdupes.c:531: undefined reference to `getcrcpartialsignature' fdupes.c:546: undefined reference to `getcrcpartialsignature' Seing as noone cared to fix it for seven years I'm removing the dead broken code. --- Makefile | 9 +-------- fdupes.c | 49 ------------------------------------------------- 2 files changed, 1 insertion(+), 57 deletions(-) diff --git a/Makefile b/Makefile index 921d910..ed5498e 100644 --- a/Makefile +++ b/Makefile @@ -25,13 +25,6 @@ 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). -# -#EXTERNAL_MD5 = -DEXTERNAL_MD5=\"md5sum\" - ##################################################################### # Developer Configuration Section # ##################################################################### @@ -80,7 +73,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) $(FILEOFFSET_64BIT) INSTALL_PROGRAM = $(INSTALL) -c -m 0755 INSTALL_DATA = $(INSTALL) -c -m 0644 diff --git a/fdupes.c b/fdupes.c index d33d859..1c985ed 100644 --- a/fdupes.c +++ b/fdupes.c @@ -33,9 +33,7 @@ #include #include -#ifndef EXTERNAL_MD5 #include "md5/md5.h" -#endif #define ISFLAG(a,b) ((a & b) == b) #define SETFLAG(a,b) (a |= b) @@ -345,10 +343,6 @@ 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; @@ -410,49 +404,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); From 239a04ceb60b9efc323e2cff2a087b12a2e6e65e Mon Sep 17 00:00:00 2001 From: Ivan Diorditsa Date: Mon, 19 Jan 2015 06:42:11 +0200 Subject: [PATCH 2/2] Add support for libcrypto md5 implementation. openssl's md5 is up to 45% faster than md5.c --- Makefile | 14 +++++++++++--- fdupes.c | 34 ++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index ed5498e..007070e 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,15 @@ FILEOFFSET_64BIT = -D_FILE_OFFSET_BITS=64 # #OMIT_GETOPT_LONG = -DOMIT_GETOPT_LONG +# +# Choose md5 implementation to use. +# +# Openssl +MD5LIB_CFLAGS = -lcrypto -DWITH_OPENSSL +# +# Internal +#MD5LIB_OBJECT_FILE = md5/md5.o + ##################################################################### # Developer Configuration Section # ##################################################################### @@ -73,8 +82,7 @@ MKDIR = mkdir -p CC ?= gcc COMPILER_OPTIONS = -Wall -O -g -CFLAGS= $(COMPILER_OPTIONS) -I. -DVERSION=\"$(VERSION)\" $(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 @@ -84,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 1c985ed..474f40e 100644 --- a/fdupes.c +++ b/fdupes.c @@ -33,7 +33,11 @@ #include #include +#ifdef WITH_OPENSSL +#include +#else #include "md5/md5.h" +#endif #define ISFLAG(a,b) ((a & b) == b) #define SETFLAG(a,b) (a |= b) @@ -348,16 +352,26 @@ 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) @@ -376,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;