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
19 #include <grpc/support/port_platform.h>
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <grpc/compression.h>
25
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"
32
33 /* Interfaces related to MD */
34
35 grpc_message_compression_algorithm
grpc_message_compression_algorithm_from_slice(const grpc_slice & str)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 }
40 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_DEFLATE)) {
41 return GRPC_MESSAGE_COMPRESS_DEFLATE;
42 }
43 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
44 return GRPC_MESSAGE_COMPRESS_GZIP;
45 }
46 return GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT;
47 }
48
grpc_stream_compression_algorithm_from_slice(const grpc_slice & str)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;
53 }
54 if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
55 return GRPC_STREAM_COMPRESS_GZIP;
56 }
57 return GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT;
58 }
59
grpc_message_compression_encoding_mdelem(grpc_message_compression_algorithm algorithm)60 grpc_mdelem grpc_message_compression_encoding_mdelem(
61 grpc_message_compression_algorithm algorithm) {
62 switch (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;
69 default:
70 break;
71 }
72 return GRPC_MDNULL;
73 }
74
grpc_stream_compression_encoding_mdelem(grpc_stream_compression_algorithm algorithm)75 grpc_mdelem grpc_stream_compression_encoding_mdelem(
76 grpc_stream_compression_algorithm algorithm) {
77 switch (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;
82 default:
83 break;
84 }
85 return GRPC_MDNULL;
86 }
87
88 /* Interfaces performing transformation between compression algorithms and
89 * levels. */
90 grpc_message_compression_algorithm
grpc_compression_algorithm_to_message_compression_algorithm(grpc_compression_algorithm algo)91 grpc_compression_algorithm_to_message_compression_algorithm(
92 grpc_compression_algorithm algo) {
93 switch (algo) {
94 case GRPC_COMPRESS_DEFLATE:
95 return GRPC_MESSAGE_COMPRESS_DEFLATE;
96 case GRPC_COMPRESS_GZIP:
97 return GRPC_MESSAGE_COMPRESS_GZIP;
98 default:
99 return GRPC_MESSAGE_COMPRESS_NONE;
100 }
101 }
102
103 grpc_stream_compression_algorithm
grpc_compression_algorithm_to_stream_compression_algorithm(grpc_compression_algorithm algo)104 grpc_compression_algorithm_to_stream_compression_algorithm(
105 grpc_compression_algorithm algo) {
106 switch (algo) {
107 case GRPC_COMPRESS_STREAM_GZIP:
108 return GRPC_STREAM_COMPRESS_GZIP;
109 default:
110 return GRPC_STREAM_COMPRESS_NONE;
111 }
112 }
113
grpc_compression_bitset_to_message_bitset(uint32_t bitset)114 uint32_t grpc_compression_bitset_to_message_bitset(uint32_t bitset) {
115 return bitset & ((1u << GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT) - 1);
116 }
117
grpc_compression_bitset_to_stream_bitset(uint32_t bitset)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;
124 }
125
grpc_compression_bitset_from_message_stream_compression_bitset(uint32_t message_bitset,uint32_t stream_bitset)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;
132 }
133
grpc_compression_algorithm_from_message_stream_compression_algorithm(grpc_compression_algorithm * algorithm,grpc_message_compression_algorithm message_algorithm,grpc_stream_compression_algorithm stream_algorithm)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;
141 return 0;
142 }
143 if (message_algorithm == GRPC_MESSAGE_COMPRESS_NONE) {
144 switch (stream_algorithm) {
145 case GRPC_STREAM_COMPRESS_NONE:
146 *algorithm = GRPC_COMPRESS_NONE;
147 return 1;
148 case GRPC_STREAM_COMPRESS_GZIP:
149 *algorithm = GRPC_COMPRESS_STREAM_GZIP;
150 return 1;
151 default:
152 *algorithm = GRPC_COMPRESS_NONE;
153 return 0;
154 }
155 } else {
156 switch (message_algorithm) {
157 case GRPC_MESSAGE_COMPRESS_NONE:
158 *algorithm = GRPC_COMPRESS_NONE;
159 return 1;
160 case GRPC_MESSAGE_COMPRESS_DEFLATE:
161 *algorithm = GRPC_COMPRESS_DEFLATE;
162 return 1;
163 case GRPC_MESSAGE_COMPRESS_GZIP:
164 *algorithm = GRPC_COMPRESS_GZIP;
165 return 1;
166 default:
167 *algorithm = GRPC_COMPRESS_NONE;
168 return 0;
169 }
170 }
171 return 0;
172 }
173
174 /* Interfaces for message compression. */
175
grpc_message_compression_algorithm_name(grpc_message_compression_algorithm algorithm,const char ** name)176 int grpc_message_compression_algorithm_name(
177 grpc_message_compression_algorithm algorithm, const char** name) {
178 GRPC_API_TRACE(
179 "grpc_message_compression_algorithm_name(algorithm=%d, name=%p)", 2,
180 ((int)algorithm, name));
181 switch (algorithm) {
182 case GRPC_MESSAGE_COMPRESS_NONE:
183 *name = "identity";
184 return 1;
185 case GRPC_MESSAGE_COMPRESS_DEFLATE:
186 *name = "deflate";
187 return 1;
188 case GRPC_MESSAGE_COMPRESS_GZIP:
189 *name = "gzip";
190 return 1;
191 case GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT:
192 return 0;
193 }
194 return 0;
195 }
196
197 /* TODO(dgq): Add the ability to specify parameters to the individual
198 * compression algorithms */
grpc_message_compression_algorithm_for_level(grpc_compression_level level,uint32_t accepted_encodings)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,
202 ((int)level));
203 if (level > GRPC_COMPRESS_LEVEL_HIGH) {
204 gpr_log(GPR_ERROR, "Unknown message compression level %d.",
205 static_cast<int>(level));
206 abort();
207 }
208
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;
213 }
214
215 GPR_ASSERT(level > 0);
216
217 /* Establish a "ranking" or compression algorithms in increasing order of
218 * compression.
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};
223
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;
234 break;
235 }
236 }
237 if (algos_supported_idx == num_supported) break;
238 }
239
240 switch (level) {
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];
249 default:
250 abort();
251 };
252 }
253
grpc_message_compression_algorithm_parse(grpc_slice value,grpc_message_compression_algorithm * algorithm)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;
258 return 1;
259 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_DEFLATE)) {
260 *algorithm = GRPC_MESSAGE_COMPRESS_DEFLATE;
261 return 1;
262 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
263 *algorithm = GRPC_MESSAGE_COMPRESS_GZIP;
264 return 1;
265 } else {
266 return 0;
267 }
268 return 0;
269 }
270
271 /* Interfaces for stream compression. */
272
grpc_stream_compression_algorithm_parse(grpc_slice value,grpc_stream_compression_algorithm * algorithm)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;
277 return 1;
278 } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
279 *algorithm = GRPC_STREAM_COMPRESS_GZIP;
280 return 1;
281 } else {
282 return 0;
283 }
284 return 0;
285 }
286