Botan 1.10.17
fd_unix.cpp
Go to the documentation of this file.
1/*
2* Pipe I/O for Unix
3* (C) 1999-2007 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/pipe.h>
9#include <botan/exceptn.h>
10#include <unistd.h>
11
12namespace Botan {
13
14/*
15* Write data from a pipe into a Unix fd
16*/
17int operator<<(int fd, Pipe& pipe)
18 {
19 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
20 while(pipe.remaining())
21 {
22 size_t got = pipe.read(&buffer[0], buffer.size());
23 size_t position = 0;
24 while(got)
25 {
26 ssize_t ret = write(fd, &buffer[position], got);
27 if(ret == -1)
28 throw Stream_IO_Error("Pipe output operator (unixfd) has failed");
29 position += ret;
30 got -= ret;
31 }
32 }
33 return fd;
34 }
35
36/*
37* Read data from a Unix fd into a pipe
38*/
39int operator>>(int fd, Pipe& pipe)
40 {
41 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
42 while(true)
43 {
44 ssize_t ret = read(fd, &buffer[0], buffer.size());
45 if(ret == 0) break;
46 if(ret == -1)
47 throw Stream_IO_Error("Pipe input operator (unixfd) has failed");
48 pipe.write(&buffer[0], ret);
49 }
50 return fd;
51 }
52
53}
size_t size() const
Definition secmem.h:29
void write(const byte in[], size_t length)
Definition pipe_rw.cpp:34
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition pipe_rw.cpp:138
size_t read(byte output[], size_t length)
Definition pipe_rw.cpp:89
int operator>>(int fd, Pipe &pipe)
Definition fd_unix.cpp:39
int operator<<(int fd, Pipe &pipe)
Definition fd_unix.cpp:17
Stream_IO_Error(const std::string &err)
Definition exceptn.h:167