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