Crypto++  5.6.5
Free C++ class library of cryptographic schemes
files.h
Go to the documentation of this file.
1 // files.h - 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(NULL), m_space(NULL), 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(NULL), m_space(NULL), 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(NULL), m_space(NULL), 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  lword MaxRetrievable() const;
63  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
64  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
65  lword Skip(lword skipMax=ULONG_MAX);
66 
67 private:
68  void StoreInitialize(const NameValuePairs &parameters);
69 
71  std::istream *m_stream;
72  byte *m_space;
73  size_t m_len;
74  bool m_waiting;
75 };
76 
77 //! \class FileSource
78 //! \brief Implementation of Store interface
79 //! \details file-based implementation of Store interface
80 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
81 {
82 public:
83  typedef FileStore::Err Err;
86 
87  //! \brief Construct a FileSource
88  FileSource(BufferedTransformation *attachment = NULL)
89  : SourceTemplate<FileStore>(attachment) {}
90 
91  //! \brief Construct a FileSource
92  //! \param in an existing stream
93  //! \param pumpAll flag indicating if source data should be pumped to its attached transformation
94  //! \param attachment an optional attached transformation
95  FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
96  : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
97 
98  //! \brief Construct a FileSource
99  //! \param filename the narrow name of the file to open
100  //! \param pumpAll flag indicating if source data should be pumped to its attached transformation
101  //! \param attachment an optional attached transformation
102  //! \param binary flag indicating if the file is binary
103  FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
104  : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
105 
106 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
107  //! \brief Construct a FileSource
108  //! \param filename the Unicode name of the file to open
109  //! \param pumpAll flag indicating if source data should be pumped to its attached transformation
110  //! \param attachment an optional attached transformation
111  //! \param binary flag indicating if the file is binary
112  //! \details On non-Windows OS, this function assumes that setlocale() has been called.
113  FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
114  : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
115 #endif
116 
117  //! \brief Retrieves the internal stream
118  //! \returns the internal stream pointer
119  std::istream* GetStream() {return m_store.GetStream();}
120 };
121 
122 //! \class FileSink
123 //! \brief Implementation of Store interface
124 //! \details file-based implementation of Sink interface
125 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
126 {
127 public:
128  //! \brief Exception thrown when file-based error is encountered
129  class Err : public Exception
130  {
131  public:
132  Err(const std::string &s) : Exception(IO_ERROR, s) {}
133  };
134  //! \brief Exception thrown when file-based open error is encountered
135  class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
136  //! \brief Exception thrown when file-based write error is encountered
137  class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
138 
139  //! \brief Construct a FileSink
140  FileSink() : m_stream(NULL) {}
141 
142  //! \brief Construct a FileSink
143  //! \param out an existing stream
144  FileSink(std::ostream &out)
146 
147  //! \brief Construct a FileSink
148  //! \param filename the narrow name of the file to open
149  //! \param binary flag indicating if the file is binary
150  FileSink(const char *filename, bool binary=true)
152 
153 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
154  //! \brief Construct a FileSink
155  //! \param filename the Unicode name of the file to open
156  //! \details On non-Windows OS, this function assumes that setlocale() has been called.
157  FileSink(const wchar_t *filename, bool binary=true)
159 #endif
160 
161  //! \brief Retrieves the internal stream
162  //! \returns the internal stream pointer
163  std::ostream* GetStream() {return m_stream;}
164 
165  void IsolatedInitialize(const NameValuePairs &parameters);
166  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
167  bool IsolatedFlush(bool hardFlush, bool blocking);
168 
169 private:
171  std::ostream *m_stream;
172 };
173 
174 NAMESPACE_END
175 
176 #endif
Create a working space in a BufferedTransformation.
Definition: filters.h:162
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
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
virtual size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)=0
Input multiple bytes for processing.
virtual void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: cryptlib.h:1516
Implementation of Store interface.
Definition: files.h:80
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:299
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)
Construct a FileSource.
Definition: files.h:103
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:1363
FileSink(std::ostream &out)
Construct a FileSink.
Definition: files.h:144
FileSink(const char *filename, bool binary=true)
Construct a FileSink.
Definition: files.h:150
FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)
Construct a FileSource.
Definition: files.h:113
FileSource(BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition: files.h:88
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:1291
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:59
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:563
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:137
FileStore(const char *filename)
Construct a FileStore.
Definition: files.h:47
Exception thrown when file-based error is encountered.
Definition: files.h:129
virtual bool IsolatedFlush(bool hardFlush, bool blocking)=0
Flushes data buffered by this object, without signal propagation.
Implementation of BufferedTransformation's attachment interface.
Implementation of Store interface.
Definition: files.h:23
std::istream * GetStream()
Retrieves the internal stream.
Definition: files.h:119
Exception thrown when file-based open error is encountered.
Definition: files.h:135
std::ostream * GetStream()
Retrieves the internal stream.
Definition: files.h:163
FileSink()
Construct a FileSink.
Definition: files.h:140
Implementation of BufferedTransformation's attachment interface.
Definition: simple.h:331
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition: files.h:95
Crypto++ library namespace.
const char * InputBinaryMode()
bool
Definition: argnames.h:61
Implementation of Store interface.
Definition: files.h:125
Exception thrown when file-based read error is encountered.
Definition: files.h:35
Ensures an object is not copyable.
Definition: misc.h:210
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:278
Exception thrown when file-based open error is encountered.
Definition: files.h:33