1 /*
2 * Filter
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_FILTER_H__
9 #define BOTAN_FILTER_H__
10 
11 #include <botan/secmem.h>
12 #include <vector>
13 
14 namespace Botan {
15 
16 /**
17 * This class represents general abstract filter objects.
18 */
19 class BOTAN_DLL Filter
20    {
21    public:
22       /**
23       * @return descriptive name for this filter
24       */
25       virtual std::string name() const = 0;
26 
27       /**
28       * Write a portion of a message to this filter.
29       * @param input the input as a byte array
30       * @param length the length of the byte array input
31       */
32       virtual void write(const byte input[], size_t length) = 0;
33 
34       /**
35       * Start a new message. Must be closed by end_msg() before another
36       * message can be started.
37       */
start_msg()38       virtual void start_msg() {}
39 
40       /**
41       * Notify that the current message is finished; flush buffers and
42       * do end-of-message processing (if any).
43       */
end_msg()44       virtual void end_msg() {}
45 
46       /**
47       * Check whether this filter is an attachable filter.
48       * @return true if this filter is attachable, false otherwise
49       */
attachable()50       virtual bool attachable() { return true; }
51 
~Filter()52       virtual ~Filter() {}
53    protected:
54       /**
55       * @param in some input for the filter
56       * @param length the length of in
57       */
58       void send(const byte in[], size_t length);
59 
60       /**
61       * @param in some input for the filter
62       */
send(byte in)63       void send(byte in) { send(&in, 1); }
64 
65       /**
66       * @param in some input for the filter
67       */
send(const MemoryRegion<byte> & in)68       void send(const MemoryRegion<byte>& in) { send(&in[0], in.size()); }
69 
70       /**
71       * @param in some input for the filter
72       * @param length the number of bytes of in to send
73       */
send(const MemoryRegion<byte> & in,size_t length)74       void send(const MemoryRegion<byte>& in, size_t length)
75          {
76          send(&in[0], length);
77          }
78 
79       Filter();
80    private:
Filter(const Filter &)81       Filter(const Filter&) {}
82       Filter& operator=(const Filter&) { return (*this); }
83 
84       /**
85       * Start a new message in *this and all following filters. Only for
86       * internal use, not intended for use in client applications.
87       */
88       void new_msg();
89 
90       /**
91       * End a new message in *this and all following filters. Only for
92       * internal use, not intended for use in client applications.
93       */
94       void finish_msg();
95 
96       friend class Pipe;
97       friend class Fanout_Filter;
98 
99       size_t total_ports() const;
current_port()100       size_t current_port() const { return port_num; }
101 
102       /**
103       * Set the active port
104       * @param new_port the new value
105       */
106       void set_port(size_t new_port);
107 
owns()108       size_t owns() const { return filter_owns; }
109 
110       /**
111       * Attach another filter to this one
112       * @param f filter to attach
113       */
114       void attach(Filter* f);
115 
116       /**
117       * @param filters the filters to set
118       * @param count number of items in filters
119       */
120       void set_next(Filter* filters[], size_t count);
121       Filter* get_next() const;
122 
123       SecureVector<byte> write_queue;
124       std::vector<Filter*> next;
125       size_t port_num, filter_owns;
126 
127       // true if filter belongs to a pipe --> prohibit filter sharing!
128       bool owned;
129    };
130 
131 /**
132 * This is the abstract Fanout_Filter base class.
133 **/
134 class BOTAN_DLL Fanout_Filter : public Filter
135    {
136    protected:
137       /**
138       * Increment the number of filters past us that we own
139       */
incr_owns()140       void incr_owns() { ++filter_owns; }
141 
set_port(size_t n)142       void set_port(size_t n) { Filter::set_port(n); }
143 
set_next(Filter * f[],size_t n)144       void set_next(Filter* f[], size_t n) { Filter::set_next(f, n); }
145 
attach(Filter * f)146       void attach(Filter* f) { Filter::attach(f); }
147    };
148 
149 /**
150 * The type of checking to be performed by decoders:
151 * NONE - no checks, IGNORE_WS - perform checks, but ignore
152 * whitespaces, FULL_CHECK - perform checks, also complain
153 * about white spaces.
154 */
155 enum Decoder_Checking { NONE, IGNORE_WS, FULL_CHECK };
156 
157 }
158 
159 #endif
160