Botan 3.0.0-alpha0
Crypto and TLS for C&
Classes | Public Types | Public Member Functions | Static Public Attributes | List of all members
Botan::Pipe Class Referencefinal

#include <pipe.h>

Inheritance diagram for Botan::Pipe:
Botan::DataSource

Classes

class  Invalid_Message_Number
 

Public Types

typedef size_t message_id
 

Public Member Functions

void append (Filter *filt)
 
void append_filter (Filter *filt)
 
bool check_available (size_t n) override
 
bool check_available_msg (size_t n, message_id msg) const
 
size_t default_msg () const
 
size_t discard_next (size_t N)
 
void end_msg ()
 
bool end_of_data () const override
 
size_t get_bytes_read () const override
 
size_t get_bytes_read (message_id msg) const
 
virtual std::string id () const
 
message_id message_count () const
 
Pipeoperator= (const Pipe &)=delete
 
size_t peek (uint8_t &output, size_t offset, message_id msg=DEFAULT_MESSAGE) const
 
size_t peek (uint8_t output[], size_t length, size_t offset) const override
 
size_t peek (uint8_t output[], size_t length, size_t offset, message_id msg) const
 
size_t peek_byte (uint8_t &out) const
 
 Pipe (const Pipe &)=delete
 
 Pipe (Filter *=nullptr, Filter *=nullptr, Filter *=nullptr, Filter *=nullptr)
 
 Pipe (std::initializer_list< Filter * > filters)
 
void pop ()
 
void prepend (Filter *filt)
 
void prepend_filter (Filter *filt)
 
void process_msg (const secure_vector< uint8_t > &in)
 
void process_msg (const std::string &in)
 
void process_msg (const std::vector< uint8_t > &in)
 
void process_msg (const uint8_t in[], size_t length)
 
void process_msg (DataSource &in)
 
size_t read (uint8_t &output, message_id msg=DEFAULT_MESSAGE)
 
size_t read (uint8_t output[], size_t length) override
 
size_t read (uint8_t output[], size_t length, message_id msg)
 
secure_vector< uint8_t > read_all (message_id msg=DEFAULT_MESSAGE)
 
std::string read_all_as_string (message_id msg=DEFAULT_MESSAGE)
 
size_t read_byte (uint8_t &out)
 
size_t remaining (message_id msg=DEFAULT_MESSAGE) const
 
void reset ()
 
void set_default_msg (message_id msg)
 
void start_msg ()
 
void write (const secure_vector< uint8_t > &in)
 
void write (const std::string &in)
 
void write (const std::vector< uint8_t > &in)
 
void write (const uint8_t in[], size_t length)
 
void write (DataSource &in)
 
void write (uint8_t in)
 
 ~Pipe ()
 

Static Public Attributes

static const message_id DEFAULT_MESSAGE
 
static const message_id LAST_MESSAGE
 

Detailed Description

This class represents pipe objects. A set of filters can be placed into a pipe, and information flows through the pipe until it reaches the end, where the output is collected for retrieval. If you're familiar with the Unix shell environment, this design will sound quite familiar.

Definition at line 29 of file pipe.h.

Member Typedef Documentation

◆ message_id

typedef size_t Botan::Pipe::message_id

An opaque type that identifies a message in this Pipe

Definition at line 35 of file pipe.h.

Constructor & Destructor Documentation

◆ Pipe() [1/3]

Botan::Pipe::Pipe ( Filter f1 = nullptr,
Filter f2 = nullptr,
Filter f3 = nullptr,
Filter f4 = nullptr 
)

Construct a Pipe of up to four filters. The filters are set up in the same order as the arguments.

Definition at line 33 of file pipe.cpp.

33 :
34 Pipe({f1,f2,f3,f4})
35 {
36 }
Pipe(Filter *=nullptr, Filter *=nullptr, Filter *=nullptr, Filter *=nullptr)
Definition: pipe.cpp:33

◆ Pipe() [2/3]

Botan::Pipe::Pipe ( std::initializer_list< Filter * >  filters)
explicit

