/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/io/gzip_stream.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 207 lines · 90 code · 40 blank · 77 comment · 0 complexity · 1535b018b21eb7175ccd268ffa82cd77 MD5 · raw file

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