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