Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / compression / compression_args.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 <limits.h>
22 #include <string.h>
23
24 #include <grpc/compression.h>
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/compression/compression_args.h"
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gpr/useful.h"
34
35 grpc_compression_algorithm
36 grpc_channel_args_get_channel_default_compression_algorithm(
37     const grpc_channel_args* a) {
38   size_t i;
39   if (a == nullptr) return GRPC_COMPRESS_NONE;
40   for (i = 0; i < a->num_args; ++i) {
41     if (a->args[i].type == GRPC_ARG_INTEGER &&
42         !strcmp(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, a->args[i].key)) {
43       grpc_compression_algorithm default_algorithm =
44           static_cast<grpc_compression_algorithm>(a->args[i].value.integer);
45       return default_algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT
46                  ? default_algorithm
47                  : GRPC_COMPRESS_NONE;
48     }
49   }
50   return GRPC_COMPRESS_NONE;
51 }
52
53 grpc_channel_args* grpc_channel_args_set_channel_default_compression_algorithm(
54     grpc_channel_args* a, grpc_compression_algorithm algorithm) {
55   GPR_ASSERT(algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT);
56   grpc_arg tmp;
57   tmp.type = GRPC_ARG_INTEGER;
58   tmp.key = (char*)GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM;
59   tmp.value.integer = algorithm;
60   return grpc_channel_args_copy_and_add(a, &tmp, 1);
61 }
62
63 /** Returns 1 if the argument for compression algorithm's enabled states bitset
64  * was found in \a a, returning the arg's value in \a states. Otherwise, returns
65  * 0. */
66 static int find_compression_algorithm_states_bitset(const grpc_channel_args* a,
67                                                     int** states_arg) {
68   if (a != nullptr) {
69     size_t i;
70     for (i = 0; i < a->num_args; ++i) {
71       if (a->args[i].type == GRPC_ARG_INTEGER &&
72           !strcmp(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
73                   a->args[i].key)) {
74         *states_arg = &a->args[i].value.integer;
75         **states_arg =
76             (**states_arg & ((1 << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1)) |
77             0x1; /* forcefully enable support for no compression */
78         return 1;
79       }
80     }
81   }
82   return 0; /* GPR_FALSE */
83 }
84
85 grpc_channel_args* grpc_channel_args_compression_algorithm_set_state(
86     grpc_channel_args** a, grpc_compression_algorithm algorithm, int state) {
87   int* states_arg = nullptr;
88   grpc_channel_args* result = *a;
89   const int states_arg_found =
90       find_compression_algorithm_states_bitset(*a, &states_arg);
91
92   if (grpc_channel_args_get_channel_default_compression_algorithm(*a) ==
93           algorithm &&
94       state == 0) {
95     const char* algo_name = nullptr;
96     GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algo_name) != 0);
97     gpr_log(GPR_ERROR,
98             "Tried to disable default compression algorithm '%s'. The "
99             "operation has been ignored.",
100             algo_name);
101   } else if (states_arg_found) {
102     if (state != 0) {
103       GPR_BITSET((unsigned*)states_arg, algorithm);
104     } else if (algorithm != GRPC_COMPRESS_NONE) {
105       GPR_BITCLEAR((unsigned*)states_arg, algorithm);
106     }
107   } else {
108     /* create a new arg */
109     grpc_arg tmp;
110     tmp.type = GRPC_ARG_INTEGER;
111     tmp.key = (char*)GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET;
112     /* all enabled by default */
113     tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
114     if (state != 0) {
115       GPR_BITSET((unsigned*)&tmp.value.integer, algorithm);
116     } else if (algorithm != GRPC_COMPRESS_NONE) {
117       GPR_BITCLEAR((unsigned*)&tmp.value.integer, algorithm);
118     }
119     result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
120     grpc_channel_args_destroy(*a);
121     *a = result;
122   }
123   return result;
124 }
125
126 uint32_t grpc_channel_args_compression_algorithm_get_states(
127     const grpc_channel_args* a) {
128   int* states_arg;
129   if (find_compression_algorithm_states_bitset(a, &states_arg)) {
130     return static_cast<uint32_t>(*states_arg);
131   } else {
132     return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */
133   }
134 }