[dovecot-cvs] dovecot/src/lib-mail message-body-search.c, 1.30, 1.31 message-body-search.h, 1.7, 1.8

tss at dovecot.org tss at dovecot.org
Tue Apr 3 17:51:30 EEST 2007


Update of /var/lib/cvs/dovecot/src/lib-mail
In directory talvi:/tmp/cvs-serv32504/lib-mail

Modified Files:
	message-body-search.c message-body-search.h 
Log Message:
Message body search API changed to init/search/deinit. Searching now builds
the init structure only once instead of for every message.



Index: message-body-search.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-mail/message-body-search.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- message-body-search.c	9 Jan 2007 20:19:32 -0000	1.30
+++ message-body-search.c	3 Apr 2007 14:51:28 -0000	1.31
@@ -1,4 +1,4 @@
-/* Copyright (C) 2002 Timo Sirainen */
+/* Copyright (C) 2002-2007 Timo Sirainen */
 
 #include "lib.h"
 #include "base64.h"
@@ -14,19 +14,20 @@
 
 #define DECODE_BLOCK_SIZE 8192
 
-struct body_search_context {
+struct message_body_search_context {
 	pool_t pool;
 
-	const char *key;
-	size_t key_len;
+	char *key;
+	char *charset;
+	unsigned int key_len;
 
-	const char *charset;
+	struct header_search_context *hdr_search_ctx;
 	unsigned int unknown_charset:1;
 	unsigned int search_header:1;
 };
 
 struct part_search_context {
-	struct body_search_context *body_ctx;
+	struct message_body_search_context *body_ctx;
 
 	struct charset_translation *translation;
 
@@ -103,18 +104,13 @@
 				  struct istream *input,
 				  const struct message_part *part)
 {
-	struct header_search_context *hdr_search_ctx;
+	struct header_search_context *hdr_search_ctx =
+		ctx->body_ctx->hdr_search_ctx;
 	struct message_header_parser_ctx *hdr_ctx;
 	struct message_header_line *hdr;
 	int ret;
 	bool found = FALSE;
 
-	hdr_search_ctx = message_header_search_init(pool_datastack_create(),
-						    ctx->body_ctx->key,
-						    "UTF-8", NULL);
-	/* Our key is in UTF-8. It can't be invalid. */
-	i_assert(hdr_search_ctx != NULL);
-
 	/* we default to text content-type */
 	ctx->content_type_text = TRUE;
 
@@ -122,6 +118,8 @@
 				      part->header_size.physical_size);
 	i_stream_seek(input, 0);
 
+	message_header_search_reset(hdr_search_ctx);
+
 	hdr_ctx = message_parse_header_init(input, NULL, TRUE);
 	while ((ret = message_parse_header_next(hdr_ctx, &hdr)) > 0) {
 		if (hdr->eoh)
@@ -170,7 +168,8 @@
 					 buffer_t *block)
 {
 	const unsigned char *p, *end, *key;
-	size_t key_len, block_size, *matches, match_count, value;
+	unsigned int key_len;
+	size_t block_size, *matches, match_count, value;
 	ssize_t i;
 
 	key = (const unsigned char *) ctx->body_ctx->key;
@@ -360,41 +359,54 @@
 	return found;
 }
 
-static bool
-message_body_search_init(struct body_search_context *ctx,
-			 const char *key, const char *charset,
-			 bool *unknown_charset_r, bool search_header)
+int message_body_search_init(pool_t pool, const char *key, const char *charset,
+			     bool search_header,
+			     struct message_body_search_context **ctx_r)
 {
+	struct message_body_search_context *ctx;
+	bool unknown_charset;
 	size_t key_len;
 
-	memset(ctx, 0, sizeof(struct body_search_context));
-
 	/* get the key uppercased */
-	key = charset_to_ucase_utf8_string(charset, unknown_charset_r,
-					   (const unsigned char *) key,
+	t_push();
+	key = charset_to_ucase_utf8_string(charset, &unknown_charset,
+					   (const unsigned char *)key,
 					   strlen(key), &key_len);
-	if (key == NULL)
-		return FALSE;
+	if (key == NULL) {
+		t_pop();
+		return unknown_charset ? 0 : -1;
+	}
 
-	ctx->key = key;
+	ctx = *ctx_r = p_new(pool, struct message_body_search_context, 1);
+	ctx->pool = pool;
+	ctx->key = p_strdup(pool, key);
 	ctx->key_len = key_len;
-	ctx->charset = charset;
+	ctx->charset = p_strdup(pool, charset);
 	ctx->unknown_charset = charset == NULL;
-	ctx->search_header = search_header;
+	ctx->hdr_search_ctx = !search_header ? NULL :
+		message_header_search_init(pool, ctx->key, "UTF-8", NULL);
 
-	i_assert(ctx->key_len <= SSIZE_T_MAX/sizeof(size_t));
+	t_pop();
+	return 1;
+}
 
-	return TRUE;
+void message_body_search_deinit(struct message_body_search_context **_ctx)
+{
+	struct message_body_search_context *ctx = *_ctx;
+
+	*_ctx = NULL;
+	message_header_search_free(&ctx->hdr_search_ctx);
+	p_free(ctx->pool, ctx->key);
+	p_free(ctx->pool, ctx->charset);
+	p_free(ctx->pool, ctx);
 }
 
-static int message_body_search_ctx(struct body_search_context *ctx,
-				   struct istream *input,
-				   const struct message_part *part)
+int message_body_search(struct message_body_search_context *ctx,
+			struct istream *input, const struct message_part *part)
 {
 	struct part_search_context part_ctx;
-	int ret;
+	int ret = 0;
 
-	ret = 0;
 	while (part != NULL && ret == 0) {
 		i_assert(input->v_offset <= part->physical_pos);
 
@@ -403,7 +415,7 @@
 		memset(&part_ctx, 0, sizeof(part_ctx));
 		part_ctx.body_ctx = ctx;
 		part_ctx.ignore_header =
-			part->parent == NULL && !ctx->search_header;
+			part->parent == NULL && ctx->hdr_search_ctx == NULL;
 
 		t_push();
 
@@ -412,7 +424,7 @@
 			ret = 1;
 		} else if (part->children != NULL) {
 			/* multipart/xxx or message/rfc822 */
-			if (message_body_search_ctx(ctx, input, part->children))
+			if (message_body_search(ctx, input, part->children))
 				ret = 1;
 		} else {
 			if (input->v_offset != part->physical_pos +
@@ -433,25 +445,3 @@
 
 	return ret;
 }
-
-int message_body_search(const char *key, const char *charset,
-			struct istream *input,
-			const struct message_part *part, bool search_header,
-                        enum message_body_search_error *error_r)
-{
-        struct body_search_context ctx;
-	int ret;
-	bool unknown_charset;
-
-	if (!message_body_search_init(&ctx, key, charset, &unknown_charset,
-				      search_header)) {
-		*error_r = unknown_charset ?
-			MESSAGE_BODY_SEARCH_ERROR_UNKNOWN_CHARSET :
-                        MESSAGE_BODY_SEARCH_ERROR_INVALID_KEY;
-		return -1;
-	}
-
-	if ((ret = message_body_search_ctx(&ctx, input, part)) < 0)
-		*error_r = MESSAGE_BODY_SEARCH_ERROR_MESSAGE_PART_BROKEN;
-	return ret;
-}

Index: message-body-search.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-mail/message-body-search.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- message-body-search.h	13 Jan 2006 20:26:16 -0000	1.7
+++ message-body-search.h	3 Apr 2007 14:51:28 -0000	1.8
@@ -2,22 +2,19 @@
 #define __MESSAGE_BODY_SEARCH_H
 
 struct message_part;
+struct message_body_search_context;
 
-enum message_body_search_error {
-	/* Don't know the given charset. */
-	MESSAGE_BODY_SEARCH_ERROR_UNKNOWN_CHARSET,
-	/* Key contains invalid characters in given charset. */
-	MESSAGE_BODY_SEARCH_ERROR_INVALID_KEY,
-	/* Message_part doesn't match the reality in input stream. */
-	MESSAGE_BODY_SEARCH_ERROR_MESSAGE_PART_BROKEN
-};
+/* Returns 1 if ok, 0 if unknown charset, -1 if key contains invalid characters
+   in given charset. If charset is NULL, the key isn't assumed to be in any
+   specific charset but is compared to message data without any translation. */
+int message_body_search_init(pool_t pool, const char *key, const char *charset,
+			     bool search_header,
+			     struct message_body_search_context **ctx_r);
+void message_body_search_deinit(struct message_body_search_context **ctx);
 
-/* Returns 1 if key is found from input buffer, 0 if not and -1 if error.
-   If charset is NULL, the key isn't assumed to be in any specific charset but
-   is compared to message data without any translation. */
-int message_body_search(const char *key, const char *charset,
-			struct istream *input,
-			const struct message_part *part, bool search_header,
-                        enum message_body_search_error *error_r);
+/* Returns 1 if key is found from input buffer, 0 if not and -1 if message_part
+   is invalid. */
+int message_body_search(struct message_body_search_context *ctx,
+			struct istream *input, const struct message_part *part);
 
 #endif



More information about the dovecot-cvs mailing list