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