00001 #ifndef CRYPTOPP_FILES_H
00002 #define CRYPTOPP_FILES_H
00003
00004 #include "cryptlib.h"
00005 #include "filters.h"
00006
00007 #include <iostream>
00008 #include <fstream>
00009
00010 NAMESPACE_BEGIN(CryptoPP)
00011
00013 class FileStore : public Store
00014 {
00015 public:
00016 class Err : public BufferedTransformation::Err
00017 {
00018 public:
00019 Err(const std::string &s) : BufferedTransformation::Err(INPUT_ERROR, s) {}
00020 };
00021 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
00022 class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
00023
00024 FileStore(std::istream &in);
00025 FileStore(const char *filename);
00026
00027 std::istream& GetStream() {return m_in;}
00028
00029 unsigned long MaxRetrievable() const;
00030 unsigned int Peek(byte &outByte) const;
00031
00032 unsigned long TransferTo(BufferedTransformation &target, unsigned long transferMax=ULONG_MAX);
00033 unsigned long CopyTo(BufferedTransformation &target, unsigned long copyMax=ULONG_MAX) const;
00034
00035 private:
00036 std::ifstream m_file;
00037 std::istream &m_in;
00038 SecByteBlock m_buffer;
00039 };
00040
00042 class FileSource : public Source
00043 {
00044 public:
00045 typedef FileStore::Err Err;
00046 typedef FileStore::OpenErr OpenErr;
00047 typedef FileStore::ReadErr ReadErr;
00048
00049 FileSource(std::istream &in, bool pumpAll, BufferedTransformation *outQueue = NULL);
00050 FileSource(const char *filename, bool pumpAll, BufferedTransformation *outQueue = NULL);
00051
00052 std::istream& GetStream() {return m_store.GetStream();}
00053
00054 unsigned long Pump(unsigned long pumpMax=ULONG_MAX)
00055 {return m_store.TransferTo(*AttachedTransformation(), pumpMax);}
00056 unsigned int PumpMessages(unsigned int count=UINT_MAX)
00057 {return m_store.TransferMessagesTo(*AttachedTransformation(), count);}
00058
00059 private:
00060 FileStore m_store;
00061 };
00062
00064 class FileSink : public Sink
00065 {
00066 public:
00067 class Err : public BufferedTransformation::Err
00068 {
00069 public:
00070 Err(const std::string &s) : BufferedTransformation::Err(OUTPUT_ERROR, s) {}
00071 };
00072 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
00073 class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
00074
00075 FileSink(std::ostream &out);
00076 FileSink(const char *filename, bool binary=true);
00077
00078 std::ostream& GetStream() {return out;}
00079
00080 void Flush(bool=true, int=-1);
00081 void MessageEnd(int=-1);
00082 void Put(byte inByte)
00083 {
00084 out.put(inByte);
00085 if (!out.good())
00086 throw WriteErr();
00087 }
00088
00089 void Put(const byte *inString, unsigned int length);
00090
00091 private:
00092 std::ofstream file;
00093 std::ostream& out;
00094 };
00095
00096 NAMESPACE_END
00097
00098 #endif