Construct a Pipe from a list of filters

Parameters
filtersthe set of filters to use

Definition at line 41 of file pipe.cpp.

42 {
43 m_outputs.reset(new Output_Buffers);
44 m_pipe = nullptr;
45 m_default_read = 0;
46 m_inside_msg = false;
47
48 for(auto arg : args)
49 do_append(arg);
50 }

◆ Pipe() [3/3]

Botan::Pipe::Pipe ( const Pipe )
delete

◆ ~Pipe()

Botan::Pipe::~Pipe ( )

Definition at line 55 of file pipe.cpp.

56 {
57 destruct(m_pipe);
58 }

Member Function Documentation

◆ append()

void Botan::Pipe::append ( Filter filt)

Insert a new filter at the back of the pipe Deprecated because runtime modification of Pipes is deprecated. You can instead use append_filter which only works before the first message is processed.

Parameters
filtthe new filter to insert

Definition at line 204 of file pipe.cpp.

205 {
206 do_append(filter);
207 }

◆ append_filter()

void Botan::Pipe::append_filter ( Filter filt)

Append a new filter onto the filter sequence. This may only be called immediately after initial construction, before any calls to start_msg have been made.

This function (unlike append) is not deprecated, as it allows only modification of the pipe at initialization (before use) rather than after messages have been processed.

Definition at line 209 of file pipe.cpp.

210 {
211 if(m_outputs->message_count() != 0)
212 throw Invalid_State("Cannot call Pipe::append_filter after start_msg");
213
214 do_append(filter);
215 }

◆ check_available()

bool Botan::Pipe::check_available ( size_t  n)
overridevirtual

Implements Botan::DataSource.

Definition at line 171 of file pipe_rw.cpp.

172 {
173 return (n <= remaining(default_msg()));
174 }
size_t default_msg() const
Definition: pipe.h:239
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition: pipe_rw.cpp:131

References default_msg(), and remaining().

◆ check_available_msg()

bool Botan::Pipe::check_available_msg ( size_t  n,
message_id  msg 
) const

Definition at line 176 of file pipe_rw.cpp.

177 {
178 return (n <= remaining(msg));
179 }

References remaining().

◆ default_msg()

size_t Botan::Pipe::default_msg ( ) const
inline
Returns
currently set default message

Definition at line 239 of file pipe.h.

239{ return m_default_read; }

Referenced by check_available(), get_bytes_read(), read_all(), and read_all_as_string().

◆ 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 39 of file data_src.cpp.

40 {
41 uint8_t buf[64] = { 0 };
42 size_t discarded = 0;
43
44 while(n)
45 {
46 const size_t got = this->read(buf, std::min(n, sizeof(buf)));
47 discarded += got;
48 n -= got;
49
50 if(got == 0)
51 break;
52 }
53
54 return discarded;
55 }
virtual size_t read(uint8_t out[], size_t length)=0

References Botan::DataSource::read().

◆ end_msg()

void Botan::Pipe::end_msg ( )

End the current message.

Definition at line 158 of file pipe.cpp.

159 {
160 if(!m_inside_msg)
161 throw Invalid_State("Pipe::end_msg: Message was already ended");
162 m_pipe->finish_msg();
163 clear_endpoints(m_pipe);
164 if(dynamic_cast<Null_Filter*>(m_pipe))
165 {
166 delete m_pipe;
167 m_pipe = nullptr;
168 }
169 m_inside_msg = false;
170
171 m_outputs->retire();
172 }

Referenced by process_msg().

◆ end_of_data()

bool Botan::Pipe::end_of_data ( ) const
overridevirtual

Test whether this pipe has any data that can be read from.

Returns
true if there is more data to read, false otherwise

Implements Botan::DataSource.

Definition at line 85 of file pipe.cpp.

86 {
87 return (remaining() == 0);
88 }

References remaining().

◆ get_bytes_read() [1/2]

size_t Botan::Pipe::get_bytes_read ( ) const
overridevirtual
Returns
the number of bytes read from the default message.

