#include <imap/message.h>
-#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 {
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
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;
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;
}
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;
}
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;
#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];
}
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];
}
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) {
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))
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
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) */