Botan  1.10.9
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  bool nothing_attached = true;
31  for(size_t j = 0; j != total_ports(); ++j)
32  if(next[j])
33  {
34  if(write_queue.size())
35  next[j]->write(&write_queue[0], write_queue.size());
36  next[j]->write(input, length);
37  nothing_attached = false;
38  }
39 
40  if(nothing_attached)
41  write_queue += std::make_pair(input, length);
42  else
43  write_queue.clear();
44  }
45 
46 /*
47 * Start a new message
48 */
49 void Filter::new_msg()
50  {
51  start_msg();
52  for(size_t j = 0; j != total_ports(); ++j)
53  if(next[j])
54  next[j]->new_msg();
55  }
56 
57 /*
58 * End the current message
59 */
60 void Filter::finish_msg()
61  {
62  end_msg();
63  for(size_t j = 0; j != total_ports(); ++j)
64  if(next[j])
65  next[j]->finish_msg();
66  }
67 
68 /*
69 * Attach a filter to the current port
70 */
71 void Filter::attach(Filter* new_filter)
72  {
73  if(new_filter)
74  {
75  Filter* last = this;
76  while(last->get_next())
77  last = last->get_next();
78  last->next[last->current_port()] = new_filter;
79  }
80  }
81 
82 /*
83 * Set the active port on a filter
84 */
85 void Filter::set_port(size_t new_port)
86  {
87  if(new_port >= total_ports())
88  throw Invalid_Argument("Filter: Invalid port number");
89  port_num = new_port;
90  }
91 
92 /*
93 * Return the next Filter in the logical chain
94 */
95 Filter* Filter::get_next() const
96  {
97  if(port_num < next.size())
98  return next[port_num];
99  return 0;
100  }
101 
102 /*
103 * Set the next Filters
104 */
105 void Filter::set_next(Filter* filters[], size_t size)
106  {
107  next.clear();
108 
109  port_num = 0;
110  filter_owns = 0;
111 
112  while(size && filters && filters[size-1] == 0)
113  --size;
114 
115  if(filters && size)
116  next.assign(filters, filters + size);
117  }
118 
119 /*
120 * Return the total number of ports
121 */
122 size_t Filter::total_ports() const
123  {
124  return next.size();
125  }
126 
127 }
virtual void write(const byte input[], size_t length)=0
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
unsigned char byte
Definition: types.h:22
void send(const byte in[], size_t length)
Definition: filter.cpp:28
size_t size() const
Definition: secmem.h:29
virtual void start_msg()
Definition: filter.h:38
virtual void end_msg()
Definition: filter.h:44