DigestStream.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // DigestStream.h
  3. //
  4. // Library: Foundation
  5. // Package: Crypt
  6. // Module: DigestStream
  7. //
  8. // Definition of classes DigestInputStream and DigestOutputStream.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_DigestStream_INCLUDED
  16. #define Foundation_DigestStream_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/BufferedStreamBuf.h"
  19. #include "Poco/DigestEngine.h"
  20. #include <istream>
  21. #include <ostream>
  22. namespace Poco {
  23. class Foundation_API DigestBuf: public BufferedStreamBuf
  24. /// This streambuf computes a digest of all data going
  25. /// through it.
  26. {
  27. public:
  28. DigestBuf(DigestEngine& eng);
  29. DigestBuf(DigestEngine& eng, std::istream& istr);
  30. DigestBuf(DigestEngine& eng, std::ostream& ostr);
  31. ~DigestBuf();
  32. int readFromDevice(char* buffer, std::streamsize length);
  33. int writeToDevice(const char* buffer, std::streamsize length);
  34. void close();
  35. private:
  36. DigestEngine& _eng;
  37. std::istream* _pIstr;
  38. std::ostream* _pOstr;
  39. static const int BUFFER_SIZE;
  40. };
  41. class Foundation_API DigestIOS: public virtual std::ios
  42. /// The base class for DigestInputStream and DigestOutputStream.
  43. ///
  44. /// This class is needed to ensure the correct initialization
  45. /// order of the stream buffer and base classes.
  46. {
  47. public:
  48. DigestIOS(DigestEngine& eng);
  49. DigestIOS(DigestEngine& eng, std::istream& istr);
  50. DigestIOS(DigestEngine& eng, std::ostream& ostr);
  51. ~DigestIOS();
  52. DigestBuf* rdbuf();
  53. protected:
  54. DigestBuf _buf;
  55. };
  56. class Foundation_API DigestInputStream: public DigestIOS, public std::istream
  57. /// This istream computes a digest of
  58. /// all the data passing through it,
  59. /// using a DigestEngine.
  60. {
  61. public:
  62. DigestInputStream(DigestEngine& eng, std::istream& istr);
  63. ~DigestInputStream();
  64. };
  65. class Foundation_API DigestOutputStream: public DigestIOS, public std::ostream
  66. /// This ostream computes a digest of
  67. /// all the data passing through it,
  68. /// using a DigestEngine.
  69. /// To ensure that all data has been incorporated
  70. /// into the digest, call close() or flush() before
  71. /// you obtain the digest from the digest engine.
  72. {
  73. public:
  74. DigestOutputStream(DigestEngine& eng);
  75. DigestOutputStream(DigestEngine& eng, std::ostream& ostr);
  76. ~DigestOutputStream();
  77. void close();
  78. };
  79. } // namespace Poco
  80. #endif // Foundation_DigestStream_INCLUDED