ext-cryptopp/files.h

181 lines
7.5 KiB
C
Raw Normal View History

// files.h - originally written and placed in the public domain by Wei Dai
2016-11-12 13:38:50 +00:00
//! \file files.h
//! \brief Classes providing file-based library services
//! \since Crypto++ 1.0
2015-11-05 06:59:46 +00:00
#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)
2016-11-12 13:38:50 +00:00
//! \class FileStore
//! \brief Implementation of Store interface
//! \details file-based implementation of Store interface
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
{
public:
2016-11-12 13:38:50 +00:00
//! \brief Exception thrown when file-based error is encountered
2015-11-05 06:59:46 +00:00
class Err : public Exception
{
public:
Err(const std::string &s) : Exception(IO_ERROR, s) {}
};
2016-11-12 13:38:50 +00:00
//! \brief Exception thrown when file-based open error is encountered
2015-11-05 06:59:46 +00:00
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
2016-11-12 13:38:50 +00:00
//! \brief Exception thrown when file-based read error is encountered
2015-11-05 06:59:46 +00:00
class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
2016-11-12 13:38:50 +00:00
//! \brief Construct a FileStore
2017-03-01 11:10:06 +00:00
FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
2016-11-12 13:38:50 +00:00
//! \brief Construct a FileStore
//! \param in an existing stream
2017-03-01 11:10:06 +00:00
FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
2015-11-05 06:59:46 +00:00
{StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
2016-11-12 13:38:50 +00:00
//! \brief Construct a FileStore
//! \param filename the narrow name of the file to open
2017-03-01 11:10:06 +00:00
FileStore(const char *filename) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
{StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
2016-11-12 13:38:50 +00:00
#if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 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.
2015-11-05 06:59:46 +00:00
FileStore(const wchar_t *filename)
{StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
#endif
2016-11-12 13:38:50 +00:00
//! \brief Retrieves the internal stream
//! \returns the internal stream pointer
2015-11-05 06:59:46 +00:00
std::istream* GetStream() {return m_stream;}
2017-05-02 22:25:58 +00:00
//! \brief Retrieves the internal stream
//! \returns the internal stream pointer
const std::istream* GetStream() const {return m_stream;}
2015-11-05 06:59:46 +00:00
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);
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
member_ptr<std::ifstream> m_file;
std::istream *m_stream;
byte *m_space;
size_t m_len;
bool m_waiting;
};
2016-11-12 13:38:50 +00:00
//! \class FileSource
//! \brief Implementation of Store interface
//! \details file-based implementation of Store interface
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
{
public:
typedef FileStore::Err Err;
typedef FileStore::OpenErr OpenErr;
typedef FileStore::ReadErr ReadErr;
2016-11-19 09:40:07 +00:00
//! \brief Construct a FileSource
2017-03-01 11:10:06 +00:00
FileSource(BufferedTransformation *attachment = NULLPTR)
2015-11-05 06:59:46 +00:00
: SourceTemplate<FileStore>(attachment) {}
2016-11-12 13:38:50 +00:00
2016-11-19 09:40:07 +00:00
//! \brief Construct a FileSource
2016-11-12 13:38:50 +00:00
//! \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
2017-03-01 11:10:06 +00:00
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
2015-11-05 06:59:46 +00:00
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
2016-11-12 13:38:50 +00:00
2016-11-19 09:40:07 +00:00
//! \brief Construct a FileSource
2016-11-12 13:38:50 +00:00
//! \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
2017-03-01 11:10:06 +00:00
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
2015-11-05 06:59:46 +00:00
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
2016-11-12 13:38:50 +00:00
#if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 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.
2017-03-01 11:10:06 +00:00
FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
2015-11-05 06:59:46 +00:00
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
#endif
2016-11-12 13:38:50 +00:00
//! \brief Retrieves the internal stream
//! \returns the internal stream pointer
2015-11-05 06:59:46 +00:00
std::istream* GetStream() {return m_store.GetStream();}
};
2016-11-12 13:38:50 +00:00
//! \class FileSink
//! \brief Implementation of Store interface
//! \details file-based implementation of Sink interface
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
{
public:
2016-11-12 13:38:50 +00:00
//! \brief Exception thrown when file-based error is encountered
2015-11-05 06:59:46 +00:00
class Err : public Exception
{
public:
Err(const std::string &s) : Exception(IO_ERROR, s) {}
};
2016-11-12 13:38:50 +00:00
//! \brief Exception thrown when file-based open error is encountered
2015-11-05 06:59:46 +00:00
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
2016-11-12 13:38:50 +00:00
//! \brief Exception thrown when file-based write error is encountered
2015-11-05 06:59:46 +00:00
class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
2016-11-12 13:38:50 +00:00
//! \brief Construct a FileSink
2017-03-01 11:10:06 +00:00
FileSink() : m_stream(NULLPTR) {}
2016-11-12 13:38:50 +00:00
//! \brief Construct a FileSink
//! \param out an existing stream
2015-11-05 06:59:46 +00:00
FileSink(std::ostream &out)
{IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
2016-11-12 13:38:50 +00:00
//! \brief Construct a FileSink
//! \param filename the narrow name of the file to open
//! \param binary flag indicating if the file is binary
2015-11-05 06:59:46 +00:00
FileSink(const char *filename, bool binary=true)
{IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
2016-11-12 13:38:50 +00:00
2015-11-05 06:59:46 +00:00
#if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
2016-11-12 13:38:50 +00:00
//! \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.
2015-11-05 06:59:46 +00:00
FileSink(const wchar_t *filename, bool binary=true)
{IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
#endif
2016-11-12 13:38:50 +00:00
//! \brief Retrieves the internal stream
//! \returns the internal stream pointer
2015-11-05 06:59:46 +00:00
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