files.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // files.h - originally written and placed in the public domain by Wei Dai
  2. /// \file files.h
  3. /// \brief Classes providing file-based library services
  4. /// \since Crypto++ 1.0
  5. #ifndef CRYPTOPP_FILES_H
  6. #define CRYPTOPP_FILES_H
  7. #include "cryptlib.h"
  8. #include "filters.h"
  9. #include "argnames.h"
  10. #include "smartptr.h"
  11. #include <iostream>
  12. #include <fstream>
  13. NAMESPACE_BEGIN(CryptoPP)
  14. /// \brief Implementation of Store interface
  15. /// \details file-based implementation of Store interface
  16. class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
  17. {
  18. public:
  19. /// \brief Exception thrown when file-based error is encountered
  20. class Err : public Exception
  21. {
  22. public:
  23. Err(const std::string &s) : Exception(IO_ERROR, s) {}
  24. };
  25. /// \brief Exception thrown when file-based open error is encountered
  26. class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
  27. /// \brief Exception thrown when file-based read error is encountered
  28. class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
  29. /// \brief Construct a FileStore
  30. FileStore() : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0) {}
  31. /// \brief Construct a FileStore
  32. /// \param in an existing stream
  33. FileStore(std::istream &in) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
  34. {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
  35. /// \brief Construct a FileStore
  36. /// \param filename the narrow name of the file to open
  37. FileStore(const char *filename) : m_stream(NULLPTR), m_space(NULLPTR), m_len(0), m_waiting(0)
  38. {StoreInitialize(MakeParameters(Name::InputFileName(), filename ? filename : ""));}
  39. #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
  40. /// \brief Construct a FileStore
  41. /// \param filename the Unicode name of the file to open
  42. /// \details On non-Windows OS, this function assumes that setlocale() has been called.
  43. FileStore(const wchar_t *filename)
  44. {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
  45. #endif
  46. /// \brief Retrieves the internal stream
  47. /// \return the internal stream pointer
  48. std::istream* GetStream() {return m_stream;}
  49. /// \brief Retrieves the internal stream
  50. /// \return the internal stream pointer
  51. const std::istream* GetStream() const {return m_stream;}
  52. /// \brief Provides the number of bytes ready for retrieval
  53. /// \return the number of bytes ready for retrieval
  54. /// \details All retrieval functions return the actual number of bytes retrieved, which is
  55. /// the lesser of the request number and MaxRetrievable()
  56. lword MaxRetrievable() const;
  57. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
  58. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
  59. lword Skip(lword skipMax=ULONG_MAX);
  60. private:
  61. void StoreInitialize(const NameValuePairs &parameters);
  62. member_ptr<std::ifstream> m_file;
  63. std::istream *m_stream;
  64. byte *m_space;
  65. size_t m_len;
  66. bool m_waiting;
  67. };
  68. /// \brief Implementation of Store interface
  69. /// \details file-based implementation of Store interface
  70. class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
  71. {
  72. public:
  73. typedef FileStore::Err Err;
  74. typedef FileStore::OpenErr OpenErr;
  75. typedef FileStore::ReadErr ReadErr;
  76. /// \brief Construct a FileSource
  77. FileSource(BufferedTransformation *attachment = NULLPTR)
  78. : SourceTemplate<FileStore>(attachment) {}
  79. /// \brief Construct a FileSource
  80. /// \param in an existing stream
  81. /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
  82. /// \param attachment an optional attached transformation
  83. FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
  84. : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
  85. /// \brief Construct a FileSource
  86. /// \param filename the narrow name of the file to open
  87. /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
  88. /// \param attachment an optional attached transformation
  89. /// \param binary flag indicating if the file is binary
  90. FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
  91. : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
  92. #if defined(CRYPTOPP_UNIX_AVAILABLE) || defined(CRYPTOPP_DOXYGEN_PROCESSING) || _MSC_VER >= 1400
  93. /// \brief Construct a FileSource
  94. /// \param filename the Unicode name of the file to open
  95. /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
  96. /// \param attachment an optional attached transformation
  97. /// \param binary flag indicating if the file is binary
  98. /// \details On non-Windows OS, this function assumes that setlocale() has been called.
  99. FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULLPTR, bool binary=true)
  100. : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
  101. #endif
  102. /// \brief Retrieves the internal stream
  103. /// \return the internal stream pointer
  104. std::istream* GetStream() {return m_store.GetStream();}
  105. };
  106. /// \brief Implementation of Store interface
  107. /// \details file-based implementation of Sink interface
  108. class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
  109. {
  110. public:
  111. /// \brief Exception thrown when file-based error is encountered
  112. class Err : public Exception
  113. {
  114. public:
  115. Err(const std::string &s) : Exception(IO_ERROR, s) {}
  116. };
  117. /// \brief Exception thrown when file-based open error is encountered
  118. class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
  119. /// \brief Exception thrown when file-based write error is encountered
  120. class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
  121. /// \brief Construct a FileSink
  122. FileSink() : m_stream(NULLPTR) {}
  123. /// \brief Construct a FileSink
  124. /// \param out an existing stream
  125. FileSink(std::ostream &out)
  126. {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
  127. /// \brief Construct a FileSink
  128. /// \param filename the narrow name of the file to open
  129. /// \param binary flag indicating if the file is binary
  130. FileSink(const char *filename, bool binary=true)
  131. {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
  132. #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
  133. /// \brief Construct a FileSink
  134. /// \param filename the Unicode name of the file to open
  135. /// \details On non-Windows OS, this function assumes that setlocale() has been called.
  136. FileSink(const wchar_t *filename, bool binary=true)
  137. {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
  138. #endif
  139. /// \brief Retrieves the internal stream
  140. /// \return the internal stream pointer
  141. std::ostream* GetStream() {return m_stream;}
  142. void IsolatedInitialize(const NameValuePairs &parameters);
  143. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
  144. bool IsolatedFlush(bool hardFlush, bool blocking);
  145. private:
  146. member_ptr<std::ofstream> m_file;
  147. std::ostream *m_stream;
  148. };
  149. NAMESPACE_END
  150. #endif