files.cpp
// files.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#ifndef CRYPTOPP_IMPORTS
#include "files.h"
#include <limits>
NAMESPACE_BEGIN(CryptoPP)
#if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
void Files_TestInstantiations()
{
FileStore f0;
FileSource f1;
FileSink f2;
}
#endif
void FileStore::StoreInitialize(const NameValuePairs &parameters)
{
m_waiting = false;
m_stream = NULL;
m_file.release();
const char *fileName = NULL;
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
const wchar_t *fileNameWide = NULL;
if (!parameters.GetValue(Name::InputFileNameWide(), fileNameWide))
#endif
if (!parameters.GetValue(Name::InputFileName(), fileName))
{
parameters.GetValue(Name::InputStreamPointer(), m_stream);
return;
}
std::ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? std::ios::binary : std::ios::openmode(0);
m_file.reset(new std::ifstream);
#ifdef CRYPTOPP_UNIX_AVAILABLE
std::string narrowed;
if (fileNameWide)
fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
#endif
#if _MSC_VER >= 1400
if (fileNameWide)
{
m_file->open(fileNameWide, std::ios::in | binary);
if (!*m_file)
throw OpenErr(StringNarrow(fileNameWide, false));
}
#endif
if (fileName)
{
m_file->open(fileName, std::ios::in | binary);
if (!*m_file)
throw OpenErr(fileName);
}
m_stream = m_file.get();
}
lword FileStore::MaxRetrievable() const
{
if (!m_stream)
return 0;
std::streampos current = m_stream->tellg();
std::streampos end = m_stream->seekg(0, std::ios::end).tellg();
m_stream->seekg(current);
return end-current;
}
size_t FileStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
{
if (!m_stream)
{
transferBytes = 0;
return 0;
}
lword size=transferBytes;
transferBytes = 0;
if (m_waiting)
goto output;
while (size && m_stream->good())
{
{
size_t spaceSize = 1024;
m_space = HelpCreatePutSpace(target, channel, 1, UnsignedMin(size_t(SIZE_MAX), size), spaceSize);
m_stream->read((char *)m_space, (unsigned int)STDMIN(size, (lword)spaceSize));
}
m_len = (size_t)m_stream->gcount();
size_t blockedBytes;
output:
blockedBytes = target.ChannelPutModifiable2(channel, m_space, m_len, 0, blocking);
m_waiting = blockedBytes > 0;
if (m_waiting)
return blockedBytes;
size -= m_len;
transferBytes += m_len;
}
if (!m_stream->good() && !m_stream->eof())
throw ReadErr();
return 0;
}
size_t FileStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
{
if (!m_stream)
return 0;
if (begin == 0 && end == 1)
{
int result = m_stream->peek();
if (result == std::char_traits<char>::eof())
return 0;
else
{
size_t blockedBytes = target.ChannelPut(channel, byte(result), blocking);
begin += 1-blockedBytes;
return blockedBytes;
}
}
// TODO: figure out what happens on cin
std::streampos current = m_stream->tellg();
std::streampos endPosition = m_stream->seekg(0, std::ios::end).tellg();
std::streampos newPosition = current + static_cast<std::streamoff>(begin);
if (newPosition >= endPosition)
{
m_stream->seekg(current);
return 0; // don't try to seek beyond the end of file
}
m_stream->seekg(newPosition);
try
{
assert(!m_waiting);
lword copyMax = end-begin;
size_t blockedBytes = const_cast<FileStore *>(this)->TransferTo2(target, copyMax, channel, blocking);
begin += copyMax;
if (blockedBytes)
{
const_cast<FileStore *>(this)->m_waiting = false;
return blockedBytes;
}
}
catch(...)
{
m_stream->clear();
m_stream->seekg(current);
throw;
}
m_stream->clear();
m_stream->seekg(current);
return 0;
}
lword FileStore::Skip(lword skipMax)
{
if (!m_stream)
return 0;
lword oldPos = m_stream->tellg();
std::istream::off_type offset;
if (!SafeConvert(skipMax, offset))
throw InvalidArgument("FileStore: maximum seek offset exceeded");
m_stream->seekg(offset, std::ios::cur);
return (lword)m_stream->tellg() - oldPos;
}
void FileSink::IsolatedInitialize(const NameValuePairs &parameters)
{
m_stream = NULL;
m_file.release();
const char *fileName = NULL;
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
const wchar_t *fileNameWide = NULL;
if (!parameters.GetValue(Name::OutputFileNameWide(), fileNameWide))
#endif
if (!parameters.GetValue(Name::OutputFileName(), fileName))
{
parameters.GetValue(Name::OutputStreamPointer(), m_stream);
return;
}
std::ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? std::ios::binary : std::ios::openmode(0);
m_file.reset(new std::ofstream);
#ifdef CRYPTOPP_UNIX_AVAILABLE
std::string narrowed;
if (fileNameWide)
fileName = (narrowed = StringNarrow(fileNameWide)).c_str();
#elif (CRYPTOPP_MSC_VERSION >= 1400)
if (fileNameWide)
{
m_file->open(fileNameWide, std::ios::out | std::ios::trunc | binary);
if (!*m_file)
throw OpenErr(StringNarrow(fileNameWide, false));
}
#endif
if (fileName)
{
m_file->open(fileName, std::ios::out | std::ios::trunc | binary);
if (!*m_file)
throw OpenErr(fileName);
}
m_stream = m_file.get();
}
bool FileSink::IsolatedFlush(bool hardFlush, bool blocking)
{
CRYPTOPP_UNUSED(hardFlush), CRYPTOPP_UNUSED(blocking);
if (!m_stream)
throw Err("FileSink: output stream not opened");
m_stream->flush();
if (!m_stream->good())
throw WriteErr();
return false;
}
size_t FileSink::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
CRYPTOPP_UNUSED(blocking);
if (!m_stream)
throw Err("FileSink: output stream not opened");
while (length > 0)
{
std::streamsize size;
if (!SafeConvert(length, size))
size = ((std::numeric_limits<std::streamsize>::max)());
m_stream->write((const char *)inString, size);
inString += size;
length -= (size_t)size;
}
if (messageEnd)
m_stream->flush();
if (!m_stream->good())
throw WriteErr();
return 0;
}
NAMESPACE_END
#endif
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:182
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.
Definition: cryptlib.cpp:476
Name::InputFileNameWide
const char * InputFileNameWide()
const wchar_t *
Definition: argnames.h:58
SafeConvert
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
Definition: misc.h:517
NameValuePairs::GetValueWithDefault
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:348
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:1865
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.
Definition: files.cpp:74
FileSource
file-based implementation of Source interface
Definition: files.h:55
Name::OutputStreamPointer
const char * OutputStreamPointer()
std::ostream *
Definition: argnames.h:63
Name::InputFileName
const char * InputFileName()
const char *
Definition: argnames.h:57
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1352
FileSink::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: files.cpp:231
FileSink::IsolatedFlush
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition: files.cpp:218
Name::InputStreamPointer
const char * InputStreamPointer()
std::istream *
Definition: argnames.h:59
Name::OutputBinaryMode
const char * OutputBinaryMode()
bool
Definition: argnames.h:64
FileStore::Skip
lword Skip(lword skipMax=ULONG_MAX)
Discard skipMax bytes from the output buffer.
Definition: files.cpp:166
Name::OutputFileName
const char * OutputFileName()
const char *
Definition: argnames.h:61
FileSink::WriteErr
Definition: files.h:87
FileSink::Err
Definition: files.h:81
UnsignedMin
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:503
STDMIN
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:467
FileStore
file-based implementation of Store interface
Definition: files.h:15
FileSink::OpenErr
Definition: files.h:86
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.
Definition: files.cpp:113
FileSink::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: files.cpp:179
CryptoPP
Crypto++ library namespace.
NameValuePairs::GetValue
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:335
Name::InputBinaryMode
const char * InputBinaryMode()
bool
Definition: argnames.h:60
FileStore::MaxRetrievable
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition: files.cpp:63
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:177
FileSink
file-based implementation of Sink interface
Definition: files.h:78
FileStore::ReadErr
Definition: files.h:24
Name::OutputFileNameWide
const char * OutputFileNameWide()
const wchar_t *
Definition: argnames.h:62
SIZE_MAX
#define SIZE_MAX
The maximum value of a machine word.
Definition: misc.h:95
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:277