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 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
00031
00032 FileStore(const wchar_t *filename)
00033 {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
00034 #endif
00035
00036 std::istream* GetStream() {return m_stream;}
00037
00038 lword MaxRetrievable() const;
00039 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
00040 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
00041 lword Skip(lword skipMax=ULONG_MAX);
00042
00043 private:
00044 void StoreInitialize(const NameValuePairs ¶meters);
00045
00046 member_ptr<std::ifstream> m_file;
00047 std::istream *m_stream;
00048 byte *m_space;
00049 size_t m_len;
00050 bool m_waiting;
00051 };
00052
00053
00054 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
00055 {
00056 public:
00057 typedef FileStore::Err Err;
00058 typedef FileStore::OpenErr OpenErr;
00059 typedef FileStore::ReadErr ReadErr;
00060
00061 FileSource(BufferedTransformation *attachment = NULL)
00062 : SourceTemplate<FileStore>(attachment) {}
00063 FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
00064 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
00065 FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
00066 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
00067 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
00068
00069 FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
00070 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
00071 #endif
00072
00073 std::istream* GetStream() {return m_store.GetStream();}
00074 };
00075
00076
00077 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
00078 {
00079 public:
00080 class Err : public Exception
00081 {
00082 public:
00083 Err(const std::string &s) : Exception(IO_ERROR, s) {}
00084 };
00085 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
00086 class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
00087
00088 FileSink() : m_stream(NULL) {}
00089 FileSink(std::ostream &out)
00090 {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
00091 FileSink(const char *filename, bool binary=true)
00092 {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
00093 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
00094
00095 FileSink(const wchar_t *filename, bool binary=true)
00096 {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
00097 #endif
00098
00099 std::ostream* GetStream() {return m_stream;}
00100
00101 void IsolatedInitialize(const NameValuePairs ¶meters);
00102 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
00103 bool IsolatedFlush(bool hardFlush, bool blocking);
00104
00105 private:
00106 member_ptr<std::ofstream> m_file;
00107 std::ostream *m_stream;
00108 };
00109
00110 NAMESPACE_END
00111
00112 #endif