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