[Pkg-gnupg-commit] [gnupg2] 108/241: common: Add a function for copying data from one iobuf to another.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed Dec 9 20:32:02 UTC 2015


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch master
in repository gnupg2.

commit fd4b9e232805b2e30b29903568c95cc0aad8bbec
Author: Neal H. Walfield <neal at g10code.com>
Date:   Thu Nov 5 12:19:45 2015 +0100

    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 <neal at g10code.com>
---
 common/iobuf.c | 35 +++++++++++++++++++++++++++++++++++
 common/iobuf.h |  8 ++++++++
 2 files changed, 43 insertions(+)

diff --git a/common/iobuf.c b/common/iobuf.c
index 795ff11..ca8609e 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)
diff --git a/common/iobuf.h b/common/iobuf.h
index bce6c31..e106292 100644
--- a/common/iobuf.h
+++ b/common/iobuf.h
@@ -552,6 +552,14 @@ int iobuf_write_temp (iobuf_t dest, iobuf_t source);
    BUFFER.  Returns the number of bytes actually copied.  */
 size_t iobuf_temp_to_buffer (iobuf_t a, byte * buffer, size_t buflen);
 
+/* 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 successfully written.  If an error
+   occured, then any buffered bytes are not returned to SOURCE and are
+   effectively lost.  To check if an error occured, use
+   iobuf_error.  */
+size_t iobuf_copy (iobuf_t dest, iobuf_t source);
+
 /* Return the size of any underlying file.  This only works with
    file_filter based pipelines.
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gnupg2.git



More information about the Pkg-gnupg-commit mailing list