Botan  1.11.15
src/lib/filters/fd_unix/fd_unix.cpp
Go to the documentation of this file.
00001 /*
00002 * Pipe I/O for Unix
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/pipe.h>
00009 #include <botan/exceptn.h>
00010 #include <unistd.h>
00011 
00012 namespace Botan {
00013 
00014 /*
00015 * Write data from a pipe into a Unix fd
00016 */
00017 int operator<<(int fd, Pipe& pipe)
00018    {
00019    secure_vector<byte> buffer(DEFAULT_BUFFERSIZE);
00020    while(pipe.remaining())
00021       {
00022       size_t got = pipe.read(&buffer[0], buffer.size());
00023       size_t position = 0;
00024       while(got)
00025          {
00026          ssize_t ret = write(fd, &buffer[position], got);
00027          if(ret == -1)
00028             throw Stream_IO_Error("Pipe output operator (unixfd) has failed");
00029          position += ret;
00030          got -= ret;
00031          }
00032       }
00033    return fd;
00034    }
00035 
00036 /*
00037 * Read data from a Unix fd into a pipe
00038 */
00039 int operator>>(int fd, Pipe& pipe)
00040    {
00041    secure_vector<byte> buffer(DEFAULT_BUFFERSIZE);
00042    while(true)
00043       {
00044       ssize_t ret = read(fd, &buffer[0], buffer.size());
00045       if(ret == 0) break;
00046       if(ret == -1)
00047          throw Stream_IO_Error("Pipe input operator (unixfd) has failed");
00048       pipe.write(&buffer[0], ret);
00049       }
00050    return fd;
00051    }
00052 
00053 }