#ifdef USE_HCACHE
-#define MUTTNG_HCACHE_ID "0x004"
-
#if defined(HAVE_QDBM)
#include <depot.h>
#include <cabin.h>
struct hcache_t {
#if defined(HAVE_QDBM)
- VILLA *db;
+ VILLA *db;
#elif defined(HAVE_GDBM)
- GDBM_FILE db;
+ GDBM_FILE db;
#endif
- char *folder;
- unsigned int crc;
+ char *folder;
+ unsigned int crc;
};
typedef union {
restore_list (&e->userhdrs, d, off);
}
-static
-unsigned int crc32 (unsigned int crc, unsigned char const *p, ssize_t len)
+static unsigned int crc32(unsigned int crc, const void *src, ssize_t len)
{
- int i;
+ int i;
+ const unsigned char *p = src;
- while (len--) {
- crc ^= *p++;
- for (i = 0; i < 8; i++)
- crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
- }
- return crc;
+ while (len--) {
+ crc ^= *p++;
+ for (i = 0; i < 8; i++)
+ crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
+ }
+ return crc;
}
-static int generate_crc32 ()
+static int generate_crc32(void)
{
- int crc = 0;
-
- crc = crc32 (crc, (unsigned char const *)
- MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613",
- m_strlen
- (MUTTNG_HCACHE_ID "sithglan@stud.uni-erlangen.de[sithglan]|hcache.c|20041108231548|29613"));
+ static int crc = 0;
+ crc = crc32(crc, "madmutt.2007.05.13", m_strlen("madmutt.2007.05.13"));
#ifdef HAVE_LANGINFO_CODESET
- crc = crc32(crc, (unsigned char const *) MCharset.charset, m_strlen(MCharset.charset));
- crc = crc32(crc, (unsigned char const *) "HAVE_LANGINFO_CODESET",
- m_strlen("HAVE_LANGINFO_CODESET"));
+ crc = crc32(crc, MCharset.charset, m_strlen(MCharset.charset));
+ crc = crc32(crc, "HAVE_LANGINFO_CODESET",
+ m_strlen("HAVE_LANGINFO_CODESET"));
#endif
-
- crc = crc32(crc, (unsigned char const *) "USE_POP", m_strlen("USE_POP"));
-
- crc = crc32(crc, (unsigned char const *) "MIXMASTER",
- m_strlen("MIXMASTER"));
-
- crc = crc32(crc, (unsigned char const *) "USE_IMAP", m_strlen("USE_IMAP"));
-
+ crc = crc32(crc, "USE_POP", m_strlen("USE_POP"));
+ crc = crc32(crc, "MIXMASTER", m_strlen("MIXMASTER"));
+ crc = crc32(crc, "USE_IMAP", m_strlen("USE_IMAP"));
#ifdef USE_NNTP
- crc = crc32(crc, (unsigned char const *) "USE_NNTP", m_strlen("USE_NNTP"));
+ crc = crc32(crc, "USE_NNTP", m_strlen("USE_NNTP"));
#endif
- return crc;
-}
-
-static int crc32_matches (const char *d, unsigned int crc)
-{
- int off = sizeof (validate);
- unsigned int mycrc = 0;
-
- if (!d) {
- return 0;
- }
-
- restore_int (&mycrc, (unsigned char *) d, &off);
-
- return (crc == mycrc);
+ return crc;
}
static const char *
p_delete(db);
}
-#if defined(HAVE_QDBM)
-void *
-mutt_hcache_fetch(hcache_t *db, const char *filename,
- ssize_t(*keylen) (const char *fn))
-{
- hcache_t *h = db;
- char path[_POSIX_PATH_MAX];
- int ksize;
- char *data = NULL;
-
- if (!h)
- return NULL;
-
- m_strcpy(path, sizeof(path), h->folder);
- m_strcat(path, sizeof(path), filename);
-
- ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
-
- data = vlget(h->db, path, ksize, NULL);
-
- if (!crc32_matches(data, h->crc))
- {
- p_delete(&data);
- return NULL;
- }
-
- return data;
-}
-
-int
-mutt_hcache_store(hcache_t *db, const char *filename, HEADER * header,
- unsigned long uid_validity,
- ssize_t(*keylen) (const char *fn))
+void *mutt_hcache_fetch(hcache_t *db, const char *filename,
+ ssize_t (*keylen)(const char *fn))
{
- hcache_t *h = db;
- char path[_POSIX_PATH_MAX];
- int ret;
- int ksize, dsize;
- char *data = NULL;
-
- if (!h)
- return -1;
-
- m_strcpy(path, sizeof(path), h->folder);
- m_strcat(path, sizeof(path), filename);
-
- ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
+ char path[_POSIX_PATH_MAX];
+ void *data = NULL;
- data = mutt_hcache_dump(db, header, &dsize, uid_validity);
-
- ret = vlput(h->db, path, ksize, data, dsize, VL_DOVER);
-
- p_delete(&data);
-
- return ret;
-}
-
-int
-mutt_hcache_delete(hcache_t *db, const char *filename,
- ssize_t(*keylen) (const char *fn))
-{
- hcache_t *h = db;
- char path[_POSIX_PATH_MAX];
- int ksize;
-
- if (!h)
- return -1;
-
- m_strcpy(path, sizeof(path), h->folder);
- m_strcat(path, sizeof(path), filename);
-
- ksize = strlen(h->folder) + keylen(path + strlen(h->folder));
+ if (!db)
+ return NULL;
- return vlout(h->db, path, ksize);
-}
+ snprintf(path, sizeof(path), "%s%s", db->folder, filename);
+ {
+#if defined(HAVE_QDBM)
+ int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
+ data = vlget(db->db, path, ksize, NULL);
#elif defined(HAVE_GDBM)
+ datum k = { .dptr = path, .dsize = keylen(path) };
-void *mutt_hcache_fetch (hcache_t *db, const char *filename,
- ssize_t (*keylen) (const char *fn))
-{
- hcache_t *h = db;
- datum key;
- datum data;
- char path[_POSIX_PATH_MAX];
-
- if (!h) {
- return NULL;
- }
-
- m_strcpy(path, sizeof(path), h->folder);
- strncat (path, filename, sizeof (path) - m_strlen(path));
-
- key.dptr = path;
- key.dsize = keylen (path);
+ data = gdbm_fetch(db->db, k).dtpr;
+#endif
+ }
- data = gdbm_fetch (h->db, key);
+ if (data) {
+ int off = sizeof(validate);
+ unsigned crc = 0;
- if (!crc32_matches (data.dptr, h->crc)) {
- p_delete(&data.dptr);
- return NULL;
- }
+ restore_int(&crc, data, &off);
+ if (crc != db->crc)
+ p_delete(&data);
+ }
- return data.dptr;
+ return data;
}
-int
-mutt_hcache_store (hcache_t *db, const char *filename, HEADER * header,
- unsigned long uid_validity, ssize_t (*keylen) (const char *fn))
+int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
+ unsigned long uid_validity,
+ ssize_t (*keylen)(const char *fn))
{
- hcache_t *h = db;
- datum key;
- datum data;
- char path[_POSIX_PATH_MAX];
- int ret;
-
- if (!h) {
- return -1;
- }
+ char path[_POSIX_PATH_MAX];
+ int ret, dsize;
+ void *data = NULL;
- m_strcpy(path, sizeof(path), h->folder);
- strncat (path, filename, sizeof (path) - m_strlen(path));
+ if (!db)
+ return -1;
- key.dptr = path;
- key.dsize = keylen (path);
+ snprintf(path, sizeof(path), "%s%s", db->folder, filename);
+ data = mutt_hcache_dump(db, header, &dsize, uid_validity);
- data.dptr = mutt_hcache_dump (db, header, &data.dsize, uid_validity);
+ {
+#if defined(HAVE_QDBM)
+ int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
- ret = gdbm_store (h->db, key, data, GDBM_REPLACE);
+ ret = vlput(db->db, path, ksize, data, dsize, VL_DOVER);
+#elif defined(HAVE_GDBM)
+ datum k = { .dptr = path, .dsize = keylen(path) };
+ datum v = { .dptr = data, .dsize = dsize };
- p_delete(&data.dptr);
+ ret = gdbm_store (db->db, k, v, GDBM_REPLACE);
+#endif
+ }
- return ret;
+ p_delete(&data);
+ return ret;
}
-int
-mutt_hcache_delete (hcache_t *db, const char *filename,
- ssize_t (*keylen) (const char *fn))
+int mutt_hcache_delete(hcache_t *db, const char *filename,
+ ssize_t (*keylen)(const char *fn))
{
- datum key;
- hcache_t *h = db;
- char path[_POSIX_PATH_MAX];
+ char path[_POSIX_PATH_MAX];
- if (!h) {
- return -1;
- }
-
- m_strcpy(path, sizeof(path), h->folder);
- strncat (path, filename, sizeof (path) - m_strlen(path));
+ if (!db)
+ return -1;
- key.dptr = path;
- key.dsize = keylen (path);
+ snprintf(path, sizeof(path), "%s%s", db->folder, filename);
- return gdbm_delete (h->db, key);
-}
+ {
+#if defined(HAVE_QDBM)
+ int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
+ return vlout(db->db, path, ksize);
+#elif defined(HAVE_GDBM)
+ datum k = { .dptr = path, .dsize = keylen(path) };
+ return gdbm_delete(db->db, k);
#endif
+ }
+}
#endif /* USE_HCACHE */