aboutsummaryrefslogtreecommitdiffstats
path: root/modules/fd_unix/fd_unix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/fd_unix/fd_unix.cpp')
-rw-r--r--modules/fd_unix/fd_unix.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/modules/fd_unix/fd_unix.cpp b/modules/fd_unix/fd_unix.cpp
new file mode 100644
index 000000000..8b7b2648a
--- /dev/null
+++ b/modules/fd_unix/fd_unix.cpp
@@ -0,0 +1,50 @@
+/*************************************************
+* Pipe I/O for Unix Source File *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/pipe.h>
+#include <unistd.h>
+
+namespace Botan {
+
+/*************************************************
+* Write data from a pipe into a Unix fd *
+*************************************************/
+int operator<<(int fd, Pipe& pipe)
+ {
+ SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
+ while(pipe.remaining())
+ {
+ u32bit got = pipe.read(buffer, buffer.size());
+ u32bit position = 0;
+ while(got)
+ {
+ ssize_t ret = write(fd, buffer + position, got);
+ if(ret == -1)
+ throw Stream_IO_Error("Pipe output operator (unixfd) has failed");
+ position += ret;
+ got -= ret;
+ }
+ }
+ return fd;
+ }
+
+/*************************************************
+* Read data from a Unix fd into a pipe *
+*************************************************/
+int operator>>(int fd, Pipe& pipe)
+ {
+ SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
+ while(true)
+ {
+ ssize_t ret = read(fd, buffer, buffer.size());
+ if(ret == 0) break;
+ if(ret == -1)
+ throw Stream_IO_Error("Pipe input operator (unixfd) has failed");
+ pipe.write(buffer, ret);
+ }
+ return fd;
+ }
+
+}