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