Botan  1.11.15
src/lib/filters/filter.h
Go to the documentation of this file.
00001 /*
00002 * Filter
00003 * (C) 1999-2007 Jack Lloyd
00004 * (C) 2013 Joel Low
00005 *
00006 * Botan is released under the Simplified BSD License (see license.txt)
00007 */
00008 
00009 #ifndef BOTAN_FILTER_H__
00010 #define BOTAN_FILTER_H__
00011 
00012 #include <botan/secmem.h>
00013 #include <vector>
00014 #include <string>
00015 
00016 namespace Botan {
00017 
00018 /**
00019 * This class represents general abstract filter objects.
00020 */
00021 class BOTAN_DLL Filter
00022    {
00023    public:
00024       /**
00025       * @return descriptive name for this filter
00026       */
00027       virtual std::string name() const = 0;
00028 
00029       /**
00030       * Write a portion of a message to this filter.
00031       * @param input the input as a byte array
00032       * @param length the length of the byte array input
00033       */
00034       virtual void write(const byte input[], size_t length) = 0;
00035 
00036       /**
00037       * Start a new message. Must be closed by end_msg() before another
00038       * message can be started.
00039       */
00040       virtual void start_msg() {}
00041 
00042       /**
00043       * Notify that the current message is finished; flush buffers and
00044       * do end-of-message processing (if any).
00045       */
00046       virtual void end_msg() {}
00047 
00048       /**
00049       * Check whether this filter is an attachable filter.
00050       * @return true if this filter is attachable, false otherwise
00051       */
00052       virtual bool attachable() { return true; }
00053 
00054       virtual ~Filter() {}
00055    protected:
00056       /**
00057       * @param in some input for the filter
00058       * @param length the length of in
00059       */
00060       virtual void send(const byte in[], size_t length);
00061 
00062       /**
00063       * @param in some input for the filter
00064       */
00065       void send(byte in) { send(&in, 1); }
00066 
00067       /**
00068       * @param in some input for the filter
00069       */
00070       void send(const secure_vector<byte>& in) { send(&in[0], in.size()); }
00071 
00072       /**
00073       * @param in some input for the filter
00074       */
00075       void send(const std::vector<byte>& in) { send(&in[0], in.size()); }
00076 
00077       /**
00078       * @param in some input for the filter
00079       * @param length the number of bytes of in to send
00080       */
00081       void send(const secure_vector<byte>& in, size_t length)
00082          {
00083          send(&in[0], length);
00084          }
00085 
00086       /**
00087       * @param in some input for the filter
00088       * @param length the number of bytes of in to send
00089       */
00090       void send(const std::vector<byte>& in, size_t length)
00091          {
00092          send(&in[0], length);
00093          }
00094 
00095       Filter();
00096 
00097       Filter(const Filter&) = delete;
00098 
00099       Filter& operator=(const Filter&) = delete;
00100 
00101    private:
00102       /**
00103       * Start a new message in *this and all following filters. Only for
00104       * internal use, not intended for use in client applications.
00105       */
00106       void new_msg();
00107 
00108       /**
00109       * End a new message in *this and all following filters. Only for
00110       * internal use, not intended for use in client applications.
00111       */
00112       void finish_msg();
00113 
00114       friend class Pipe;
00115       friend class Fanout_Filter;
00116 
00117       size_t total_ports() const;
00118       size_t current_port() const { return port_num; }
00119 
00120       /**
00121       * Set the active port
00122       * @param new_port the new value
00123       */
00124       void set_port(size_t new_port);
00125 
00126       size_t owns() const { return filter_owns; }
00127 
00128       /**
00129       * Attach another filter to this one
00130       * @param f filter to attach
00131       */
00132       void attach(Filter* f);
00133 
00134       /**
00135       * @param filters the filters to set
00136       * @param count number of items in filters
00137       */
00138       void set_next(Filter* filters[], size_t count);
00139       Filter* get_next() const;
00140 
00141       secure_vector<byte> write_queue;
00142       std::vector<Filter*> next;
00143       size_t port_num, filter_owns;
00144 
00145       // true if filter belongs to a pipe --> prohibit filter sharing!
00146       bool owned;
00147    };
00148 
00149 /**
00150 * This is the abstract Fanout_Filter base class.
00151 **/
00152 class BOTAN_DLL Fanout_Filter : public Filter
00153    {
00154    protected:
00155       /**
00156       * Increment the number of filters past us that we own
00157       */
00158       void incr_owns() { ++filter_owns; }
00159 
00160       void set_port(size_t n) { Filter::set_port(n); }
00161 
00162       void set_next(Filter* f[], size_t n) { Filter::set_next(f, n); }
00163 
00164       void attach(Filter* f) { Filter::attach(f); }
00165 
00166    private:
00167       friend class Threaded_Fork;
00168       using Filter::write_queue;
00169       using Filter::total_ports;
00170       using Filter::next;
00171    };
00172 
00173 /**
00174 * The type of checking to be performed by decoders:
00175 * NONE - no checks, IGNORE_WS - perform checks, but ignore
00176 * whitespaces, FULL_CHECK - perform checks, also complain
00177 * about white spaces.
00178 */
00179 enum Decoder_Checking { NONE, IGNORE_WS, FULL_CHECK };
00180 
00181 }
00182 
00183 #endif