Implements Botan::DataSource.

Definition at line 161 of file pipe_rw.cpp.

162 {
163 return m_outputs->get_bytes_read(default_msg());
164 }

References default_msg().

◆ get_bytes_read() [2/2]

size_t Botan::Pipe::get_bytes_read ( message_id  msg) const
Returns
the number of bytes read from the specified message.

Definition at line 166 of file pipe_rw.cpp.

167 {
168 return m_outputs->get_bytes_read(msg);
169 }

◆ id()

virtual std::string Botan::DataSource::id ( ) const
inlinevirtualinherited

return the id of this data source

Returns
std::string representing the id of this data source

Reimplemented in Botan::DataSource_Stream.

Definition at line 59 of file data_src.h.

59{ return ""; }

◆ message_count()

Pipe::message_id Botan::Pipe::message_count ( ) const

Get the number of messages the are in this pipe.

Returns
number of messages the are in this pipe

Definition at line 297 of file pipe.cpp.

298 {
299 return m_outputs->message_count();
300 }

Referenced by set_default_msg().

◆ operator=()

Pipe & Botan::Pipe::operator= ( const Pipe )
delete

◆ peek() [1/3]

size_t Botan::Pipe::peek ( uint8_t &  output,
size_t  offset,
message_id  msg = DEFAULT_MESSAGE 
) const

Read a single byte from the specified message but do not modify the internal offset. Consecutive calls to peek() will return portions of the message starting at the same position.

Parameters
outputthe byte to write the peeked message byte to
offsetthe offset from the current position in message
msgthe number identifying the message to peek from
Returns
number of bytes actually peeked and written into output

Definition at line 156 of file pipe_rw.cpp.

157 {
158 return peek(&out, 1, offset, msg);
159 }
size_t peek(uint8_t output[], size_t length, size_t offset) const override
Definition: pipe_rw.cpp:148

References peek().

◆ peek() [2/3]

size_t Botan::Pipe::peek ( uint8_t  output[],
size_t  length,
size_t  offset 
) const
overridevirtual

Read from the default message but do not modify the internal offset. Consecutive calls to peek() will return portions of the message starting at the same position.

Parameters
outputthe byte array to write the peeked message part to
lengththe length of the byte array output
offsetthe offset from the current position in message
Returns
number of bytes actually peeked and written into output

Implements Botan::DataSource.

Definition at line 148 of file pipe_rw.cpp.

149 {
150 return peek(output, length, offset, DEFAULT_MESSAGE);
151 }
static const message_id DEFAULT_MESSAGE
Definition: pipe.h:62

References DEFAULT_MESSAGE, and peek().

Referenced by peek().

◆ peek() [3/3]

size_t Botan::Pipe::peek ( uint8_t  output[],
size_t  length,
size_t  offset,
message_id  msg 
) const

Read from the specified message but do not modify the internal offset. Consecutive calls to peek() will return portions of the message starting at the same position.

Parameters
outputthe byte array to write the peeked message part to
lengththe length of the byte array output
offsetthe offset from the current position in message
msgthe number identifying the message to peek from
Returns
number of bytes actually peeked and written into output

Definition at line 139 of file pipe_rw.cpp.

141 {
142 return m_outputs->peek(output, length, offset, get_message_no("peek", msg));
143 }

◆ peek_byte()

