From 9eb790d6eb4a56416d342816e1140aabd9c12de8 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sat, 12 May 2007 23:47:46 +0200 Subject: [PATCH] we don't really need md5 for hcache at all. Signed-off-by: Pierre Habouzit --- Makefile.am | 2 +- imap/message.c | 8 +- lib-mx/hcache.c | 206 ++++++++++++++++----------------------------- lib-mx/hcache.h | 13 +-- lib-mx/mh.c | 4 +- pop/Makefile.am | 5 +- md5.c => pop/md5.c | 18 ++-- md5.h => pop/md5.h | 0 8 files changed, 101 insertions(+), 155 deletions(-) rename md5.c => pop/md5.c (95%) rename md5.h => pop/md5.h (100%) diff --git a/Makefile.am b/Makefile.am index afdbd40..9978ecd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -22,7 +22,7 @@ madmutt_SOURCES = $(BUILT_SOURCES) \ handler.c headers.c help.c hook.c main.c muttlib.c mutt_idna.c \ pager.c pattern.c postpone.c recvattach.c recvcmd.c score.c send.c \ sendlib.c send_smtp.c sort.c state.c thread.c account.c remailer.c \ - mutt_sasl.c md5.c + mutt_sasl.c madmutt_LDADD = $(top_builddir)/lib-mime/libmime.a \ $(top_builddir)/lib-mx/libmx.a \ diff --git a/imap/message.c b/imap/message.c index 3980496..d780c9c 100644 --- a/imap/message.c +++ b/imap/message.c @@ -57,7 +57,7 @@ int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend) "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL"; #ifdef USE_HCACHE - void *hc = NULL; + hcache_t *hc = NULL; unsigned long *uid_validity = NULL; char uid_buf[64]; #endif /* USE_HCACHE */ @@ -164,7 +164,7 @@ int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend) if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) { imap_free_header_data((void *)&h.data); m_fclose(&fp); - mutt_hcache_close (hc); + mutt_hcache_close (&hc); return -1; } } @@ -264,7 +264,7 @@ int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend) imap_free_header_data ((void *)&h.data); m_fclose(&fp); #ifdef USE_HCACHE - mutt_hcache_close (hc); + mutt_hcache_close (&hc); #endif /* USE_HCACHE */ return -1; } @@ -280,7 +280,7 @@ int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend) } #ifdef USE_HCACHE - mutt_hcache_close (hc); + mutt_hcache_close (&hc); #endif /* USE_HCACHE */ m_fclose(&fp); diff --git a/lib-mx/hcache.c b/lib-mx/hcache.c index 846a6cf..4b07b5a 100644 --- a/lib-mx/hcache.c +++ b/lib-mx/hcache.c @@ -25,21 +25,18 @@ #include -#include "md5.h" #include "charset.h" #include "mutt.h" #include "hcache.h" -struct header_cache { +struct hcache_t { #if defined(HAVE_QDBM) VILLA *db; - char *folder; - unsigned int crc; #elif defined(HAVE_GDBM) GDBM_FILE db; +#endif char *folder; unsigned int crc; -#endif }; typedef union { @@ -383,42 +380,30 @@ static int crc32_matches (const char *d, unsigned int crc) return (crc == mycrc); } -/* Append md5sumed folder to path if path is a directory. */ -static const char *mutt_hcache_per_folder (const char *path, - const char *folder) +static const char * +mutt_hcache_per_folder(const char *path, const char *folder) { - static char mutt_hcache_per_folder_path[_POSIX_PATH_MAX]; - struct stat path_stat; - MD5_CTX md5; - unsigned char md5sum[16]; - int ret; - - ret = stat (path, &path_stat); - if (ret < 0) { - return path; - } + static char buf[_POSIX_PATH_MAX]; + struct stat st; + int pos; - if (!S_ISDIR (path_stat.st_mode)) { - return path; - } - - MD5Init (&md5); - MD5Update (&md5, (unsigned char *) folder, m_strlen(folder)); - MD5Final (md5sum, &md5); + if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) { + return path; + } - ret = snprintf (mutt_hcache_per_folder_path, _POSIX_PATH_MAX, - "%s/%02x%02x%02x%02x%02x%02x%02x%02x" - "%02x%02x%02x%02x%02x%02x%02x%02x", - path, md5sum[0], md5sum[1], md5sum[2], md5sum[3], - md5sum[4], md5sum[5], md5sum[6], md5sum[7], md5sum[8], - md5sum[9], md5sum[10], md5sum[11], md5sum[12], - md5sum[13], md5sum[14], md5sum[15]); - - if (ret <= 0) { - return path; - } + pos = m_strcpy(buf, sizeof(buf), path); + pos += m_strputc(buf + pos, sizeof(buf) - pos, '/'); - return mutt_hcache_per_folder_path; + while (*folder) { + if (isalnum((unsigned char)*folder) || *folder == '.') { + pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder); + } else { + pos += m_strputc(buf + pos, sizeof(buf) - pos, '_'); + } + folder++; + } + pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb"); + return buf; } /* This function transforms a header into a char so that it is useable by @@ -426,7 +411,7 @@ static const char *mutt_hcache_per_folder (const char *path, static void *mutt_hcache_dump (void *_db, HEADER * h, int *off, unsigned long uid_validity) { - struct header_cache *db = _db; + struct hcache_t *db = _db; unsigned char *d = NULL; *off = 0; @@ -489,58 +474,63 @@ HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh) return h; } -#if defined(HAVE_QDBM) -void * -mutt_hcache_open(const char *path, const char *folder) +hcache_t *mutt_hcache_open(const char *path, const char *folder) { - struct header_cache *h = p_new(struct header_cache, 1); - int flags = VL_OWRITER | VL_OCREAT; - h->db = NULL; - h->folder = m_strdup(folder); - h->crc = generate_crc32(); + hcache_t *h = p_new(hcache_t, 1); - if (!path || path[0] == '\0') - { - p_delete(&h->folder); - p_delete(&h); - return NULL; - } + h->folder = m_strdup(folder); + h->crc = generate_crc32(); - path = mutt_hcache_per_folder(path, folder); + if (m_strisempty(path)) { + p_delete(&h->folder); + p_delete(&h); + return NULL; + } - if (option(OPTHCACHECOMPRESS)) - flags |= VL_OZCOMP; + path = mutt_hcache_per_folder(path, folder); - h->db = vlopen(path, flags, VL_CMPLEX); - if (h->db) - return h; - else - { - p_delete(&h->folder); - p_delete(&h); + { +#if defined(HAVE_QDBM) + int flags = VL_OWRITER | VL_OCREAT; + if (option(OPTHCACHECOMPRESS)) + flags |= VL_OZCOMP; - return NULL; - } + h->db = vlopen(path, flags, VL_CMPLEX); +#elif defined(HAVE_GDBM) + int pagesize = atoi(HeaderCachePageSize) ?: 16384; + + h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL); +#endif + } + + if (!h->db) { + p_delete(&h->folder); + p_delete(&h); + } + return h; } -void -mutt_hcache_close(void *db) +void mutt_hcache_close(hcache_t **db) { - struct header_cache *h = db; + if (!*db) + return; - if (!h) - return; +#if defined(HAVE_QDBM) + vlclose((*db)->db); +#elif defined(HAVE_GDBM) + gdbm_close((*db)->db); +#endif - vlclose(h->db); - p_delete(&h->folder); - p_delete(&h); + p_delete(&(*db)->folder); + p_delete(db); } +#if defined(HAVE_QDBM) void * -mutt_hcache_fetch(void *db, const char *filename, +mutt_hcache_fetch(hcache_t *db, const char *filename, ssize_t(*keylen) (const char *fn)) { - struct header_cache *h = db; + hcache_t *h = db; char path[_POSIX_PATH_MAX]; int ksize; char *data = NULL; @@ -565,11 +555,11 @@ mutt_hcache_fetch(void *db, const char *filename, } int -mutt_hcache_store(void *db, const char *filename, HEADER * header, +mutt_hcache_store(hcache_t *db, const char *filename, HEADER * header, unsigned long uid_validity, ssize_t(*keylen) (const char *fn)) { - struct header_cache *h = db; + hcache_t *h = db; char path[_POSIX_PATH_MAX]; int ret; int ksize, dsize; @@ -593,10 +583,10 @@ mutt_hcache_store(void *db, const char *filename, HEADER * header, } int -mutt_hcache_delete(void *db, const char *filename, +mutt_hcache_delete(hcache_t *db, const char *filename, ssize_t(*keylen) (const char *fn)) { - struct header_cache *h = db; + hcache_t *h = db; char path[_POSIX_PATH_MAX]; int ksize; @@ -613,58 +603,10 @@ mutt_hcache_delete(void *db, const char *filename, #elif defined(HAVE_GDBM) -void *mutt_hcache_open (const char *path, const char *folder) -{ - struct header_cache *h = p_new(struct header_cache, 1); - int pagesize = - atoi (HeaderCachePageSize) ? atoi (HeaderCachePageSize) : 16384; - h->db = NULL; - h->folder = m_strdup(folder); - h->crc = generate_crc32 (); - - if (!path || path[0] == '\0') { - p_delete(&h->folder); - p_delete(&h); - return NULL; - } - - path = mutt_hcache_per_folder (path, folder); - - h->db = gdbm_open ((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL); - if (h->db) { - return h; - } - - /* if rw failed try ro */ - h->db = gdbm_open ((char *) path, pagesize, GDBM_READER, 00600, NULL); - if (h->db) { - return h; - } - else { - p_delete(&h->folder); - p_delete(&h); - - return NULL; - } -} - -void mutt_hcache_close (void *db) -{ - struct header_cache *h = db; - - if (!h) { - return; - } - - gdbm_close (h->db); - p_delete(&h->folder); - p_delete(&h); -} - -void *mutt_hcache_fetch (void *db, const char *filename, +void *mutt_hcache_fetch (hcache_t *db, const char *filename, ssize_t (*keylen) (const char *fn)) { - struct header_cache *h = db; + hcache_t *h = db; datum key; datum data; char path[_POSIX_PATH_MAX]; @@ -690,10 +632,10 @@ void *mutt_hcache_fetch (void *db, const char *filename, } int -mutt_hcache_store (void *db, const char *filename, HEADER * header, +mutt_hcache_store (hcache_t *db, const char *filename, HEADER * header, unsigned long uid_validity, ssize_t (*keylen) (const char *fn)) { - struct header_cache *h = db; + hcache_t *h = db; datum key; datum data; char path[_POSIX_PATH_MAX]; @@ -719,11 +661,11 @@ mutt_hcache_store (void *db, const char *filename, HEADER * header, } int -mutt_hcache_delete (void *db, const char *filename, +mutt_hcache_delete (hcache_t *db, const char *filename, ssize_t (*keylen) (const char *fn)) { datum key; - struct header_cache *h = db; + hcache_t *h = db; char path[_POSIX_PATH_MAX]; if (!h) { diff --git a/lib-mx/hcache.h b/lib-mx/hcache.h index a764817..d173814 100644 --- a/lib-mx/hcache.h +++ b/lib-mx/hcache.h @@ -12,15 +12,18 @@ #define _MUTT_HCACHE_H #ifdef USE_HCACHE -void *mutt_hcache_open(const char *path, const char *folder); -void mutt_hcache_close(void *db); +typedef struct hcache_t hcache_t; + +hcache_t *mutt_hcache_open(const char *path, const char *folder); +void mutt_hcache_close(hcache_t **db); + HEADER *mutt_hcache_restore(const unsigned char *d, HEADER **oh); -void *mutt_hcache_fetch(void *db, const char *filename, +void *mutt_hcache_fetch(hcache_t *db, const char *filename, ssize_t (*keylen)(const char *fn)); -int mutt_hcache_store(void *db, const char *filename, HEADER *h, +int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *h, unsigned long uid_validity, ssize_t (*keylen)(const char *fn)); -int mutt_hcache_delete(void *db, const char *filename, +int mutt_hcache_delete(hcache_t *db, const char *filename, ssize_t (*keylen)(const char *fn)); #endif /* USE_HCACHE */ diff --git a/lib-mx/mh.c b/lib-mx/mh.c index 38ad5e3..ab24c4f 100644 --- a/lib-mx/mh.c +++ b/lib-mx/mh.c @@ -730,7 +730,7 @@ static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md) int count; #ifdef USE_HCACHE - void *hc = NULL; + hcache_t *hc = NULL; void *data; struct timeval *when = NULL; struct stat lastchanged; @@ -781,7 +781,7 @@ static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md) #endif } #ifdef USE_HCACHE - mutt_hcache_close (hc); + mutt_hcache_close (&hc); #endif } diff --git a/pop/Makefile.am b/pop/Makefile.am index 6fca1fb..45261db 100644 --- a/pop/Makefile.am +++ b/pop/Makefile.am @@ -7,8 +7,9 @@ AUTOMAKE_OPTIONS = foreign INCLUDES = -I$(top_srcdir) -I../intl noinst_LIBRARIES = libpop.a -noinst_HEADERS = pop.h +noinst_HEADERS = pop.h md5.h -libpop_a_SOURCES = pop.c pop_auth.c pop_lib.c pop.h +libpop_a_SOURCES = pop.h md5.h \ + pop.c md5.c pop_auth.c pop_lib.c -include $(top_builddir)/tools/cflags.mk diff --git a/md5.c b/pop/md5.c similarity index 95% rename from md5.c rename to pop/md5.c index aa36b26..0ed0205 100644 --- a/md5.c +++ b/pop/md5.c @@ -119,10 +119,10 @@ void MD5Update (context, input, inputLen) unsigned char *input; /* input block */ unsigned int inputLen; /* length of input block */ { - unsigned int i, index, partLen; + unsigned int i, idx, partLen; /* Compute number of bytes mod 64 */ - index = (unsigned int) ((context->count[0] >> 3) & 0x3F); + idx = (unsigned int) ((context->count[0] >> 3) & 0x3F); /* Update number of bits */ if ((context->count[0] += ((uint32_t) inputLen << 3)) @@ -130,25 +130,25 @@ void MD5Update (context, input, inputLen) context->count[1]++; context->count[1] += ((uint32_t) inputLen >> 29); - partLen = 64 - index; + partLen = 64 - idx; /* Transform as many times as possible. */ if (inputLen >= partLen) { - MD5_memcpy ((unsigned char *) & context->buffer[index], (unsigned char *) input, partLen); + MD5_memcpy ((unsigned char *) & context->buffer[idx], (unsigned char *) input, partLen); MD5Transform (context->state, context->buffer); for (i = partLen; i + 63 < inputLen; i += 64) MD5Transform (context->state, &input[i]); - index = 0; + idx = 0; } else i = 0; /* Buffer remaining input */ MD5_memcpy - ((unsigned char *) & context->buffer[index], (unsigned char *) & input[i], inputLen - i); + ((unsigned char *) & context->buffer[idx], (unsigned char *) & input[i], inputLen - i); } /* MD5 finalization. Ends an MD5 message-digest operation, writing the @@ -159,15 +159,15 @@ void MD5Final (digest, context) MD5_CTX *context; /* context */ { unsigned char bits[8]; - unsigned int index, padLen; + unsigned int idx, padLen; /* Save number of bits */ Encode (bits, context->count, 8); /* Pad out to 56 mod 64. */ - index = (unsigned int) ((context->count[0] >> 3) & 0x3f); - padLen = (index < 56) ? (56 - index) : (120 - index); + idx = (unsigned int) ((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); MD5Update (context, PADDING, padLen); /* Append length (before padding) */ diff --git a/md5.h b/pop/md5.h similarity index 100% rename from md5.h rename to pop/md5.h -- 2.20.1