Botan 3.9.0
Crypto and TLS for C&
pipe_rw.cpp
Go to the documentation of this file.
1/*
2* Pipe Reading/Writing
3* (C) 1999-2007 Jack Lloyd
4* 2012 Markus Wanner
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/pipe.h>
10
11#include <botan/filter.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/mem_utils.h>
14#include <botan/internal/out_buf.h>
15
16namespace Botan {
17
18/*
19* Look up the canonical ID for a queue
20*/
21Pipe::message_id Pipe::get_message_no(std::string_view func_name, message_id msg) const {
22 if(msg == DEFAULT_MESSAGE) {
23 msg = default_msg();
24 } else if(msg == LAST_MESSAGE) {
25 msg = message_count() - 1;
26 }
27
28 if(msg >= message_count()) {
29 throw Invalid_Message_Number(func_name, msg);
30 }
31
32 return msg;
33}
34
35void Pipe::write(std::span<const uint8_t> input) {
36 this->write(input.data(), input.size());
37}
38
39/*
40* Write into a Pipe
41*/
42void Pipe::write(const uint8_t input[], size_t length) {
43 if(!m_inside_msg) {
44 throw Invalid_State("Cannot write to a Pipe while it is not processing");
45 }
46 m_pipe->write(input, length);
47}
48
49/*
50* Write a string into a Pipe
51*/
52void Pipe::write(std::string_view str) {
54}
55
56/*
57* Write a single byte into a Pipe
58*/
59void Pipe::write(uint8_t input) {
60 write(&input, 1);
61}
62
63/*
64* Write the contents of a DataSource into a Pipe
65*/
66void Pipe::write(DataSource& source) {
68 while(!source.end_of_data()) {
69 size_t got = source.read(buffer.data(), buffer.size());
70 write(buffer.data(), got);
71 }
72}
73
74/*
75* Read some data from the pipe
76*/
77size_t Pipe::read(uint8_t output[], size_t length, message_id msg) {
78 return m_outputs->read(output, length, get_message_no("read", msg));
79}
80
81/*
82* Read some data from the pipe
83*/
84size_t Pipe::read(uint8_t output[], size_t length) {
85 return read(output, length, DEFAULT_MESSAGE);
86}
87
88/*
89* Read a single byte from the pipe
90*/
91size_t Pipe::read(uint8_t& out, message_id msg) {
92 return read(&out, 1, msg);
93}
94
95/*
96* Return all data in the pipe
97*/
99 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
101 size_t got = read(buffer.data(), buffer.size(), msg);
102 buffer.resize(got);
103 return buffer;
104}
105
106/*
107* Return all data in the pipe as a string
108*/
110 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
112 std::string str;
113 str.reserve(remaining(msg));
114
115 while(true) {
116 size_t got = read(buffer.data(), buffer.size(), msg);
117 if(got == 0) {
118 break;
119 }
120 str.append(cast_uint8_ptr_to_char(buffer.data()), got);
121 }
122
123 return str;
124}
125
126/*
127* Find out how many bytes are ready to read
128*/
129size_t Pipe::remaining(message_id msg) const {
130 return m_outputs->remaining(get_message_no("remaining", msg));
131}
132
133/*
134* Peek at some data in the pipe
135*/
136size_t Pipe::peek(uint8_t output[], size_t length, size_t offset, message_id msg) const {
137 return m_outputs->peek(output, length, offset, get_message_no("peek", msg));
138}
139
140/*
141* Peek at some data in the pipe
142*/
143size_t Pipe::peek(uint8_t output[], size_t length, size_t offset) const {
144 return peek(output, length, offset, DEFAULT_MESSAGE);
145}
146
147/*
148* Peek at a byte in the pipe
149*/
150size_t Pipe::peek(uint8_t& out, size_t offset, message_id msg) const {
151 return peek(&out, 1, offset, msg);
152}
153
154size_t Pipe::get_bytes_read() const {
155 return m_outputs->get_bytes_read(default_msg());
156}
157
159 return m_outputs->get_bytes_read(msg);
160}
161
162bool Pipe::check_available(size_t n) {
163 return (n <= remaining(default_msg()));
164}
165
166bool Pipe::check_available_msg(size_t n, message_id msg) const {
167 return (n <= remaining(msg));
168}
169
170} // namespace Botan
DataSource()=default
virtual size_t read(uint8_t out[], size_t length)=0
virtual bool end_of_data() const =0
size_t message_id
Definition pipe.h:39
static const message_id LAST_MESSAGE
Definition pipe.h:57
size_t read(uint8_t output[], size_t length) override
Definition pipe_rw.cpp:84
bool check_available_msg(size_t n, message_id msg) const
Definition pipe_rw.cpp:166
std::string read_all_as_string(message_id msg=DEFAULT_MESSAGE)
Definition pipe_rw.cpp:109
void write(const uint8_t in[], size_t length)
Definition pipe_rw.cpp:42
size_t get_bytes_read() const override
Definition pipe_rw.cpp:154
size_t default_msg() const
Definition pipe.h:247
secure_vector< uint8_t > read_all(message_id msg=DEFAULT_MESSAGE)
Definition pipe_rw.cpp:98
static const message_id DEFAULT_MESSAGE
Definition pipe.h:62
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition pipe_rw.cpp:129
size_t peek(uint8_t output[], size_t length, size_t offset) const override
Definition pipe_rw.cpp:143
message_id message_count() const
Definition pipe.cpp:312
bool check_available(size_t n) override
Definition pipe_rw.cpp:162
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:28
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:282
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69
constexpr size_t DefaultBufferSize
Definition types.h:137