Botan  1.11.10
filter.cpp
Go to the documentation of this file.
1 /*
2 * Filter
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/filter.h>
9 #include <botan/secqueue.h>
10 #include <botan/exceptn.h>
11 
12 namespace Botan {
13 
14 /*
15 * Filter Constructor
16 */
18  {
19  next.resize(1);
20  port_num = 0;
21  filter_owns = 0;
22  owned = false;
23  }
24 
25 /*
26 * Send data to all ports
27 */
28 void Filter::send(const byte input[], size_t length)
29  {
30  if(!length)
31  return;
32 
33  bool nothing_attached = true;
34  for(size_t j = 0; j != total_ports(); ++j)
35  if(next[j])
36  {
37  if(write_queue.size())
38  next[j]->write(&write_queue[0], write_queue.size());
39  next[j]->write(input, length);
40  nothing_attached = false;
41  }
42 
43  if(nothing_attached)
44  write_queue += std::make_pair(input, length);
45  else
46  write_queue.clear();
47  }
48 
49 /*
50 * Start a new message
51 */
52 void Filter::new_msg()
53  {
54  start_msg();
55  for(size_t j = 0; j != total_ports(); ++j)
56  if(next[j])
57  next[j]->new_msg();
58  }
59 
60 /*
61 * End the current message
62 */
63 void Filter::finish_msg()
64  {
65  end_msg();
66  for(size_t j = 0; j != total_ports(); ++j)
67  if(next[j])
68  next[j]->finish_msg();
69  }
70 
71 /*
72 * Attach a filter to the current port
73 */
74 void Filter::attach(Filter* new_filter)
75  {
76  if(new_filter)
77  {
78  Filter* last = this;
79  while(last->get_next())
80  last = last->get_next();
81  last->next[last->current_port()] = new_filter;
82  }
83  }
84 
85 /*
86 * Set the active port on a filter
87 */
88 void Filter::set_port(size_t new_port)
89  {
90  if(new_port >= total_ports())
91  throw Invalid_Argument("Filter: Invalid port number");
92  port_num = new_port;
93  }
94 
95 /*
96 * Return the next Filter in the logical chain
97 */
98 Filter* Filter::get_next() const
99  {
100  if(port_num < next.size())
101  return next[port_num];
102  return nullptr;
103  }
104 
105 /*
106 * Set the next Filters
107 */
108 void Filter::set_next(Filter* filters[], size_t size)
109  {
110  next.clear();
111 
112  port_num = 0;
113  filter_owns = 0;
114 
115  while(size && filters && (filters[size-1] == nullptr))
116  --size;
117 
118  if(filters && size)
119  next.assign(filters, filters + size);
120  }
121 
122 /*
123 * Return the total number of ports
124 */
125 size_t Filter::total_ports() const
126  {
127  return next.size();
128  }
129 
130 }
virtual void write(const byte input[], size_t length)=0
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
virtual void send(const byte in[], size_t length)
Definition: filter.cpp:28
uint8_t byte
Definition: types.h:30
virtual void start_msg()
Definition: filter.h:40
Definition: buf_comp.h:15
virtual void end_msg()
Definition: filter.h:46