Botan 1.10.17

pipe_rw.cpp
Go to the documentation of this file.
1 /*
2 * Pipe Reading/Writing
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/pipe.h>
9 #include <botan/internal/out_buf.h>
10 #include <botan/secqueue.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 byte input[], size_t length)
35 {
36 if(!inside_msg)
37 throw Invalid_State("Cannot write to a Pipe while it is not processing");
38 pipe->write(input, length);
39 }
40
41 /*
42 * Write into a Pipe
43 */
44 void Pipe::write(const MemoryRegion<byte>& input)
45 {
46 write(&input[0], input.size());
47 }
48
49 /*
50 * Write a string into a Pipe
51 */
52 void Pipe::write(const std::string& str)
53 {
54 write(reinterpret_cast<const byte*>(str.data()), str.size());
55 }
56
57 /*
58 * Write a single byte into a Pipe
59 */
60 void Pipe::write(byte input)
61 {
62 write(&input, 1);
63 }
64
65 /*
66 * Write the contents of a DataSource into a Pipe
67 */
68 void Pipe::write(DataSource& source)
69 {
70 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
71 while(!source.end_of_data())
72 {
73 size_t got = source.read(&buffer[0], buffer.size());
74 write(&buffer[0], got);
75 }
76 }
77
78 /*
79 * Read some data from the pipe
80 */
81 size_t Pipe::read(byte output[], size_t length, message_id msg)
82 {
83 return outputs->read(output, length, get_message_no("read", msg));
84 }
85
86 /*
87 * Read some data from the pipe
88 */
89 size_t Pipe::read(byte output[], size_t length)
90 {
91 return read(output, length, DEFAULT_MESSAGE);
92 }
93
94 /*
95 * Read a single byte from the pipe
96 */
97 size_t Pipe::read(byte& out, message_id msg)
98 {
99 return read(&out, 1, msg);
100 }
101
102 /*
103 * Return all data in the pipe
104 */
105 SecureVector<byte> Pipe::read_all(message_id msg)
106 {
107 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
108 SecureVector<byte> buffer(remaining(msg));
109 size_t got = read(&buffer[0], buffer.size(), msg);
110 buffer.resize(got);
111 return buffer;
112 }
113
114 /*
115 * Return all data in the pipe as a string
116 */
117 std::string Pipe::read_all_as_string(message_id msg)
118 {
119 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
120 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
121 std::string str;
122 str.reserve(remaining(msg));
123
124 while(true)
125 {
126 size_t got = read(&buffer[0], buffer.size(), msg);
127 if(got == 0)
128 break;
129 str.append(reinterpret_cast<const char*>(&buffer[0]), got);
130 }
131
132 return str;
133 }
134
135 /*
136 * Find out how many bytes are ready to read
137 */
138 size_t Pipe::remaining(message_id msg) const
139 {
140 return outputs->remaining(get_message_no("remaining", msg));
141 }
142
143 bool Pipe::check_available(size_t n)
144 {
145 return (n <= remaining(DEFAULT_MESSAGE));
146 }
147
148 bool Pipe::check_available_msg(size_t n, message_id msg)
149 {
150 return (n <= remaining(msg));
151 }
152
153 /*
154 * Peek at some data in the pipe
155 */
156 size_t Pipe::peek(byte output[], size_t length,
157 size_t offset, message_id msg) const
158 {
159 return outputs->peek(output, length, offset, get_message_no("peek", msg));
160 }
161
162 /*
163 * Peek at some data in the pipe
164 */
165 size_t Pipe::peek(byte output[], size_t length, size_t offset) const
166 {
167 return peek(output, length, offset, DEFAULT_MESSAGE);
168 }
169
170 /*
171 * Peek at a byte in the pipe
172 */
173 size_t Pipe::peek(byte& out, size_t offset, message_id msg) const
174 {
175 return peek(&out, 1, offset, msg);
176 }
177
178 }
Botan::Output_Buffers::read
size_t read(byte[], size_t, Pipe::message_id)
Definition: out_buf.cpp:17
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::Filter::write
virtual void write(const byte input[], size_t length)=0
Botan::Pipe::remaining
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition: pipe_rw.cpp:138
Botan::Pipe::default_msg
size_t default_msg() const
Definition: pipe.h:209
Botan::DataSource::read
virtual size_t read(byte out[], size_t length)=0
Botan::Pipe::read
size_t read(byte output[], size_t length)
Definition: pipe_rw.cpp:89
Botan::Pipe::read_all_as_string
std::string read_all_as_string(message_id=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:117
Botan::Pipe::write
void write(const byte in[], size_t length)
Definition: pipe_rw.cpp:34
Botan::Output_Buffers::remaining
size_t remaining(Pipe::message_id) const
Definition: out_buf.cpp:42
Botan::Pipe::message_id
size_t message_id
Definition: pipe.h:31
Botan::Invalid_State
Definition: exceptn.h:25
Botan::DataSource
Definition: data_src.h:20
Botan::Pipe::DEFAULT_MESSAGE
static const message_id DEFAULT_MESSAGE
Definition: pipe.h:57
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Pipe::check_available_msg
bool check_available_msg(size_t n, message_id msg)
Definition: pipe_rw.cpp:148
Botan::Pipe::read_all
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:105
Botan::Pipe::LAST_MESSAGE
static const message_id LAST_MESSAGE
Definition: pipe.h:52
Botan::DataSource::end_of_data
virtual bool end_of_data() const =0
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::Pipe::message_count
message_id message_count() const
Definition: pipe.cpp:282
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::Output_Buffers::peek
size_t peek(byte[], size_t, size_t, Pipe::message_id) const
Definition: out_buf.cpp:29
Botan::SecureVector< byte >
Botan::Pipe::check_available
bool check_available(size_t n)
Definition: pipe_rw.cpp:143
Botan::Pipe::peek
size_t peek(byte output[], size_t length, size_t offset) const
Definition: pipe_rw.cpp:165