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