changeset 2972:7c4238c78aee HEAD

Reorganized mbox md5 summing code so it doesn't require sync context anymore.
author Timo Sirainen <tss@iki.fi>
date Wed, 15 Dec 2004 20:43:40 +0200
parents 1f7bcbb09f90
children 32cc9186d8eb
files src/lib-storage/index/mbox/Makefile.am src/lib-storage/index/mbox/mbox-md5.c src/lib-storage/index/mbox/mbox-md5.h src/lib-storage/index/mbox/mbox-sync-md5.c src/lib-storage/index/mbox/mbox-sync-parse.c src/lib-storage/index/mbox/mbox-sync-private.h
diffstat 6 files changed, 144 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/mbox/Makefile.am	Wed Dec 15 20:06:47 2004 +0200
+++ b/src/lib-storage/index/mbox/Makefile.am	Wed Dec 15 20:43:40 2004 +0200
@@ -16,8 +16,8 @@
 	mbox-list.c \
 	mbox-lock.c \
 	mbox-mail.c \
+	mbox-md5.c \
 	mbox-save.c \
-	mbox-sync-md5.c \
 	mbox-sync-parse.c \
 	mbox-sync-rewrite.c \
 	mbox-sync-update.c \
@@ -30,5 +30,6 @@
 	mbox-file.h \
 	mbox-from.h \
 	mbox-lock.h \
+	mbox-md5.h \
 	mbox-storage.h \
 	mbox-sync-private.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-storage/index/mbox/mbox-md5.c	Wed Dec 15 20:43:40 2004 +0200
