1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*************************************************
* Pipe Source File *
* (C) 1999-2005 The Botan Project *
*************************************************/
#include <botan/pipe.h>
#include <botan/out_buf.h>
#include <botan/secqueue.h>
namespace Botan {
namespace {
/*************************************************
* A Filter that does nothing *
*************************************************/
class Null_Filter : public Filter
{
public:
void write(const byte input[], u32bit length)
{ send(input, length); }
};
}
/*************************************************
* Pipe Constructor *
*************************************************/
Pipe::Pipe(Filter* f1, Filter* f2, Filter* f3, Filter* f4)
{
init();
append(f1);
append(f2);
append(f3);
append(f4);
}
/*************************************************
* Pipe Constructor *
*************************************************/
Pipe::Pipe(Filter* filter_array[], u32bit count)
{
init();
for(u32bit j = 0; j != count; j++)
append(filter_array[j]);
}
/*************************************************
* Pipe Destructor *
*************************************************/
Pipe::~Pipe()
{
destruct(pipe);
delete outputs;
}
/*************************************************
* Initialize the Pipe *
*************************************************/
void Pipe::init()
{
outputs = new Output_Buffers;
pipe = 0;
default_read = 0;
inside_msg = false;
}
/*************************************************
* Reset the Pipe *
*************************************************/
void Pipe::reset()
{
if(inside_msg)
throw Invalid_State("Pipe cannot be reset while it is processing");
destruct(pipe);
pipe = 0;
inside_msg = false;
}
/*************************************************
* Destroy the Pipe *
*************************************************/
void Pipe::destruct(Filter* to_kill)
{
if(!to_kill || dynamic_cast<SecureQueue*>(to_kill))
return;
for(u32bit j = 0; j != to_kill->total_ports(); j++)
destruct(to_kill->next[j]);
delete to_kill;
}
/*************************************************
* Test if the Pipe has any data in it *
*************************************************/
bool Pipe::end_of_data() const
{
return (remaining() == 0);
}
/*************************************************
* Set the default read message *
*************************************************/
void Pipe::set_default_msg(u32bit msg)
{
if(msg >= message_count())
throw Invalid_Argument("Pipe::set_default_msg: msg number is too high");
default_read = msg;
}
/*************************************************
* Process a full message at once *
*************************************************/
void Pipe::process_msg(const byte input[], u32bit length)
{
start_msg();
write(input, length);
end_msg();
}
/*************************************************
* Process a full message at once *
*************************************************/
void Pipe::process_msg(const MemoryRegion<byte>& input)
{
process_msg(input.begin(), input.size());
}
/*************************************************
* Process a full message at once *
*************************************************/
void Pipe::process_msg(const std::string& input)
{
process_msg((const byte*)input.c_str(), input.length());
}
/*************************************************
* Process a full message at once *
*************************************************/
void Pipe::process_msg(DataSource& input)
{
start_msg();
write(input);
end_msg();
}
/*************************************************
* Start a new message *
*************************************************/
void Pipe::start_msg()
{
if(inside_msg)
throw Invalid_State("Pipe::start_msg: Message was already started");
if(pipe == 0)
pipe = new Null_Filter;
find_endpoints(pipe);
pipe->new_msg();
inside_msg = true;
}
/*************************************************
* End the current message *
*************************************************/
void Pipe::end_msg()
{
if(!inside_msg)
throw Invalid_State("Pipe::end_msg: Message was already ended");
pipe->finish_msg();
clear_endpoints(pipe);
if(dynamic_cast<Null_Filter*>(pipe))
{
delete pipe;
pipe = 0;
}
inside_msg = false;
outputs->retire();
}
/*************************************************
* Find the endpoints of the Pipe *
*************************************************/
void Pipe::find_endpoints(Filter* f)
{
for(u32bit j = 0; j != f->total_ports(); j++)
if(f->next[j] && !dynamic_cast<SecureQueue*>(f->next[j]))
find_endpoints(f->next[j]);
else
{
SecureQueue* q = new SecureQueue;
f->next[j] = q;
outputs->add(q);
}
}
/*************************************************
* Remove the SecureQueues attached to the Filter *
*************************************************/
void Pipe::clear_endpoints(Filter* f)
{
if(!f) return;
for(u32bit j = 0; j != f->total_ports(); j++)
{
if(f->next[j] && dynamic_cast<SecureQueue*>(f->next[j]))
f->next[j] = 0;
clear_endpoints(f->next[j]);
}
}
/*************************************************
* Append a Filter to the Pipe *
*************************************************/
void Pipe::append(Filter* filter)
{
if(inside_msg)
throw Invalid_State("Cannot append to a Pipe while it is processing");
if(!filter)
return;
if(dynamic_cast<SecureQueue*>(filter))
throw Invalid_Argument("Pipe::append: SecureQueue cannot be used");
if(!pipe) pipe = filter;
else pipe->attach(filter);
}
/*************************************************
* Prepend a Filter to the Pipe *
*************************************************/
void Pipe::prepend(Filter* filter)
{
if(inside_msg)
throw Invalid_State("Cannot prepend to a Pipe while it is processing");
if(!filter)
return;
if(dynamic_cast<SecureQueue*>(filter))
throw Invalid_Argument("Pipe::prepend: SecureQueue cannot be used");
if(pipe) filter->attach(pipe);
pipe = filter;
}
/*************************************************
* Pop a Filter off the Pipe *
*************************************************/
void Pipe::pop()
{
if(inside_msg)
throw Invalid_State("Cannot pop off a Pipe while it is processing");
if(!pipe)
return;
if(pipe->total_ports() > 1)
throw Invalid_State("Cannot pop off a Filter with multiple ports");
Filter* f = pipe;
u32bit owns = f->owns();
pipe = pipe->next[0];
delete f;
while(owns--)
{
f = pipe;
pipe = pipe->next[0];
delete f;
}
}
/*************************************************
* Return the number of messages in this Pipe *
*************************************************/
u32bit Pipe::message_count() const
{
return outputs->message_count();
}
}
|