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;
40 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_DEFLATE)) {
41 return GRPC_MESSAGE_COMPRESS_DEFLATE;
43 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
44 return GRPC_MESSAGE_COMPRESS_GZIP;
46 return GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT;
49 grpc_stream_compression_algorithm grpc_stream_compression_algorithm_from_slice(
50 const grpc_slice& str) {
51 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_IDENTITY)) {
52 return GRPC_STREAM_COMPRESS_NONE;
54 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
55 return GRPC_STREAM_COMPRESS_GZIP;
57 return GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT;
60 grpc_mdelem grpc_message_compression_encoding_mdelem(
61 grpc_message_compression_algorithm algorithm) {
63 case GRPC_MESSAGE_COMPRESS_NONE:
64 return GRPC_MDELEM_GRPC_ENCODING_IDENTITY;
65 case GRPC_MESSAGE_COMPRESS_DEFLATE:
66 return GRPC_MDELEM_GRPC_ENCODING_DEFLATE;
67 case GRPC_MESSAGE_COMPRESS_GZIP:
68 return GRPC_MDELEM_GRPC_ENCODING_GZIP;
75 grpc_mdelem grpc_stream_compression_encoding_mdelem(
76 grpc_stream_compression_algorithm algorithm) {
78 case GRPC_STREAM_COMPRESS_NONE:
79 return GRPC_MDELEM_CONTENT_ENCODING_IDENTITY;
80 case GRPC_STREAM_COMPRESS_GZIP:
81 return GRPC_MDELEM_CONTENT_ENCODING_GZIP;
88 /* Interfaces performing transformation between compression algorithms and
90 grpc_message_compression_algorithm
91 grpc_compression_algorithm_to_message_compression_algorithm(
92 grpc_compression_algorithm algo) {
94 case GRPC_COMPRESS_DEFLATE:
95 return GRPC_MESSAGE_COMPRESS_DEFLATE;
96 case GRPC_COMPRESS_GZIP:
97 return GRPC_MESSAGE_COMPRESS_GZIP;
99 return GRPC_MESSAGE_COMPRESS_NONE;
103 grpc_stream_compression_algorithm
104 grpc_compression_algorithm_to_stream_compression_algorithm(
105 grpc_compression_algorithm algo) {
107 case GRPC_COMPRESS_STREAM_GZIP:
108 return GRPC_STREAM_COMPRESS_GZIP;
110 return GRPC_STREAM_COMPRESS_NONE;
114 uint32_t grpc_compression_bitset_to_message_bitset(uint32_t bitset) {
115 return bitset & ((1u << GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT) - 1);
118 uint32_t grpc_compression_bitset_to_stream_bitset(uint32_t bitset) {
119 uint32_t identity = (bitset & 1u);
120 uint32_t other_bits =
121 (bitset >> (GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT - 1)) &
122 ((1u << GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT) - 2);
123 return identity | other_bits;
126 uint32_t grpc_compression_bitset_from_message_stream_compression_bitset(
127 uint32_t message_bitset, uint32_t stream_bitset) {
128 uint32_t offset_stream_bitset =
129 (stream_bitset & 1u) |
130 ((stream_bitset & (~1u)) << (GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT - 1));
131 return message_bitset | offset_stream_bitset;
134 int grpc_compression_algorithm_from_message_stream_compression_algorithm(
135 grpc_compression_algorithm* algorithm,
136 grpc_message_compression_algorithm message_algorithm,
137 grpc_stream_compression_algorithm stream_algorithm) {
138 if (message_algorithm != GRPC_MESSAGE_COMPRESS_NONE &&
139 stream_algorithm != GRPC_STREAM_COMPRESS_NONE) {
140 *algorithm = GRPC_COMPRESS_NONE;
143 if (message_algorithm == GRPC_MESSAGE_COMPRESS_NONE) {
144 switch (stream_algorithm) {
145 case GRPC_STREAM_COMPRESS_NONE:
146 *algorithm = GRPC_COMPRESS_NONE;
148 case GRPC_STREAM_COMPRESS_GZIP:
149 *algorithm = GRPC_COMPRESS_STREAM_GZIP;
152 *algorithm = GRPC_COMPRESS_NONE;
156 switch (message_algorithm) {
157 case GRPC_MESSAGE_COMPRESS_NONE:
158 *algorithm = GRPC_COMPRESS_NONE;
160 case GRPC_MESSAGE_COMPRESS_DEFLATE:
161 *algorithm = GRPC_COMPRESS_DEFLATE;
163 case GRPC_MESSAGE_COMPRESS_GZIP:
164 *algorithm = GRPC_COMPRESS_GZIP;
167 *algorithm = GRPC_COMPRESS_NONE;
174 /* Interfaces for message compression. */
176 int grpc_message_compression_algorithm_name(
177 grpc_message_compression_algorithm algorithm, const char** name) {
179 "grpc_message_compression_algorithm_name(algorithm=%d, name=%p)", 2,
180 ((int)algorithm, name));
182 case GRPC_MESSAGE_COMPRESS_NONE:
185 case GRPC_MESSAGE_COMPRESS_DEFLATE:
188 case GRPC_MESSAGE_COMPRESS_GZIP:
191 case GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT:
197 /* TODO(dgq): Add the ability to specify parameters to the individual
198 * compression algorithms */
199 grpc_message_compression_algorithm grpc_message_compression_algorithm_for_level(
200 grpc_compression_level level, uint32_t accepted_encodings) {
201 GRPC_API_TRACE("grpc_message_compression_algorithm_for_level(level=%d)", 1,
203 if (level > GRPC_COMPRESS_LEVEL_HIGH) {
204 gpr_log(GPR_ERROR, "Unknown message compression level %d.",
205 static_cast<int>(level));
209 const size_t num_supported =
210 GPR_BITCOUNT(accepted_encodings) - 1; /* discard NONE */
211 if (level == GRPC_COMPRESS_LEVEL_NONE || num_supported == 0) {
212 return GRPC_MESSAGE_COMPRESS_NONE;
215 GPR_ASSERT(level > 0);
217 /* Establish a "ranking" or compression algorithms in increasing order of
219 * This is simplistic and we will probably want to introduce other dimensions
220 * in the future (cpu/memory cost, etc). */
221 const grpc_message_compression_algorithm algos_ranking[] = {
222 GRPC_MESSAGE_COMPRESS_GZIP, GRPC_MESSAGE_COMPRESS_DEFLATE};
224 /* intersect algos_ranking with the supported ones keeping the ranked order */
225 grpc_message_compression_algorithm
226 sorted_supported_algos[GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT];
227 size_t algos_supported_idx = 0;
228 for (size_t i = 0; i < GPR_ARRAY_SIZE(algos_ranking); i++) {
229 const grpc_message_compression_algorithm alg = algos_ranking[i];
230 for (size_t j = 0; j < num_supported; j++) {
231 if (GPR_BITGET(accepted_encodings, alg) == 1) {
232 /* if \a alg in supported */
233 sorted_supported_algos[algos_supported_idx++] = alg;
237 if (algos_supported_idx == num_supported) break;
241 case GRPC_COMPRESS_LEVEL_NONE:
242 abort(); /* should have been handled already */
243 case GRPC_COMPRESS_LEVEL_LOW:
244 return sorted_supported_algos[0];
245 case GRPC_COMPRESS_LEVEL_MED:
246 return sorted_supported_algos[num_supported / 2];
247 case GRPC_COMPRESS_LEVEL_HIGH:
248 return sorted_supported_algos[num_supported - 1];
254 int grpc_message_compression_algorithm_parse(
255 grpc_slice value, grpc_message_compression_algorithm* algorithm) {
256 if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_IDENTITY)) {
257 *algorithm = GRPC_MESSAGE_COMPRESS_NONE;
259 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_DEFLATE)) {
260 *algorithm = GRPC_MESSAGE_COMPRESS_DEFLATE;
262 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
263 *algorithm = GRPC_MESSAGE_COMPRESS_GZIP;
271 /* Interfaces for stream compression. */
273 int grpc_stream_compression_algorithm_parse(
274 grpc_slice value, grpc_stream_compression_algorithm* algorithm) {
275 if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_IDENTITY)) {
276 *algorithm = GRPC_STREAM_COMPRESS_NONE;
278 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
279 *algorithm = GRPC_STREAM_COMPRESS_GZIP;