protobuf/src/google/protobuf/io/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 
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.
79  inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
80  inline int ZlibErrorCode() const { return zerror_; }
81 
82  // implements ZeroCopyInputStream ----------------------------------
83  bool Next(const void** data, int* size) override;
84  void BackUp(int count) override;
85  bool Skip(int count) override;
86  int64_t ByteCount() const override;
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_;
100 
101  int Inflate(int flush);
102  void DoNextOutput(const void** data, int* size);
103 
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.
146  inline const char* ZlibErrorMessage() const { return zcontext_.msg; }
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) override;
171  void BackUp(int count) override;
172  int64_t ByteCount() const override;
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 
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__
http2_test_server.format
format
Definition: http2_test_server.py:118
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/macros.h:40
grpc::protobuf::io::ZeroCopyInputStream
GRPC_CUSTOM_ZEROCOPYINPUTSTREAM ZeroCopyInputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:101
grpc._compression.Deflate
Deflate
Definition: _compression.py:18
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::io::GzipOutputStream::ZlibErrorMessage
const char * ZlibErrorMessage() const
Definition: protobuf/src/google/protobuf/io/gzip_stream.h:146
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::python::cmessage::Init
static int Init(CMessage *self, PyObject *args, PyObject *kwargs)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:1287
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
absl::base_internal::Next
static AllocList * Next(int i, AllocList *prev, LowLevelAlloc::Arena *arena)
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:453
google::protobuf::io::GzipInputStream::byte_count_
int64_t byte_count_
Definition: protobuf/src/google/protobuf/io/gzip_stream.h:99
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
GZIP
#define GZIP
Definition: bloaty/third_party/zlib/deflate.h:23
google::protobuf::io::GzipInputStream::Format
Format
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/gzip_stream.h:61
google::protobuf::io::GzipInputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/gzip_stream.h:58
io
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
google::protobuf::io::GzipInputStream::ZlibErrorCode
int ZlibErrorCode() const
Definition: protobuf/src/google/protobuf/io/gzip_stream.h:80
z_stream_s
Definition: bloaty/third_party/zlib/zlib.h:86
google::protobuf::io::ZeroCopyInputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h:126
absl::Skip
static PerThreadSynch * Skip(PerThreadSynch *x)
Definition: abseil-cpp/absl/synchronization/mutex.cc:837
testing::internal::posix::Close
int Close(int fd)
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2050
google::protobuf::io::GzipOutputStream::Format
Format
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/gzip_stream.h:109
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
google::protobuf::io::ZeroCopyOutputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h:183
absl::Format
bool Format(FormatRawSink raw_sink, const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:504
google::protobuf::io::GzipInputStream::ZlibErrorMessage
const char * ZlibErrorMessage() const
Definition: protobuf/src/google/protobuf/io/gzip_stream.h:79
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
versiongenerate.buffer_size
int buffer_size
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/xcode/Scripts/versiongenerate.py:65
google::protobuf::io::GzipOutputStream::ZlibErrorCode
int ZlibErrorCode() const
Definition: protobuf/src/google/protobuf/io/gzip_stream.h:147
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::io::GzipOutputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/gzip_stream.h:106


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:59