Botan 2.19.2
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#include <botan/filter.h>
11#include <botan/internal/out_buf.h>
12
13namespace Botan {
14
15/*
16* Look up the canonical ID for a queue
17*/
18Pipe::message_id Pipe::get_message_no(const std::string& func_name,
19 message_id msg) const
20 {
21 if(msg == DEFAULT_MESSAGE)
22 msg = default_msg();
23 else if(msg == LAST_MESSAGE)
24 msg = message_count() - 1;
25
26 if(msg >= message_count())
27 throw Invalid_Message_Number(func_name, msg);
28
29 return msg;
30 }
31
32/*
33* Write into a Pipe
34*/
35void Pipe::write(const uint8_t input[], size_t length)
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 }
41
42/*
43* Write a string into a Pipe
44*/
45void Pipe::write(const std::string& str)
46 {
47 write(cast_char_ptr_to_uint8(str.data()), str.size());
48 }
49
50/*
51* Write a single byte into a Pipe
52*/
53void Pipe::write(uint8_t input)
54 {
55 write(&input, 1);
56 }
57
58/*
59* Write the contents of a DataSource into a Pipe
60*/
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 }
70
71/*
72* Read some data from the pipe
73*/
74size_t Pipe::read(uint8_t output[], size_t length, message_id msg)
75 {
76 return m_outputs->read(output, length, get_message_no("read", msg));
77 }
78
79/*
80* Read some data from the pipe
81*/
82size_t Pipe::read(uint8_t output[], size_t length)
83 {
84 return read(output, length, DEFAULT_MESSAGE);
85 }
86
87/*
88* Read a single byte from the pipe
89*/
90size_t Pipe::read(uint8_t& out, message_id msg)
91 {
92 return read(&out, 1, msg);
93 }
94
95/*
96* Return all data in the pipe
97*/
99 {
100 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
102 size_t got = read(buffer.data(), buffer.size(), msg);
103 buffer.resize(got);
104 return buffer;
105 }
106
107/*
108* Return all data in the pipe as a string
109*/
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 }
127
128/*
129* Find out how many bytes are ready to read
130*/
132 {
133 return m_outputs->remaining(get_message_no("remaining", msg));
134 }
135
136/*
137* Peek at some data in the pipe
138*/
139size_t Pipe::peek(uint8_t output[], size_t length,
140 size_t offset, message_id msg) const
141 {
142 return m_outputs->peek(output, length, offset, get_message_no("peek", msg));
143 }
144
145/*
146* Peek at some data in the pipe
147*/
148size_t Pipe::peek(uint8_t output[], size_t length, size_t offset) const
149 {
150 return peek(output, length, offset, DEFAULT_MESSAGE);
151 }
152
153/*
154* Peek at a byte in the pipe
155*/
156size_t Pipe::peek(uint8_t& out, size_t offset, message_id msg) const
157 {
158 return peek(&out, 1, offset, msg);
159 }
160
162 {
163 return m_outputs->get_bytes_read(default_msg());
164 }
165
167 {
168 return m_outputs->get_bytes_read(msg);
169 }
170
172 {
173 return (n <= remaining(default_msg()));
174 }
175
177 {
178 return (n <= remaining(msg));
179 }
180
181}
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0
virtual bool end_of_data() const =0
virtual void write(const uint8_t input[], size_t length)=0
size_t message_id
Definition: pipe.h:35
static const message_id LAST_MESSAGE
Definition: pipe.h:57
std::string read_all_as_string(message_id msg=DEFAULT_MESSAGE) BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:110
size_t read(uint8_t output[], size_t length) override BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:82
void write(const uint8_t in[], size_t length)
Definition: pipe_rw.cpp:35
size_t get_bytes_read() const override
Definition: pipe_rw.cpp:161
size_t default_msg() const
Definition: pipe.h:239
static const message_id DEFAULT_MESSAGE
Definition: pipe.h:62
bool check_available_msg(size_t n, message_id msg)
Definition: pipe_rw.cpp:176
size_t peek(uint8_t output[], size_t length, size_t offset) const override BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:148
message_id message_count() const
Definition: pipe.cpp:297
secure_vector< uint8_t > read_all(message_id msg=DEFAULT_MESSAGE) BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:98
bool check_available(size_t n) override
Definition: pipe_rw.cpp:171
size_t remaining(message_id msg=DEFAULT_MESSAGE) const BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:131
Definition: alg_id.cpp:13
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:195
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:190