From 5d0104e059e3bd33be6acc3c00b3f3944a3897f6 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 13 May 2007 00:41:07 +0200 Subject: [PATCH] simplify hcache yet a bit more. Signed-off-by: Pierre Habouzit --- lib-mx/hcache.c | 264 +++++++++++++++--------------------------------- 1 file changed, 84 insertions(+), 180 deletions(-) diff --git a/lib-mx/hcache.c b/lib-mx/hcache.c index 4b07b5a..413d6c8 100644 --- a/lib-mx/hcache.c +++ b/lib-mx/hcache.c @@ -13,8 +13,6 @@ #ifdef USE_HCACHE -#define MUTTNG_HCACHE_ID "0x004" - #if defined(HAVE_QDBM) #include #include @@ -31,12 +29,12 @@ 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 { @@ -325,59 +323,36 @@ static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off) 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 * @@ -525,161 +500,90 @@ void mutt_hcache_close(hcache_t **db) 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 */ -- 2.20.1