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>
21 #include "src/core/lib/compression/compression_internal.h"
26 #include <grpc/compression.h>
28 #include "src/core/lib/compression/algorithm_metadata.h"
29 #include "src/core/lib/gpr/useful.h"
30 #include "src/core/lib/slice/slice_utils.h"
31 #include "src/core/lib/surface/api_trace.h"
32 #include "src/core/lib/transport/static_metadata.h"
34 /* Interfaces related to MD */
36 grpc_message_compression_algorithm
37 grpc_message_compression_algorithm_from_slice(const grpc_slice& str) {
38 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_IDENTITY)) {
39 return GRPC_MESSAGE_COMPRESS_NONE;
41 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_DEFLATE)) {
42 return GRPC_MESSAGE_COMPRESS_DEFLATE;
44 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
45 return GRPC_MESSAGE_COMPRESS_GZIP;
47 return GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT;
50 grpc_stream_compression_algorithm grpc_stream_compression_algorithm_from_slice(
51 const grpc_slice& str) {
52 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_IDENTITY)) {
53 return GRPC_STREAM_COMPRESS_NONE;
55 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
56 return GRPC_STREAM_COMPRESS_GZIP;
58 return GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT;
61 grpc_mdelem grpc_message_compression_encoding_mdelem(
62 grpc_message_compression_algorithm algorithm) {
64 case GRPC_MESSAGE_COMPRESS_NONE:
65 return GRPC_MDELEM_GRPC_ENCODING_IDENTITY;
66 case GRPC_MESSAGE_COMPRESS_DEFLATE:
67 return GRPC_MDELEM_GRPC_ENCODING_DEFLATE;
68 case GRPC_MESSAGE_COMPRESS_GZIP:
69 return GRPC_MDELEM_GRPC_ENCODING_GZIP;
76 grpc_mdelem grpc_stream_compression_encoding_mdelem(
77 grpc_stream_compression_algorithm algorithm) {
79 case GRPC_STREAM_COMPRESS_NONE:
80 return GRPC_MDELEM_CONTENT_ENCODING_IDENTITY;
81 case GRPC_STREAM_COMPRESS_GZIP:
82 return GRPC_MDELEM_CONTENT_ENCODING_GZIP;
89 /* Interfaces performing transformation between compression algorithms and
91 grpc_message_compression_algorithm
92 grpc_compression_algorithm_to_message_compression_algorithm(
93 grpc_compression_algorithm algo) {
95 case GRPC_COMPRESS_DEFLATE:
96 return GRPC_MESSAGE_COMPRESS_DEFLATE;
97 case GRPC_COMPRESS_GZIP:
98 return GRPC_MESSAGE_COMPRESS_GZIP;
100 return GRPC_MESSAGE_COMPRESS_NONE;
104 grpc_stream_compression_algorithm
105 grpc_compression_algorithm_to_stream_compression_algorithm(
106 grpc_compression_algorithm algo) {
108 case GRPC_COMPRESS_STREAM_GZIP:
109 return GRPC_STREAM_COMPRESS_GZIP;
111 return GRPC_STREAM_COMPRESS_NONE;
115 uint32_t grpc_compression_bitset_to_message_bitset(uint32_t bitset) {
116 return bitset & ((1u << GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT) - 1);
119 uint32_t grpc_compression_bitset_to_stream_bitset(uint32_t bitset) {
120 uint32_t identity = (bitset & 1u);
121 uint32_t other_bits =
122 (bitset >> (GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT - 1)) &
123 ((1u << GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT) - 2);
124 return identity | other_bits;
127 uint32_t grpc_compression_bitset_from_message_stream_compression_bitset(
128 uint32_t message_bitset, uint32_t stream_bitset) {
129 uint32_t offset_stream_bitset =
130 (stream_bitset & 1u) |
131 ((stream_bitset & (~1u)) << (GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT - 1));
132 return message_bitset | offset_stream_bitset;
135 int grpc_compression_algorithm_from_message_stream_compression_algorithm(
136 grpc_compression_algorithm* algorithm,
137 grpc_message_compression_algorithm message_algorithm,
138 grpc_stream_compression_algorithm stream_algorithm) {
139 if (message_algorithm != GRPC_MESSAGE_COMPRESS_NONE &&
140 stream_algorithm != GRPC_STREAM_COMPRESS_NONE) {
141 *algorithm = GRPC_COMPRESS_NONE;
144 if (message_algorithm == GRPC_MESSAGE_COMPRESS_NONE) {
145 switch (stream_algorithm) {
146 case GRPC_STREAM_COMPRESS_NONE:
147 *algorithm = GRPC_COMPRESS_NONE;
149 case GRPC_STREAM_COMPRESS_GZIP:
150 *algorithm = GRPC_COMPRESS_STREAM_GZIP;
153 *algorithm = GRPC_COMPRESS_NONE;
157 switch (message_algorithm) {
158 case GRPC_MESSAGE_COMPRESS_NONE:
159 *algorithm = GRPC_COMPRESS_NONE;
161 case GRPC_MESSAGE_COMPRESS_DEFLATE:
162 *algorithm = GRPC_COMPRESS_DEFLATE;
164 case GRPC_MESSAGE_COMPRESS_GZIP:
165 *algorithm = GRPC_COMPRESS_GZIP;
168 *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;
270 /* Interfaces for stream compression. */
272 int grpc_stream_compression_algorithm_parse(
273 grpc_slice value, grpc_stream_compression_algorithm* algorithm) {
274 if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_IDENTITY)) {
275 *algorithm = GRPC_STREAM_COMPRESS_NONE;
277 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
278 *algorithm = GRPC_STREAM_COMPRESS_GZIP;