Botan  1.11.15
src/lib/filters/data_src.h
Go to the documentation of this file.
00001 /*
00002 * DataSource
00003 * (C) 1999-2007 Jack Lloyd
00004 *     2012 Markus Wanner
00005 *
00006 * Botan is released under the Simplified BSD License (see license.txt)
00007 */
00008 
00009 #ifndef BOTAN_DATA_SRC_H__
00010 #define BOTAN_DATA_SRC_H__
00011 
00012 #include <botan/secmem.h>
00013 #include <string>
00014 #include <iosfwd>
00015 
00016 namespace Botan {
00017 
00018 /**
00019 * This class represents an abstract data source object.
00020 */
00021 class BOTAN_DLL DataSource
00022    {
00023    public:
00024       /**
00025       * Read from the source. Moves the internal offset so that every
00026       * call to read will return a new portion of the source.
00027       *
00028       * @param out the byte array to write the result to
00029       * @param length the length of the byte array out
00030       * @return length in bytes that was actually read and put
00031       * into out
00032       */
00033       virtual size_t read(byte out[], size_t length) = 0;
00034 
00035       /**
00036       * Read from the source but do not modify the internal
00037       * offset. Consecutive calls to peek() will return portions of
00038       * the source starting at the same position.
00039       *
00040       * @param out the byte array to write the output to
00041       * @param length the length of the byte array out
00042       * @param peek_offset the offset into the stream to read at
00043       * @return length in bytes that was actually read and put
00044       * into out
00045       */
00046       virtual size_t peek(byte out[], size_t length,
00047                           size_t peek_offset) const = 0;
00048 
00049       /**
00050       * Test whether the source still has data that can be read.
00051       * @return true if there is still data to read, false otherwise
00052       */
00053       virtual bool end_of_data() const = 0;
00054       /**
00055       * return the id of this data source
00056       * @return std::string representing the id of this data source
00057       */
00058       virtual std::string id() const { return ""; }
00059 
00060       /**
00061       * Read one byte.
00062       * @param out the byte to read to
00063       * @return length in bytes that was actually read and put
00064       * into out
00065       */
00066       size_t read_byte(byte& out);
00067 
00068       /**
00069       * Peek at one byte.
00070       * @param out an output byte
00071       * @return length in bytes that was actually read and put
00072       * into out
00073       */
00074       size_t peek_byte(byte& out) const;
00075 
00076       /**
00077       * Discard the next N bytes of the data
00078       * @param N the number of bytes to discard
00079       * @return number of bytes actually discarded
00080       */
00081       size_t discard_next(size_t N);
00082 
00083       /**
00084       * @return number of bytes read so far.
00085       */
00086       virtual size_t get_bytes_read() const = 0;
00087 
00088       DataSource() {}
00089       virtual ~DataSource() {}
00090       DataSource& operator=(const DataSource&) = delete;
00091       DataSource(const DataSource&) = delete;
00092    };
00093 
00094 /**
00095 * This class represents a Memory-Based DataSource
00096 */
00097 class BOTAN_DLL DataSource_Memory : public DataSource
00098    {
00099    public:
00100       size_t read(byte[], size_t);
00101       size_t peek(byte[], size_t, size_t) const;
00102       bool end_of_data() const;
00103 
00104       /**
00105       * Construct a memory source that reads from a string
00106       * @param in the string to read from
00107       */
00108       DataSource_Memory(const std::string& in);
00109 
00110       /**
00111       * Construct a memory source that reads from a byte array
00112       * @param in the byte array to read from
00113       * @param length the length of the byte array
00114       */
00115       DataSource_Memory(const byte in[], size_t length) :
00116          source(in, in + length), offset(0) {}
00117 
00118       /**
00119       * Construct a memory source that reads from a secure_vector
00120       * @param in the MemoryRegion to read from
00121       */
00122       DataSource_Memory(const secure_vector<byte>& in) :
00123          source(in), offset(0) {}
00124 
00125       /**
00126       * Construct a memory source that reads from a std::vector
00127       * @param in the MemoryRegion to read from
00128       */
00129       DataSource_Memory(const std::vector<byte>& in) :
00130          source(in.begin(), in.end()), offset(0) {}
00131 
00132       virtual size_t get_bytes_read() const { return offset; }
00133    private:
00134       secure_vector<byte> source;
00135       size_t offset;
00136    };
00137 
00138 /**
00139 * This class represents a Stream-Based DataSource.
00140 */
00141 class BOTAN_DLL DataSource_Stream : public DataSource
00142    {
00143    public:
00144       size_t read(byte[], size_t);
00145       size_t peek(byte[], size_t, size_t) const;
00146       bool end_of_data() const;
00147       std::string id() const;
00148 
00149       DataSource_Stream(std::istream&,
00150                         const std::string& id = "<std::istream>");
00151 
00152       /**
00153       * Construct a Stream-Based DataSource from file
00154       * @param file the name of the file
00155       * @param use_binary whether to treat the file as binary or not
00156       */
00157       DataSource_Stream(const std::string& file, bool use_binary = false);
00158 
00159       DataSource_Stream(const DataSource_Stream&) = delete;
00160 
00161       DataSource_Stream& operator=(const DataSource_Stream&) = delete;
00162 
00163       ~DataSource_Stream();
00164 
00165       virtual size_t get_bytes_read() const { return total_read; }
00166    private:
00167       const std::string identifier;
00168 
00169       std::istream* source_p;
00170       std::istream& source;
00171       size_t total_read;
00172    };
00173 
00174 }
00175 
00176 #endif