Crypto++  5.6.3
Free C++ class library of cryptographic schemes
files.h
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 &parameters);
46 
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;
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)
92  FileSink(const char *filename, bool binary=true)
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)
98 #endif
99 
100  std::ostream* GetStream() {return m_stream;}
101 
102  void IsolatedInitialize(const NameValuePairs &parameters);
103  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
104  bool IsolatedFlush(bool hardFlush, bool blocking);
105 
106 private:
108  std::ostream *m_stream;
109 };
110 
111 NAMESPACE_END
112 
113 #endif
Create a working space in a BufferedTransformation.
Definition: filters.h:162
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:139
Standard names for retrieving values by name when working with NameValuePairs.
const char * InputFileNameWide()
const wchar_t *
Definition: argnames.h:58
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:1505
file-based implementation of Source interface
Definition: files.h:55
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
const char * OutputStreamPointer()
std::ostream *
Definition: argnames.h:63
Acts as a Source for pre-existing, static data.
Definition: simple.h:244
const char * InputFileName()
const char *
Definition: argnames.h:57
Interface for buffered transformations.
Definition: cryptlib.h:1352
const char * InputStreamPointer()
std::istream *
Definition: argnames.h:59
const char * OutputBinaryMode()
bool
Definition: argnames.h:64
Transform a Store into a Source.
Definition: filters.h:1223
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:41
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
const char * OutputFileName()
const char *
Definition: argnames.h:61
virtual bool IsolatedFlush(bool hardFlush, bool blocking)=0
Flushes data buffered by this object, without signal propagation.
Implementation of BufferedTransformation's attachment interface.
file-based implementation of Store interface
Definition: files.h:15
Implementation of BufferedTransformation's attachment interface.
Definition: simple.h:276
Crypto++ library namespace.
const char * InputBinaryMode()
bool
Definition: argnames.h:60
file-based implementation of Sink interface
Definition: files.h:78
Ensures an object is not copyable.
Definition: misc.h:208
const char * OutputFileNameWide()
const wchar_t *
Definition: argnames.h:62
Interface for retrieving values given their names.
Definition: cryptlib.h:277