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