You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

gzip_stream.h 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // Must be included last.
  48. #include <google/protobuf/port_def.inc>
  49. namespace google {
  50. namespace protobuf {
  51. namespace io {
  52. // A ZeroCopyInputStream that reads compressed data through zlib
  53. class PROTOBUF_EXPORT GzipInputStream PROTOBUF_FUTURE_FINAL
  54. : public ZeroCopyInputStream {
  55. public:
  56. // Format key for constructor
  57. enum Format {
  58. // zlib will autodetect gzip header or deflate stream
  59. AUTO = 0,
  60. // GZIP streams have some extra header data for file attributes.
  61. GZIP = 1,
  62. // Simpler zlib stream format.
  63. ZLIB = 2,
  64. };
  65. // buffer_size and format may be -1 for default of 64kB and GZIP format
  66. explicit GzipInputStream(ZeroCopyInputStream* sub_stream,
  67. Format format = AUTO, int buffer_size = -1);
  68. virtual ~GzipInputStream();
  69. // Return last error message or NULL if no error.
  70. inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
  71. inline int ZlibErrorCode() const { return zerror_; }
  72. // implements ZeroCopyInputStream ----------------------------------
  73. bool Next(const void** data, int* size) override;
  74. void BackUp(int count) override;
  75. bool Skip(int count) override;
  76. int64_t ByteCount() const override;
  77. private:
  78. Format format_;
  79. ZeroCopyInputStream* sub_stream_;
  80. z_stream zcontext_;
  81. int zerror_;
  82. void* output_buffer_;
  83. void* output_position_;
  84. size_t output_buffer_length_;
  85. int64_t byte_count_;
  86. int Inflate(int flush);
  87. void DoNextOutput(const void** data, int* size);
  88. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipInputStream);
  89. };
  90. class PROTOBUF_EXPORT GzipOutputStream PROTOBUF_FUTURE_FINAL
  91. : public ZeroCopyOutputStream {
  92. public:
  93. // Format key for constructor
  94. enum Format {
  95. // GZIP streams have some extra header data for file attributes.
  96. GZIP = 1,
  97. // Simpler zlib stream format.
  98. ZLIB = 2,
  99. };
  100. struct PROTOBUF_EXPORT Options {
  101. // Defaults to GZIP.
  102. Format format;
  103. // What size buffer to use internally. Defaults to 64kB.
  104. int buffer_size;
  105. // A number between 0 and 9, where 0 is no compression and 9 is best
  106. // compression. Defaults to Z_DEFAULT_COMPRESSION (see zlib.h).
  107. int compression_level;
  108. // Defaults to Z_DEFAULT_STRATEGY. Can also be set to Z_FILTERED,
  109. // Z_HUFFMAN_ONLY, or Z_RLE. See the documentation for deflateInit2 in
  110. // zlib.h for definitions of these constants.
  111. int compression_strategy;
  112. Options(); // Initializes with default values.
  113. };
  114. // Create a GzipOutputStream with default options.
  115. explicit GzipOutputStream(ZeroCopyOutputStream* sub_stream);
  116. // Create a GzipOutputStream with the given options.
  117. GzipOutputStream(ZeroCopyOutputStream* sub_stream, const Options& options);
  118. virtual ~GzipOutputStream();
  119. // Return last error message or NULL if no error.
  120. inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
  121. inline int ZlibErrorCode() const { return zerror_; }
  122. // Flushes data written so far to zipped data in the underlying stream.
  123. // It is the caller's responsibility to flush the underlying stream if
  124. // necessary.
  125. // Compression may be less efficient stopping and starting around flushes.
  126. // Returns true if no error.
  127. //
  128. // Please ensure that block size is > 6. Here is an excerpt from the zlib
  129. // doc that explains why:
  130. //
  131. // In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that avail_out
  132. // is greater than six to avoid repeated flush markers due to
  133. // avail_out == 0 on return.
  134. bool Flush();
  135. // Writes out all data and closes the gzip stream.
  136. // It is the caller's responsibility to close the underlying stream if
  137. // necessary.
  138. // Returns true if no error.
  139. bool Close();
  140. // implements ZeroCopyOutputStream ---------------------------------
  141. bool Next(void** data, int* size) override;
  142. void BackUp(int count) override;
  143. int64_t ByteCount() const override;
  144. private:
  145. ZeroCopyOutputStream* sub_stream_;
  146. // Result from calling Next() on sub_stream_
  147. void* sub_data_;
  148. int sub_data_size_;
  149. z_stream zcontext_;
  150. int zerror_;
  151. void* input_buffer_;
  152. size_t input_buffer_length_;
  153. // Shared constructor code.
  154. void Init(ZeroCopyOutputStream* sub_stream, const Options& options);
  155. // Do some compression.
  156. // Takes zlib flush mode.
  157. // Returns zlib error code.
  158. int Deflate(int flush);
  159. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipOutputStream);
  160. };
  161. } // namespace io
  162. } // namespace protobuf
  163. } // namespace google
  164. #include <google/protobuf/port_undef.inc>
  165. #endif // GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__