Crypto++ 7.0 Free C++ class library of cryptographic schemes

files.h
Go to the documentation of this file.
1 // files.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file files.h
4 /// \brief Classes providing file-based library services
5 /// \since Crypto++ 1.0
6
7 #ifndef CRYPTOPP_FILES_H
8 #define CRYPTOPP_FILES_H
9
10 #include "cryptlib.h"
11 #include "filters.h"
12 #include "argnames.h"
13 #include "smartptr.h"
14
15 #include <iostream>
16 #include <fstream>
17
18 NAMESPACE_BEGIN(CryptoPP)
19
20 /// \brief Implementation of Store interface
21 /// \details file-based implementation of Store interface
22 class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
23 {
24 public:
25 /// \brief Exception thrown when file-based error is encountered
26 class Err : public Exception
27 {
28 public:
29 Err(const std::string &s) : Exception(IO_ERROR, s) {}
30 };
31 /// \brief Exception thrown when file-based open error is encountered
32 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
33 /// \brief Exception thrown when file-based read error is encountered
34 class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
35
36 /// \brief Construct a FileStore
37 FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
38
39 /// \brief Construct a FileStore
40 /// \param in an existing stream
41 FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
42 {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
43
44 /// \brief Construct a FileStore
45 /// \param filename the narrow name of the file to open
46 FileStore(const char *filename) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
47 {StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
48
49 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
50 /// \brief Construct a FileStore
51 /// \param filename the Unicode name of the file to open
52 /// \details On non-Windows OS, this function assumes that setlocale() has been called.
53 FileStore(const wchar_t *filename)
54 {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
55 #endif
56
57 /// \brief Retrieves the internal stream
58 /// \returns the internal stream pointer
59 std::istream* GetStream() {return m_stream;}
60
61 /// \brief Retrieves the internal stream
62 /// \returns the internal stream pointer
63 const std::istream* GetStream() const {return m_stream;}
64
65 lword MaxRetrievable() const;
66 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
67 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
68 lword Skip(lword skipMax=ULONG_MAX);
69
70 private:
71 void StoreInitialize(const NameValuePairs &parameters);
72
73 member_ptr<std::ifstream> m_file;
74 std::istream *m_stream;
75 byte *m_space;
76 size_t m_len;
77 bool m_waiting;
78 };
79
80 /// \brief Implementation of Store interface
81 /// \details file-based implementation of Store interface
82 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
83 {
84 public:
85 typedef FileStore::Err Err;
86 typedef FileStore::OpenErr OpenErr;
87 typedef FileStore::ReadErr ReadErr;
88
89 /// \brief Construct a FileSource
90 FileSource(BufferedTransformation *attachment = NULLPTR)
91 : SourceTemplate<FileStore>(attachment) {}
92
93 /// \brief Construct a FileSource
94 /// \param in an existing stream
95 /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
96 /// \param attachment an optional attached transformation
97 FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
98 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
99
100 /// \brief Construct a FileSource
101 /// \param filename the narrow name of the file to open
102 /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
103 /// \param attachment an optional attached transformation
104 /// \param binary flag indicating if the file is binary
105 FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
106 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
107
108 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
109 /// \brief Construct a FileSource
110 /// \param filename the Unicode name of the file to open
111 /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
112 /// \param attachment an optional attached transformation
113 /// \param binary flag indicating if the file is binary
114 /// \details On non-Windows OS, this function assumes that setlocale() has been called.
115 FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
116 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
117 #endif
118
119 /// \brief Retrieves the internal stream
120 /// \returns the internal stream pointer
121 std::istream* GetStream() {return m_store.GetStream();}
122 };
123
124 /// \brief Implementation of Store interface
125 /// \details file-based implementation of Sink interface
126 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
127 {
128 public:
129 /// \brief Exception thrown when file-based error is encountered
130 class Err : public Exception
131 {
132 public:
133 Err(const std::string &s) : Exception(IO_ERROR, s) {}
134 };
135 /// \brief Exception thrown when file-based open error is encountered
136 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
137 /// \brief Exception thrown when file-based write error is encountered
138 class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
139
140 /// \brief Construct a FileSink
141 FileSink() : m_stream(NULLPTR) {}
142
143 /// \brief Construct a FileSink
144 /// \param out an existing stream
145 FileSink(std::ostream &out)
146 {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
147
148 /// \brief Construct a FileSink
149 /// \param filename the narrow name of the file to open
150 /// \param binary flag indicating if the file is binary
151 FileSink(const char *filename, bool binary=true)
152 {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
153
154 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
155 /// \brief Construct a FileSink
156 /// \param filename the Unicode name of the file to open
157 /// \details On non-Windows OS, this function assumes that setlocale() has been called.
158 FileSink(const wchar_t *filename, bool binary=true)
159 {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
160 #endif
161
162 /// \brief Retrieves the internal stream
163 /// \returns the internal stream pointer
164 std::ostream* GetStream() {return m_stream;}
165
166 void IsolatedInitialize(const NameValuePairs &parameters);
167 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
168 bool IsolatedFlush(bool hardFlush, bool blocking);
169
170 private:
171 member_ptr<std::ofstream> m_file;
172 std::ostream *m_stream;
173 };
174
175 NAMESPACE_END
176
177 #endif
FilterPutSpaceHelper
Create a working space in a BufferedTransformation.
Definition: filters.h:158
Exception
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:158
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
Name::InputFileNameWide
const char * InputFileNameWide()
const wchar_t *
Definition: argnames.h:59
FileStore::GetStream
std::istream * GetStream()
Retrieves the internal stream.
Definition: files.h:59
BufferedTransformation::Put2
virtual size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)=0
Input multiple bytes for processing.
BufferedTransformation::IsolatedInitialize
virtual void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: cryptlib.h:1721
FileSource
Implementation of Store interface.
Definition: files.h:82
FileStore::FileStore
FileStore()
Construct a FileStore.
Definition: files.h:37
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
smartptr.h
Classes for automatic resource management.
Name::OutputStreamPointer
const char * OutputStreamPointer()
std::ostream *
Definition: argnames.h:64
Store
Acts as a Source for pre-existing, static data.
Definition: simple.h:306
FileSource::FileSource
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)
Construct a FileSource.
Definition: files.h:105
FileStore::FileStore
FileStore(const wchar_t *filename)
Construct a FileStore.
Definition: files.h:53
Name::InputFileName
const char * InputFileName()
const char *
Definition: argnames.h:58
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1564
FileSink::FileSink
FileSink(std::ostream &out)
Construct a FileSink.
Definition: files.h:145
member_ptr< std::ifstream >
FileSink::FileSink
FileSink(const char *filename, bool binary=true)
Construct a FileSink.
Definition: files.h:151
FileSource::FileSource
FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)
Construct a FileSource.
Definition: files.h:115
FileSource::FileSource
FileSource(BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition: files.h:90
Name::InputStreamPointer
const char * InputStreamPointer()
std::istream *
Definition: argnames.h:60
Name::OutputBinaryMode
const char * OutputBinaryMode()
bool
Definition: argnames.h:65
SourceTemplate
Transform a Store into a Source.
Definition: filters.h:1362
DEFAULT_CHANNEL
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.h:482
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
Name::OutputFileName
const char * OutputFileName()
const char *
Definition: argnames.h:62
FileStore::FileStore
FileStore(std::istream &in)
Construct a FileStore.
Definition: files.h:41
FileSink::WriteErr
Exception thrown when file-based write error is encountered.
Definition: files.h:138
FileStore::FileStore
FileStore(const char *filename)
Construct a FileStore.
Definition: files.h:46
FileSink::Err
Exception thrown when file-based error is encountered.
Definition: files.h:130
BufferedTransformation::IsolatedFlush
virtual bool IsolatedFlush(bool hardFlush, bool blocking)=0
Flushes data buffered by this object, without signal propagation.
filters.h
Implementation of BufferedTransformation&#39;s attachment interface.
FileStore
Implementation of Store interface.
Definition: files.h:22
FileSource::GetStream
std::istream * GetStream()
Retrieves the internal stream.
Definition: files.h:121
FileSink::OpenErr
Exception thrown when file-based open error is encountered.
Definition: files.h:136
FileSink::GetStream
std::ostream * GetStream()
Retrieves the internal stream.
Definition: files.h:164
FileSink::FileSink
FileSink()
Construct a FileSink.
Definition: files.h:141
Sink
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: simple.h:337
FileSource::FileSource
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition: files.h:97
CryptoPP
Crypto++ library namespace.
Name::InputBinaryMode
const char * InputBinaryMode()
bool
Definition: argnames.h:61
FileSink
Implementation of Store interface.
Definition: files.h:126
FileStore::ReadErr
Exception thrown when file-based read error is encountered.
Definition: files.h:34
NotCopyable
Ensures an object is not copyable.
Definition: misc.h:228
FileStore::GetStream
const std::istream * GetStream() const
Retrieves the internal stream.
Definition: files.h:63
FileStore::Err
Exception thrown when file-based error is encountered.
Definition: files.h:26
Name::OutputFileNameWide
const char * OutputFileNameWide()
const wchar_t *
Definition: argnames.h:63
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293
FileStore::OpenErr
Exception thrown when file-based open error is encountered.
Definition: files.h:32