size_t Botan::DataSource::peek_byte ( uint8_t &  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 31 of file data_src.cpp.

32 {
33 return peek(&out, 1, 0);
34 }
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0

References Botan::DataSource::peek().

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

◆ pop()

void Botan::Pipe::pop ( )

Remove the first filter at the front of the pipe.

Definition at line 274 of file pipe.cpp.

275 {
276 if(m_inside_msg)
277 throw Invalid_State("Cannot pop off a Pipe while it is processing");
278
279 if(!m_pipe)
280 return;
281
282 if(m_pipe->total_ports() > 1)
283 throw Invalid_State("Cannot pop off a Filter with multiple ports");
284
285 size_t to_remove = m_pipe->owns() + 1;
286
287 while(to_remove--)
288 {
289 std::unique_ptr<Filter> to_destroy(m_pipe);
290 m_pipe = m_pipe->m_next[0];
291 }
292 }

◆ prepend()

void Botan::Pipe::prepend ( Filter filt)

Insert a new filter at the front of the pipe Deprecated because runtime modification of Pipes is deprecated. You can instead use prepend_filter which only works before the first message is processed.

Parameters
filtthe new filter to insert

Definition at line 217 of file pipe.cpp.

218 {
219 do_prepend(filter);
220 }

◆ prepend_filter()

void Botan::Pipe::prepend_filter ( Filter filt)

Prepend a new filter onto the filter sequence. This may only be called immediately after initial construction, before any calls to start_msg have been made.

This function (unlike prepend) is not deprecated, as it allows only modification of the pipe at initialization (before use) rather than after messages have been processed.

Definition at line 222 of file pipe.cpp.

223 {
224 if(m_outputs->message_count() != 0)
225 throw Invalid_State("Cannot call Pipe::prepend_filter after start_msg");
226
227 do_prepend(filter);
228 }

◆ process_msg() [1/5]

void Botan::Pipe::process_msg ( const secure_vector< uint8_t > &  in)

Perform start_msg(), write() and end_msg() sequentially.

Parameters
inthe secure_vector containing the data to write

Definition at line 113 of file pipe.cpp.

114 {
115 process_msg(input.data(), input.size());
116 }
void process_msg(const uint8_t in[], size_t length)
Definition: pipe.cpp:103

References process_msg().

◆ process_msg() [2/5]

void Botan::Pipe::process_msg ( const std::string &  in)

Perform start_msg(), write() and end_msg() sequentially.

Parameters
inthe string containing the data to write

Definition at line 126 of file pipe.cpp.

127 {
128 process_msg(cast_char_ptr_to_uint8(input.data()), input.length());
129 }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:183

References Botan::cast_char_ptr_to_uint8(), and process_msg().

◆ process_msg() [3/5]

void Botan::Pipe::process_msg ( const std::vector< uint8_t > &  in)

Perform start_msg(), write() and end_msg() sequentially.

Parameters
inthe secure_vector containing the data to write

Definition at line 118 of file pipe.cpp.

119 {
120 process_msg(input.data(), input.size());
121 }

References process_msg().

Referenced by process_msg().

◆ process_msg() [4/5]

void Botan::Pipe::process_msg ( const uint8_t  in[],
size_t  length 
)

Perform start_msg(), write() and end_msg() sequentially.

Parameters
inthe byte array containing the data to write
lengththe length of the byte array to write

Definition at line 103 of file pipe.cpp.

104 {
105 start_msg();
106 write(input, length);
107 end_msg();
108 }
void end_msg()
Definition: pipe.cpp:158
void write(const uint8_t in[], size_t length)
Definition: pipe_rw.cpp:35
void start_msg()
Definition: pipe.cpp:144

References end_msg(), start_msg(), and write().

Referenced by process_msg().

◆ process_msg() [5/5]

void Botan::Pipe::process_msg ( DataSource in)

Perform start_msg(), write() and end_msg() sequentially.

Parameters
inthe DataSource providing the data to write

Definition at line 134 of file pipe.cpp.

135 {
136 start_msg();
137 write(input);
138 end_msg();
139 }

References end_msg(), start_msg(), and write().

◆ read() [1/3]

size_t Botan::Pipe::read ( uint8_t &  output,
message_id  msg = DEFAULT_MESSAGE 
)

Read a single byte from the pipe. Moves the internal offset so that every call to read will return a new portion of the message.

Parameters
outputthe byte to write the result to
msgthe message to read from
Returns
number of bytes actually read into output

Definition at line 90 of file pipe_rw.cpp.

91 {
92 return read(&out, 1, msg);
93 }
size_t read(uint8_t output[], size_t length) override
Definition: pipe_rw.cpp:82

References read().

◆ read() [2/3]

size_t Botan::Pipe::read ( uint8_t  output[],
size_t  length 
)
overridevirtual

Read the default message from the pipe. Moves the internal offset so that every call to read will return a new portion of the message.

Parameters
outputthe byte array to write the read bytes to
lengththe length of the byte array output
Returns
number of bytes actually read into output

Implements Botan::DataSource.

Definition at line 82 of file pipe_rw.cpp.

83 {
84 return read(output, length, DEFAULT_MESSAGE);
85 }

References DEFAULT_MESSAGE, and read().

Referenced by Botan::operator<<(), read(), read_all(), and read_all_as_string().

◆ read() [3/3]

size_t Botan::Pipe::read ( uint8_t  output[],
size_t  length,
message_id  msg 
)

Read a specified message from the pipe. Moves the internal offset so that every call to read will return a new portion of the message.

Parameters
outputthe byte array to write the read bytes to
lengththe length of the byte array output
msgthe number identifying the message to read from
Returns
number of bytes actually read into output

Definition at line 74 of file pipe_rw.cpp.

75 {
76 return m_outputs->read(output, length, get_message_no("read", msg));
77 }

◆ read_all()

secure_vector< uint8_t > Botan::Pipe::read_all ( message_id  msg = DEFAULT_MESSAGE)

Read the full contents of the pipe.

Parameters
msgthe number identifying the message to read from
Returns
secure_vector holding the contents of the pipe

Definition at line 98 of file pipe_rw.cpp.

99 {
100 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
101 secure_vector<uint8_t> buffer(remaining(msg));
102 size_t got = read(buffer.data(), buffer.size(), msg);
103 buffer.resize(got);
104 return buffer;
105 }

References DEFAULT_MESSAGE, default_msg(), read(), and remaining().

◆ read_all_as_string()

std::string Botan::Pipe::read_all_as_string ( message_id  msg = DEFAULT_MESSAGE)

Read the full contents of the pipe.

Parameters
msgthe number identifying the message to read from
Returns
string holding the contents of the pipe

Definition at line 110 of file pipe_rw.cpp.

111 {
112 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
113 secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE);
114 std::string str;
115 str.reserve(remaining(msg));
116
117 while(true)
118 {
119 size_t got = read(buffer.data(), buffer.size(), msg);
120 if(got == 0)
121 break;
122 str.append(cast_uint8_ptr_to_char(buffer.data()), got);
123 }
124
125 return str;
126 }
#define BOTAN_DEFAULT_BUFFER_SIZE
Definition: build.h:361
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:188

