Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.4 KiB

  1. #ifndef CRYPTOPP_FILES_H
  2. #define CRYPTOPP_FILES_H
  3. #include "cryptlib.h"
  4. #include "filters.h"
  5. #include "argnames.h"
  6. #include <iostream>
  7. #include <fstream>
  8. NAMESPACE_BEGIN(CryptoPP)
  9. //! file-based implementation of Store interface
  10. class CRYPTOPP_DLL FileStore : public Store, private FilterPutSpaceHelper, public NotCopyable
  11. {
  12. public:
  13. class Err : public Exception
  14. {
  15. public:
  16. Err(const std::string &s) : Exception(IO_ERROR, s) {}
  17. };
  18. class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileStore: error opening file for reading: " + filename) {}};
  19. class ReadErr : public Err {public: ReadErr() : Err("FileStore: error reading file") {}};
  20. FileStore() : m_stream(NULL) {}
  21. FileStore(std::istream &in)
  22. {StoreInitialize(MakeParameters(Name::InputStreamPointer(), &in));}
  23. FileStore(const char *filename)
  24. {StoreInitialize(MakeParameters(Name::InputFileName(), filename));}
  25. #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
  26. //! specify file with Unicode name. On non-Windows OS, this function assumes that setlocale() has been called.
  27. FileStore(const wchar_t *filename)
  28. {StoreInitialize(MakeParameters(Name::InputFileNameWide(), filename));}
  29. #endif
  30. std::istream* GetStream() {return m_stream;}
  31. lword MaxRetrievable() const;
  32. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
  33. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
  34. lword Skip(lword skipMax=ULONG_MAX);
  35. private:
  36. void StoreInitialize(const NameValuePairs &parameters);
  37. member_ptr<std::ifstream> m_file;
  38. std::istream *m_stream;
  39. byte *m_space;
  40. size_t m_len;
  41. bool m_waiting;
  42. };
  43. //! file-based implementation of Source interface
  44. class CRYPTOPP_DLL FileSource : public SourceTemplate<FileStore>
  45. {
  46. public:
  47. typedef FileStore::Err Err;
  48. typedef FileStore::OpenErr OpenErr;
  49. typedef FileStore::ReadErr ReadErr;
  50. FileSource(BufferedTransformation *attachment = NULL)
  51. : SourceTemplate<FileStore>(attachment) {}
  52. FileSource(std::istream &in, bool pumpAll, BufferedTransformation *attachment = NULL)
  53. : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputStreamPointer(), &in));}
  54. FileSource(const char *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
  55. : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileName(), filename)(Name::InputBinaryMode(), binary));}
  56. #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
  57. //! specify file with Unicode name. On non-Windows OS, this function assumes that setlocale() has been called.
  58. FileSource(const wchar_t *filename, bool pumpAll, BufferedTransformation *attachment = NULL, bool binary=true)
  59. : SourceTemplate<FileStore>(attachment) {SourceInitialize(pumpAll, MakeParameters(Name::InputFileNameWide(), filename)(Name::InputBinaryMode(), binary));}
  60. #endif
  61. std::istream* GetStream() {return m_store.GetStream();}
  62. };
  63. //! file-based implementation of Sink interface
  64. class CRYPTOPP_DLL FileSink : public Sink, public NotCopyable
  65. {
  66. public:
  67. class Err : public Exception
  68. {
  69. public:
  70. Err(const std::string &s) : Exception(IO_ERROR, s) {}
  71. };
  72. class OpenErr : public Err {public: OpenErr(const std::string &filename) : Err("FileSink: error opening file for writing: " + filename) {}};
  73. class WriteErr : public Err {public: WriteErr() : Err("FileSink: error writing file") {}};
  74. FileSink() : m_stream(NULL) {}
  75. FileSink(std::ostream &out)
  76. {IsolatedInitialize(MakeParameters(Name::OutputStreamPointer(), &out));}
  77. FileSink(const char *filename, bool binary=true)
  78. {IsolatedInitialize(MakeParameters(Name::OutputFileName(), filename)(Name::OutputBinaryMode(), binary));}
  79. #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400
  80. //! specify file with Unicode name. On non-Windows OS, this function assumes that setlocale() has been called.
  81. FileSink(const wchar_t *filename, bool binary=true)
  82. {IsolatedInitialize(MakeParameters(Name::OutputFileNameWide(), filename)(Name::OutputBinaryMode(), binary));}
  83. #endif
  84. std::ostream* GetStream() {return m_stream;}
  85. void IsolatedInitialize(const NameValuePairs &parameters);
  86. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
  87. bool IsolatedFlush(bool hardFlush, bool blocking);
  88. private:
  89. member_ptr<std::ofstream> m_file;
  90. std::ostream *m_stream;
  91. };
  92. NAMESPACE_END
  93. #endif