2003-02-24 01:11:57 +00:00
|
|
|
#ifndef CRYPTOPP_FILES_H
|
|
|
|
#define CRYPTOPP_FILES_H
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "filters.h"
|
2003-04-15 00:38:48 +00:00
|
|
|
#include "argnames.h"
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2004-07-22 00:51:57 +00:00
|
|
|
//! file-based implementation of Store interface
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2003-02-24 01:11:57 +00:00
|
|
|
class Err : public Exception
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2003-02-24 01:11:57 +00:00
|
|
|
Err(const std::string &s) : Exception(IO_ERROR, s) {}
|
2002-10-04 17:31:41 +00:00
|
|
|
};
|
2003-02-24 01:11:57 +00:00
|
|
|
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
|
|
|
|
class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2003-02-24 01:11:57 +00:00
|
|
|
FileStore() : m_stream(NULL) {}
|
2002-10-04 17:31:41 +00:00
|
|
|
FileStore(std::istream &in)
|
2003-04-15 00:38:48 +00:00
|
|
|
{StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
|
2003-02-24 01:11:57 +00:00
|
|
|
FileStore(const char *filename)
|
2003-04-15 00:38:48 +00:00
|
|
|
{StoreInitialize(MakeParameters(Name::InputFileName(), filename));}
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
std::istream* GetStream() {return m_stream;}
|
|
|
|
|
2005-07-12 04:23:32 +00:00
|
|
|
lword MaxRetrievable() const;
|
|
|
|
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
|
|
|
|
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
|
|
|
|
lword Skip(lword skipMax=ULONG_MAX);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void StoreInitialize(const NameValuePairs ¶meters);
|
|
|
|
|
2003-07-04 00:17:37 +00:00
|
|
|
member_ptr<std::ifstream> m_file;
|
2002-10-04 17:31:41 +00:00
|
|
|
std::istream *m_stream;
|
|
|
|
byte *m_space;
|
2005-07-12 04:23:32 +00:00
|
|
|
size_t m_len;
|
2002-10-04 17:31:41 +00:00
|
|
|
bool m_waiting;
|
|
|
|
};
|
|
|
|
|
2004-07-22 00:51:57 +00:00
|
|
|
//! file-based implementation of Source interface
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2003-02-24 01:11:57 +00:00
|
|
|
typedef FileStore::Err Err;
|
|
|
|
typedef FileStore::OpenErr OpenErr;
|
|
|
|
typedef FileStore::ReadErr ReadErr;
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2003-02-24 01:11:57 +00:00
|
|
|
FileSource(BufferedTransformation *attachment = NULL)
|
|
|
|
: SourceTemplate<FileStore>(attachment) {}
|
|
|
|
FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
|
2003-04-15 00:38:48 +00:00
|
|
|
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
|
2003-02-24 01:11:57 +00:00
|
|
|
FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
|
2003-04-15 00:38:48 +00:00
|
|
|
: SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
std::istream* GetStream() {return m_store.GetStream();}
|
|
|
|
};
|
|
|
|
|
2004-07-22 00:51:57 +00:00
|
|
|
//! file-based implementation of Sink interface
|
2003-07-04 00:17:37 +00:00
|
|
|
class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2003-02-24 01:11:57 +00:00
|
|
|
class Err : public Exception
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2003-02-24 01:11:57 +00:00
|
|
|
Err(const std::string &s) : Exception(IO_ERROR, s) {}
|
2002-10-04 17:31:41 +00:00
|
|
|
};
|
2003-02-24 01:11:57 +00:00
|
|
|
class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
|
|
|
|
class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2003-02-24 01:11:57 +00:00
|
|
|
FileSink() : m_stream(NULL) {}
|
2002-10-04 17:31:41 +00:00
|
|
|
FileSink(std::ostream &out)
|
2003-04-15 00:38:48 +00:00
|
|
|
{IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
|
2003-02-24 01:11:57 +00:00
|
|
|
FileSink(const char *filename, bool binary=true)
|
2003-04-15 00:38:48 +00:00
|
|
|
{IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)("OutputBinaryMode", binary));}
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
std::ostream* GetStream() {return m_stream;}
|
|
|
|
|
|
|
|
void IsolatedInitialize(const NameValuePairs ¶meters);
|
2005-07-12 04:23:32 +00:00
|
|
|
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
|
2003-02-24 01:11:57 +00:00
|
|
|
bool IsolatedFlush(bool hardFlush, bool blocking);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
private:
|
2003-07-04 00:17:37 +00:00
|
|
|
member_ptr<std::ofstream> m_file;
|
2002-10-04 17:31:41 +00:00
|
|
|
std::ostream *m_stream;
|
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|