files.h
Go to the documentation of this file.
// files.h - originally written and placed in the public domain by Wei Dai
/// \file files.h
/// \brief Classes providing file-based library services
/// \since Crypto++ 1.0
#ifndef CRYPTOPP_FILES_H
#define CRYPTOPP_FILES_H
#include "cryptlib.h"
#include "filters.h"
#include "argnames.h"
#include "smartptr.h"
#include <iostream>
#include <fstream>
NAMESPACE_BEGIN(CryptoPP)
/// \brief Implementation of Store interface
/// \details file-based implementation of Store interface
class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
{
public:
/// \brief Exception thrown when file-based error is encountered
class Err : public Exception
{
public:
Err(const std::string &s) : Exception(IO_ERROR, s) {}
};
/// \brief Exception thrown when file-based open error is encountered
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
/// \brief Exception thrown when file-based read error is encountered
class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
/// \brief Construct a FileStore
FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
/// \brief Construct a FileStore
/// \param in an existing stream
FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
{StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
/// \brief Construct a FileStore
/// \param filename the narrow name of the file to open
FileStore(const char *filename) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
{StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
#if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
/// \brief Construct a FileStore
/// \param filename the Unicode name of the file to open
/// \details On non-Windows OS, this function assumes that setlocale() has been called.
FileStore(const wchar_t *filename)
{StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
#endif
/// \brief Retrieves the internal stream
/// \return the internal stream pointer
std::istream* GetStream() {return m_stream;}
/// \brief Retrieves the internal stream
/// \return the internal stream pointer
const std::istream* GetStream() const {return m_stream;}
/// \brief Provides the number of bytes ready for retrieval
/// \return the number of bytes ready for retrieval
/// \details All retrieval functions return the actual number of bytes retrieved, which is
/// the lesser of the request number and MaxRetrievable()
lword MaxRetrievable() const;
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
lword Skip(lword skipMax=ULONG_MAX);
private:
void StoreInitialize(const NameValuePairs &parameters);
member_ptr<std::ifstream> m_file;
std::istream *m_stream;
byte *m_space;
size_t m_len;
bool m_waiting;
};
/// \brief Implementation of Store interface
/// \details file-based implementation of Store interface
class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
{
public:
typedef FileStore::Err Err;
typedef FileStore::OpenErr OpenErr;
typedef FileStore::ReadErr ReadErr;
/// \brief Construct a FileSource
FileSource(BufferedTransformation *attachment = NULLPTR)
: SourceTemplate<FileStore>(attachment) {}
/// \brief Construct a FileSource
/// \param in an existing stream
/// \param pumpAll flag indicating if source data should be pumped to its attached transformation
/// \param attachment an optional attached transformation
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
/// \brief Construct a FileSource
/// \param filename the narrow name of the file to open
/// \param pumpAll flag indicating if source data should be pumped to its attached transformation
/// \param attachment an optional attached transformation
/// \param binary flag indicating if the file is binary
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
#if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
/// \brief Construct a FileSource
/// \param filename the Unicode name of the file to open
/// \param pumpAll flag indicating if source data should be pumped to its attached transformation
/// \param attachment an optional attached transformation
/// \param binary flag indicating if the file is binary
/// \details On non-Windows OS, this function assumes that setlocale() has been called.
FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
#endif
/// \brief Retrieves the internal stream
/// \return the internal stream pointer
std::istream* GetStream() {return m_store.GetStream();}
};
/// \brief Implementation of Store interface
/// \details file-based implementation of Sink interface
class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
{
public:
/// \brief Exception thrown when file-based error is encountered
class Err : public Exception
{
public:
Err(const std::string &s) : Exception(IO_ERROR, s) {}
};
/// \brief Exception thrown when file-based open error is encountered
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
/// \brief Exception thrown when file-based write error is encountered
class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
/// \brief Construct a FileSink
FileSink() : m_stream(NULLPTR) {}
/// \brief Construct a FileSink
/// \param out an existing stream
FileSink(std::ostream &out)
{IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
/// \brief Construct a FileSink
/// \param filename the narrow name of the file to open
/// \param binary flag indicating if the file is binary
FileSink(const char *filename, bool binary=true)
{IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
#if defined(CRYPTOPP_UNIX_AVAILABLE) || (CRYPTOPP_MSC_VERSION >= 1400)
/// \brief Construct a FileSink
/// \param filename the Unicode name of the file to open
/// \details On non-Windows OS, this function assumes that setlocale() has been called.
FileSink(const wchar_t *filename, bool binary=true)
{IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
#endif
/// \brief Retrieves the internal stream
/// \return the internal stream pointer
std::ostream* GetStream() {return m_stream;}
void IsolatedInitialize(const NameValuePairs &parameters);
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
bool IsolatedFlush(bool hardFlush, bool blocking);
private:
member_ptr<std::ofstream> m_file;
std::ostream *m_stream;
};
NAMESPACE_END
#endif
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition algparam.h:508
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
BufferedTransformation
Interface for buffered transformations.
Definition cryptlib.h:1657
Exception
Base class for all exceptions thrown by the library.
Definition cryptlib.h:164
FileSink::Err
Exception thrown when file-based error is encountered.
Definition files.h:135
FileSink::OpenErr
Exception thrown when file-based open error is encountered.
Definition files.h:140
FileSink::WriteErr
Exception thrown when file-based write error is encountered.
Definition files.h:142
FileSink
Implementation of Store interface.
Definition files.h:131
FileSink::GetStream
std::ostream * GetStream()
Retrieves the internal stream.
Definition files.h:168
FileSink::FileSink
FileSink()
Construct a FileSink.
Definition files.h:145
FileSink::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
FileSink::FileSink
FileSink(const char *filename, bool binary=true)
Construct a FileSink.
Definition files.h:155
FileSink::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
FileSink::FileSink
FileSink(std::ostream &out)
Construct a FileSink.
Definition files.h:149
FileSink::IsolatedFlush
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
FileSource
Implementation of Store interface.
Definition files.h:87
FileSource::FileSource
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition files.h:101
FileSource::FileSource
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)
Construct a FileSource.
Definition files.h:109
FileSource::GetStream
std::istream * GetStream()
Retrieves the internal stream.
Definition files.h:125
FileSource::FileSource
FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true)
Construct a FileSource.
Definition files.h:119
FileSource::FileSource
FileSource(BufferedTransformation *attachment=NULL)
Construct a FileSource.
Definition files.h:94
FileStore::Err
Exception thrown when file-based error is encountered.
Definition files.h:27
FileStore::OpenErr
Exception thrown when file-based open error is encountered.
Definition files.h:32
FileStore::ReadErr
Exception thrown when file-based read error is encountered.
Definition files.h:34
FileStore
Implementation of Store interface.
Definition files.h:23
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.
FileStore::Skip
lword Skip(lword skipMax=ULONG_MAX)
Discard skipMax bytes from the output buffer.
FileStore::FileStore
FileStore(const char *filename)
Construct a FileStore.
Definition files.h:46
FileStore::MaxRetrievable
lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
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.
FileStore::FileStore
FileStore(std::istream &in)
Construct a FileStore.
Definition files.h:41
FileStore::GetStream
std::istream * GetStream()
Retrieves the internal stream.
Definition files.h:59
FileStore::GetStream
const std::istream * GetStream() const
Retrieves the internal stream.
Definition files.h:63
FileStore::FileStore
FileStore()
Construct a FileStore.
Definition files.h:37
FileStore::FileStore
FileStore(const wchar_t *filename)
Construct a FileStore.
Definition files.h:53
NameValuePairs
Interface for retrieving values given their names.
Definition cryptlib.h:327
NotCopyable
Ensures an object is not copyable.
Definition misc.h:241
Sink
Implementation of BufferedTransformation's attachment interface.
Definition simple.h:479
SourceTemplate
Transform a Store into a Source.
Definition filters.h:1429
SourceTemplate::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition filters.h:1437
Store
Acts as a Source for pre-existing, static data.
Definition simple.h:448
member_ptr
Pointer that overloads operator ->
Definition smartptr.h:38
LWORD_MAX
const lword LWORD_MAX
Large word type max value.
Definition config_int.h:174
lword
word64 lword
Large word type.
Definition config_int.h:168
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
DEFAULT_CHANNEL
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition cryptlib.h:516
filters.h
Implementation of BufferedTransformation's attachment interface.
CryptoPP
Crypto++ library namespace.
Name::InputStreamPointer
const char * InputStreamPointer()
std::istream *
Definition argnames.h:60
Name::InputFileName
const char * InputFileName()
const char *
Definition argnames.h:58
Name::OutputBinaryMode
const char * OutputBinaryMode()
bool
Definition argnames.h:65
Name::OutputStreamPointer
const char * OutputStreamPointer()
std::ostream *
Definition argnames.h:64
Name::InputBinaryMode
const char * InputBinaryMode()
bool
Definition argnames.h:61
Name::OutputFileNameWide
const char * OutputFileNameWide()
const wchar_t *
Definition argnames.h:63
Name::OutputFileName
const char * OutputFileName()
const char *
Definition argnames.h:62
Name::InputFileNameWide
const char * InputFileNameWide()
const wchar_t *
Definition argnames.h:59
smartptr.h
Classes for automatic resource management.
FilterPutSpaceHelper
Create a working space in a BufferedTransformation.
Definition filters.h:171