3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
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>
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"
35 grpc_compression_algorithm
36 grpc_channel_args_get_channel_default_compression_algorithm(
37 const grpc_channel_args* a) {
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
50 return GRPC_COMPRESS_NONE;
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);
57 tmp.type = GRPC_ARG_INTEGER;
58 tmp.key = const_cast<char*>(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM);
59 tmp.value.integer = algorithm;
60 return grpc_channel_args_copy_and_add(a, &tmp, 1);
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
66 static int find_compression_algorithm_states_bitset(const grpc_channel_args* a,
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,
74 *states_arg = &a->args[i].value.integer;
76 (**states_arg & ((1 << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1)) |
77 0x1; /* forcefully enable support for no compression */
82 return 0; /* GPR_FALSE */
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);
92 if (grpc_channel_args_get_channel_default_compression_algorithm(*a) ==
95 const char* algo_name = nullptr;
96 GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algo_name) != 0);
98 "Tried to disable default compression algorithm '%s'. The "
99 "operation has been ignored.",
101 } else if (states_arg_found) {
103 GPR_BITSET((unsigned*)states_arg, algorithm);
104 } else if (algorithm != GRPC_COMPRESS_NONE) {
105 GPR_BITCLEAR((unsigned*)states_arg, algorithm);
108 /* create a new arg */
110 tmp.type = GRPC_ARG_INTEGER;
112 const_cast<char*>(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET);
113 /* all enabled by default */
114 tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
116 GPR_BITSET((unsigned*)&tmp.value.integer, algorithm);
117 } else if (algorithm != GRPC_COMPRESS_NONE) {
118 GPR_BITCLEAR((unsigned*)&tmp.value.integer, algorithm);
120 result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
121 grpc_channel_args_destroy(*a);
127 uint32_t grpc_channel_args_compression_algorithm_get_states(
128 const grpc_channel_args* a) {
130 if (find_compression_algorithm_states_bitset(a, &states_arg)) {
131 return static_cast<uint32_t>(*states_arg);
133 return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */