Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / lib / compression / compression_internal.cc
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 "src/core/lib/compression/compression_internal.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <grpc/compression.h>
27
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"
33
34 /* Interfaces related to MD */
35
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;
40   }
41   if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_DEFLATE)) {
42     return GRPC_MESSAGE_COMPRESS_DEFLATE;
43   }
44   if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
45     return GRPC_MESSAGE_COMPRESS_GZIP;
46   }
47   return GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT;
48 }
49
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;
54   }
55   if (grpc_slice_eq_static_interned(str, GRPC_MDSTR_GZIP)) {
56     return GRPC_STREAM_COMPRESS_GZIP;
57   }
58   return GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT;
59 }
60
61 grpc_mdelem grpc_message_compression_encoding_mdelem(
62     grpc_message_compression_algorithm algorithm) {
63   switch (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;
70     default:
71       break;
72   }
73   return GRPC_MDNULL;
74 }
75
76 grpc_mdelem grpc_stream_compression_encoding_mdelem(
77     grpc_stream_compression_algorithm algorithm) {
78   switch (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;
83     default:
84       break;
85   }
86   return GRPC_MDNULL;
87 }
88
89 /* Interfaces performing transformation between compression algorithms and
90  * levels. */
91 grpc_message_compression_algorithm
92 grpc_compression_algorithm_to_message_compression_algorithm(
93     grpc_compression_algorithm algo) {
94   switch (algo) {
95     case GRPC_COMPRESS_DEFLATE:
96       return GRPC_MESSAGE_COMPRESS_DEFLATE;
97     case GRPC_COMPRESS_GZIP:
98       return GRPC_MESSAGE_COMPRESS_GZIP;
99     default:
100       return GRPC_MESSAGE_COMPRESS_NONE;
101   }
102 }
103
104 grpc_stream_compression_algorithm
105 grpc_compression_algorithm_to_stream_compression_algorithm(
106     grpc_compression_algorithm algo) {
107   switch (algo) {
108     case GRPC_COMPRESS_STREAM_GZIP:
109       return GRPC_STREAM_COMPRESS_GZIP;
110     default:
111       return GRPC_STREAM_COMPRESS_NONE;
112   }
113 }
114
115 uint32_t grpc_compression_bitset_to_message_bitset(uint32_t bitset) {
116   return bitset & ((1u << GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT) - 1);
117 }
118
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;
125 }
126
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;
133 }
134
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;
142     return 0;
143   }
144   if (message_algorithm == GRPC_MESSAGE_COMPRESS_NONE) {
145     switch (stream_algorithm) {
146       case GRPC_STREAM_COMPRESS_NONE:
147         *algorithm = GRPC_COMPRESS_NONE;
148         return 1;
149       case GRPC_STREAM_COMPRESS_GZIP:
150         *algorithm = GRPC_COMPRESS_STREAM_GZIP;
151         return 1;
152       default:
153         *algorithm = GRPC_COMPRESS_NONE;
154         return 0;
155     }
156   } else {
157     switch (message_algorithm) {
158       case GRPC_MESSAGE_COMPRESS_NONE:
159         *algorithm = GRPC_COMPRESS_NONE;
160         return 1;
161       case GRPC_MESSAGE_COMPRESS_DEFLATE:
162         *algorithm = GRPC_COMPRESS_DEFLATE;
163         return 1;
164       case GRPC_MESSAGE_COMPRESS_GZIP:
165         *algorithm = GRPC_COMPRESS_GZIP;
166         return 1;
167       default:
168         *algorithm = GRPC_COMPRESS_NONE;
169         return 0;
170     }
171   }
172 }
173
174 /* Interfaces for message compression. */
175
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 */
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
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 }
269
270 /* Interfaces for stream compression. */
271
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;
276     return 1;
277   } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
278     *algorithm = GRPC_STREAM_COMPRESS_GZIP;
279     return 1;
280   } else {
281     return 0;
282   }
283 }