2 * Copyright notice from original mutt:
3 * Copyright (C) 2004 Thomas Glanzmann <sithglan@stud.uni-erlangen.de>
4 * Copyright (C) 2004 Tobias Werth <sitowert@stud.uni-erlangen.de>
5 * Copyright (C) 2004 Brian Fundakowski Feldman <green@FreeBSD.org>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 #include <lib-lib/lib-lib.h>
16 #if defined(HAVE_QDBM)
20 #elif defined(HAVE_GDBM)
24 #include <imap/message.h>
31 #if defined(HAVE_QDBM)
33 #elif defined(HAVE_GDBM)
41 struct timeval timeval;
42 unsigned long uid_validity;
45 #define UPPER4K(i) ((i & ~(4096 - 1)) + 4096)
47 static unsigned char *lazy_malloc(ssize_t siz)
49 return p_new(unsigned char, UPPER4K(siz));
52 static void lazy_realloc(unsigned char **p, ssize_t siz)
54 p_realloc(p, UPPER4K(siz));
57 static unsigned char *dump_int (unsigned int i, unsigned char *d, int *off)
59 lazy_realloc (&d, *off + sizeof (int));
60 memcpy (d + *off, &i, sizeof (int));
61 (*off) += sizeof (int);
66 static void restore_int (unsigned int *i, const unsigned char *d, int *off)
68 memcpy (i, d + *off, sizeof (int));
69 (*off) += sizeof (int);
72 static unsigned char *dump_char (char *c, unsigned char *d, int *off)
78 d = dump_int (size, d, off);
82 size = m_strlen(c) + 1;
83 d = dump_int (size, d, off);
84 lazy_realloc (&d, *off + size);
85 memcpy (d + *off, c, size);
91 static void restore_char (char **c, const unsigned char *d, int *off)
95 restore_int (&size, d, off);
102 *c = p_dup(d + *off, size);
106 static unsigned char *dump_address (address_t * a, unsigned char *d, int *off)
108 unsigned int counter = 0;
109 unsigned int start_off = *off;
111 d = dump_int (0xdeadbeef, d, off);
114 d = dump_char (a->personal, d, off);
115 d = dump_char (a->mailbox, d, off);
116 d = dump_int (a->group, d, off);
121 memcpy (d + start_off, &counter, sizeof (int));
126 static void restore_address (address_t ** a, const unsigned char *d, int *off)
128 unsigned int counter;
130 restore_int (&counter, d, off);
133 *a = p_new(address_t, 1);
134 restore_char (&(*a)->personal, d, off);
135 restore_char (&(*a)->mailbox, d, off);
136 restore_int ((unsigned int *) &(*a)->group, d, off);
144 static unsigned char *dump_list (string_list_t * l, unsigned char *d, int *off)
146 unsigned int counter = 0;
147 unsigned int start_off = *off;
149 d = dump_int (0xdeadbeef, d, off);
152 d = dump_char (l->data, d, off);
157 memcpy (d + start_off, &counter, sizeof (int));
162 static void restore_list (string_list_t ** l, const unsigned char *d, int *off)
164 unsigned int counter;
166 restore_int (&counter, d, off);
169 *l = p_new(string_list_t, 1);
170 restore_char (&(*l)->data, d, off);
178 static unsigned char *dump_parameter (parameter_t * p, unsigned char *d,
181 unsigned int counter = 0;
182 unsigned int start_off = *off;
184 d = dump_int (0xdeadbeef, d, off);
187 d = dump_char (p->attribute, d, off);
188 d = dump_char (p->value, d, off);
193 memcpy (d + start_off, &counter, sizeof (int));
199 restore_parameter (parameter_t ** p, const unsigned char *d, int *off)
201 unsigned int counter;
203 restore_int (&counter, d, off);
206 *p = parameter_new();
207 restore_char (&(*p)->attribute, d, off);
208 restore_char (&(*p)->value, d, off);
214 static unsigned char *dump_body (BODY * c, unsigned char *d, int *off)
216 lazy_realloc (&d, *off + sizeof (BODY));
217 memcpy (d + *off, c, sizeof (BODY));
218 *off += sizeof (BODY);
220 d = dump_char (c->xtype, d, off);
221 d = dump_char (c->subtype, d, off);
223 d = dump_parameter (c->parameter, d, off);
225 d = dump_char (c->description, d, off);
226 d = dump_char (c->form_name, d, off);
227 d = dump_char (c->filename, d, off);
228 d = dump_char (c->d_filename, d, off);
233 static void restore_body (BODY * c, const unsigned char *d, int *off)
235 memcpy (c, d + *off, sizeof (BODY));
236 *off += sizeof (BODY);
238 restore_char (&c->xtype, d, off);
239 restore_char (&c->subtype, d, off);
241 restore_parameter (&c->parameter, d, off);
243 restore_char (&c->description, d, off);
244 restore_char (&c->form_name, d, off);
245 restore_char (&c->filename, d, off);
246 restore_char (&c->d_filename, d, off);
249 static unsigned char *dump_envelope (ENVELOPE * e, unsigned char *d, int *off)
251 d = dump_address (e->return_path, d, off);
252 d = dump_address (e->from, d, off);
253 d = dump_address (e->to, d, off);
254 d = dump_address (e->cc, d, off);
255 d = dump_address (e->bcc, d, off);
256 d = dump_address (e->sender, d, off);
257 d = dump_address (e->reply_to, d, off);
258 d = dump_address (e->mail_followup_to, d, off);
260 d = dump_char (e->subject, d, off);
262 d = dump_int (e->real_subj - e->subject, d, off);
265 d = dump_int (-1, d, off);
267 d = dump_char (e->message_id, d, off);
268 d = dump_char (e->supersedes, d, off);
269 d = dump_char (e->date, d, off);
270 d = dump_char (e->x_label, d, off);
271 d = dump_char (e->list_post, d, off);
274 d = dump_char (e->newsgroups, d, off);
275 d = dump_char (e->xref, d, off);
276 d = dump_char (e->followup_to, d, off);
277 d = dump_char (e->x_comment_to, d, off);
280 d = dump_list (e->references, d, off);
281 d = dump_list (e->in_reply_to, d, off);
282 d = dump_list (e->userhdrs, d, off);
287 static void restore_envelope (ENVELOPE * e, const unsigned char *d, int *off)
291 restore_address (&e->return_path, d, off);
292 restore_address (&e->from, d, off);
293 restore_address (&e->to, d, off);
294 restore_address (&e->cc, d, off);
295 restore_address (&e->bcc, d, off);
296 restore_address (&e->sender, d, off);
297 restore_address (&e->reply_to, d, off);
298 restore_address (&e->mail_followup_to, d, off);
300 restore_char (&e->subject, d, off);
301 restore_int ((unsigned int *) (&real_subj_off), d, off);
302 if (0 <= real_subj_off) {
303 e->real_subj = e->subject + real_subj_off;
308 restore_char (&e->message_id, d, off);
309 restore_char (&e->supersedes, d, off);
310 restore_char (&e->date, d, off);
311 restore_char (&e->x_label, d, off);
312 restore_char (&e->list_post, d, off);
315 restore_char (&e->newsgroups, d, off);
316 restore_char (&e->xref, d, off);
317 restore_char (&e->followup_to, d, off);
318 restore_char (&e->x_comment_to, d, off);
321 restore_list (&e->references, d, off);
322 restore_list (&e->in_reply_to, d, off);
323 restore_list (&e->userhdrs, d, off);
326 static unsigned int crc32(unsigned int crc, const void *src, ssize_t len)
329 const unsigned char *p = src;
333 for (i = 0; i < 8; i++)
334 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
339 static int generate_crc32(void)
343 crc = crc32(crc, "madmutt.2007.05.13", m_strlen("madmutt.2007.05.13"));
344 #ifdef HAVE_LANGINFO_CODESET
345 crc = crc32(crc, MCharset.charset, m_strlen(MCharset.charset));
346 crc = crc32(crc, "HAVE_LANGINFO_CODESET",
347 m_strlen("HAVE_LANGINFO_CODESET"));
349 crc = crc32(crc, "USE_POP", m_strlen("USE_POP"));
350 crc = crc32(crc, "MIXMASTER", m_strlen("MIXMASTER"));
351 crc = crc32(crc, "USE_IMAP", m_strlen("USE_IMAP"));
353 crc = crc32(crc, "USE_NNTP", m_strlen("USE_NNTP"));
359 mutt_hcache_per_folder(const char *path, const char *folder)
361 static char buf[_POSIX_PATH_MAX];
365 if (stat(path, &st) < 0 || !S_ISDIR(st.st_mode)) {
369 pos = m_strcpy(buf, sizeof(buf), path);
370 pos += m_strputc(buf + pos, sizeof(buf) - pos, '/');
373 if (isalnum((unsigned char)*folder) || *folder == '.') {
374 pos += m_strputc(buf + pos, sizeof(buf) - pos, *folder);
376 pos += m_strputc(buf + pos, sizeof(buf) - pos, '_');
380 pos += m_strcpy(buf + pos, sizeof(buf) - pos, ".hdb");
384 /* This function transforms a header into a char so that it is useable by
386 static void *mutt_hcache_dump (void *_db, HEADER * h, int *off,
387 unsigned long uid_validity)
389 struct hcache_t *db = _db;
390 unsigned char *d = NULL;
394 d = lazy_malloc (sizeof (validate));
397 memcpy (d, &uid_validity, sizeof (unsigned long));
402 gettimeofday (&now, NULL);
403 memcpy (d, &now, sizeof (struct timeval));
405 *off += sizeof (validate);
407 d = dump_int (db->crc, d, off);
409 lazy_realloc (&d, *off + sizeof (HEADER));
410 memcpy (d + *off, h, sizeof (HEADER));
411 *off += sizeof (HEADER);
413 d = dump_envelope (h->env, d, off);
414 d = dump_body (h->content, d, off);
415 d = dump_char (h->maildir_flags, d, off);
420 HEADER *mutt_hcache_restore (const unsigned char *d, HEADER ** oh)
423 HEADER *h = header_new();
426 off += sizeof (validate);
429 off += sizeof (unsigned int);
431 memcpy (h, d + off, sizeof (HEADER));
432 off += sizeof (HEADER);
434 h->env = envelope_new();
435 restore_envelope (h->env, d, &off);
437 h->content = body_new();
438 restore_body (h->content, d, &off);
440 restore_char (&h->maildir_flags, d, &off);
442 /* this is needed for maildir style mailboxes */
445 h->path = m_strdup((*oh)->path);
452 hcache_t *mutt_hcache_open(const char *path, const char *folder)
454 hcache_t *h = p_new(hcache_t, 1);
456 h->folder = m_strdup(folder);
457 h->crc = generate_crc32();
459 if (m_strisempty(path)) {
460 p_delete(&h->folder);
465 path = mutt_hcache_per_folder(path, folder);
468 #if defined(HAVE_QDBM)
469 int flags = VL_OWRITER | VL_OCREAT;
470 if (option(OPTHCACHECOMPRESS))
473 h->db = vlopen(path, flags, VL_CMPLEX);
474 #elif defined(HAVE_GDBM)
475 int pagesize = atoi(HeaderCachePageSize) ?: 16384;
477 h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
482 p_delete(&h->folder);
488 void mutt_hcache_close(hcache_t **db)
493 #if defined(HAVE_QDBM)
495 #elif defined(HAVE_GDBM)
496 gdbm_close((*db)->db);
499 p_delete(&(*db)->folder);
503 void *mutt_hcache_fetch(hcache_t *db, const char *filename,
504 ssize_t (*keylen)(const char *fn))
506 char path[_POSIX_PATH_MAX];
512 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
515 #if defined(HAVE_QDBM)
516 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
517 data = vlget(db->db, path, ksize, NULL);
518 #elif defined(HAVE_GDBM)
519 datum k = { .dptr = path, .dsize = keylen(path) };
521 data = gdbm_fetch(db->db, k).dtpr;
526 int off = sizeof(validate);
529 restore_int(&crc, data, &off);
537 int mutt_hcache_store(hcache_t *db, const char *filename, HEADER *header,
538 unsigned long uid_validity,
539 ssize_t (*keylen)(const char *fn))
541 char path[_POSIX_PATH_MAX];
548 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
549 data = mutt_hcache_dump(db, header, &dsize, uid_validity);
552 #if defined(HAVE_QDBM)
553 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
555 ret = vlput(db->db, path, ksize, data, dsize, VL_DOVER);
556 #elif defined(HAVE_GDBM)
557 datum k = { .dptr = path, .dsize = keylen(path) };
558 datum v = { .dptr = data, .dsize = dsize };
560 ret = gdbm_store (db->db, k, v, GDBM_REPLACE);
568 int mutt_hcache_delete(hcache_t *db, const char *filename,
569 ssize_t (*keylen)(const char *fn))
571 char path[_POSIX_PATH_MAX];
576 snprintf(path, sizeof(path), "%s%s", db->folder, filename);
579 #if defined(HAVE_QDBM)
580 int ksize = strlen(db->folder) + keylen(path + strlen(db->folder));
581 return vlout(db->db, path, ksize);
582 #elif defined(HAVE_GDBM)
583 datum k = { .dptr = path, .dsize = keylen(path) };
584 return gdbm_delete(db->db, k);
589 #endif /* USE_HCACHE */