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 8.6 kB

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