Botan 3.13.0
Crypto and TLS for C&
filter.h
Go to the documentation of this file.
1/*
2* Filter
3* (C) 1999-2007 Jack Lloyd
4* (C) 2013 Joel Low
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_FILTER_H_
10#define BOTAN_FILTER_H_
11
12#include <botan/secmem.h>
13#include <span>
14#include <string>
15#include <vector>
16
17namespace Botan {
18
19/**
20* This class represents general abstract filter objects.
21*/
23 public:
24 /**
25 * Return a descriptive name for this filter
26 */
27 virtual std::string name() const = 0;
28
29 /**
30 * Write a portion of a message to this filter.
31 *
32 * @param input the input as a byte array
33 * @param length the length of the byte array input
34 */
35 virtual void write(const uint8_t input[], size_t length) = 0;
36
37 /**
38 * Start a new message. Must be closed by end_msg() before another
39 * message can be started.
40 */
41 virtual void start_msg() {}
42
43 /**
44 * Notify that the current message is finished; flush buffers and
45 * do end-of-message processing (if any).
46 */
47 virtual void end_msg() {}
48
49 /**
50 * Check whether this filter is an attachable filter.
51 * @return true if this filter is attachable, false otherwise
52 */
53 virtual bool attachable() { return true; }
54
55 virtual ~Filter() = default;
56
57 Filter(const Filter&) = delete;
58 Filter(Filter&&) = delete;
59 Filter& operator=(const Filter&) = delete;
60 Filter& operator=(Filter&&) = delete;
61
62 protected:
63 /**
64 * Send some bytes to the next filter in the chain
65 *
66 * @param in some input for the filter
67 * @param length the length of in
68 */
69 virtual void send(const uint8_t in[], size_t length);
70
71 /**
72 * Send some bytes to the next filter in the chain
73 *
74 * @param in some input for the filter
75 */
76 void send(uint8_t in) { send(&in, 1); }
77
78 /**
79 * Send some bytes to the next filter in the chain
80 *
81 * @param in some input for the filter
82 */
83 void send(std::span<const uint8_t> in) { send(in.data(), in.size()); }
84
85 /**
86 * Send some bytes to the next filter in the chain
87 *
88 * @param in some input for the filter
89 * @param length the number of bytes of in to send
90 *
91 * This previously took a std::vector, for which the length field (allowing
92 * using just a prefix of the vector) somewhat made sense. It makes less
93 * sense now that we are using a span here; you can just use `first` to get
94 * a prefix.
95 */
96 void send(std::span<const uint8_t> in, size_t length);
97
98 /**
99 * Default constructor
100 */
101 Filter();
102
103 private:
104 /**
105 * Start a new message in *this and all following filters. Only for
106 * internal use, not intended for use in client applications.
107 */
108 void new_msg();
109
110 /**
111 * End a new message in *this and all following filters. Only for
112 * internal use, not intended for use in client applications.
113 */
114 void finish_msg();
115
116 friend class Pipe;
117 friend class Fanout_Filter;
118 friend class Threaded_Fork;
119
120 size_t total_ports() const;
121
122 size_t current_port() const { return m_port_num; }
123
124 /**
125 * Set the active port
126 * @param new_port the new value
127 */
128 void set_port(size_t new_port);
129
130 size_t owns() const { return m_filter_owns; }
131
132 /**
133 * Attach another filter to this one
134 * @param f filter to attach
135 */
136 void attach(Filter* f);
137
138 /**
139 * @param filters the filters to set
140 * @param count number of items in filters
141 */
142 void set_next(Filter* filters[], size_t count);
143 Filter* get_next() const;
144
145 secure_vector<uint8_t> m_write_queue;
146 std::vector<Filter*> m_next; // not owned
147 size_t m_port_num = 0;
148 size_t m_filter_owns = 0;
149
150 // true if filter belongs to a pipe --> prohibit filter sharing!
151 bool m_owned = false;
152};
153
154/**
155* This is the abstract Fanout_Filter base class.
156**/
158 protected:
159 /**
160 * Increment the number of filters past us that we own
161 */
162 void incr_owns() { ++m_filter_owns; }
163
164 /**
165 * Select which of the attached filters subsequent output is sent to
166 * @param n the index of the port to select
167 */
168 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
169 void set_port(size_t n) { Filter::set_port(n); }
170
171 /**
172 * Set the filters which follow this one
173 * @param f the filters to attach
174 * @param n the number of filters in f
175 */
176 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
177 void set_next(Filter* f[], size_t n) { Filter::set_next(f, n); }
178
179 /**
180 * Attach a filter to the end of this filter chain
181 * @param f the filter to attach
182 */
183 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
184 void attach(Filter* f) { Filter::attach(f); }
185};
186
187/**
188* The type of checking to be performed by decoders:
189* NONE - no checks, IGNORE_WS - perform checks, but ignore
190* whitespaces, FULL_CHECK - perform checks, also complain
191* about white spaces.
192*/
193enum Decoder_Checking : uint8_t /* NOLINT(*-use-enum-class) */ { NONE, IGNORE_WS, FULL_CHECK };
194
195} // namespace Botan
196
197#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
void set_next(Filter *f[], size_t n)
Definition filter.h:177
void set_port(size_t n)
Definition filter.h:169
void attach(Filter *f)
Definition filter.h:184
Filter(Filter &&)=delete
Filter(const Filter &)=delete
virtual void send(const uint8_t in[], size_t length)
Definition filter.cpp:30
virtual ~Filter()=default
virtual void end_msg()
Definition filter.h:47
virtual std::string name() const =0
virtual bool attachable()
Definition filter.h:53
void send(uint8_t in)
Definition filter.h:76
friend class Fanout_Filter
Definition filter.h:117
Filter & operator=(Filter &&)=delete
void send(std::span< const uint8_t > in)
Definition filter.h:83
virtual void write(const uint8_t input[], size_t length)=0
friend class Threaded_Fork
Definition filter.h:118
Filter & operator=(const Filter &)=delete
virtual void start_msg()
Definition filter.h:41
friend class Pipe
Definition filter.h:116
Decoder_Checking
Definition filter.h:193
@ FULL_CHECK
Definition filter.h:193
@ NONE
Definition filter.h:193
@ IGNORE_WS
Definition filter.h:193