Botan  2.7.0
Crypto and TLS for C++11
basefilt.h
Go to the documentation of this file.
1 /*
2 * Basic Filters
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_BASEFILT_H_
10 #define BOTAN_BASEFILT_H_
11 
12 #include <botan/filter.h>
13 
14 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
15  #include <thread>
16 #endif
17 
18 namespace Botan {
19 
20 /**
21 * BitBucket is a filter which simply discards all inputs
22 */
23 class BOTAN_PUBLIC_API(2,0) BitBucket final : public Filter
24  {
25  public:
26  void write(const uint8_t[], size_t) override { /* discard */ }
27 
28  std::string name() const override { return "BitBucket"; }
29  };
30 
31 /**
32 * This class represents Filter chains. A Filter chain is an ordered
33 * concatenation of Filters, the input to a Chain sequentially passes
34 * through all the Filters contained in the Chain.
35 */
36 
37 class BOTAN_PUBLIC_API(2,0) Chain final : public Fanout_Filter
38  {
39  public:
40  void write(const uint8_t input[], size_t length) override { send(input, length); }
41 
42  std::string name() const override;
43 
44  /**
45  * Construct a chain of up to four filters. The filters are set
46  * up in the same order as the arguments.
47  */
48  Chain(Filter* = nullptr, Filter* = nullptr,
49  Filter* = nullptr, Filter* = nullptr);
50 
51  /**
52  * Construct a chain from range of filters
53  * @param filter_arr the list of filters
54  * @param length how many filters
55  */
56  Chain(Filter* filter_arr[], size_t length);
57  };
58 
59 /**
60 * This class represents a fork filter, whose purpose is to fork the
61 * flow of data. It causes an input message to result in n messages at
62 * the end of the filter, where n is the number of forks.
63 */
64 class BOTAN_PUBLIC_API(2,0) Fork : public Fanout_Filter
65  {
66  public:
67  void write(const uint8_t input[], size_t length) override { send(input, length); }
68  void set_port(size_t n) { Fanout_Filter::set_port(n); }
69 
70  std::string name() const override;
71 
72  /**
73  * Construct a Fork filter with up to four forks.
74  */
75  Fork(Filter*, Filter*, Filter* = nullptr, Filter* = nullptr);
76 
77  /**
78  * Construct a Fork from range of filters
79  * @param filter_arr the list of filters
80  * @param length how many filters
81  */
82  Fork(Filter* filter_arr[], size_t length);
83  };
84 
85 #if defined(BOTAN_HAS_THREAD_UTILS)
86 
87 /**
88 * This class is a threaded version of the Fork filter. While this uses
89 * threads, the class itself is NOT thread-safe. This is meant as a drop-
90 * in replacement for Fork where performance gains are possible.
91 */
92 class BOTAN_PUBLIC_API(2,0) Threaded_Fork final : public Fork
93  {
94  public:
95  std::string name() const override;
96 
97  /**
98  * Construct a Threaded_Fork filter with up to four forks.
99  */
100  Threaded_Fork(Filter*, Filter*, Filter* = nullptr, Filter* = nullptr);
101 
102  /**
103  * Construct a Threaded_Fork from range of filters
104  * @param filter_arr the list of filters
105  * @param length how many filters
106  */
107  Threaded_Fork(Filter* filter_arr[], size_t length);
108 
109  ~Threaded_Fork();
110 
111  private:
112  void set_next(Filter* f[], size_t n);
113  void send(const uint8_t in[], size_t length) override;
114  void thread_delegate_work(const uint8_t input[], size_t length);
115  void thread_entry(Filter* filter);
116 
117  std::vector<std::shared_ptr<std::thread>> m_threads;
118  std::unique_ptr<struct Threaded_Fork_Data> m_thread_data;
119  };
120 #endif
121 
122 }
123 
124 #endif
void set_port(size_t n)
Definition: filter.h:160
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
std::string name() const override
Definition: basefilt.h:28
void set_port(size_t n)
Definition: basefilt.h:68
Definition: alg_id.cpp:13
void write(const uint8_t input[], size_t length) override
Definition: basefilt.h:67
void write(const uint8_t input[], size_t length) override
Definition: basefilt.h:40
void write(const uint8_t[], size_t) override
Definition: basefilt.h:26