dovecot-2.2: var_expand(): Added %N, which is the same as %H exc...

dovecot at dovecot.org dovecot at dovecot.org
Thu May 30 17:45:40 EEST 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/4d711ec26847
changeset: 16418:4d711ec26847
user:      Timo Sirainen <tss at iki.fi>
date:      Thu May 30 17:45:27 2013 +0300
description:
var_expand(): Added %N, which is the same as %H except based on MD5.
This gives a better distribution of values than %H.

diffstat:

 src/lib/var-expand.c |  24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

diffs (41 lines):

diff -r 714320c3cfa6 -r 4d711ec26847 src/lib/var-expand.c
--- a/src/lib/var-expand.c	Wed May 29 16:40:50 2013 +0300
+++ b/src/lib/var-expand.c	Thu May 30 17:45:27 2013 +0300
@@ -88,6 +88,29 @@
 }
 
 static const char *
+m_str_newhash(const char *str, struct var_expand_context *ctx)
+{
+	string_t *hash = t_str_new(20);
+	unsigned char result[MD5_RESULTLEN];
+	unsigned int value;
+
+	md5_get_digest(str, strlen(str), result);
+	memcpy(&value, result, sizeof(value));
+
+	if (ctx->width != 0) {
+		value %= ctx->width;
+		ctx->width = 0;
+	}
+
+	str_printfa(hash, "%x", value);
+	while ((int)str_len(hash) < ctx->offset)
+		str_insert(hash, 0, "0");
+        ctx->offset = 0;
+
+	return str_c(hash);
+}
+
+static const char *
 m_str_md5(const char *str, struct var_expand_context *ctx ATTR_UNUSED)
 {
 	unsigned char digest[16];
@@ -132,6 +155,7 @@
 	{ 'X', m_str_hex },
 	{ 'R', m_str_reverse },
 	{ 'H', m_str_hash },
+	{ 'N', m_str_newhash },
 	{ 'M', m_str_md5 },
 	{ 'D', m_str_ldap_dn },
 	{ 'T', m_str_trim },


More information about the dovecot-cvs mailing list