Security Scol plugin
files.h
Go to the documentation of this file.
1// files.h - originally written and placed in the public domain by Wei Dai
2
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
18NAMESPACE_BEGIN(CryptoPP)
19
20
22class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
23{
24public:
26 class Err : public Exception
27 {
28 public:
29 Err(const std::string &s) : Exception(IO_ERROR, s) {}
30 };
32 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
34 class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
35
37 FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
38
41 FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
42 {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
43
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
53 FileStore(const wchar_t *filename)
54 {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
55#endif
56
59 std::istream* GetStream() {return m_stream;}
60
63 const std::istream* GetStream() const {return m_stream;}
64
69 lword MaxRetrievable() const;
70 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
71 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
72 lword Skip(lword skipMax=ULONG_MAX);
73
74private:
75 void StoreInitialize(const NameValuePairs &parameters);
76
78 std::istream *m_stream;
79 byte *m_space;
80 size_t m_len;
81 bool m_waiting;
82};
83
86class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
87{
88public:
89 typedef FileStore::Err Err;
92
94 FileSource(BufferedTransformation *attachment = NULLPTR)
95 : SourceTemplate<FileStore>(attachment) {}
96
101 FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
102 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
103
109 FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
110 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
111
112#if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
119 FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
120 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
121#endif
122
125 std::istream* GetStream() {return m_store.GetStream();}
126};
127
130class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
131{
132public:
134 class Err : public Exception
135 {
136 public:
137 Err(const std::string &s) : Exception(IO_ERROR, s) {}
138 };
140 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
142 class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
143
145 FileSink() : m_stream(NULLPTR) {}
146
149 FileSink(std::ostream &out)
150 {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
151
155 FileSink(const char *filename, bool binary=true)
156 {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
157
158#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
162 FileSink(const wchar_t *filename, bool binary=true)
163 {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
164#endif
165
168 std::ostream* GetStream() {return m_stream;}
169
170 void IsolatedInitialize(const NameValuePairs &parameters);
171 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
172 bool IsolatedFlush(bool hardFlush, bool blocking);
173
174private:
176 std::ostream *m_stream;
177};
178
179NAMESPACE_END
180
181#endif
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition algparam.h:508
Standard names for retrieving values by name when working with NameValuePairs.
Interface for buffered transformations.
Definition cryptlib.h:1652
Base class for all exceptions thrown by the library.
Definition cryptlib.h:159
Exception thrown when file-based error is encountered.
Definition files.h:135
Exception thrown when file-based open error is encountered.
Definition files.h:140
Exception thrown when file-based write error is encountered.
Definition files.h:142
Implementation of Store interface.
Definition files.h:131
std::ostream * GetStream()
Retrieves the internal stream.
Definition files.h:168
FileSink()
Construct a FileSink.
Definition files.h:145
FileSink(const char *filename, bool binary=true)
Construct a FileSink.
Definition files.h:155
FileSink(std::ostream &out)
Construct a FileSink.
Definition files.h:149
Implementation of Store interface.
Definition files.h:87
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment=NULLPTR, bool binary=true)
Construct a FileSource.
Definition files.h:109
std::istream * GetStream()
Retrieves the internal stream.
Definition files.h:125
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment=NULLPTR)
Construct a FileSource.
Definition files.h:101
FileSource(BufferedTransformation *attachment=NULLPTR)
Construct a FileSource.
Definition files.h:94
Exception thrown when file-based error is encountered.
Definition files.h:27
Exception thrown when file-based open error is encountered.
Definition files.h:32
Exception thrown when file-based read error is encountered.
Definition files.h:34
Implementation of Store interface.
Definition files.h:23
FileStore(const char *filename)
Construct a FileStore.
Definition files.h:46
FileStore(std::istream &in)
Construct a FileStore.
Definition files.h:41
std::istream * GetStream()
Retrieves the internal stream.
Definition files.h:59
const std::istream * GetStream() const
Retrieves the internal stream.
Definition files.h:63
FileStore()
Construct a FileStore.
Definition files.h:37
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input a byte array for processing.
Definition simple.h:211
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition simple.h:222
Interface for retrieving values given their names.
Definition cryptlib.h:322
Ensures an object is not copyable.
Definition misc.h:239
Implementation of BufferedTransformation's attachment interface.
Definition simple.h:479
Transform a Store into a Source.
Definition filters.h:1429
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition filters.h:1437
Acts as a Source for pre-existing, static data.
Definition simple.h:448
Pointer that overloads operator ->
Definition smartptr.h:38
const lword LWORD_MAX
Large word type max value.
Definition config_int.h:164
word64 lword
Large word type.
Definition config_int.h:158
Abstract base classes that provide a uniform interface to this library.
Implementation of BufferedTransformation's attachment interface.
Classes for automatic resource management.
Create a working space in a BufferedTransformation.
Definition filters.h:171