dovecot-2.2: hash-format object can now be reset to build multip...

dovecot at dovecot.org dovecot at dovecot.org
Thu Jun 28 03:20:55 EEST 2012


details:   http://hg.dovecot.org/dovecot-2.2/rev/d419c6cde162
changeset: 14695:d419c6cde162
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Jun 28 03:20:34 2012 +0300
description:
hash-format object can now be reset to build multiple hashes.

diffstat:

 src/lib/hash-format.c |  32 ++++++++++++++++++++++++--------
 src/lib/hash-format.h |   4 ++++
 2 files changed, 28 insertions(+), 8 deletions(-)

diffs (93 lines):

diff -r 8909e8428b82 -r d419c6cde162 src/lib/hash-format.c
--- a/src/lib/hash-format.c	Thu Jun 28 03:18:55 2012 +0300
+++ b/src/lib/hash-format.c	Thu Jun 28 03:20:34 2012 +0300
@@ -27,6 +27,7 @@
 	const char *str;
 
 	struct hash_format_list *list, **pos;
+	unsigned char *digest;
 };
 
 static int
@@ -148,6 +149,16 @@
 		list->method->loop(list->context, data, size);
 }
 
+void hash_format_reset(struct hash_format *format)
+{
+	struct hash_format_list *list;
+
+	for (list = format->list; list != NULL; list = list->next) {
+		memset(list->context, 0, list->method->context_size);
+		list->method->init(list->context);
+	}
+}
+
 static void
 hash_format_digest(string_t *dest, const struct hash_format_list *list,
 		   const unsigned char *digest)
@@ -182,21 +193,18 @@
 	}
 }
 
-void hash_format_deinit(struct hash_format **_format, string_t *dest)
+void hash_format_write(struct hash_format *format, string_t *dest)
 {
-	struct hash_format *format = *_format;
 	struct hash_format_list *list;
 	const char *p;
-	unsigned char *digest;
 	unsigned int i, max_digest_size = 0;
 
-	*_format = NULL;
-
 	for (list = format->list; list != NULL; list = list->next) {
 		if (max_digest_size < list->method->digest_size)
 			max_digest_size = list->method->digest_size;
 	}
-	digest = p_malloc(format->pool, max_digest_size);
+	if (format->digest == NULL)
+		format->digest = p_malloc(format->pool, max_digest_size);
 
 	list = format->list;
 	for (i = 0; format->str[i] != '\0'; i++) {
@@ -207,15 +215,23 @@
 
 		/* we already verified that the string is ok */
 		i_assert(list != NULL);
-		list->method->result(list->context, digest);
-		hash_format_digest(dest, list, digest);
+		list->method->result(list->context, format->digest);
+		hash_format_digest(dest, list, format->digest);
 		list = list->next;
 
 		p = strchr(format->str+i, '}');
 		i_assert(p != NULL);
 		i = p - format->str;
 	}
+}
 
+void hash_format_deinit(struct hash_format **_format, string_t *dest)
+{
+	struct hash_format *format = *_format;
+
+	*_format = NULL;
+
+	hash_format_write(format, dest);
 	pool_unref(&format->pool);
 }
 
diff -r 8909e8428b82 -r d419c6cde162 src/lib/hash-format.h
--- a/src/lib/hash-format.h	Thu Jun 28 03:18:55 2012 +0300
+++ b/src/lib/hash-format.h	Thu Jun 28 03:20:34 2012 +0300
@@ -11,6 +11,10 @@
 /* Add more data to hash. */
 void hash_format_loop(struct hash_format *format,
 		      const void *data, size_t size);
+/* Finish the hash and write it into given string. */
+void hash_format_write(struct hash_format *format, string_t *dest);
+/* Reset hash to initial state. */
+void hash_format_reset(struct hash_format *format);
 /* Write the hash into given string and free used memory. */
 void hash_format_deinit(struct hash_format **format, string_t *dest);
 /* Free used memory without writing to string. */


More information about the dovecot-cvs mailing list