Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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/internal/out_buf.h>
11
12 namespace Botan {
13
14 /*
15 * Look up the canonical ID for a queue
16 */
17 Pipe::message_id Pipe::get_message_no(const std::string& func_name,
18 message_id msg) const
19 {
20 if(msg == DEFAULT_MESSAGE)
21 msg = default_msg();
22 else if(msg == LAST_MESSAGE)
23 msg = message_count() - 1;
24
25 if(msg >= message_count())
26 throw Invalid_Message_Number(func_name, msg);
27
28 return msg;
29 }
30
31 /*
32 * Write into a Pipe
33 */
34 void Pipe::write(const uint8_t input[], size_t length)
35 {
36 if(!m_inside_msg)
37 throw Invalid_State("Cannot write to a Pipe while it is not processing");
38 m_pipe->write(input, length);
39 }
40
41 /*
42 * Write a string into a Pipe
43 */
44 void Pipe::write(const std::string& str)
45 {
46 write(reinterpret_cast<const uint8_t*>(str.data()), str.size());
47 }
48
49 /*
50 * Write a single byte into a Pipe
51 */
52 void Pipe::write(uint8_t input)
53 {
54 write(&input, 1);
55 }
56
57 /*
58 * Write the contents of a DataSource into a Pipe
59 */
60 void Pipe::write(DataSource& source)
61 {
62 secure_vector<uint8_t> buffer(DEFAULT_BUFFERSIZE);
63 while(!source.end_of_data())
64 {
65 size_t got = source.read(buffer.data(), buffer.size());
66 write(buffer.data(), got);
67 }
68 }
69
70 /*
71 * Read some data from the pipe
72 */
73 size_t Pipe::read(uint8_t output[], size_t length, message_id msg)
74 {
75 return m_outputs->read(output, length, get_message_no("read", msg));
76 }
77
78 /*
79 * Read some data from the pipe
80 */
81 size_t Pipe::read(uint8_t output[], size_t length)
82 {
83 return read(output, length, DEFAULT_MESSAGE);
84 }
85
86 /*
87 * Read a single byte from the pipe
88 */
89 size_t Pipe::read(uint8_t& out, message_id msg)
90 {
91 return read(&out, 1, msg);
92 }
93
94 /*
95 * Return all data in the pipe
96 */
97 secure_vector<uint8_t> Pipe::read_all(message_id msg)
98 {
99 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
100 secure_vector<uint8_t> buffer(remaining(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 */
109 std::string Pipe::read_all_as_string(message_id msg)
110 {
111 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
112 secure_vector<uint8_t> buffer(DEFAULT_BUFFERSIZE);
113 std::string str;
114 str.reserve(remaining(msg));
115
116 while(true)
117 {
118 size_t got = read(buffer.data(), buffer.size(), msg);
119 if(got == 0)
120 break;
121 str.append(reinterpret_cast<const char*>(buffer.data()), got);
122 }
123
124 return str;
125 }
126
127 /*
128 * Find out how many bytes are ready to read
129 */
130 size_t Pipe::remaining(message_id msg) const
131 {
132 return m_outputs->remaining(get_message_no("remaining", msg));
133 }
134
135 /*
136 * Peek at some data in the pipe
137 */
138 size_t Pipe::peek(uint8_t output[], size_t length,
139 size_t offset, message_id msg) const
140 {
141 return m_outputs->peek(output, length, offset, get_message_no("peek", msg));
142 }
143
144 /*
145 * Peek at some data in the pipe
146 */
147 size_t Pipe::peek(uint8_t output[], size_t length, size_t offset) const
148 {
149 return peek(output, length, offset, DEFAULT_MESSAGE);
150 }
151
152 /*
153 * Peek at a byte in the pipe
154 */
155 size_t Pipe::peek(uint8_t& out, size_t offset, message_id msg) const
156 {
157 return peek(&out, 1, offset, msg);
158 }
159
160 size_t Pipe::get_bytes_read() const
161 {
162 return m_outputs->get_bytes_read(default_msg());
163 }
164
165 size_t Pipe::get_bytes_read(message_id msg) const
166 {
167 return m_outputs->get_bytes_read(msg);
168 }
169
170 bool Pipe::check_available(size_t n)
171 {
172 return (n <= remaining(default_msg()));
173 }
174
175 bool Pipe::check_available_msg(size_t n, message_id msg)
176 {
177 return (n <= remaining(msg));
178 }
179
180 }
Botan::Pipe::remaining
size_t remaining(message_id msg=DEFAULT_MESSAGE) const BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:130
Botan::Pipe::get_bytes_read
size_t get_bytes_read() const override
Definition: pipe_rw.cpp:160
Botan::Pipe::message_id
size_t message_id
Definition: pipe.h:33
Botan::Filter::write
virtual void write(const uint8_t input[], size_t length)=0
Botan::Pipe::check_available
bool check_available(size_t n) override
Definition: pipe_rw.cpp:170
Botan::Invalid_State
Definition: exceptn.h:61
Botan::Pipe::read
size_t read(uint8_t output[], size_t length) override BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:81
Botan::Pipe::read_all
secure_vector< uint8_t > read_all(message_id msg=DEFAULT_MESSAGE) BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:97
Botan::Pipe::peek
size_t peek(uint8_t output[], size_t length, size_t offset) const override BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:147
Botan::DataSource
Definition: data_src.h:21
Botan::Pipe::DEFAULT_MESSAGE
static const message_id DEFAULT_MESSAGE
Definition: pipe.h:59
Botan::Pipe::check_available_msg
bool check_available_msg(size_t n, message_id msg)
Definition: pipe_rw.cpp:175
Botan::Output_Buffers::peek
size_t peek(uint8_t[], size_t, size_t, Pipe::message_id) const
Definition: out_buf.cpp:29
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::Pipe::LAST_MESSAGE
static const message_id LAST_MESSAGE
Definition: pipe.h:54
Botan::DataSource::end_of_data
virtual bool end_of_data() const =0
Botan::DataSource::read
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0
Botan::Pipe::read_all_as_string
std::string read_all_as_string(message_id=DEFAULT_MESSAGE) BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:109
Botan
Definition: alg_id.cpp:13
Botan::Output_Buffers::remaining
size_t remaining(Pipe::message_id) const
Definition: out_buf.cpp:42
Botan::Output_Buffers::get_bytes_read
size_t get_bytes_read(Pipe::message_id) const
Definition: out_buf.cpp:53
Botan::Pipe::default_msg
size_t default_msg() const
Definition: pipe.h:236
Botan::Output_Buffers::read
size_t read(uint8_t[], size_t, Pipe::message_id)
Definition: out_buf.cpp:17
Botan::Pipe::message_count
message_id message_count() const
Definition: pipe.cpp:288
Botan::Pipe::write
void write(const uint8_t in[], size_t length)
Definition: pipe_rw.cpp:34