[dovecot-cvs] dovecot/src/lib-mail message-body-search.c, 1.31, 1.32 message-body-search.h, 1.8, 1.9 message-header-search.c, 1.18, 1.19 message-header-search.h, 1.6, 1.7

tss at dovecot.org tss at dovecot.org
Tue Apr 3 18:02:39 EEST 2007


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

Modified Files:
	message-body-search.c message-body-search.h 
	message-header-search.c message-header-search.h 
Log Message:
Search API cleanups



Index: message-body-search.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-mail/message-body-search.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -d -r1.31 -r1.32
--- message-body-search.c	3 Apr 2007 14:51:28 -0000	1.31
+++ message-body-search.c	3 Apr 2007 15:02:36 -0000	1.32
@@ -18,11 +18,10 @@
 	pool_t pool;
 
 	char *key;
-	char *charset;
+	char *key_charset;
 	unsigned int key_len;
 
-	struct header_search_context *hdr_search_ctx;
-	unsigned int unknown_charset:1;
+	struct message_header_search_context *hdr_search_ctx;
 	unsigned int search_header:1;
 };
 
@@ -104,7 +103,7 @@
 				  struct istream *input,
 				  const struct message_part *part)
 {
-	struct header_search_context *hdr_search_ctx =
+	struct message_header_search_context *hdr_search_ctx =
 		ctx->body_ctx->hdr_search_ctx;
 	struct message_header_parser_ctx *hdr_ctx;
 	struct message_header_line *hdr;
@@ -126,8 +125,8 @@
 			continue;
 
 		if (!ctx->ignore_header) {
-			if (message_header_search(hdr->value, hdr->value_len,
-						  hdr_search_ctx)) {
+			if (message_header_search(hdr_search_ctx,
+						  hdr->value, hdr->value_len)) {
 				found = TRUE;
 				break;
 			}
@@ -366,6 +365,7 @@
 	struct message_body_search_context *ctx;
 	bool unknown_charset;
 	size_t key_len;
+	int ret;
 
 	/* get the key uppercased */
 	t_push();
@@ -381,10 +381,12 @@
 	ctx->pool = pool;
 	ctx->key = p_strdup(pool, key);
 	ctx->key_len = key_len;
-	ctx->charset = p_strdup(pool, charset);
-	ctx->unknown_charset = charset == NULL;
-	ctx->hdr_search_ctx = !search_header ? NULL :
-		message_header_search_init(pool, ctx->key, "UTF-8", NULL);
+	ctx->key_charset = p_strdup(pool, charset);
+	if (search_header) {
+		ret = message_header_search_init(pool, ctx->key, "UTF-8",
+						 &ctx->hdr_search_ctx);
+		i_assert(ret > 0); /* the search key is in UTF-8 */
+	}
 
 	t_pop();
 	return 1;
@@ -395,9 +397,9 @@
 	struct message_body_search_context *ctx = *_ctx;
 
 	*_ctx = NULL;
-	message_header_search_free(&ctx->hdr_search_ctx);
+	message_header_search_deinit(&ctx->hdr_search_ctx);
 	p_free(ctx->pool, ctx->key);
-	p_free(ctx->pool, ctx->charset);
+	p_free(ctx->pool, ctx->key_charset);
 	p_free(ctx->pool, ctx);
 }
 

Index: message-body-search.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-mail/message-body-search.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- message-body-search.h	3 Apr 2007 14:51:28 -0000	1.8
+++ message-body-search.h	3 Apr 2007 15:02:36 -0000	1.9
@@ -10,6 +10,7 @@
 int message_body_search_init(pool_t pool, const char *key, const char *charset,
 			     bool search_header,
 			     struct message_body_search_context **ctx_r);
+/* Deinitialize search context. Not needed if you just destroy the pool. */
 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 message_part

Index: message-header-search.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-mail/message-header-search.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- message-header-search.c	28 Jun 2006 13:10:44 -0000	1.18
+++ message-header-search.c	3 Apr 2007 15:02:36 -0000	1.19
@@ -11,7 +11,7 @@
 
 #include <ctype.h>
 
-struct header_search_context {
+struct message_header_search_context {
 	pool_t pool;
 
 	unsigned char *key;
@@ -28,30 +28,30 @@
 };
 
 static void search_loop(const unsigned char *data, size_t size,
-			struct header_search_context *ctx);
+			struct message_header_search_context *ctx);
 
-struct header_search_context *
-message_header_search_init(pool_t pool, const char *key, const char *charset,
-			   bool *unknown_charset)
+int message_header_search_init(pool_t pool, const char *key,
+			       const char *charset,
+			       struct message_header_search_context **ctx_r)
 {
-	struct header_search_context *ctx;
+	struct message_header_search_context *ctx;
 	size_t key_len;
 	const unsigned char *p;
-
-	ctx = p_new(pool, struct header_search_context, 1);
-	ctx->pool = pool;
+	bool unknown_charset;
 
 	/* get the key uppercased */
-	key = charset_to_ucase_utf8_string(charset, unknown_charset,
+	t_push();
+	key = charset_to_ucase_utf8_string(charset, &unknown_charset,
 					   (const unsigned char *) key,
 					   strlen(key), &key_len);
 
 	if (key == NULL) {
-		/* invalid key */
-		p_free(pool, ctx);
-		return NULL;
+		t_pop();
+		return unknown_charset ? 0 : -1;
 	}
 
+	ctx = *ctx_r = p_new(pool, struct message_header_search_context, 1);
+	ctx->pool = pool;
 	ctx->key = (unsigned char *) p_strdup(pool, key);
 	ctx->key_len = key_len;
 	ctx->key_charset = p_strdup(pool, charset);
@@ -68,27 +68,28 @@
 	i_assert(ctx->key_len <= SSIZE_T_MAX/sizeof(size_t));
 	ctx->match_buf = buffer_create_static_hard(pool, sizeof(size_t) *
 						   ctx->key_len);
-	return ctx;
+	t_pop();
+	return 1;
 }
 
-void message_header_search_free(struct header_search_context **_ctx)
+void message_header_search_deinit(struct message_header_search_context **_ctx)
 {
-        struct header_search_context *ctx = *_ctx;
+        struct message_header_search_context *ctx = *_ctx;
 	pool_t pool;
 
+	*_ctx = NULL;
+
 	buffer_free(ctx->match_buf);
 
 	pool = ctx->pool;
 	p_free(pool, ctx->key);
 	p_free(pool, ctx->key_charset);
 	p_free(pool, ctx);
-
-	*_ctx = NULL;
 }
 
 static void search_with_charset(const unsigned char *data, size_t size,
 				const char *charset,
-				struct header_search_context *ctx)
+				struct message_header_search_context *ctx)
 {
 	const void *utf8_data;
 	size_t utf8_size;
@@ -119,7 +120,7 @@
 }
 
 static void search_loop(const unsigned char *data, size_t size,
-			struct header_search_context *ctx)
+			struct message_header_search_context *ctx)
 {
 	size_t pos, *matches, match_count, value;
 	ssize_t i;
@@ -194,7 +195,7 @@
 static bool search_block(const unsigned char *data, size_t size,
 			 const char *charset, void *context)
 {
-	struct header_search_context *ctx = context;
+	struct message_header_search_context *ctx = context;
 
 	t_push();
 	if (charset != NULL) {
@@ -208,15 +209,15 @@
 	return !ctx->found;
 }
 
-bool message_header_search(const unsigned char *header_block, size_t size,
-			   struct header_search_context *ctx)
+bool message_header_search(struct message_header_search_context *ctx,
+			   const unsigned char *header_block, size_t size)
 {
 	if (!ctx->found)
 		message_header_decode(header_block, size, search_block, ctx);
 	return ctx->found;
 }
 
-void message_header_search_reset(struct header_search_context *ctx)
+void message_header_search_reset(struct message_header_search_context *ctx)
 {
 	buffer_set_used_size(ctx->match_buf, 0);
 	ctx->found = FALSE;

Index: message-header-search.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-mail/message-header-search.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- message-header-search.h	14 Jan 2006 18:47:35 -0000	1.6
+++ message-header-search.h	3 Apr 2007 15:02:36 -0000	1.7
@@ -1,24 +1,23 @@
 #ifndef __MESSAGE_HEADER_SEARCH_H
 #define __MESSAGE_HEADER_SEARCH_H
 
-struct header_search_context;
-
-/* Initialize new search. Returns NULL if charset is unknown or key is not
-   valid in specified charset. */
-struct header_search_context *
-message_header_search_init(pool_t pool, const char *key, const char *charset,
-			   bool *unknown_charset);
+struct message_header_search_context;
 
-/* Free search context. Not needed if you just destroy the pool. */
-void message_header_search_free(struct header_search_context **ctx);
+/* Returns 1 if ok, 0 if unknown charset, -1 if key contains invalid characters
+   in given charset. */
+int message_header_search_init(pool_t pool, const char *key,
+			       const char *charset,
+			       struct message_header_search_context **ctx_r);
+/* Deinitialize search context. Not needed if you just destroy the pool. */
+void message_header_search_deinit(struct message_header_search_context **ctx);
 
 /* Returns TRUE if key is found from header. This function may be called
    multiple times with partial header blocks, but the blocks must contain only
    full lines so RFC2047 parsing can be done. */
-bool message_header_search(const unsigned char *header_block, size_t size,
-			   struct header_search_context *ctx);
+bool message_header_search(struct message_header_search_context *ctx,
+			   const unsigned char *header_block, size_t size);
 
 /* Next call to message_header_search() will begin a new header. */
-void message_header_search_reset(struct header_search_context *ctx);
+void message_header_search_reset(struct message_header_search_context *ctx);
 
 #endif



More information about the dovecot-cvs mailing list