@@ -0,0 +1,116 @@
+/* Copyright (C) 2004 Timo Sirainen */
+
+#include "lib.h"
+#include "md5.h"
+#include "message-parser.h"
+#include "mbox-md5.h"
+
+#include <stdlib.h>
+
+struct mbox_md5_context {
+	struct md5_context hdr_md5_ctx;
+	int seen_received_hdr;
+};
+
+struct mbox_md5_header_func {
+	const char *header;
+	int (*func)(struct mbox_md5_context *ctx,
+		    struct message_header_line *hdr);
+};
+
+static int parse_date(struct mbox_md5_context *ctx,
+		      struct message_header_line *hdr)
+{
+	if (!ctx->seen_received_hdr) {
+		/* Received-header contains date too, and more trusted one */
+		md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
+	}
+	return TRUE;
+}
+
+static int parse_delivered_to(struct mbox_md5_context *ctx,
+			      struct message_header_line *hdr)
+{
+	md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
+	return TRUE;
+}
+
+static int parse_message_id(struct mbox_md5_context *ctx,
+			    struct message_header_line *hdr)
+{
+	if (!ctx->seen_received_hdr) {
+		/* Received-header contains unique ID too,
+		   and more trusted one */
+		md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
+	}
+	return TRUE;
+}
+
+static int parse_received(struct mbox_md5_context *ctx,
+			  struct message_header_line *hdr)
+{
+	if (!ctx->seen_received_hdr) {
+		/* get only the first received-header */
+		md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
+		if (!hdr->continues)
+			ctx->seen_received_hdr = TRUE;
+	}
+	return TRUE;
+}
+
+static int parse_x_delivery_id(struct mbox_md5_context *ctx,
+			       struct message_header_line *hdr)
+{
+	/* Let the local delivery agent help generate unique ID's but don't
+	   blindly trust this header alone as it could just as easily come from
+	   the remote. */
+	md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
+	return TRUE;
+}
+
+
+static struct mbox_md5_header_func md5_header_funcs[] = {
+	{ "Date", parse_date },
+	{ "Delivered-To", parse_delivered_to },
+	{ "Message-ID", parse_message_id },
+	{ "Received", parse_received },
+	{ "X-Delivery-ID", parse_x_delivery_id }
+};
+#define MD5_HEADER_FUNCS_COUNT \
+	(sizeof(md5_header_funcs) / sizeof(*md5_header_funcs))
+
+static int bsearch_header_func_cmp(const void *p1, const void *p2)
+{
+	const char *key = p1;
+	const struct mbox_md5_header_func *func = p2;
+
+	return strcasecmp(key, func->header);
+}
+
+struct mbox_md5_context *mbox_md5_init(void)
+{
+	struct mbox_md5_context *ctx;
+
+	ctx = i_new(struct mbox_md5_context, 1);
+	md5_init(&ctx->hdr_md5_ctx);
+	return ctx;
+}
+
+void mbox_md5_continue(struct mbox_md5_context *ctx,
+		       struct message_header_line *hdr)
+{
+	struct mbox_md5_header_func *func;
+
+	func = bsearch(hdr->name, md5_header_funcs,
+		       MD5_HEADER_FUNCS_COUNT, sizeof(*md5_header_funcs),
+		       bsearch_header_func_cmp);
+	if (func != NULL)
+		(void)func->func(ctx, hdr);
+}
+
+void mbox_md5_finish(struct mbox_md5_context *ctx,
+		     unsigned char result[16])
+{
+	md5_final(&ctx->hdr_md5_ctx, result);
+	i_free(ctx);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-storage/index/mbox/mbox-md5.h	Wed Dec 15 20:43:40 2004 +0200
@@ -0,0 +1,10 @@
+#ifndef __MBOX_MD5_H
+#define __MBOX_MD5_H
+
+struct mbox_md5_context *mbox_md5_init(void);
+void mbox_md5_continue(struct mbox_md5_context *ctx,
+		       struct message_header_line *hdr);
+void mbox_md5_finish(struct mbox_md5_context *ctx,
+		     unsigned char result[16]);
+
+#endif
--- a/src/lib-storage/index/mbox/mbox-sync-md5.c	Wed Dec 15 20:06:47 2004 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-/* Copyright (C) 2004 Timo Sirainen */
-
-#include "lib.h"
-#include "message-parser.h"
-#include "mbox-sync-private.h"
-
-#include <stdlib.h>
-
-static int parse_date(struct mbox_sync_mail_context *ctx,
-		      struct message_header_line *hdr)
-{
-	if (!ctx->seen_received_hdr) {
-		/* Received-header contains date too, and more trusted one */
-		md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
-	}
-	return TRUE;
-}
-
-static int parse_delivered_to(struct mbox_sync_mail_context *ctx,
-			      struct message_header_line *hdr)
-{
-	md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
-	return TRUE;
-}
-
-static int parse_message_id(struct mbox_sync_mail_context *ctx,
-			    struct message_header_line *hdr)
-{
-	if (!ctx->seen_received_hdr) {
-		/* Received-header contains unique ID too,
-		   and more trusted one */
-		md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
-	}
-	return TRUE;
-}
-
-static int parse_received(struct mbox_sync_mail_context *ctx,
-			  struct message_header_line *hdr)
-{
-	if (!ctx->seen_received_hdr) {
-		/* get only the first received-header */
-		md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
-		if (!hdr->continues)
-			ctx->seen_received_hdr = TRUE;
-	}
-	return TRUE;
-}
-
-static int parse_x_delivery_id(struct mbox_sync_mail_context *ctx,
-			       struct message_header_line *hdr)
-{
-	/* Let the local delivery agent help generate unique ID's but don't
-	   blindly trust this header alone as it could just as easily come from
-	   the remote. */
-	md5_update(&ctx->hdr_md5_ctx, hdr->value, hdr->value_len);
-	return TRUE;
-}
-
-
-static struct mbox_sync_header_func md5_header_funcs[] = {
-	{ "Date", parse_date },
-	{ "Delivered-To", parse_delivered_to },
-	{ "Message-ID", parse_message_id },
-	{ "Received", parse_received },
-	{ "X-Delivery-ID", parse_x_delivery_id }
-};
-#define MD5_HEADER_FUNCS_COUNT \
-	(sizeof(md5_header_funcs) / sizeof(*md5_header_funcs))
-
-void mbox_sync_md5(struct mbox_sync_mail_context *ctx,
-		   struct message_header_line *hdr)
-{
-	struct mbox_sync_header_func *func;
-
-	func = bsearch(hdr->name, md5_header_funcs,
-		       MD5_HEADER_FUNCS_COUNT, sizeof(*md5_header_funcs),
-		       mbox_sync_bsearch_header_func_cmp);
-	if (func != NULL)
-		(void)func->func(ctx, hdr);
-}
--- a/src/lib-storage/index/mbox/mbox-sync-parse.c	Wed Dec 15 20:06:47 2004 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync-parse.c	Wed Dec 15 20:43:40 2004 +0200
@@ -11,12 +11,19 @@
 #include "message-parser.h"
 #include "mail-index.h"
 #include "mbox-storage.h"
+#include "mbox-md5.h"
 #include "mbox-sync-private.h"
 
 #include <stdlib.h>
 
 #define IS_LWSP_LF(c) (IS_LWSP(c) || (c) == '\n')
 
+struct mbox_sync_header_func {
+	const char *header;
+	int (*func)(struct mbox_sync_mail_context *ctx,
+		    struct message_header_line *hdr);
+};
+
 struct mbox_flag_type mbox_status_flags[] = {
 	{ 'R', MAIL_SEEN },
 	{ 'O', MBOX_NONRECENT },
@@ -292,7 +299,7 @@
 };
 #define HEADER_FUNCS_COUNT (sizeof(header_funcs) / sizeof(*header_funcs))
 
-int mbox_sync_bsearch_header_func_cmp(const void *p1, const void *p2)
+static int mbox_sync_bsearch_header_func_cmp(const void *p1, const void *p2)
 {
 	const char *key = p1;
 	const struct mbox_sync_header_func *func = p2;
@@ -307,6 +314,7 @@
 	struct message_header_parser_ctx *hdr_ctx;
 	struct message_header_line *hdr;
 	struct mbox_sync_header_func *func;
+	struct mbox_md5_context *mbox_md5_ctx;
 	size_t line_start_pos;
 	int i, ret;
 
@@ -321,7 +329,7 @@
 	ctx->content_length = (uoff_t)-1;
 	str_truncate(ctx->header, 0);
 
-	md5_init(&ctx->hdr_md5_ctx);
+        mbox_md5_ctx = mbox_md5_init();
 
         line_start_pos = 0;
 	hdr_ctx = message_parse_header_init(input, NULL, FALSE);
@@ -360,7 +368,7 @@
 			buffer_append(ctx->header, hdr->full_value,
 				      hdr->full_value_len);
 		} else {
-			mbox_sync_md5(ctx, hdr);
+			mbox_md5_continue(mbox_md5_ctx, hdr);
 			buffer_append(ctx->header, hdr->value,
 				      hdr->value_len);
 		}
@@ -370,7 +378,7 @@
 	i_assert(ret != 0);
 	message_parse_header_deinit(hdr_ctx);
 
-	md5_final(&ctx->hdr_md5_ctx, ctx->hdr_md5_sum);
+	mbox_md5_finish(mbox_md5_ctx, ctx->hdr_md5_sum);
 
 	if ((ctx->seq == 1 && sync_ctx->base_uid_validity == 0) ||
 	    (ctx->seq > 1 && sync_ctx->dest_first_mail)) {
@@ -393,6 +401,7 @@
 	struct message_header_parser_ctx *hdr_ctx;
 	struct message_header_line *hdr;
 	struct header_func *func;
+	struct mbox_md5_context *mbox_md5_ctx;
 	const void *data;
 	uint32_t uid;
 	int ret;
@@ -402,7 +411,7 @@
 	   the MD5 sum. */
 
 	memset(&ctx, 0, sizeof(ctx));
-	md5_init(&ctx.hdr_md5_ctx);
+        mbox_md5_ctx = mbox_md5_init();
 
 	hdr_ctx = message_parse_header_init(ibox->mbox_stream, NULL, FALSE);
 	while ((ret = message_parse_header_next(hdr_ctx, &hdr)) > 0) {
@@ -424,13 +433,13 @@
 					break;
 			}
 		} else {
-			mbox_sync_md5(&ctx, hdr);
+			mbox_md5_continue(mbox_md5_ctx, hdr);
 		}
 	}
 	i_assert(ret != 0);
 	message_parse_header_deinit(hdr_ctx);
 
