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
12namespace Botan {
13
14/*
15* Look up the canonical ID for a queue
16*/
17Pipe::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*/
34void 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*/
45 {
46 write(&input[0], input.size());
47 }
48
49/*
50* Write a string into a Pipe
51*/
52void 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*/
60void Pipe::write(byte input)
61 {
62 write(&input, 1);
63 }
64
65/*
66* Write the contents of a DataSource into a Pipe
67*/
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*/
81size_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*/
89size_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*/
97size_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*/
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*/
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*/
139 {
140 return outputs->remaining(get_message_no("remaining", msg));
141 }
142
144 {
145 return (n <= remaining(DEFAULT_MESSAGE));
146 }
147
149 {
150 return (n <= remaining(msg));
151 }
152
153/*
154* Peek at some data in the pipe
155*/
156size_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*/
165size_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*/
173size_t Pipe::peek(byte& out, size_t offset, message_id msg) const
174 {
175 return peek(&out, 1, offset, msg);
176 }
177
178}
virtual size_t read(byte out[], size_t length)=0
virtual bool end_of_data() const =0
size_t size() const
Definition secmem.h:29
void resize(size_t n)
Definition secmem.h:217
size_t message_id
Definition pipe.h:31
static const message_id LAST_MESSAGE
Definition pipe.h:52
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition pipe_rw.cpp:105
size_t default_msg() const
Definition pipe.h:209
static const message_id DEFAULT_MESSAGE
Definition pipe.h:57
void write(const byte in[], size_t length)
Definition pipe_rw.cpp:34
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition pipe_rw.cpp:138
size_t read(byte output[], size_t length)
Definition pipe_rw.cpp:89
bool check_available_msg(size_t n, message_id msg)
Definition pipe_rw.cpp:148
bool check_available(size_t n)
Definition pipe_rw.cpp:143
message_id message_count() const
Definition pipe.cpp:282
std::string read_all_as_string(message_id=DEFAULT_MESSAGE)
Definition pipe_rw.cpp:117
size_t peek(byte output[], size_t length, size_t offset) const
Definition pipe_rw.cpp:165
Invalid_State(const std::string &err)
Definition exceptn.h:27
Invalid_Message_Number(const std::string &where, message_id msg)
Definition pipe.h:43