diff options
author | Neal H. Walfield <[email protected]> | 2015-11-05 11:19:45 +0000 |
---|---|---|
committer | Neal H. Walfield <[email protected]> | 2015-11-05 13:09:01 +0000 |
commit | fd4b9e232805b2e30b29903568c95cc0aad8bbec (patch) | |
tree | 83f520893b5146e2b281b8ad9b791dfb9fdfd329 /common/iobuf.c | |
parent | doc: Note that gpgkey2ssh is deprecated. (diff) | |
download | gnupg-fd4b9e232805b2e30b29903568c95cc0aad8bbec.tar.gz gnupg-fd4b9e232805b2e30b29903568c95cc0aad8bbec.zip |
common: Add a function for copying data from one iobuf to another.
* common/iobuf.c (iobuf_copy): New function.
--
Signed-off-by: Neal H. Walfield <[email protected]>
Diffstat (limited to 'common/iobuf.c')
-rw-r--r-- | common/iobuf.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/common/iobuf.c b/common/iobuf.c index 795ff11b1..ca8609e70 100644 --- a/common/iobuf.c +++ b/common/iobuf.c @@ -2208,6 +2208,41 @@ iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen) return n; } +/* Copies the data from the input iobuf SOURCE to the output iobuf + DEST until either an error is encountered or EOF is reached. + Returns the number of bytes copies. */ +size_t +iobuf_copy (iobuf_t dest, iobuf_t source) +{ + char *temp; + /* Use a 1 MB buffer. */ + const size_t temp_size = 1024 * 1024; + + size_t nread; + size_t nwrote = 0; + int err; + + assert (source->use == IOBUF_INPUT || source->use == IOBUF_INPUT_TEMP); + assert (dest->use == IOBUF_OUTPUT || source->use == IOBUF_OUTPUT_TEMP); + + temp = xmalloc (temp_size); + while (1) + { + nread = iobuf_read (source, temp, temp_size); + if (nread == -1) + /* EOF. */ + break; + + err = iobuf_write (dest, temp, nread); + if (err) + break; + nwrote += nread; + } + xfree (temp); + + return nwrote; +} + void iobuf_flush_temp (iobuf_t temp) |