References BOTAN_DEFAULT_BUFFER_SIZE, Botan::cast_uint8_ptr_to_char(), DEFAULT_MESSAGE, default_msg(), read(), and remaining().

◆ read_byte()

size_t Botan::DataSource::read_byte ( uint8_t &  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 23 of file data_src.cpp.

24 {
25 return read(&out, 1);
26 }

References Botan::DataSource::read().

Referenced by Botan::PEM_Code::decode(), Botan::BER_Decoder::discard_remaining(), and Botan::ASN1::maybe_BER().

◆ remaining()

size_t Botan::Pipe::remaining ( message_id  msg = DEFAULT_MESSAGE) const

Find out how many bytes are ready to read.

Parameters
msgthe number identifying the message for which the information is desired
Returns
number of bytes that can still be read

Definition at line 131 of file pipe_rw.cpp.

132 {
133 return m_outputs->remaining(get_message_no("remaining", msg));
134 }

Referenced by check_available(), check_available_msg(), end_of_data(), Botan::operator<<(), read_all(), and read_all_as_string().

◆ reset()

void Botan::Pipe::reset ( )

Reset this pipe to an empty pipe.

Definition at line 63 of file pipe.cpp.

64 {
65 destruct(m_pipe);
66 m_pipe = nullptr;
67 m_inside_msg = false;
68 }

◆ set_default_msg()

void Botan::Pipe::set_default_msg ( message_id  msg)

Set the default message

Parameters
msgthe number identifying the message which is going to be the new default message

Definition at line 93 of file pipe.cpp.

94 {
95 if(msg >= message_count())
96 throw Invalid_Argument("Pipe::set_default_msg: msg number is too high");
97 m_default_read = msg;
98 }
message_id message_count() const
Definition: pipe.cpp:297

References message_count().

◆ start_msg()

void Botan::Pipe::start_msg ( )

Start a new message in the pipe. A potential other message in this pipe must be closed with end_msg() before this function may be called.

Definition at line 144 of file pipe.cpp.

145 {
146 if(m_inside_msg)
147 throw Invalid_State("Pipe::start_msg: Message was already started");
148 if(m_pipe == nullptr)
149 m_pipe = new Null_Filter;
150 find_endpoints(m_pipe);
151 m_pipe->new_msg();
152 m_inside_msg = true;
153 }

Referenced by process_msg().

◆ write() [1/6]

void Botan::Pipe::write ( const secure_vector< uint8_t > &  in)
inline

Write input to the pipe, i.e. to its first filter.

Parameters
inthe secure_vector containing the data to write

Definition at line 75 of file pipe.h.

76 { write(in.data(), in.size()); }

◆ write() [2/6]

void Botan::Pipe::write ( const std::string &  in)

Write input to the pipe, i.e. to its first filter.

Parameters
inthe string containing the data to write

Definition at line 45 of file pipe_rw.cpp.

46 {
47 write(cast_char_ptr_to_uint8(str.data()), str.size());
48 }

References Botan::cast_char_ptr_to_uint8(), and write().

◆ write() [3/6]

void Botan::Pipe::write ( const std::vector< uint8_t > &  in)
inline

Write input to the pipe, i.e. to its first filter.

Parameters
inthe std::vector containing the data to write

Definition at line 82 of file pipe.h.

83 { write(in.data(), in.size()); }

◆ write() [4/6]

void Botan::Pipe::write ( const uint8_t  in[],
size_t  length 
)

Write input to the pipe, i.e. to its first filter.

Parameters
inthe byte array to write
lengththe length of the byte array in

Definition at line 35 of file pipe_rw.cpp.

36 {
37 if(!m_inside_msg)
38 throw Invalid_State("Cannot write to a Pipe while it is not processing");
39 m_pipe->write(input, length);
40 }
virtual void write(const uint8_t input[], size_t length)=0

References Botan::Filter::write().

Referenced by Botan::operator>>(), process_msg(), and write().

◆ write() [5/6]

void Botan::Pipe::write ( DataSource in)

Write input to the pipe, i.e. to its first filter.

Parameters
inthe DataSource to read the data from

Definition at line 61 of file pipe_rw.cpp.

62 {
63 secure_vector<uint8_t> buffer(BOTAN_DEFAULT_BUFFER_SIZE);
64 while(!source.end_of_data())
65 {
66 size_t got = source.read(buffer.data(), buffer.size());
67 write(buffer.data(), got);
68 }
69 }

References BOTAN_DEFAULT_BUFFER_SIZE, Botan::DataSource::end_of_data(), Botan::DataSource::read(), and write().

◆ write() [6/6]

void Botan::Pipe::write ( uint8_t  in)

Write input to the pipe, i.e. to its first filter.

Parameters
ina single byte to be written

Definition at line 53 of file pipe_rw.cpp.

54 {
55 write(&input, 1);
56 }

References write().

Member Data Documentation

◆ DEFAULT_MESSAGE

const Pipe::message_id Botan::Pipe::DEFAULT_MESSAGE
static
Initial value:
=
static_cast<Pipe::message_id>(-1)
size_t message_id
Definition: pipe.h:35

A meta-id for the default message (set with set_default_msg)

Definition at line 62 of file pipe.h.

Referenced by peek(), read(), read_all(), and read_all_as_string().

◆ LAST_MESSAGE

const Pipe::message_id Botan::Pipe::LAST_MESSAGE
static
Initial value:
=
static_cast<Pipe::message_id>(-2)

A meta-id for whatever the last message is

Definition at line 57 of file pipe.h.


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