Botan 1.10.17
Botan::DataSource_Command Class Reference

#include <unix_cmd.h>

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

Public Member Functions

bool check_available (size_t n)
 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

◆ DataSource_Command()

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

DataSource_Command Constructor

Definition at line 220 of file unix_cmd.cpp.

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

References Botan::split_on().

◆ ~DataSource_Command()

Botan::DataSource_Command::~DataSource_Command ( )

DataSource_Command Destructor

Definition at line 238 of file unix_cmd.cpp.

239 {
240 if(!end_of_data())
241 shutdown_pipe();
242 }

References end_of_data().

Member Function Documentation

◆ check_available()

bool Botan::DataSource_Command::check_available ( size_t n)
virtual

Implements Botan::DataSource.

Definition at line 102 of file unix_cmd.cpp.

103 {
104 throw Stream_IO_Error("Cannot check available bytes on a pipe");
105 }
Stream_IO_Error(const std::string &err)
Definition exceptn.h:167

References Botan::Stream_IO_Error::Stream_IO_Error().

◆ discard_next()

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.

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 }
size_t read_byte(byte &out)
Definition data_src.cpp:19

References read_byte().

Referenced by check_available().

◆ end_of_data()

bool Botan::DataSource_Command::end_of_data ( ) const
virtual

Check if we reached EOF

Implements Botan::DataSource.

Definition at line 110 of file unix_cmd.cpp.

111 {
112 return (pipe) ? false : true;
113 }

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

◆ fd()

int Botan::DataSource_Command::fd ( ) const

Return the Unix file descriptor of the pipe

Definition at line 118 of file unix_cmd.cpp.

119 {
120 if(!pipe)
121 return -1;
122 return pipe->fd;
123 }

◆ id()

std::string Botan::DataSource_Command::id ( ) const
virtual

Return a human-readable ID for this stream

Reimplemented from Botan::DataSource.

Definition at line 128 of file unix_cmd.cpp.

129 {
130 return "Unix command: " + arg_list[0];
131 }

◆ peek()

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.

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 }
Invalid_State(const std::string &err)
Definition exceptn.h:27

References end_of_data(), Botan::Invalid_State::Invalid_State(), and Botan::Stream_IO_Error::Stream_IO_Error().

◆ peek_byte()

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.

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

References peek().

Referenced by check_available(), and Botan::ASN1::maybe_BER().

◆ read()

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.

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

References end_of_data(), and read().

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

◆ read_byte()

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.

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

References read().

Referenced by check_available(), Botan::PEM_Code::decode(), discard_next(), and Botan::PGP_decode().


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