File indexing completed on 2025-01-18 09:55:02
0001
0002
0003
0004
0005
0006
0007 #ifndef CRYPTOPP_FILES_H
0008 #define CRYPTOPP_FILES_H
0009
0010 #include "cryptlib.h"
0011 #include "filters.h"
0012 #include "argnames.h"
0013 #include "smartptr.h"
0014
0015 #include <iostream>
0016 #include <fstream>
0017
0018 NAMESPACE_BEGIN(CryptoPP)
0019
0020
0021
0022 class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
0023 {
0024 public:
0025
0026 class Err : public Exception
0027 {
0028 public:
0029 Err(const std::string &s) : Exception(IO_ERROR, s) {}
0030 };
0031
0032 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
0033
0034 class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
0035
0036
0037 FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
0038
0039
0040
0041 FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
0042 {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
0043
0044
0045
0046 FileStore(const char *filename) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
0047 {StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
0048
0049 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
0050
0051
0052
0053 FileStore(const wchar_t *filename)
0054 {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
0055 #endif
0056
0057
0058
0059 std::istream* GetStream() {return m_stream;}
0060
0061
0062
0063 const std::istream* GetStream() const {return m_stream;}
0064
0065
0066
0067
0068
0069 lword MaxRetrievable() const;
0070 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
0071 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
0072 lword Skip(lword skipMax=ULONG_MAX);
0073
0074 private:
0075 void StoreInitialize(const NameValuePairs ¶meters);
0076
0077 member_ptr<std::ifstream> m_file;
0078 std::istream *m_stream;
0079 byte *m_space;
0080 size_t m_len;
0081 bool m_waiting;
0082 };
0083
0084
0085
0086 class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
0087 {
0088 public:
0089 typedef FileStore::Err Err;
0090 typedef FileStore::OpenErr OpenErr;
0091 typedef FileStore::ReadErr ReadErr;
0092
0093
0094 FileSource(BufferedTransformation *attachment = NULLPTR)
0095 : SourceTemplate<FileStore>(attachment) {}
0096
0097
0098
0099
0100
0101 FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
0102 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
0103
0104
0105
0106
0107
0108
0109 FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
0110 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
0111
0112 #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || (CRYPTOPP_MSC_VERSION >= 1400)
0113
0114
0115
0116
0117
0118
0119 FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
0120 : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
0121 #endif
0122
0123
0124
0125 std::istream* GetStream() {return m_store.GetStream();}
0126 };
0127
0128
0129
0130 class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
0131 {
0132 public:
0133
0134 class Err : public Exception
0135 {
0136 public:
0137 Err(const std::string &s) : Exception(IO_ERROR, s) {}
0138 };
0139
0140 class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
0141
0142 class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
0143
0144
0145 FileSink() : m_stream(NULLPTR) {}
0146
0147
0148
0149 FileSink(std::ostream &out)
0150 {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
0151
0152
0153
0154
0155 FileSink(const char *filename, bool binary=true)
0156 {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
0157
0158 #if defined(CRYPTOPP_UNIX_AVAILABLE) || (CRYPTOPP_MSC_VERSION >= 1400)
0159
0160
0161
0162 FileSink(const wchar_t *filename, bool binary=true)
0163 {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
0164 #endif
0165
0166
0167
0168 std::ostream* GetStream() {return m_stream;}
0169
0170 void IsolatedInitialize(const NameValuePairs ¶meters);
0171 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0172 bool IsolatedFlush(bool hardFlush, bool blocking);
0173
0174 private:
0175 member_ptr<std::ofstream> m_file;
0176 std::ostream *m_stream;
0177 };
0178
0179 NAMESPACE_END
0180
0181 #endif