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