3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
24 #include <grpc/compression.h>
26 #include "src/core/lib/compression/algorithm_metadata.h"
27 #include "src/core/lib/compression/compression_internal.h"
28 #include "src/core/lib/gpr/useful.h"
29 #include "src/core/lib/slice/slice_utils.h"
30 #include "src/core/lib/surface/api_trace.h"
31 #include "src/core/lib/transport/static_metadata.h"
33 /* Interfaces related to MD */
35 grpc_message_compression_algorithm
36 grpc_message_compression_algorithm_from_slice(const grpc_slice& str) {
37 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_IDENTITY))
38 return GRPC_MESSAGE_COMPRESS_NONE;
39 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_DEFLATE))
40 return GRPC_MESSAGE_COMPRESS_DEFLATE;
41 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP))
42 return GRPC_MESSAGE_COMPRESS_GZIP;
43 return GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT;
46 grpc_stream_compression_algorithm grpc_stream_compression_algorithm_from_slice(
47 const grpc_slice& str) {
48 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_IDENTITY))
49 return GRPC_STREAM_COMPRESS_NONE;
50 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP))
51 return GRPC_STREAM_COMPRESS_GZIP;
52 return GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT;
55 grpc_mdelem grpc_message_compression_encoding_mdelem(
56 grpc_message_compression_algorithm algorithm) {
58 case GRPC_MESSAGE_COMPRESS_NONE:
59 return GRPC_MDELEM_GRPC_ENCODING_IDENTITY;
60 case GRPC_MESSAGE_COMPRESS_DEFLATE:
61 return GRPC_MDELEM_GRPC_ENCODING_DEFLATE;
62 case GRPC_MESSAGE_COMPRESS_GZIP:
63 return GRPC_MDELEM_GRPC_ENCODING_GZIP;
70 grpc_mdelem grpc_stream_compression_encoding_mdelem(
71 grpc_stream_compression_algorithm algorithm) {
73 case GRPC_STREAM_COMPRESS_NONE:
74 return GRPC_MDELEM_CONTENT_ENCODING_IDENTITY;
75 case GRPC_STREAM_COMPRESS_GZIP:
76 return GRPC_MDELEM_CONTENT_ENCODING_GZIP;
83 /* Interfaces performing transformation between compression algorithms and
85 grpc_message_compression_algorithm
86 grpc_compression_algorithm_to_message_compression_algorithm(
87 grpc_compression_algorithm algo) {
89 case GRPC_COMPRESS_DEFLATE:
90 return GRPC_MESSAGE_COMPRESS_DEFLATE;
91 case GRPC_COMPRESS_GZIP:
92 return GRPC_MESSAGE_COMPRESS_GZIP;
94 return GRPC_MESSAGE_COMPRESS_NONE;
98 grpc_stream_compression_algorithm
99 grpc_compression_algorithm_to_stream_compression_algorithm(
100 grpc_compression_algorithm algo) {
102 case GRPC_COMPRESS_STREAM_GZIP:
103 return GRPC_STREAM_COMPRESS_GZIP;
105 return GRPC_STREAM_COMPRESS_NONE;
109 uint32_t grpc_compression_bitset_to_message_bitset(uint32_t bitset) {
110 return bitset & ((1u << GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT) - 1);
113 uint32_t grpc_compression_bitset_to_stream_bitset(uint32_t bitset) {
114 uint32_t identity = (bitset & 1u);
115 uint32_t other_bits =
116 (bitset >> (GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT - 1)) &
117 ((1u << GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT) - 2);
118 return identity | other_bits;
121 uint32_t grpc_compression_bitset_from_message_stream_compression_bitset(
122 uint32_t message_bitset, uint32_t stream_bitset) {
123 uint32_t offset_stream_bitset =
124 (stream_bitset & 1u) |
125 ((stream_bitset & (~1u)) << (GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT - 1));
126 return message_bitset | offset_stream_bitset;
129 int grpc_compression_algorithm_from_message_stream_compression_algorithm(
130 grpc_compression_algorithm* algorithm,
131 grpc_message_compression_algorithm message_algorithm,
132 grpc_stream_compression_algorithm stream_algorithm) {
133 if (message_algorithm != GRPC_MESSAGE_COMPRESS_NONE &&
134 stream_algorithm != GRPC_STREAM_COMPRESS_NONE) {
135 *algorithm = GRPC_COMPRESS_NONE;
138 if (message_algorithm == GRPC_MESSAGE_COMPRESS_NONE) {
139 switch (stream_algorithm) {
140 case GRPC_STREAM_COMPRESS_NONE:
141 *algorithm = GRPC_COMPRESS_NONE;
143 case GRPC_STREAM_COMPRESS_GZIP:
144 *algorithm = GRPC_COMPRESS_STREAM_GZIP;
147 *algorithm = GRPC_COMPRESS_NONE;
151 switch (message_algorithm) {
152 case GRPC_MESSAGE_COMPRESS_NONE:
153 *algorithm = GRPC_COMPRESS_NONE;
155 case GRPC_MESSAGE_COMPRESS_DEFLATE:
156 *algorithm = GRPC_COMPRESS_DEFLATE;
158 case GRPC_MESSAGE_COMPRESS_GZIP:
159 *algorithm = GRPC_COMPRESS_GZIP;
162 *algorithm = GRPC_COMPRESS_NONE;
169 /* Interfaces for message compression. */
171 int grpc_message_compression_algorithm_name(
172 grpc_message_compression_algorithm algorithm, const char** name) {
174 "grpc_message_compression_algorithm_name(algorithm=%d, name=%p)", 2,
175 ((int)algorithm, name));
177 case GRPC_MESSAGE_COMPRESS_NONE:
180 case GRPC_MESSAGE_COMPRESS_DEFLATE:
183 case GRPC_MESSAGE_COMPRESS_GZIP:
186 case GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT:
192 /* TODO(dgq): Add the ability to specify parameters to the individual
193 * compression algorithms */
194 grpc_message_compression_algorithm grpc_message_compression_algorithm_for_level(
195 grpc_compression_level level, uint32_t accepted_encodings) {
196 GRPC_API_TRACE("grpc_message_compression_algorithm_for_level(level=%d)", 1,
198 if (level > GRPC_COMPRESS_LEVEL_HIGH) {
199 gpr_log(GPR_ERROR, "Unknown message compression level %d.",
200 static_cast<int>(level));
204 const size_t num_supported =
205 GPR_BITCOUNT(accepted_encodings) - 1; /* discard NONE */
206 if (level == GRPC_COMPRESS_LEVEL_NONE || num_supported == 0) {
207 return GRPC_MESSAGE_COMPRESS_NONE;
210 GPR_ASSERT(level > 0);
212 /* Establish a "ranking" or compression algorithms in increasing order of
214 * This is simplistic and we will probably want to introduce other dimensions
215 * in the future (cpu/memory cost, etc). */
216 const grpc_message_compression_algorithm algos_ranking[] = {
217 GRPC_MESSAGE_COMPRESS_GZIP, GRPC_MESSAGE_COMPRESS_DEFLATE};
219 /* intersect algos_ranking with the supported ones keeping the ranked order */
220 grpc_message_compression_algorithm
221 sorted_supported_algos[GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT];
222 size_t algos_supported_idx = 0;
223 for (size_t i = 0; i < GPR_ARRAY_SIZE(algos_ranking); i++) {
224 const grpc_message_compression_algorithm alg = algos_ranking[i];
225 for (size_t j = 0; j < num_supported; j++) {
226 if (GPR_BITGET(accepted_encodings, alg) == 1) {
227 /* if \a alg in supported */
228 sorted_supported_algos[algos_supported_idx++] = alg;
232 if (algos_supported_idx == num_supported) break;
236 case GRPC_COMPRESS_LEVEL_NONE:
237 abort(); /* should have been handled already */
238 case GRPC_COMPRESS_LEVEL_LOW:
239 return sorted_supported_algos[0];
240 case GRPC_COMPRESS_LEVEL_MED:
241 return sorted_supported_algos[num_supported / 2];
242 case GRPC_COMPRESS_LEVEL_HIGH:
243 return sorted_supported_algos[num_supported - 1];
249 int grpc_message_compression_algorithm_parse(
250 grpc_slice value, grpc_message_compression_algorithm* algorithm) {
251 if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_IDENTITY)) {
252 *algorithm = GRPC_MESSAGE_COMPRESS_NONE;
254 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_DEFLATE)) {
255 *algorithm = GRPC_MESSAGE_COMPRESS_DEFLATE;
257 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
258 *algorithm = GRPC_MESSAGE_COMPRESS_GZIP;
266 /* Interfaces for stream compression. */
268 int grpc_stream_compression_algorithm_parse(
269 grpc_slice value, grpc_stream_compression_algorithm* algorithm) {
270 if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_IDENTITY)) {
271 *algorithm = GRPC_STREAM_COMPRESS_NONE;
273 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
274 *algorithm = GRPC_STREAM_COMPRESS_GZIP;