1#ifndef CRYPTOPP_FILES_H
2#define CRYPTOPP_FILES_H
3
4#include "cryptlib.h"
5#include "filters.h"
6#include "argnames.h"
7
8#include <iostream>
9#include <fstream>
10
11NAMESPACE_BEGIN(CryptoPP)
12
13//! file-based implementation of Store interface
14class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
15{
16public:
17	class Err : public Exception
18	{
19	public:
20		Err(const std::string &s) : Exception(IO_ERROR, s) {}
21	};
22	class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
23	class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
24
25	FileStore() : m_stream(NULL) {}
26	FileStore(std::istream &in)
27		{StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
28	FileStore(const char *filename)
29		{StoreInitialize(MakeParameters(Name::InputFileName(), filename));}
30
31	std::istream* GetStream() {return m_stream;}
32
33	lword MaxRetrievable() const;
34	size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
35	size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
36	lword Skip(lword skipMax=ULONG_MAX);
37
38private:
39	void StoreInitialize(const NameValuePairs &parameters);
40
41	member_ptr<std::ifstream> m_file;
42	std::istream *m_stream;
43	byte *m_space;
44	size_t m_len;
45	bool m_waiting;
46};
47
48//! file-based implementation of Source interface
49class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
50{
51public:
52	typedef FileStore::Err Err;
53	typedef FileStore::OpenErr OpenErr;
54	typedef FileStore::ReadErr ReadErr;
55
56	FileSource(BufferedTransformation *attachment = NULL)
57		: SourceTemplate<FileStore>(attachment) {}
58	FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
59		: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
60	FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
61		: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
62
63	std::istream* GetStream() {return m_store.GetStream();}
64};
65
66//! file-based implementation of Sink interface
67class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
68{
69public:
70	class Err : public Exception
71	{
72	public:
73		Err(const std::string &s) : Exception(IO_ERROR, s) {}
74	};
75	class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
76	class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
77
78	FileSink() : m_stream(NULL) {}
79	FileSink(std::ostream &out)
80		{IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
81	FileSink(const char *filename, bool binary=true)
82		{IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)("OutputBinaryMode", binary));}
83
84	std::ostream* GetStream() {return m_stream;}
85
86	void IsolatedInitialize(const NameValuePairs &parameters);
87	size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
88	bool IsolatedFlush(bool hardFlush, bool blocking);
89
90private:
91	member_ptr<std::ofstream> m_file;
92	std::ostream *m_stream;
93};
94
95NAMESPACE_END
96
97#endif
98