-	md5_final(&ctx.hdr_md5_ctx, ctx.hdr_md5_sum);
+	mbox_md5_finish(mbox_md5_ctx, ctx.hdr_md5_sum);
 
 	if (ctx.mail.uid != 0) {
 		/* match by X-UID header */
--- a/src/lib-storage/index/mbox/mbox-sync-private.h	Wed Dec 15 20:06:47 2004 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync-private.h	Wed Dec 15 20:43:40 2004 +0200
@@ -67,7 +67,6 @@
 	size_t header_first_change, header_last_change;
 	string_t *header, *uidl;
 
-	struct md5_context hdr_md5_ctx;
 	unsigned char hdr_md5_sum[16];
 
 	uoff_t content_length;
@@ -82,7 +81,6 @@
 	unsigned int updated:1;
 	unsigned int recent:1;
 	unsigned int dirty:1;
-	unsigned int seen_received_hdr:1;
 	unsigned int uid_broken:1;
 };
 
@@ -118,12 +116,6 @@
 	unsigned int delay_writes:1;
 };
 
-struct mbox_sync_header_func {
-	const char *header;
-	int (*func)(struct mbox_sync_mail_context *ctx,
-		    struct message_header_line *hdr);
-};
-
 int mbox_sync(struct index_mailbox *ibox, enum mbox_sync_flags flags);
 int mbox_sync_has_changed(struct index_mailbox *ibox, int leave_dirty);
 
@@ -147,9 +139,4 @@
 void mbox_sync_move_buffer(struct mbox_sync_mail_context *ctx,
 			   size_t pos, size_t need, size_t have);
 
-int mbox_sync_bsearch_header_func_cmp(const void *p1, const void *p2);
-
-void mbox_sync_md5(struct mbox_sync_mail_context *ctx,
-		   struct message_header_line *hdr);
-
 #endif