00001 #ifndef CRYPTOPP_FILES_H
00002 #define CRYPTOPP_FILES_H
00003
00004 #include "cryptlib.h"
00005 #include "filters.h"
00006 #include "argnames.h"
00007
00008 #include <iostream>
00009 #include <fstream>
00010
00011 NAMESPACE_BEGIN(CryptoPP)
00012
00013
00014 class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
00015 {
00016 public:
00017 class Err : public Exception
00018 {
00019 public:
00020 Err(const std::string &s) : Exception(IO_ERROR, s) {}
00021 };
00022 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
00023 class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
00024
00025 FileStore() : m_stream(NULL) {}
00026 FileStore(std::istream &in)
00027 {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
00028 FileStore(const char *filename)
00029 {StoreInitialize(MakeParameters(Name::InputFileName(), filename));}
00030
00031 std::istream* GetStream() {return m_stream;}
00032
00033 lword MaxRetrievable() const;
00034 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00035 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
00036 lword Skip(lword skipMax=ULONG_MAX);
00037
00038 private:
00039 void StoreInitialize(const NameValuePairs ¶meters);
00040
00041 member_ptr<std::ifstream> m_file;
00042 std::istream *m_stream;
00043 byte *m_space;
00044 size_t m_len;
00045 bool m_waiting;
00046 };
00047
00048
00049 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
00050 {
00051 public:
00052 typedef FileStore::Err Err;
00053 typedef FileStore::OpenErr OpenErr;
00054 typedef FileStore::ReadErr ReadErr;
00055
00056 FileSource(BufferedTransformation *attachment = NULL)
00057 : SourceTemplate<FileStore>(attachment) {}
00058 FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
00059 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
00060 FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
00061 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
00062
00063 std::istream* GetStream() {return m_store.GetStream();}
00064 };
00065
00066
00067 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
00068 {
00069 public:
00070 class Err : public Exception
00071 {
00072 public:
00073 Err(const std::string &s) : Exception(IO_ERROR, s) {}
00074 };
00075 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
00076 class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
00077
00078 FileSink() : m_stream(NULL) {}
00079 FileSink(std::ostream &out)
00080 {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
00081 FileSink(const char *filename, bool binary=true)
00082 {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)("OutputBinaryMode", binary));}
00083
00084 std::ostream* GetStream() {return m_stream;}
00085
00086 void IsolatedInitialize(const NameValuePairs ¶meters);
00087 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
00088 bool IsolatedFlush(bool hardFlush, bool blocking);
00089
00090 private:
00091 member_ptr<std::ofstream> m_file;
00092 std::ostream *m_stream;
00093 };
00094
00095 NAMESPACE_END
00096
00097 #endif