aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/utils/chunk.c
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2013-10-22 14:22:35 +0200
committerTobias Brunner <tobias@strongswan.org>2013-10-23 17:20:39 +0200
commit46cded2627e8796c58a5d494518a6c2ee78fe5ef (patch)
tree692c8a3a581611153941e1f1e1cb57b5bb58e353 /src/libstrongswan/utils/chunk.c
parentb08292a52064c37bc68f1bff05ba7ebf5db5ac0c (diff)
downloadstrongswan-46cded2627e8796c58a5d494518a6c2ee78fe5ef.tar.bz2
strongswan-46cded2627e8796c58a5d494518a6c2ee78fe5ef.tar.xz
chunk: Add helper function to create a chunk from data read from a file descriptor
Diffstat (limited to 'src/libstrongswan/utils/chunk.c')
-rw-r--r--src/libstrongswan/utils/chunk.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/libstrongswan/utils/chunk.c b/src/libstrongswan/utils/chunk.c
index f5b3ac675..644b8060f 100644
--- a/src/libstrongswan/utils/chunk.c
+++ b/src/libstrongswan/utils/chunk.c
@@ -243,6 +243,38 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force
return good;
}
+/**
+ * Described in header.
+ */
+chunk_t chunk_from_fd(int fd)
+{
+ char buf[8096];
+ char *pos = buf;
+ ssize_t len, total = 0;
+
+ while (TRUE)
+ {
+ len = read(fd, pos, buf + sizeof(buf) - pos);
+ if (len < 0)
+ {
+ DBG1(DBG_LIB, "reading from file descriptor failed: %s",
+ strerror(errno));
+ return chunk_empty;
+ }
+ if (len == 0)
+ {
+ break;
+ }
+ total += len;
+ if (total == sizeof(buf))
+ {
+ DBG1(DBG_LIB, "buffer too small to read from file descriptor");
+ return chunk_empty;
+ }
+ }
+ return chunk_clone(chunk_create(buf, total));
+}
+
/** hex conversion digits */
static char hexdig_upper[] = "0123456789ABCDEF";