Debian Codegraph @

test/core/compression/args_utils.cc

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "test/core/compression/args_utils.h"
16
17 #include <string.h>
18
19 #include "absl/types/optional.h"
20
21 #include <grpc/compression.h>
22 #include <grpc/support/log.h>
23
24 #include "src/core/lib/channel/channel_args.h"
25 #include "src/core/lib/compression/compression_internal.h"
26 #include "src/core/lib/gpr/useful.h"
27
28 const grpc_channel_args*
29 grpc_channel_args_set_channel_default_compression_algorithm(
30 const grpc_channel_args* a, grpc_compression_algorithm algorithm) {
31 GPR_ASSERT(algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT);
32 grpc_arg tmp;
33 tmp.type = GRPC_ARG_INTEGER;
34 tmp.key = const_cast<char*>(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
35 tmp.value.integer = algorithm;
36 return grpc_channel_args_copy_and_add(a, &tmp, 1);
37 }
38
39 /** Returns 1 if the argument for compression algorithm's enabled states bitset
40 * was found in \a a, returning the arg's value in \a states. Otherwise, returns
41 * 0. */
42 static int find_compression_algorithm_states_bitset(const grpc_channel_args* a,
43 int** states_arg) {
44 if (a != nullptr) {
45 size_t i;
46 for (i = 0; i < a->num_args; ++i) {
47 if (a->args[i].type == GRPC_ARG_INTEGER &&
48 !strcmp(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
49 a->args[i].key)) {
50 *states_arg = &a->args[i].value.integer;
51 **states_arg =
52 (**states_arg & ((1 << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1)) |
53 0x1; /* forcefully enable support for no compression */
54 return 1;
55 }
56 }
57 }
58 return 0; /* GPR_FALSE */
59 }
60
61 const grpc_channel_args* grpc_channel_args_compression_algorithm_set_state(
62 const grpc_channel_args** a, grpc_compression_algorithm algorithm,
63 int state) {
64 int* states_arg = nullptr;
65 const grpc_channel_args* result = *a;
66 const int states_arg_found =
67 find_compression_algorithm_states_bitset(*a, &states_arg);
68
69 if (grpc_core::DefaultCompressionAlgorithmFromChannelArgs(*a) == algorithm &&
70 state == 0) {
71 const char* algo_name = nullptr;
72 GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algo_name) != 0);
73 gpr_log(GPR_ERROR,
74 "Tried to disable default compression algorithm '%s'. The "
75 "operation has been ignored.",
76 algo_name);
77 } else if (states_arg_found) {
78 if (state != 0) {
79 grpc_core::SetBit(reinterpret_cast<unsigned*>(states_arg), algorithm);
80 } else if (algorithm != GRPC_COMPRESS_NONE) {
81 grpc_core::ClearBit(reinterpret_cast<unsigned*>(states_arg), algorithm);
82 }
83 } else {
84 /* create a new arg */
85 grpc_arg tmp;
86 tmp.type = GRPC_ARG_INTEGER;
87 tmp.key =
88 const_cast<char*>(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
89 /* all enabled by default */
90 tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
91 if (state != 0) {
92 grpc_core::SetBit(reinterpret_cast<unsigned*>(&tmp.value.integer),
93 algorithm);
94 } else if (algorithm != GRPC_COMPRESS_NONE) {
95 grpc_core::ClearBit(reinterpret_cast<unsigned*>(&tmp.value.integer),
96 algorithm);
97 }
98 result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
99 grpc_channel_args_destroy(*a);
100 *a = result;
101 }
102 return result;
103 }