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