#include "botan-1.6/include/filter.h"
#include "botan-1.6/include/secqueue.h"
#include "botan-1.6/include/libstate.h"
namespace Enctain {
namespace Botan {
Filter::Filter()
{
next.resize(1);
port_num = 0;
filter_owns = 0;
owned = false;
}
void Filter::send(const byte input[], u32bit length)
{
global_state().pulse(PIPE_WRITE);
bool nothing_attached = true;
for(u32bit j = 0; j != total_ports(); ++j)
if(next[j])
{
if(write_queue.has_items())
next[j]->write(write_queue, write_queue.size());
next[j]->write(input, length);
nothing_attached = false;
}
if(nothing_attached)
write_queue.append(input, length);
else if(write_queue.has_items())
write_queue.destroy();
}
void Filter::new_msg()
{
start_msg();
for(u32bit j = 0; j != total_ports(); ++j)
if(next[j])
next[j]->new_msg();
}
void Filter::finish_msg()
{
end_msg();
for(u32bit j = 0; j != total_ports(); ++j)
if(next[j])
next[j]->finish_msg();
}
void Filter::attach(Filter* new_filter)
{
if(new_filter)
{
Filter* last = this;
while(last->get_next())
last = last->get_next();
last->next[last->current_port()] = new_filter;
}
}
void Filter::set_port(u32bit new_port)
{
if(new_port >= total_ports())
throw Invalid_Argument("Filter: Invalid port number");
port_num = new_port;
}
Filter* Filter::get_next() const
{
if(port_num < next.size())
return next[port_num];
return 0;
}
void Filter::set_next(Filter* filters[], u32bit size)
{
while(size && filters && filters[size-1] == 0)
--size;
next.clear();
next.resize(size);
port_num = 0;
filter_owns = 0;
for(u32bit j = 0; j != size; ++j)
next[j] = filters[j];
}
u32bit Filter::total_ports() const
{
return next.size();
}
}
}