gzip_stream.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: brianolson@google.com (Brian Olson)
  31. //
  32. // This file contains the definition for classes GzipInputStream and
  33. // GzipOutputStream.
  34. //
  35. // GzipInputStream decompresses data from an underlying
  36. // ZeroCopyInputStream and provides the decompressed data as a
  37. // ZeroCopyInputStream.
  38. //
  39. // GzipOutputStream is an ZeroCopyOutputStream that compresses data to
  40. // an underlying ZeroCopyOutputStream.
  41. #ifndef GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__
  42. #define GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__
  43. #include <google/protobuf/stubs/common.h>
  44. #include <google/protobuf/io/zero_copy_stream.h>
  45. #include <google/protobuf/port.h>
  46. #include <zlib.h>
  47. #include <google/protobuf/port_def.inc>
  48. namespace google {
  49. namespace protobuf {
  50. namespace io {
  51. // A ZeroCopyInputStream that reads compressed data through zlib
  52. class PROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream {
  53. public:
  54. // Format key for constructor
  55. enum Format {
  56. // zlib will autodetect gzip header or deflate stream
  57. AUTO = 0,
  58. // GZIP streams have some extra header data for file attributes.
  59. GZIP = 1,
  60. // Simpler zlib stream format.
  61. ZLIB = 2,
  62. };
  63. // buffer_size and format may be -1 for default of 64kB and GZIP format
  64. explicit GzipInputStream(ZeroCopyInputStream* sub_stream,
  65. Format format = AUTO, int buffer_size = -1);
  66. virtual ~GzipInputStream();
  67. // Return last error message or NULL if no error.
  68. inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
  69. inline int ZlibErrorCode() const { return zerror_; }
  70. // implements ZeroCopyInputStream ----------------------------------
  71. bool Next(const void** data, int* size) override;
  72. void BackUp(int count) override;
  73. bool Skip(int count) override;
  74. int64_t ByteCount() const override;
  75. private:
  76. Format format_;
  77. ZeroCopyInputStream* sub_stream_;
  78. z_stream zcontext_;
  79. int zerror_;
  80. void* output_buffer_;
  81. void* output_position_;
  82. size_t output_buffer_length_;
  83. int64_t byte_count_;
  84. int Inflate(int flush);
  85. void DoNextOutput(const void** data, int* size);
  86. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipInputStream);
  87. };
  88. class PROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream {
  89. public:
  90. // Format key for constructor
  91. enum Format {
  92. // GZIP streams have some extra header data for file attributes.
  93. GZIP = 1,
  94. // Simpler zlib stream format.
  95. ZLIB = 2,
  96. };
  97. struct PROTOBUF_EXPORT Options {
  98. // Defaults to GZIP.
  99. Format format;
  100. // What size buffer to use internally. Defaults to 64kB.
  101. int buffer_size;
  102. // A number between 0 and 9, where 0 is no compression and 9 is best
  103. // compression. Defaults to Z_DEFAULT_COMPRESSION (see zlib.h).
  104. int compression_level;
  105. // Defaults to Z_DEFAULT_STRATEGY. Can also be set to Z_FILTERED,
  106. // Z_HUFFMAN_ONLY, or Z_RLE. See the documentation for deflateInit2 in
  107. // zlib.h for definitions of these constants.
  108. int compression_strategy;
  109. Options(); // Initializes with default values.
  110. };
  111. // Create a GzipOutputStream with default options.
  112. explicit GzipOutputStream(ZeroCopyOutputStream* sub_stream);
  113. // Create a GzipOutputStream with the given options.
  114. GzipOutputStream(ZeroCopyOutputStream* sub_stream, const Options& options);
  115. virtual ~GzipOutputStream();
  116. // Return last error message or NULL if no error.
  117. inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
  118. inline int ZlibErrorCode() const { return zerror_; }
  119. // Flushes data written so far to zipped data in the underlying stream.
  120. // It is the caller's responsibility to flush the underlying stream if
  121. // necessary.
  122. // Compression may be less efficient stopping and starting around flushes.
  123. // Returns true if no error.
  124. //
  125. // Please ensure that block size is > 6. Here is an excerpt from the zlib
  126. // doc that explains why:
  127. //
  128. // In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that avail_out
  129. // is greater than six to avoid repeated flush markers due to
  130. // avail_out == 0 on return.
  131. bool Flush();
  132. // Writes out all data and closes the gzip stream.
  133. // It is the caller's responsibility to close the underlying stream if
  134. // necessary.
  135. // Returns true if no error.
  136. bool Close();
  137. // implements ZeroCopyOutputStream ---------------------------------
  138. bool Next(void** data, int* size) override;
  139. void BackUp(int count) override;
  140. int64_t ByteCount() const override;
  141. private:
  142. ZeroCopyOutputStream* sub_stream_;
  143. // Result from calling Next() on sub_stream_
  144. void* sub_data_;
  145. int sub_data_size_;
  146. z_stream zcontext_;
  147. int zerror_;
  148. void* input_buffer_;
  149. size_t input_buffer_length_;
  150. // Shared constructor code.
  151. void Init(ZeroCopyOutputStream* sub_stream, const Options& options);
  152. // Do some compression.
  153. // Takes zlib flush mode.
  154. // Returns zlib error code.
  155. int Deflate(int flush);
  156. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipOutputStream);
  157. };
  158. } // namespace io
  159. } // namespace protobuf
  160. } // namespace google
  161. #include <google/protobuf/port_undef.inc>
  162. #endif // GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__