~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/lib/filters/fd_unix/fd_unix.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Pipe I/O for Unix
 
3
* (C) 1999-2007 Jack Lloyd
 
4
*
 
5
* Botan is released under the Simplified BSD License (see license.txt)
 
6
*/
 
7
 
 
8
#include <botan/pipe.h>
 
9
#include <botan/exceptn.h>
 
10
#include <unistd.h>
 
11
 
 
12
namespace Botan {
 
13
 
 
14
/*
 
15
* Write data from a pipe into a Unix fd
 
16
*/
 
17
int operator<<(int fd, Pipe& pipe)
 
18
   {
 
19
   secure_vector<uint8_t> buffer(DEFAULT_BUFFERSIZE);
 
20
   while(pipe.remaining())
 
21
      {
 
22
      size_t got = pipe.read(buffer.data(), buffer.size());
 
23
      size_t position = 0;
 
24
      while(got)
 
25
         {
 
26
         ssize_t ret = ::write(fd, &buffer[position], got);
 
27
         if(ret < 0)
 
28
            throw Stream_IO_Error("Pipe output operator (unixfd) has failed");
 
29
 
 
30
         position += static_cast<size_t>(ret);
 
31
         got -= static_cast<size_t>(ret);
 
32
         }
 
33
      }
 
34
   return fd;
 
35
   }
 
36
 
 
37
/*
 
38
* Read data from a Unix fd into a pipe
 
39
*/
 
40
int operator>>(int fd, Pipe& pipe)
 
41
   {
 
42
   secure_vector<uint8_t> buffer(DEFAULT_BUFFERSIZE);
 
43
   while(true)
 
44
      {
 
45
      ssize_t ret = ::read(fd, buffer.data(), buffer.size());
 
46
      if(ret < 0)
 
47
         throw Stream_IO_Error("Pipe input operator (unixfd) has failed");
 
48
      else if(ret == 0)
 
49
         break;
 
50
      pipe.write(buffer.data(), static_cast<size_t>(ret));
 
51
      }
 
52
   return fd;
 
53
   }
 
54
 
 
55
}