Botan 3.6.1
Crypto and TLS for C&
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 () override
 

Static Public Attributes

static const message_id DEFAULT_MESSAGE = static_cast<Pipe::message_id>(-1)
 
static const message_id LAST_MESSAGE = static_cast<Pipe::message_id>(-2)
 

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

An opaque type that identifies a message in this Pipe

Definition at line 34 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 38 of file pipe.cpp.

38: Pipe({f1, f2, f3, f4}) {}
Pipe(Filter *=nullptr, Filter *=nullptr, Filter *=nullptr, Filter *=nullptr)
Definition pipe.cpp:38

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

43 {
44 m_outputs = std::make_unique<Output_Buffers>();
45 m_pipe = nullptr;
46 m_default_read = 0;
47 m_inside_msg = false;
48
49 for(auto arg : args) {
50 do_append(arg);
51 }
52}

◆ Pipe() [3/3]

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

◆ ~Pipe()

Botan::Pipe::~Pipe ( )
override

Definition at line 57 of file pipe.cpp.

57 {
58 destruct(m_pipe);
59}

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

199 {
200 do_append(filter);
201}

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

203 {
204 if(m_outputs->message_count() != 0) {
205 throw Invalid_State("Cannot call Pipe::append_filter after start_msg");
206 }
207
208 do_append(filter);
209}

◆ check_available()

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

Implements Botan::DataSource.

Definition at line 157 of file pipe_rw.cpp.

157 {
158 return (n <= remaining(default_msg()));
159}
size_t default_msg() const
Definition pipe.h:230
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition pipe_rw.cpp:124

References default_msg(), and remaining().

◆ check_available_msg()

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

Definition at line 161 of file pipe_rw.cpp.

161 {
162 return (n <= remaining(msg));
163}

References remaining().

◆ default_msg()

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

Definition at line 230 of file pipe.h.

230{ 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.

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

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

Referenced by process_msg(), and 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 86 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 149 of file pipe_rw.cpp.

149 {
150 return m_outputs->get_bytes_read(default_msg());
151}

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 153 of file pipe_rw.cpp.

153 {
154 return m_outputs->get_bytes_read(msg);
155}

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

302 {
303 return m_outputs->message_count();
304}

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
nodiscard

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 145 of file pipe_rw.cpp.

145 {
146 return peek(&out, 1, offset, msg);
147}
size_t peek(uint8_t output[], size_t length, size_t offset) const override
Definition pipe_rw.cpp:138

References peek().

◆ peek() [2/3]

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

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 138 of file pipe_rw.cpp.

138 {
139 return peek(output, length, offset, DEFAULT_MESSAGE);
140}
static const message_id DEFAULT_MESSAGE
Definition pipe.h:57

References DEFAULT_MESSAGE, and peek().

Referenced by peek(), and peek().

◆ peek() [3/3]

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

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 131 of file pipe_rw.cpp.

131 {
132 return m_outputs->peek(output, length, offset, get_message_no("peek", msg));
133}

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

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

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

211 {
212 do_prepend(filter);
213}

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

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

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

112 {
113 process_msg(input.data(), input.size());
114}
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::vector< uint8_t > & in)

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

Parameters
inthe secure_vector containing the data to write

Definition at line 116 of file pipe.cpp.

116 {
117 process_msg(input.data(), input.size());
118}

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

103 {
104 start_msg();
105 write(input, length);
106 end_msg();
107}
void end_msg()
Definition pipe.cpp:154
void write(const uint8_t in[], size_t length)
Definition pipe_rw.cpp:37
void start_msg()
Definition pipe.cpp:139

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

Referenced by process_msg(), process_msg(), and 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 130 of file pipe.cpp.

130 {
131 start_msg();
132 write(input);
133 end_msg();
134}

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

123 {
124 process_msg(cast_char_ptr_to_uint8(input.data()), input.length());
125}
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273

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 )
nodiscard

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 86 of file pipe_rw.cpp.

86 {
87 return read(&out, 1, msg);
88}
size_t read(uint8_t output[], size_t length) override
Definition pipe_rw.cpp:79

References read().

◆ read() [2/3]

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

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 79 of file pipe_rw.cpp.

79 {
80 return read(output, length, DEFAULT_MESSAGE);
81}

References DEFAULT_MESSAGE, and read().

Referenced by Botan::operator<<(), Botan::operator<<(), read(), 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 )
nodiscard

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 72 of file pipe_rw.cpp.

72 {
73 return m_outputs->read(output, length, get_message_no("read", msg));
74}

◆ read_all()

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

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 93 of file pipe_rw.cpp.

93 {
94 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
96 size_t got = read(buffer.data(), buffer.size(), msg);
97 buffer.resize(got);
98 return buffer;
99}
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

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)
nodiscard

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 104 of file pipe_rw.cpp.

104 {
105 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
107 std::string str;
108 str.reserve(remaining(msg));
109
110 while(true) {
111 size_t got = read(buffer.data(), buffer.size(), msg);
112 if(got == 0) {
113 break;
114 }
115 str.append(cast_uint8_ptr_to_char(buffer.data()), got);
116 }
117
118 return str;
119}
#define BOTAN_DEFAULT_BUFFER_SIZE
Definition build.h:444
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:277

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

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

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
nodiscard

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 124 of file pipe_rw.cpp.

124 {
125 return m_outputs->remaining(get_message_no("remaining", msg));
126}

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

◆ reset()

void Botan::Pipe::reset ( )

Reset this pipe to an empty pipe.

Definition at line 64 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.

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

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

139 {
140 if(m_inside_msg) {
141 throw Invalid_State("Pipe::start_msg: Message was already started");
142 }
143 if(m_pipe == nullptr) {
144 m_pipe = new Null_Filter;
145 }
146 find_endpoints(m_pipe);
147 m_pipe->new_msg();
148 m_inside_msg = true;
149}

Referenced by process_msg(), and 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 70 of file pipe.h.

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

References write().

Referenced by write().

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

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

References write().

Referenced by write().

◆ 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 37 of file pipe_rw.cpp.

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

References Botan::Filter::write().

Referenced by Botan::operator>>(), Botan::operator>>(), process_msg(), process_msg(), write(), write(), 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.

61 {
63 while(!source.end_of_data()) {
64 size_t got = source.read(buffer.data(), buffer.size());
65 write(buffer.data(), got);
66 }
67}

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

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

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 54 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_cast<Pipe::message_id>(-1)
static

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

Definition at line 57 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_cast<Pipe::message_id>(-2)
static

A meta-id for whatever the last message is

Definition at line 52 of file pipe.h.


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