ostream-hash.c revision bcb4e51a409d94ae670de96afb8483a4f7855294
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen#include "lib.h"
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen#include "hash-method.h"
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen#include "ostream-private.h"
687d1dee0e92229232aa8be416897b640df67d07Timo Sirainen#include "ostream-hash.h"
65988f5a8abed57e9894fec77105941e046d3490Timo Sirainen
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainenstruct hash_ostream {
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen struct ostream_private ostream;
687d1dee0e92229232aa8be416897b640df67d07Timo Sirainen const struct hash_method *method;
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen void *hash_context;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen};
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainenstatic ssize_t
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Siraineno_stream_hash_sendv(struct ostream_private *stream,
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen const struct const_iovec *iov, unsigned int iov_count)
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen{
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen struct hash_ostream *hstream = (struct hash_ostream *)stream;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen unsigned int i;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen size_t bytes_left, block_len;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen ssize_t ret;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen
64510d2cc23a79d2142030bf5bade44baa490db3Timo Sirainen if ((ret = o_stream_sendv(stream->parent, iov, iov_count)) < 0) {
64510d2cc23a79d2142030bf5bade44baa490db3Timo Sirainen o_stream_copy_error_from_parent(stream);
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen return -1;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen }
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen if (ret > 0) {
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen bytes_left = ret;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen for (i = 0; i < iov_count && bytes_left > 0; i++) {
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen block_len = iov[i].iov_len <= bytes_left ?
5cbefc6537aefbf1491416c433de00fc3e649a13Timo Sirainen iov[i].iov_len : bytes_left;
f335accff54f408a8bbb328f8098ad458f2ff58eTimo Sirainen hstream->method->loop(hstream->hash_context,
f335accff54f408a8bbb328f8098ad458f2ff58eTimo Sirainen iov[i].iov_base, block_len);
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen bytes_left -= block_len;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen }
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen }
2e03303e721a293d796c0287829396f5caea76eaTimo Sirainen
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen stream->ostream.offset += ret;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen return ret;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen}
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen
db24d60c306c9d477392ff5b61cb3fc95fef3bb7Timo Sirainenstruct ostream *
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Siraineno_stream_create_hash(struct ostream *output, const struct hash_method *method,
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen void *hash_context)
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen{
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen struct hash_ostream *hstream;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen
5cbefc6537aefbf1491416c433de00fc3e649a13Timo Sirainen hstream = i_new(struct hash_ostream, 1);
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen hstream->ostream.sendv = o_stream_hash_sendv;
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen hstream->method = method;
5cbefc6537aefbf1491416c433de00fc3e649a13Timo Sirainen hstream->hash_context = hash_context;
5cbefc6537aefbf1491416c433de00fc3e649a13Timo Sirainen
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen return o_stream_create(&hstream->ostream, output,
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen o_stream_get_fd(output));
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen}
050975ee630c761ab237fce7b8f84fe189bb02d2Timo Sirainen