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

files.cpp
1 // files.cpp - originally written and placed in the public domain by Wei Dai
2
3 #include "pch.h"
4
5 #ifndef CRYPTOPP_IMPORTS
6
7 #include "files.h"
8
9 #include <iostream>
10 #include <fstream>
11 #include <limits>
12
13 ANONYMOUS_NAMESPACE_BEGIN
14
15 /// \brief Disable badbit, failbit and eof exceptions
16 /// \sa https://github.com/weidai11/cryptopp/pull/968 and
17 /// https://www.cplusplus.com/reference/ios/ios/exceptions
18 class IosExceptionMask
19 {
20 public:
21 IosExceptionMask(std::istream& stream) : m_stream(stream) {
22 m_mask = m_stream.exceptions();
23 m_stream.exceptions(static_cast<std::ios::iostate>(0));
24 }
25
26 IosExceptionMask(std::istream& stream, std::ios::iostate newMask) : m_stream(stream) {
27 m_mask = m_stream.exceptions();
28 m_stream.exceptions(newMask);
29 }
30
31 ~IosExceptionMask() {
32 m_stream.exceptions(m_mask);
33 }
34
35 private:
36 std::istream& m_stream;
37 std::ios::iostate m_mask;
38 };
39
40 ANONYMOUS_NAMESPACE_END
41
42 NAMESPACE_BEGIN(CryptoPP)
43
44 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
45 void Files_TestInstantiations()
46 {
47 FileStore f0;
48 FileSource f1;
49 FileSink f2;
50 }
51 #endif
52
53 void FileStore::StoreInitialize(const NameValuePairs &parameters)
54 {
55 m_waiting = false;
56 m_stream = NULLPTR;
57 m_file.release();
58
59 const char *fileName = NULLPTR;
60 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
61 const wchar_t *fileNameWide = NULLPTR;
62 if (!parameters.GetValue(Name::InputFileNameWide(), fileNameWide))
63 #endif
64 if (!parameters.GetValue(Name::InputFileName(), fileName))
65 {
66 parameters.GetValue(Name::InputStreamPointer(), m_stream);
67 return;
68 }
69
70 std::ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? std::ios::binary : std::ios::openmode(0);
71 m_file.reset(new std::ifstream);
72 #ifdef CRYPTOPP_UNIX_AVAILABLE
73 std::string narrowed;
74 if (fileNameWide)
75 fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
76 #endif
77 #if _MSC_VER >= 1400
78 if (fileNameWide)
79 {
80 m_file->open(fileNameWide, std::ios::in | binary);
81 if (!*m_file)
82 throw OpenErr(StringNarrow(fileNameWide, false));
83 }
84 #endif
85 if (fileName)
86 {
87 m_file->open(fileName, std::ios::in | binary);
88 if (!*m_file)
89 throw OpenErr(fileName);
90 }
91 m_stream = m_file.get();
92 }
93
94 lword FileStore::MaxRetrievable() const
95 {
96 if (!m_stream)
97 return 0;
98
99 // Disable badbit, failbit and eof exceptions
100 IosExceptionMask guard(*m_stream);
101
102 // Clear error bits due to seekg(). Also see
103 // https://github.com/weidai11/cryptopp/pull/968
104 std::streampos current = m_stream->tellg();
105 std::streampos end = m_stream->seekg(0, std::ios::end).tellg();
106 m_stream->clear();
107 m_stream->seekg(current);
108 m_stream->clear();
109
110 // Return max for a non-seekable stream
111 // https://www.cplusplus.com/reference/istream/istream/tellg
112 if (end == static_cast<std::streampos>(-1))
113 return LWORD_MAX;
114
115 return end-current;
116 }
117
118 size_t FileStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
119 {
120 if (!m_stream)
121 {
122 transferBytes = 0;
123 return 0;
124 }
125
126 lword size=transferBytes;
127 transferBytes = 0;
128
129 if (m_waiting)
130 goto output;
131
132 size_t spaceSize, blockedBytes;
133 while (size && m_stream->good())
134 {
135 spaceSize = 1024;
136 m_space = HelpCreatePutSpace(target, channel, 1, UnsignedMin(size_t(SIZE_MAX), size), spaceSize);
137 m_stream->read((char *)m_space, (std::streamsize)STDMIN(size, (lword)spaceSize));
138 m_len = (size_t)m_stream->gcount();
139
140 output:
141 blockedBytes = target.ChannelPutModifiable2(channel, m_space, m_len, 0, blocking);
142 m_waiting = blockedBytes > 0;
143 if (m_waiting)
144 return blockedBytes;
145 size -= m_len;
146 transferBytes += m_len;
147 }
148
149 if (!m_stream->good() && !m_stream->eof())
150 throw ReadErr();
151
152 return 0;
153 }
154
155 size_t FileStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
156 {
157 if (!m_stream)
158 return 0;
159
160 if (begin == 0 && end == 1)
161 {
162 int result = m_stream->peek();
163 if (result == std::char_traits<char>::eof())
164 return 0;
165 else
166 {
167 size_t blockedBytes = target.ChannelPut(channel, byte(result), blocking);
168 begin += 1-blockedBytes;
169 return blockedBytes;
170 }
171 }
172
173 // TODO: figure out what happens on cin
174 std::streampos current = m_stream->tellg();
175 std::streampos endPosition = m_stream->seekg(0, std::ios::end).tellg();
176 std::streampos newPosition = current + static_cast<std::streamoff>(begin);
177
178 if (newPosition >= endPosition)
179 {
180 m_stream->seekg(current);
181 return 0; // don't try to seek beyond the end of file
182 }
183 m_stream->seekg(newPosition);
184 try
185 {
186 CRYPTOPP_ASSERT(!m_waiting);
187 lword copyMax = end-begin;
188 size_t blockedBytes = const_cast<FileStore *>(this)->TransferTo2(target, copyMax, channel, blocking);
189 begin += copyMax;
190 if (blockedBytes)
191 {
192 const_cast<FileStore *>(this)->m_waiting = false;
193 return blockedBytes;
194 }
195 }
196 catch(...)
197 {
198 m_stream->clear();
199 m_stream->seekg(current);
200 throw;
201 }
202 m_stream->clear();
203 m_stream->seekg(current);
204
205 return 0;
206 }
207
208 lword FileStore::Skip(lword skipMax)
209 {
210 if (!m_stream)
211 return 0;
212
213 lword oldPos = m_stream->tellg();
214 std::istream::off_type offset;
215 if (!SafeConvert(skipMax, offset))
216 throw InvalidArgument("FileStore: maximum seek offset exceeded");
217 m_stream->seekg(offset, std::ios::cur);
218 return (lword)m_stream->tellg() - oldPos;
219 }
220
221 void FileSink::IsolatedInitialize(const NameValuePairs &parameters)
222 {
223 m_stream = NULLPTR;
224 m_file.release();
225
226 const char *fileName = NULLPTR;
227 #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
228 const wchar_t *fileNameWide = NULLPTR;
229 if (!parameters.GetValue(Name::OutputFileNameWide(), fileNameWide))
230 #endif
231 if (!parameters.GetValue(Name::OutputFileName(), fileName))
232 {
233 parameters.GetValue(Name::OutputStreamPointer(), m_stream);
234 return;
235 }
236
237 std::ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? std::ios::binary : std::ios::openmode(0);
238 m_file.reset(new std::ofstream);
239 #ifdef CRYPTOPP_UNIX_AVAILABLE
240 std::string narrowed;
241 if (fileNameWide)
242 fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
243 #elif (CRYPTOPP_MSC_VERSION >= 1400)
244 if (fileNameWide)
245 {
246 m_file->open(fileNameWide, std::ios::out | std::ios::trunc | binary);
247 if (!*m_file)
248 throw OpenErr(StringNarrow(fileNameWide, false));
249 }
250 #endif
251 if (fileName)
252 {
253 m_file->open(fileName, std::ios::out | std::ios::trunc | binary);
254 if (!*m_file)
255 throw OpenErr(fileName);
256 }
257 m_stream = m_file.get();
258 }
259
260 bool FileSink::IsolatedFlush(bool hardFlush, bool blocking)
261 {
262 CRYPTOPP_UNUSED(hardFlush), CRYPTOPP_UNUSED(blocking);
263 if (!m_stream)
264 throw Err("FileSink: output stream not opened");
265
266 m_stream->flush();
267 if (!m_stream->good())
268 throw WriteErr();
269
270 return false;
271 }
272
273 size_t FileSink::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
274 {
275 CRYPTOPP_UNUSED(blocking);
276 if (!m_stream)
277 throw Err("FileSink: output stream not opened");
278
279 while (length > 0)
280 {
281 std::streamsize size;
282 if (!SafeConvert(length, size))
283 size = ((std::numeric_limits<std::streamsize>::max)());
284 m_stream->write((const char *)inString, size);
285 inString += size;
286 length -= (size_t)size;
287 }
288
289 if (messageEnd)
290 m_stream->flush();
291
292 if (!m_stream->good())
293 throw WriteErr();
294
295 return 0;
296 }
297
298 NAMESPACE_END
299
300 #endif
NameValuePairs::GetValueWithDefault
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:392
FileStore
Implementation of Store interface.
Definition: files.h:22
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1634
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
UnsignedMin
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:674
SIZE_MAX
#define SIZE_MAX
The maximum value of a machine word.
Definition: misc.h:116
SafeConvert
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
Definition: misc.h:690
pch.h
Precompiled header file.
FileSink
Implementation of Store interface.
Definition: files.h:130
FileSink::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
STDMIN
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:635
FilterPutSpaceHelper::HelpCreatePutSpace
byte * HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t desiredSize, size_t &bufferSize)
Create a working space in a BufferedTransformation.
Definition: filters.h:187
FileStore::Skip
lword Skip(lword skipMax=ULONG_MAX)
Discard skipMax bytes from the output buffer.
BufferedTransformation::ChannelPutModifiable2
virtual size_t ChannelPutModifiable2(const std::string &channel, byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee on a channel.
FileStore::CopyRangeTo2
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
lword
word64 lword
Large word type.
Definition: config_int.h:158
FileStore::MaxRetrievable
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:202
StringNarrow
std::string StringNarrow(const wchar_t *str, bool throwOnError=true)
Converts a wide character C-string to a multibyte string.
CryptoPP
Crypto++ library namespace.
NameValuePairs::GetValue
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:379
BufferedTransformation::ChannelPut
size_t ChannelPut(const std::string &channel, byte inByte, bool blocking=true)
Input a byte for processing on a channel.
Definition: cryptlib.h:2177
FileSource
Implementation of Store interface.
Definition: files.h:86
FileSink::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
files.h
Classes providing file-based library services.
FileSink::IsolatedFlush
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
LWORD_MAX
const lword LWORD_MAX
Large word type max value.
Definition: config_int.h:164
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321
FileStore::TransferTo2
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.