Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / 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 <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
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;
44 }
45
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;
53 }
54
55 grpc_mdelem grpc_message_compression_encoding_mdelem(
56     grpc_message_compression_algorithm algorithm) {
57   switch (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;
64     default:
65       break;
66   }
67   return GRPC_MDNULL;
68 }
69
70 grpc_mdelem grpc_stream_compression_encoding_mdelem(
71     grpc_stream_compression_algorithm algorithm) {
72   switch (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;
77     default:
78       break;
79   }
80   return GRPC_MDNULL;
81 }
82
83 /* Interfaces performing transformation between compression algorithms and
84  * levels. */
85 grpc_message_compression_algorithm
86 grpc_compression_algorithm_to_message_compression_algorithm(
87     grpc_compression_algorithm algo) {
88   switch (algo) {
89     case GRPC_COMPRESS_DEFLATE:
90       return GRPC_MESSAGE_COMPRESS_DEFLATE;
91     case GRPC_COMPRESS_GZIP:
92       return GRPC_MESSAGE_COMPRESS_GZIP;
93     default:
94       return GRPC_MESSAGE_COMPRESS_NONE;
95   }
96 }
97
98 grpc_stream_compression_algorithm
99 grpc_compression_algorithm_to_stream_compression_algorithm(
100     grpc_compression_algorithm algo) {
101   switch (algo) {
102     case GRPC_COMPRESS_STREAM_GZIP:
103       return GRPC_STREAM_COMPRESS_GZIP;
104     default:
105       return GRPC_STREAM_COMPRESS_NONE;
106   }
107 }
108
109 uint32_t grpc_compression_bitset_to_message_bitset(uint32_t bitset) {
110   return bitset & ((1u << GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT) - 1);
111 }
112
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;
119 }
120
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;
127 }
128
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;
136     return 0;
137   }
138   if (message_algorithm == GRPC_MESSAGE_COMPRESS_NONE) {
139     switch (stream_algorithm) {
140       case GRPC_STREAM_COMPRESS_NONE:
141         *algorithm = GRPC_COMPRESS_NONE;
142         return 1;
143       case GRPC_STREAM_COMPRESS_GZIP:
144         *algorithm = GRPC_COMPRESS_STREAM_GZIP;
145         return 1;
146       default:
147         *algorithm = GRPC_COMPRESS_NONE;
148         return 0;
149     }
150   } else {
151     switch (message_algorithm) {
152       case GRPC_MESSAGE_COMPRESS_NONE:
153         *algorithm = GRPC_COMPRESS_NONE;
154         return 1;
155       case GRPC_MESSAGE_COMPRESS_DEFLATE:
156         *algorithm = GRPC_COMPRESS_DEFLATE;
157         return 1;
158       case GRPC_MESSAGE_COMPRESS_GZIP:
159         *algorithm = GRPC_COMPRESS_GZIP;
160         return 1;
161       default:
162         *algorithm = GRPC_COMPRESS_NONE;
163         return 0;
164     }
165   }
166   return 0;
167 }
168
169 /* Interfaces for message compression. */
170
171 int grpc_message_compression_algorithm_name(
172     grpc_message_compression_algorithm algorithm, const char** name) {
173   GRPC_API_TRACE(
174       "grpc_message_compression_algorithm_name(algorithm=%d, name=%p)", 2,
175       ((int)algorithm, name));
176   switch (algorithm) {
177     case GRPC_MESSAGE_COMPRESS_NONE:
178       *name = "identity";
179       return 1;
180     case GRPC_MESSAGE_COMPRESS_DEFLATE:
181       *name = "deflate";
182       return 1;
183     case GRPC_MESSAGE_COMPRESS_GZIP:
184       *name = "gzip";
185       return 1;
186     case GRPC_MESSAGE_COMPRESS_ALGORITHMS_COUNT:
187       return 0;
188   }
189   return 0;
190 }
191
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,
197                  ((int)level));
198   if (level > GRPC_COMPRESS_LEVEL_HIGH) {
199     gpr_log(GPR_ERROR, "Unknown message compression level %d.",
200             static_cast<int>(level));
201     abort();
202   }
203
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;
208   }
209
210   GPR_ASSERT(level > 0);
211
212   /* Establish a "ranking" or compression algorithms in increasing order of
213    * compression.
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};
218
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;
229         break;
230       }
231     }
232     if (algos_supported_idx == num_supported) break;
233   }
234
235   switch (level) {
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];
244     default:
245       abort();
246   };
247 }
248
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;
253     return 1;
254   } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_DEFLATE)) {
255     *algorithm = GRPC_MESSAGE_COMPRESS_DEFLATE;
256     return 1;
257   } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
258     *algorithm = GRPC_MESSAGE_COMPRESS_GZIP;
259     return 1;
260   } else {
261     return 0;
262   }
263   return 0;
264 }
265
266 /* Interfaces for stream compression. */
267
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;
272     return 1;
273   } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
274     *algorithm = GRPC_STREAM_COMPRESS_GZIP;
275     return 1;
276   } else {
277     return 0;
278   }
279   return 0;
280 }