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