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
00012
00013 class FileStore : public Store, private FilterPutSpaceHelper
00014 {
00015 public:
00016 class Err : public Exception
00017 {
00018 public:
00019 Err(const std::string &s) : Exception(IO_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() : m_stream(NULL) {}
00025 FileStore(std::istream &in)
00026 {StoreInitialize(MakeParameters("InputStreamPointer", &in));}
00027 FileStore(const char *filename)
00028 {StoreInitialize(MakeParameters("InputFileName", filename));}
00029
00030 std::istream* GetStream() {return m_stream;}
00031
00032 unsigned long MaxRetrievable() const;
00033 unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00034 unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
00035
00036 private:
00037 void StoreInitialize(const NameValuePairs ¶meters);
00038
00039 std::ifstream m_file;
00040 std::istream *m_stream;
00041 byte *m_space;
00042 unsigned int m_len;
00043 bool m_waiting;
00044 };
00045
00046
00047 class FileSource : public SourceTemplate<FileStore>
00048 {
00049 public:
00050 typedef FileStore::Err Err;
00051 typedef FileStore::OpenErr OpenErr;
00052 typedef FileStore::ReadErr ReadErr;
00053
00054 FileSource(BufferedTransformation *attachment = NULL)
00055 : SourceTemplate<FileStore>(attachment) {}
00056 FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
00057 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputStreamPointer", &in));}
00058 FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
00059 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputFileName", filename)("InputBinaryMode", binary));}
00060
00061 std::istream* GetStream() {return m_store.GetStream();}
00062 };
00063
00064
00065 class FileSink : public Sink
00066 {
00067 public:
00068 class Err : public Exception
00069 {
00070 public:
00071 Err(const std::string &s) : Exception(IO_ERROR, s) {}
00072 };
00073 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
00074 class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
00075
00076 FileSink() : m_stream(NULL) {}
00077 FileSink(std::ostream &out)
00078 {IsolatedInitialize(MakeParameters("OutputStreamPointer", &out));}
00079 FileSink(const char *filename, bool binary=true)
00080 {IsolatedInitialize(MakeParameters("OutputFileName", filename)("OutputBinaryMode", binary));}
00081
00082 std::ostream* GetStream() {return m_stream;}
00083
00084 void IsolatedInitialize(const NameValuePairs ¶meters);
00085 unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking);
00086 bool IsolatedFlush(bool hardFlush, bool blocking);
00087
00088 private:
00089 std::ofstream m_file;
00090 std::ostream *m_stream;
00091 };
00092
00093 NAMESPACE_END
00094
00095 #endif