gzip_stream.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <zlib.h>
  44. #include <google/protobuf/stubs/common.h>
  45. #include <google/protobuf/io/zero_copy_stream.h>
  46. namespace google {
  47. namespace protobuf {
  48. namespace io {
  49. // A ZeroCopyInputStream that reads compressed data through zlib
  50. class LIBPROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream {
  51. public:
  52. // Format key for constructor
  53. enum Format {
  54. // zlib will autodetect gzip header or deflate stream
  55. AUTO = 0,
  56. // GZIP streams have some extra header data for file attributes.
  57. GZIP = 1,
  58. // Simpler zlib stream format.
  59. ZLIB = 2,
  60. };
  61. // buffer_size and format may be -1 for default of 64kB and GZIP format
  62. explicit GzipInputStream(
  63. ZeroCopyInputStream* sub_stream,
  64. Format format = AUTO,
  65. int buffer_size = -1);
  66. virtual ~GzipInputStream();
  67. // Return last error message or NULL if no error.
  68. inline const char* ZlibErrorMessage() const {
  69. return zcontext_.msg;
  70. }
  71. inline int ZlibErrorCode() const {
  72. return zerror_;
  73. }
  74. // implements ZeroCopyInputStream ----------------------------------
  75. bool Next(const void** data, int* size);
  76. void BackUp(int count);
  77. bool Skip(int count);
  78. int64 ByteCount() const;
  79. private:
  80. Format format_;
  81. ZeroCopyInputStream* sub_stream_;
  82. z_stream zcontext_;
  83. int zerror_;
  84. void* output_buffer_;
  85. void* output_position_;
  86. size_t output_buffer_length_;
  87. int Inflate(int flush);
  88. void DoNextOutput(const void** data, int* size);
  89. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipInputStream);
  90. };
  91. class LIBPROTOBUF_EXPORT GzipOutputStream : 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 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(
  118. ZeroCopyOutputStream* sub_stream,
  119. const Options& options);
  120. virtual ~GzipOutputStream();
  121. // Return last error message or NULL if no error.
  122. inline const char* ZlibErrorMessage() const {
  123. return zcontext_.msg;
  124. }
  125. inline int ZlibErrorCode() const {
  126. return zerror_;
  127. }
  128. // Flushes data written so far to zipped data in the underlying stream.
  129. // It is the caller's responsibility to flush the underlying stream if
  130. // necessary.
  131. // Compression may be less efficient stopping and starting around flushes.
  132. // Returns true if no error.
  133. //
  134. // Please ensure that block size is > 6. Here is an excerpt from the zlib
  135. // doc that explains why:
  136. //
  137. // In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that avail_out
  138. // is greater than six to avoid repeated flush markers due to
  139. // avail_out == 0 on return.
  140. bool Flush();
  141. // Writes out all data and closes the gzip stream.
  142. // It is the caller's responsibility to close the underlying stream if
  143. // necessary.
  144. // Returns true if no error.
  145. bool Close();
  146. // implements ZeroCopyOutputStream ---------------------------------
  147. bool Next(void** data, int* size);
  148. void BackUp(int count);
  149. int64 ByteCount() const;
  150. private:
  151. ZeroCopyOutputStream* sub_stream_;
  152. // Result from calling Next() on sub_stream_
  153. void* sub_data_;
  154. int sub_data_size_;
  155. z_stream zcontext_;
  156. int zerror_;
  157. void* input_buffer_;
  158. size_t input_buffer_length_;
  159. // Shared constructor code.
  160. void Init(ZeroCopyOutputStream* sub_stream, const Options& options);
  161. // Do some compression.
  162. // Takes zlib flush mode.
  163. // Returns zlib error code.
  164. int Deflate(int flush);
  165. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipOutputStream);
  166. };
  167. } // namespace io
  168. } // namespace protobuf
  169. } // namespace google
  170. #endif // GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__