Botan 3.0.0
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::vector< uint8_t > &in)
 
void process_msg (const uint8_t in[], size_t length)
 
void process_msg (DataSource &in)
 
void process_msg (std::string_view 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::vector< uint8_t > &in)
 
void write (const uint8_t in[], size_t length)
 
void write (DataSource &in)
 
void write (std::string_view 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 39 of file pipe.cpp.

39 :
40 Pipe({f1,f2,f3,f4})
41 {
42 }
Pipe(Filter *=nullptr, Filter *=nullptr, Filter *=nullptr, Filter *=nullptr)
Definition: pipe.cpp:39

◆ 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 47 of file pipe.cpp.

48 {
49 m_outputs = std::make_unique<Output_Buffers>();
50 m_pipe = nullptr;
51 m_default_read = 0;
52 m_inside_msg = false;
53
54 for(auto arg : args)
55 do_append(arg);
56 }

◆ Pipe() [3/3]

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

◆ ~Pipe()

Botan::Pipe::~Pipe ( )

Definition at line 61 of file pipe.cpp.

62 {
63 destruct(m_pipe);
64 }

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 210 of file pipe.cpp.

211 {
212 do_append(filter);
213 }

◆ 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 215 of file pipe.cpp.

216 {
217 if(m_outputs->message_count() != 0)
218 throw Invalid_State("Cannot call Pipe::append_filter after start_msg");
219
220 do_append(filter);
221 }

◆ 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:236
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 236 of file pipe.h.

236{ 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 40 of file data_src.cpp.

41 {
42 uint8_t buf[64] = { 0 };
43 size_t discarded = 0;
44
45 while(n)
46 {
47 const size_t got = this->read(buf, std::min(n, sizeof(buf)));
48 discarded += got;
49 n -= got;
50
51 if(got == 0)
52 break;
53 }
54
55 return discarded;
56 }
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 164 of file pipe.cpp.

165 {
166 if(!m_inside_msg)
167 throw Invalid_State("Pipe::end_msg: Message was already ended");
168 m_pipe->finish_msg();
169 clear_endpoints(m_pipe);
170 if(dynamic_cast<Null_Filter*>(m_pipe))
171 {
172 delete m_pipe;
173 m_pipe = nullptr;
174 }
175 m_inside_msg = false;
176
177 m_outputs->retire();
178 }

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 91 of file pipe.cpp.

92 {
93 return (remaining() == 0);
94 }

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 61 of file data_src.h.

61{ 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 303 of file pipe.cpp.

304 {
305 return m_outputs->message_count();
306 }

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:59

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

33 {
34 return peek(&out, 1, 0);
35 }
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 280 of file pipe.cpp.

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

◆ 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 223 of file pipe.cpp.

224 {
225 do_prepend(filter);
226 }

◆ 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 228 of file pipe.cpp.

229 {
230 if(m_outputs->message_count() != 0)
231 throw Invalid_State("Cannot call Pipe::prepend_filter after start_msg");
232
233 do_prepend(filter);
234 }

◆ 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 119 of file pipe.cpp.

120 {
121 process_msg(input.data(), input.size());
122 }
void process_msg(const uint8_t in[], size_t length)
Definition: pipe.cpp:109

References process_msg().

◆ process_msg() [2/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 124 of file pipe.cpp.

125 {
126 process_msg(input.data(), input.size());
127 }

References process_msg().

◆ process_msg() [3/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 109 of file pipe.cpp.

110 {
111 start_msg();
112 write(input, length);
113 end_msg();
114 }
void end_msg()
Definition: pipe.cpp:164
void write(const uint8_t in[], size_t length)
Definition: pipe_rw.cpp:35
void start_msg()
Definition: pipe.cpp:150

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

Referenced by process_msg().

◆ process_msg() [4/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 140 of file pipe.cpp.

141 {
142 start_msg();
143 write(input);
144 end_msg();
145 }

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

◆ process_msg() [5/5]

void Botan::Pipe::process_msg ( std::string_view  in)

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

Parameters
inthe string containing the data to write

Definition at line 132 of file pipe.cpp.

133 {
134 process_msg(cast_char_ptr_to_uint8(input.data()), input.length());
135 }
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().

◆ 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:363
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 24 of file data_src.cpp.

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

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 69 of file pipe.cpp.

70 {
71 destruct(m_pipe);
72 m_pipe = nullptr;
73 m_inside_msg = false;
74 }

◆ 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 99 of file pipe.cpp.

100 {
101 if(msg >= message_count())
102 throw Invalid_Argument("Pipe::set_default_msg: msg number is too high");
103 m_default_read = msg;
104 }
message_id message_count() const
Definition: pipe.cpp:303

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 150 of file pipe.cpp.

151 {
152 if(m_inside_msg)
153 throw Invalid_State("Pipe::start_msg: Message was already started");
154 if(m_pipe == nullptr)
155 m_pipe = new Null_Filter;
156 find_endpoints(m_pipe);
157 m_pipe->new_msg();
158 m_inside_msg = true;
159 }

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 72 of file pipe.h.

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

◆ write() [2/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 79 of file pipe.h.

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

◆ write() [3/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() [4/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() [5/6]

void Botan::Pipe::write ( std::string_view  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() [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 59 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 54 of file pipe.h.


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