message_compress.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
23 #include <string.h>
24 
25 #include <zconf.h>
26 #include <zlib.h>
27 
28 #include <grpc/slice_buffer.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 
33 
34 #define OUTPUT_BLOCK_SIZE 1024
35 
38  int (*flate)(z_stream* zs, int flush)) {
39  int r = Z_STREAM_END; /* Do not fail on an empty input. */
40  int flush;
41  size_t i;
43  const uInt uint_max = ~static_cast<uInt>(0);
44 
45  GPR_ASSERT(GRPC_SLICE_LENGTH(outbuf) <= uint_max);
46  zs->avail_out = static_cast<uInt> GRPC_SLICE_LENGTH(outbuf);
48  flush = Z_NO_FLUSH;
49  for (i = 0; i < input->count; i++) {
50  if (i == input->count - 1) flush = Z_FINISH;
51  GPR_ASSERT(GRPC_SLICE_LENGTH(input->slices[i]) <= uint_max);
52  zs->avail_in = static_cast<uInt> GRPC_SLICE_LENGTH(input->slices[i]);
53  zs->next_in = GRPC_SLICE_START_PTR(input->slices[i]);
54  do {
55  if (zs->avail_out == 0) {
58  GPR_ASSERT(GRPC_SLICE_LENGTH(outbuf) <= uint_max);
59  zs->avail_out = static_cast<uInt> GRPC_SLICE_LENGTH(outbuf);
61  }
62  r = flate(zs, flush);
63  if (r < 0 && r != Z_BUF_ERROR /* not fatal */) {
64  gpr_log(GPR_INFO, "zlib error (%d)", r);
65  goto error;
66  }
67  } while (zs->avail_out == 0);
68  if (zs->avail_in) {
69  gpr_log(GPR_INFO, "zlib: not all input consumed");
70  goto error;
71  }
72  }
73  if (r != Z_STREAM_END) {
74  gpr_log(GPR_INFO, "zlib: Data error");
75  goto error;
76  }
77 
78  GPR_ASSERT(outbuf.refcount);
79  outbuf.data.refcounted.length -= zs->avail_out;
81 
82  return 1;
83 
84 error:
86  return 0;
87 }
88 
89 static void* zalloc_gpr(void* /*opaque*/, unsigned int items,
90  unsigned int size) {
91  return gpr_malloc(items * size);
92 }
93 
94 static void zfree_gpr(void* /*opaque*/, void* address) { gpr_free(address); }
95 
97  int gzip) {
98  z_stream zs;
99  int r;
100  size_t i;
101  size_t count_before = output->count;
102  size_t length_before = output->length;
103  memset(&zs, 0, sizeof(zs));
104  zs.zalloc = zalloc_gpr;
105  zs.zfree = zfree_gpr;
106  r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0),
107  8, Z_DEFAULT_STRATEGY);
108  GPR_ASSERT(r == Z_OK);
109  r = zlib_body(&zs, input, output, deflate) && output->length < input->length;
110  if (!r) {
111  for (i = count_before; i < output->count; i++) {
113  }
114  output->count = count_before;
115  output->length = length_before;
116  }
117  deflateEnd(&zs);
118  return r;
119 }
120 
122  int gzip) {
123  z_stream zs;
124  int r;
125  size_t i;
126  size_t count_before = output->count;
127  size_t length_before = output->length;
128  memset(&zs, 0, sizeof(zs));
129  zs.zalloc = zalloc_gpr;
130  zs.zfree = zfree_gpr;
131  r = inflateInit2(&zs, 15 | (gzip ? 16 : 0));
132  GPR_ASSERT(r == Z_OK);
133  r = zlib_body(&zs, input, output, inflate);
134  if (!r) {
135  for (i = count_before; i < output->count; i++) {
137  }
138  output->count = count_before;
139  output->length = length_before;
140  }
141  inflateEnd(&zs);
142  return r;
143 }
144 
146  size_t i;
147  for (i = 0; i < input->count; i++) {
149  }
150  return 1;
151 }
152 
155  switch (algorithm) {
156  case GRPC_COMPRESS_NONE:
157  /* the fallback path always needs to be send uncompressed: we simply
158  rely on that here */
159  return 0;
161  return zlib_compress(input, output, 0);
162  case GRPC_COMPRESS_GZIP:
163  return zlib_compress(input, output, 1);
165  break;
166  }
167  gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm);
168  return 0;
169 }
170 
173  if (!compress_inner(algorithm, input, output)) {
174  copy(input, output);
175  return 0;
176  }
177  return 1;
178 }
179 
182  switch (algorithm) {
183  case GRPC_COMPRESS_NONE:
184  return copy(input, output);
186  return zlib_decompress(input, output, 0);
187  case GRPC_COMPRESS_GZIP:
188  return zlib_decompress(input, output, 1);
190  break;
191  }
192  gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm);
193  return 0;
194 }
inflateEnd
int ZEXPORT inflateEnd(z_streamp strm)
Definition: bloaty/third_party/zlib/inflate.c:1277
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
GRPC_COMPRESS_DEFLATE
@ GRPC_COMPRESS_DEFLATE
Definition: compression_types.h:62
zlib_decompress
static int zlib_decompress(grpc_slice_buffer *input, grpc_slice_buffer *output, int gzip)
Definition: message_compress.cc:121
Z_DEFAULT_COMPRESSION
#define Z_DEFAULT_COMPRESSION
Definition: bloaty/third_party/zlib/zlib.h:193
log.h
grpc_slice_ref_internal
const grpc_slice & grpc_slice_ref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:32
inflate
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: bloaty/third_party/zlib/inflate.c:622
memset
return memset(p, 0, total)
Z_NO_FLUSH
#define Z_NO_FLUSH
Definition: bloaty/third_party/zlib/zlib.h:168
zlib_body
static int zlib_body(z_stream *zs, grpc_slice_buffer *input, grpc_slice_buffer *output, int(*flate)(z_stream *zs, int flush))
Definition: message_compress.cc:36
string.h
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
outbuf
unsigned char outbuf[SIZE]
Definition: bloaty/third_party/zlib/examples/gun.c:162
grpc_msg_compress
int grpc_msg_compress(grpc_compression_algorithm algorithm, grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:171
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
GRPC_SLICE_MALLOC
#define GRPC_SLICE_MALLOC(len)
Definition: include/grpc/slice.h:70
z_stream_s::zfree
free_func zfree
Definition: bloaty/third_party/zlib/zlib.h:99
error
grpc_error_handle error
Definition: retry_filter.cc:499
OUTPUT_BLOCK_SIZE
#define OUTPUT_BLOCK_SIZE
Definition: message_compress.cc:34
zlib_compress
static int zlib_compress(grpc_slice_buffer *input, grpc_slice_buffer *output, int gzip)
Definition: message_compress.cc:96
Z_FINISH
#define Z_FINISH
Definition: bloaty/third_party/zlib/zlib.h:172
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
z_stream_s::avail_in
uInt avail_in
Definition: bloaty/third_party/zlib/zlib.h:88
Z_STREAM_END
#define Z_STREAM_END
Definition: bloaty/third_party/zlib/zlib.h:178
GRPC_COMPRESS_NONE
@ GRPC_COMPRESS_NONE
Definition: compression_types.h:61
z_stream_s::next_out
Bytef * next_out
Definition: bloaty/third_party/zlib/zlib.h:91
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
Z_OK
#define Z_OK
Definition: bloaty/third_party/zlib/zlib.h:177
z_stream_s::avail_out
uInt avail_out
Definition: bloaty/third_party/zlib/zlib.h:92
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
slice_buffer.h
Z_BUF_ERROR
#define Z_BUF_ERROR
Definition: bloaty/third_party/zlib/zlib.h:184
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_msg_decompress
int grpc_msg_decompress(grpc_compression_algorithm algorithm, grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:180
deflateEnd
int ZEXPORT deflateEnd(z_streamp strm)
Definition: bloaty/third_party/zlib/deflate.c:1076
z_stream_s::zalloc
alloc_func zalloc
Definition: bloaty/third_party/zlib/zlib.h:98
xds_manager.items
items
Definition: xds_manager.py:55
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
z_stream_s
Definition: bloaty/third_party/zlib/zlib.h:86
zalloc_gpr
static void * zalloc_gpr(void *, unsigned int items, unsigned int size)
Definition: message_compress.cc:89
grpc_slice_buffer_add
GPRAPI void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:170
z_stream_s::next_in
z_const Bytef * next_in
Definition: bloaty/third_party/zlib/zlib.h:87
uInt
unsigned int uInt
Definition: bloaty/third_party/zlib/zconf.h:393
alloc.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
GRPC_COMPRESS_ALGORITHMS_COUNT
@ GRPC_COMPRESS_ALGORITHMS_COUNT
Definition: compression_types.h:65
GRPC_COMPRESS_GZIP
@ GRPC_COMPRESS_GZIP
Definition: compression_types.h:63
deflate
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: bloaty/third_party/zlib/deflate.c:763
slice_refcount.h
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
zfree_gpr
static void zfree_gpr(void *, void *address)
Definition: message_compress.cc:94
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
compress_inner
static int compress_inner(grpc_compression_algorithm algorithm, grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:153
message_compress.h
deflateInit2
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: bloaty/third_party/zlib/zlib.h:1797
inflateInit2
#define inflateInit2(strm, windowBits)
Definition: bloaty/third_party/zlib/zlib.h:1800
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_slice_unref_internal
void grpc_slice_unref_internal(const grpc_slice &slice)
Definition: slice_refcount.h:39
grpc_slice_buffer_add_indexed
GPRAPI size_t grpc_slice_buffer_add_indexed(grpc_slice_buffer *sb, grpc_slice slice)
Definition: slice/slice_buffer.cc:161
Z_DEFLATED
#define Z_DEFLATED
Definition: bloaty/third_party/zlib/zlib.h:209
Z_DEFAULT_STRATEGY
#define Z_DEFAULT_STRATEGY
Definition: bloaty/third_party/zlib/zlib.h:200
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:37