dovecot-2.2: Added ostream-hash for calculating a hash from data...

dovecot at dovecot.org dovecot at dovecot.org
Wed Mar 6 11:40:11 EET 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/7a08461c5559
changeset: 16002:7a08461c5559
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Mar 06 11:39:43 2013 +0200
description:
Added ostream-hash for calculating a hash from data going through ostream.

diffstat:

 src/lib/Makefile.am    |   4 ++-
 src/lib/ostream-hash.c |  55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/ostream-hash.h |  10 +++++++++
 3 files changed, 68 insertions(+), 1 deletions(-)

diffs (95 lines):

diff -r 52e5d4186006 -r 7a08461c5559 src/lib/Makefile.am
--- a/src/lib/Makefile.am	Tue Mar 05 21:44:07 2013 +0200
+++ b/src/lib/Makefile.am	Wed Mar 06 11:39:43 2013 +0200
@@ -99,8 +99,9 @@
 	nfs-workarounds.c \
 	numpack.c \
 	ostream.c \
+	ostream-buffer.c \
 	ostream-file.c \
-	ostream-buffer.c \
+	ostream-hash.c \
 	ostream-rawlog.c \
 	primes.c \
 	printf-format-fix.c \
@@ -215,6 +216,7 @@
 	nfs-workarounds.h \
 	numpack.h \
 	ostream.h \
+	ostream-hash.h \
 	ostream-private.h \
 	ostream-rawlog.h \
 	primes.h \
diff -r 52e5d4186006 -r 7a08461c5559 src/lib/ostream-hash.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/ostream-hash.c	Wed Mar 06 11:39:43 2013 +0200
@@ -0,0 +1,55 @@
+/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "hash-method.h"
+#include "ostream-private.h"
+#include "ostream-hash.h"
+
+struct hash_ostream {
+	struct ostream_private ostream;
+	const struct hash_method *method;
+	void *hash_context;
+};
+
+static ssize_t
+o_stream_hash_sendv(struct ostream_private *stream,
+		    const struct const_iovec *iov, unsigned int iov_count)
+{
+	struct hash_ostream *hstream = (struct hash_ostream *)stream;
+	unsigned int i;
+	size_t bytes_left, block_len;
+	ssize_t ret;
+
+	if ((ret = o_stream_sendv(stream->parent, iov, iov_count)) < 0) {
+		o_stream_copy_error_from_parent(stream);
+		return -1;
+	}
+	if (ret > 0) {
+		bytes_left = ret;
+		for (i = 0; i < iov_count && bytes_left > 0; i++) {
+			block_len = iov[i].iov_len <= bytes_left ?
+				iov[i].iov_len : bytes_left;
+			hstream->method->loop(hstream->hash_context,
+					      iov[i].iov_base, block_len);
+			bytes_left -= block_len;
+		}
+	}
+
+	stream->ostream.offset += ret;
+	return ret;
+}
+
+struct ostream *
+o_stream_create_hash(struct ostream *output, const struct hash_method *method,
+		     void *hash_context)
+{
+	struct hash_ostream *hstream;
+
+	hstream = i_new(struct hash_ostream, 1);
+	hstream->ostream.sendv = o_stream_hash_sendv;
+	hstream->method = method;
+	hstream->hash_context = hash_context;
+
+	return o_stream_create(&hstream->ostream, output,
+			       o_stream_get_fd(output));
+}
diff -r 52e5d4186006 -r 7a08461c5559 src/lib/ostream-hash.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/ostream-hash.h	Wed Mar 06 11:39:43 2013 +0200
@@ -0,0 +1,10 @@
+#ifndef OSTREAM_HASH_H
+#define OSTREAM_HASH_H
+
+/* hash_context must be allocated and initialized by caller. This ostream will
+   simply call method->loop() for all the data going through the ostream. */
+struct ostream *
+o_stream_create_hash(struct ostream *output, const struct hash_method *method,
+		     void *hash_context);
+
+#endif


More information about the dovecot-cvs mailing list