gzip_stream.h
Go to the documentation of this file.
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 
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.
78  inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
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:
89 
91 
92  z_stream zcontext_;
93  int zerror_;
94 
99 
100  int Inflate(int flush);
101  void DoNextOutput(const void** data, int* size);
102 
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.
120 
121  // What size buffer to use internally. Defaults to 64kB.
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).
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.
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.
141 
142  virtual ~GzipOutputStream();
143 
144  // Return last error message or NULL if no error.
145  inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
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:
175  // Result from calling Next() on sub_stream_
176  void* sub_data_;
178 
179  z_stream zcontext_;
180  int zerror_;
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 
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__
zero_copy_stream.h
google::protobuf::io::GzipInputStream::output_buffer_
void * output_buffer_
Definition: gzip_stream.h:95
google::protobuf::io::GzipInputStream::zcontext_
z_stream zcontext_
Definition: gzip_stream.h:92
google::protobuf::io::GzipOutputStream::zcontext_
z_stream zcontext_
Definition: gzip_stream.h:179
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
google::protobuf::io::GzipOutputStream::sub_data_
void * sub_data_
Definition: gzip_stream.h:176
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::io::GzipOutputStream::ZlibErrorMessage
const char * ZlibErrorMessage() const
Definition: gzip_stream.h:145
google::protobuf::python::cmessage::Init
static int Init(CMessage *self, PyObject *args, PyObject *kwargs)
Definition: python/google/protobuf/pyext/message.cc:1286
google::protobuf::io::GzipOutputStream::Options
Definition: gzip_stream.h:117
google::protobuf::io::GzipInputStream::format_
Format format_
Definition: gzip_stream.h:88
port.h
google::protobuf::io::GzipInputStream::output_buffer_length_
size_t output_buffer_length_
Definition: gzip_stream.h:97
google::protobuf::io::GzipOutputStream::sub_data_size_
int sub_data_size_
Definition: gzip_stream.h:177
google::protobuf::io::GzipInputStream::byte_count_
int64 byte_count_
Definition: gzip_stream.h:98
google::protobuf::io::GzipOutputStream::input_buffer_
void * input_buffer_
Definition: gzip_stream.h:181
google::protobuf::io::GzipOutputStream::Options::buffer_size
int buffer_size
Definition: gzip_stream.h:122
format
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:2773
google::protobuf::io::GzipInputStream::Format
Format
Definition: gzip_stream.h:61
google::protobuf::io::GzipInputStream
Definition: gzip_stream.h:58
google::protobuf::io::GzipInputStream::ZlibErrorCode
int ZlibErrorCode() const
Definition: gzip_stream.h:79
google::protobuf::io::GzipOutputStream::Options::compression_strategy
int compression_strategy
Definition: gzip_stream.h:131
google::protobuf::io::GzipOutputStream::sub_stream_
ZeroCopyOutputStream * sub_stream_
Definition: gzip_stream.h:174
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
versiongenerate.buffer_size
int buffer_size
Definition: versiongenerate.py:65
testing::internal::posix::Close
int Close(int fd)
Definition: gtest-port.h:2132
google::protobuf::io::GzipOutputStream::Format
Format
Definition: gzip_stream.h:109
common.h
google::protobuf::io::GzipInputStream::zerror_
int zerror_
Definition: gzip_stream.h:93
google::protobuf::io::GzipOutputStream::zerror_
int zerror_
Definition: gzip_stream.h:180
pump.Skip
def Skip(lines, pos, regex)
Definition: pump.py:261
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf::io::GzipOutputStream::input_buffer_length_
size_t input_buffer_length_
Definition: gzip_stream.h:182
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
google::protobuf::io::GzipOutputStream::Options::compression_level
int compression_level
Definition: gzip_stream.h:126
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
google::protobuf::io::GzipOutputStream::Options::format
Format format
Definition: gzip_stream.h:119
google::protobuf::io::GzipInputStream::ZlibErrorMessage
const char * ZlibErrorMessage() const
Definition: gzip_stream.h:78
count
GLint GLsizei count
Definition: glcorearb.h:2830
google::protobuf::io::GzipOutputStream::ZlibErrorCode
int ZlibErrorCode() const
Definition: gzip_stream.h:146
google::protobuf::io::GzipInputStream::output_position_
void * output_position_
Definition: gzip_stream.h:96
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::io::GzipOutputStream
Definition: gzip_stream.h:106
google::protobuf::io::GzipInputStream::sub_stream_
ZeroCopyInputStream * sub_stream_
Definition: gzip_stream.h:90


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54