Botan  1.10.9
Public Member Functions | List of all members
Botan::DataSource_Command Class Reference

#include <unix_cmd.h>

Inheritance diagram for Botan::DataSource_Command:
Botan::DataSource

Public Member Functions

 DataSource_Command (const std::string &, const std::vector< std::string > &paths)
 
size_t discard_next (size_t N)
 
bool end_of_data () const
 
int fd () const
 
std::string id () const
 
size_t peek (byte[], size_t, size_t) const
 
size_t peek_byte (byte &out) const
 
size_t read (byte[], size_t)
 
size_t read_byte (byte &out)
 
 ~DataSource_Command ()
 

Detailed Description

Command Output DataSource

Definition at line 49 of file unix_cmd.h.

Constructor & Destructor Documentation

Botan::DataSource_Command::DataSource_Command ( const std::string &  prog_and_args,
const std::vector< std::string > &  paths 
)

DataSource_Command Constructor

Definition at line 215 of file unix_cmd.cpp.

References Botan::split_on().

216  :
217  MAX_BLOCK_USECS(100000), KILL_WAIT(10000)
218  {
219  arg_list = split_on(prog_and_args, ' ');
220 
221  if(arg_list.size() == 0)
222  throw Invalid_Argument("DataSource_Command: No command given");
223  if(arg_list.size() > 5)
224  throw Invalid_Argument("DataSource_Command: Too many args");
225 
226  pipe = 0;
227  create_pipe(paths);
228  }
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:152
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::DataSource_Command::~DataSource_Command ( )

DataSource_Command Destructor

Definition at line 233 of file unix_cmd.cpp.

References end_of_data().

234  {
235  if(!end_of_data())
236  shutdown_pipe();
237  }
bool end_of_data() const
Definition: unix_cmd.cpp:105

Member Function Documentation

size_t Botan::DataSource::discard_next ( size_t  N)
inherited

Discard the next N bytes of the data

Parameters
Nthe number of bytes to discard
Returns
number of bytes actually discarded

Definition at line 35 of file data_src.cpp.

References n, and Botan::DataSource::read_byte().

36  {
37  size_t discarded = 0;
38  byte dummy;
39  for(size_t j = 0; j != n; ++j)
40  discarded += read_byte(dummy);
41  return discarded;
42  }
BigInt n
Definition: numthry.cpp:26
unsigned char byte
Definition: types.h:22
size_t read_byte(byte &out)
Definition: data_src.cpp:19
bool Botan::DataSource_Command::end_of_data ( ) const
virtual

Check if we reached EOF

Implements Botan::DataSource.

Definition at line 105 of file unix_cmd.cpp.

Referenced by peek(), Botan::Unix_EntropySource::poll(), read(), and ~DataSource_Command().

106  {
107  return (pipe) ? false : true;
108  }
int Botan::DataSource_Command::fd ( ) const

Return the Unix file descriptor of the pipe

Definition at line 113 of file unix_cmd.cpp.

114  {
115  if(!pipe)
116  return -1;
117  return pipe->fd;
118  }
std::string Botan::DataSource_Command::id ( ) const
virtual

Return a human-readable ID for this stream

Reimplemented from Botan::DataSource.

Definition at line 123 of file unix_cmd.cpp.

124  {
125  return "Unix command: " + arg_list[0];
126  }
size_t Botan::DataSource_Command::peek ( byte  [],
size_t  ,
size_t   
) const
virtual

Peek at the pipe contents

Implements Botan::DataSource.

Definition at line 95 of file unix_cmd.cpp.

References end_of_data().

96  {
97  if(end_of_data())
98  throw Invalid_State("DataSource_Command: Cannot peek when out of data");
99  throw Stream_IO_Error("Cannot peek/seek on a command pipe");
100  }
bool end_of_data() const
Definition: unix_cmd.cpp:105
size_t Botan::DataSource::peek_byte ( byte &  out) const
inherited

Peek at one byte.

Parameters
outan output byte
Returns
length in bytes that was actually read and put into out

Definition at line 27 of file data_src.cpp.

References Botan::DataSource::peek().

Referenced by Botan::ASN1::maybe_BER().

28  {
29  return peek(&out, 1, 0);
30  }
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const =0
size_t Botan::DataSource_Command::read ( byte  buf[],
size_t  length 
)
virtual

Read from the pipe

Implements Botan::DataSource.

Definition at line 63 of file unix_cmd.cpp.

References end_of_data().

Referenced by Botan::Unix_EntropySource::poll().

64  {
65  if(end_of_data())
66  return 0;
67 
68  fd_set set;
69  FD_ZERO(&set);
70  FD_SET(pipe->fd, &set);
71 
72  struct ::timeval tv;
73  tv.tv_sec = 0;
74  tv.tv_usec = MAX_BLOCK_USECS;
75 
76  ssize_t got = 0;
77  if(::select(pipe->fd + 1, &set, 0, 0, &tv) == 1)
78  {
79  if(FD_ISSET(pipe->fd, &set))
80  got = ::read(pipe->fd, buf, length);
81  }
82 
83  if(got <= 0)
84  {
85  shutdown_pipe();
86  return 0;
87  }
88 
89  return static_cast<size_t>(got);
90  }
size_t read(byte[], size_t)
Definition: unix_cmd.cpp:63
bool end_of_data() const
Definition: unix_cmd.cpp:105
size_t Botan::DataSource::read_byte ( byte &  out)
inherited

Read one byte.

Parameters
outthe byte to read to
Returns
length in bytes that was actually read and put into out

Definition at line 19 of file data_src.cpp.

References Botan::DataSource::read().

Referenced by Botan::PEM_Code::decode(), Botan::DataSource::discard_next(), Botan::BER_Decoder::discard_remaining(), Botan::PGP_decode(), and Botan::BER_Decoder::raw_bytes().

20  {
21  return read(&out, 1);
22  }
virtual size_t read(byte out[], size_t length)=0

The documentation for this class was generated from the following files: