Botan 3.6.1
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/out_buf.h>
14
15namespace Botan {
16
17/*
18* Look up the canonical ID for a queue
19*/
20Pipe::message_id Pipe::get_message_no(std::string_view func_name, message_id msg) const {
21 if(msg == DEFAULT_MESSAGE) {
22 msg = default_msg();
23 } else if(msg == LAST_MESSAGE) {
24 msg = message_count() - 1;
25 }
26
27 if(msg >= message_count()) {
28 throw Invalid_Message_Number(func_name, msg);
29 }
30
31 return msg;
32}
33
34/*
35* Write into a Pipe
36*/
37void Pipe::write(const uint8_t input[], size_t length) {
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}
43
44/*
45* Write a string into a Pipe
46*/
47void Pipe::write(std::string_view str) {
48 write(cast_char_ptr_to_uint8(str.data()), str.size());
49}
50
51/*
52* Write a single byte into a Pipe
53*/
54void Pipe::write(uint8_t input) {
55 write(&input, 1);
56}
57
58/*
59* Write the contents of a DataSource into a Pipe
60*/
61void Pipe::write(DataSource& source) {
63 while(!source.end_of_data()) {
64 size_t got = source.read(buffer.data(), buffer.size());
65 write(buffer.data(), got);
66 }
67}
68
69/*
70* Read some data from the pipe
71*/
72size_t Pipe::read(uint8_t output[], size_t length, message_id msg) {
73 return m_outputs->read(output, length, get_message_no("read", msg));
74}
75
76/*
77* Read some data from the pipe
78*/
79size_t Pipe::read(uint8_t output[], size_t length) {
80 return read(output, length, DEFAULT_MESSAGE);
81}
82
83/*
84* Read a single byte from the pipe
85*/
86size_t Pipe::read(uint8_t& out, message_id msg) {
87 return read(&out, 1, msg);
88}
89
90/*
91* Return all data in the pipe
92*/
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}
100
101/*
102* Return all data in the pipe as a string
103*/
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}
120
121/*
122* Find out how many bytes are ready to read
123*/
124size_t Pipe::remaining(message_id msg) const {
125 return m_outputs->remaining(get_message_no("remaining", msg));
126}
127
128/*
129* Peek at some data in the pipe
130*/
131size_t Pipe::peek(uint8_t output[], size_t length, size_t offset, message_id msg) const {
132 return m_outputs->peek(output, length, offset, get_message_no("peek", msg));
133}
134
135/*
136* Peek at some data in the pipe
137*/
138size_t Pipe::peek(uint8_t output[], size_t length, size_t offset) const {
139 return peek(output, length, offset, DEFAULT_MESSAGE);
140}
141
142/*
143* Peek at a byte in the pipe
144*/
145size_t Pipe::peek(uint8_t& out, size_t offset, message_id msg) const {
146 return peek(&out, 1, offset, msg);
147}
148
149size_t Pipe::get_bytes_read() const {
150 return m_outputs->get_bytes_read(default_msg());
151}
152
154 return m_outputs->get_bytes_read(msg);
155}
156
157bool Pipe::check_available(size_t n) {
158 return (n <= remaining(default_msg()));
159}
160
161bool Pipe::check_available_msg(size_t n, message_id msg) const {
162 return (n <= remaining(msg));
163}
164
165} // namespace Botan
virtual size_t read(uint8_t out[], size_t length)=0
virtual bool end_of_data() const =0
virtual void write(const uint8_t input[], size_t length)=0
static const message_id LAST_MESSAGE
Definition pipe.h:52
size_t read(uint8_t output[], size_t length) override
Definition pipe_rw.cpp:79
bool check_available_msg(size_t n, message_id msg) const
Definition pipe_rw.cpp:161
std::string read_all_as_string(message_id msg=DEFAULT_MESSAGE)
Definition pipe_rw.cpp:104
void write(const uint8_t in[], size_t length)
Definition pipe_rw.cpp:37
size_t get_bytes_read() const override
Definition pipe_rw.cpp:149
size_t default_msg() const
Definition pipe.h:230
secure_vector< uint8_t > read_all(message_id msg=DEFAULT_MESSAGE)
Definition pipe_rw.cpp:93
static const message_id DEFAULT_MESSAGE
Definition pipe.h:57
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition pipe_rw.cpp:124
size_t message_id
Definition pipe.h:34
size_t peek(uint8_t output[], size_t length, size_t offset) const override
Definition pipe_rw.cpp:138
message_id message_count() const
Definition pipe.cpp:302
bool check_available(size_t n) override
Definition pipe_rw.cpp:157
#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
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273