InflatingStream.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // InflatingStream.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/InflatingStream.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: ZLibStream
  9. //
  10. // Definition of the InflatingInputStream and InflatingOutputStream classes.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_InflatingStream_INCLUDED
  18. #define Foundation_InflatingStream_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/BufferedStreamBuf.h"
  21. #include <istream>
  22. #include <ostream>
  23. #if defined(POCO_UNBUNDLED)
  24. #include <zlib.h>
  25. #else
  26. #include "Poco/zlib.h"
  27. #endif
  28. namespace Poco {
  29. class Foundation_API InflatingStreamBuf: public BufferedStreamBuf
  30. /// This is the streambuf class used by InflatingInputStream and InflatingOutputStream.
  31. /// The actual work is delegated to zlib (see http://zlib.net).
  32. /// Both zlib (deflate) streams and gzip streams are supported.
  33. /// Output streams should always call close() to ensure
  34. /// proper completion of decompression.
  35. {
  36. public:
  37. enum StreamType
  38. {
  39. STREAM_ZLIB, /// Expect a zlib header, use Adler-32 checksum.
  40. STREAM_GZIP, /// Expect a gzip header, use CRC-32 checksum.
  41. STREAM_ZIP /// STREAM_ZIP is handled as STREAM_ZLIB, except that we do not check the ADLER32 value (must be checked by caller)
  42. };
  43. InflatingStreamBuf(std::istream& istr, StreamType type);
  44. /// Creates an InflatingStreamBuf for expanding the compressed data read from
  45. /// the give input stream.
  46. InflatingStreamBuf(std::istream& istr, int windowBits);
  47. /// Creates an InflatingStreamBuf for expanding the compressed data read from
  48. /// the given input stream.
  49. ///
  50. /// Please refer to the zlib documentation of inflateInit2() for a description
  51. /// of the windowBits parameter.
  52. InflatingStreamBuf(std::ostream& ostr, StreamType type);
  53. /// Creates an InflatingStreamBuf for expanding the compressed data passed through
  54. /// and forwarding it to the given output stream.
  55. InflatingStreamBuf(std::ostream& ostr, int windowBits);
  56. /// Creates an InflatingStreamBuf for expanding the compressed data passed through
  57. /// and forwarding it to the given output stream.
  58. ///
  59. /// Please refer to the zlib documentation of inflateInit2() for a description
  60. /// of the windowBits parameter.
  61. ~InflatingStreamBuf();
  62. /// Destroys the InflatingStreamBuf.
  63. int close();
  64. /// Finishes up the stream.
  65. ///
  66. /// Must be called when inflating to an output stream.
  67. void reset();
  68. /// Resets the stream buffer.
  69. protected:
  70. int readFromDevice(char* buffer, std::streamsize length);
  71. int writeToDevice(const char* buffer, std::streamsize length);
  72. int sync();
  73. private:
  74. enum
  75. {
  76. STREAM_BUFFER_SIZE = 1024,
  77. INFLATE_BUFFER_SIZE = 32768
  78. };
  79. std::istream* _pIstr;
  80. std::ostream* _pOstr;
  81. char* _buffer;
  82. z_stream _zstr;
  83. bool _eof;
  84. bool _check;
  85. };
  86. class Foundation_API InflatingIOS: public virtual std::ios
  87. /// The base class for InflatingOutputStream and InflatingInputStream.
  88. ///
  89. /// This class is needed to ensure the correct initialization
  90. /// order of the stream buffer and base classes.
  91. {
  92. public:
  93. InflatingIOS(std::ostream& ostr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB);
  94. /// Creates an InflatingIOS for expanding the compressed data passed through
  95. /// and forwarding it to the given output stream.
  96. InflatingIOS(std::ostream& ostr, int windowBits);
  97. /// Creates an InflatingIOS for expanding the compressed data passed through
  98. /// and forwarding it to the given output stream.
  99. ///
  100. /// Please refer to the zlib documentation of inflateInit2() for a description
  101. /// of the windowBits parameter.
  102. InflatingIOS(std::istream& istr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB);
  103. /// Creates an InflatingIOS for expanding the compressed data read from
  104. /// the given input stream.
  105. InflatingIOS(std::istream& istr, int windowBits);
  106. /// Creates an InflatingIOS for expanding the compressed data read from
  107. /// the given input stream.
  108. ///
  109. /// Please refer to the zlib documentation of inflateInit2() for a description
  110. /// of the windowBits parameter.
  111. ~InflatingIOS();
  112. /// Destroys the InflatingIOS.
  113. InflatingStreamBuf* rdbuf();
  114. /// Returns a pointer to the underlying stream buffer.
  115. protected:
  116. InflatingStreamBuf _buf;
  117. };
  118. class Foundation_API InflatingOutputStream: public InflatingIOS, public std::ostream
  119. /// This stream decompresses all data passing through it
  120. /// using zlib's inflate algorithm.
  121. ///
  122. /// After all data has been written to the stream, close()
  123. /// must be called to ensure completion of decompression.
  124. {
  125. public:
  126. InflatingOutputStream(std::ostream& ostr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB);
  127. /// Creates an InflatingOutputStream for expanding the compressed data passed through
  128. /// and forwarding it to the given output stream.
  129. InflatingOutputStream(std::ostream& ostr, int windowBits);
  130. /// Creates an InflatingOutputStream for expanding the compressed data passed through
  131. /// and forwarding it to the given output stream.
  132. ///
  133. /// Please refer to the zlib documentation of inflateInit2() for a description
  134. /// of the windowBits parameter.
  135. ~InflatingOutputStream();
  136. /// Destroys the InflatingOutputStream.
  137. int close();
  138. /// Finishes up the stream.
  139. ///
  140. /// Must be called to ensure all data is properly written to
  141. /// the target output stream.
  142. };
  143. class Foundation_API InflatingInputStream: public InflatingIOS, public std::istream
  144. /// This stream decompresses all data passing through it
  145. /// using zlib's inflate algorithm.
  146. /// Example:
  147. /// std::ifstream istr("data.gz", std::ios::binary);
  148. /// InflatingInputStream inflater(istr, InflatingStreamBuf::STREAM_GZIP);
  149. /// std::string data;
  150. /// inflater >> data;
  151. ///
  152. /// The underlying input stream can contain more than one gzip/deflate stream.
  153. /// After a gzip/deflate stream has been processed, reset() can be called
  154. /// to inflate the next stream.
  155. {
  156. public:
  157. InflatingInputStream(std::istream& istr, InflatingStreamBuf::StreamType type = InflatingStreamBuf::STREAM_ZLIB);
  158. /// Creates an InflatingInputStream for expanding the compressed data read from
  159. /// the given input stream.
  160. InflatingInputStream(std::istream& istr, int windowBits);
  161. /// Creates an InflatingInputStream for expanding the compressed data read from
  162. /// the given input stream.
  163. ///
  164. /// Please refer to the zlib documentation of inflateInit2() for a description
  165. /// of the windowBits parameter.
  166. ~InflatingInputStream();
  167. /// Destroys the InflatingInputStream.
  168. void reset();
  169. /// Resets the zlib machinery so that another zlib stream can be read from
  170. /// the same underlying input stream.
  171. };
  172. } // namespace Poco
  173. #endif // Foundation_InflatingStream_INCLUDED