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 grpc_channel_args_get_compression_algorithm(
36 const grpc_channel_args* a) {
38 if (a == nullptr) return GRPC_COMPRESS_NONE;
39 for (i = 0; i < a->num_args; ++i) {
40 if (a->args[i].type == GRPC_ARG_INTEGER &&
41 !strcmp(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, a->args[i].key)) {
42 return static_cast<grpc_compression_algorithm>(a->args[i].value.integer);
46 return GRPC_COMPRESS_NONE;
49 grpc_channel_args* grpc_channel_args_set_compression_algorithm(
50 grpc_channel_args* a, grpc_compression_algorithm algorithm) {
51 GPR_ASSERT(algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT);
53 tmp.type = GRPC_ARG_INTEGER;
54 tmp.key = (char*)GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM;
55 tmp.value.integer = algorithm;
56 return grpc_channel_args_copy_and_add(a, &tmp, 1);
59 /** Returns 1 if the argument for compression algorithm's enabled states bitset
60 * was found in \a a, returning the arg's value in \a states. Otherwise, returns
62 static int find_compression_algorithm_states_bitset(const grpc_channel_args* a,
66 for (i = 0; i < a->num_args; ++i) {
67 if (a->args[i].type == GRPC_ARG_INTEGER &&
68 !strcmp(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
70 *states_arg = &a->args[i].value.integer;
71 **states_arg |= 0x1; /* forcefully enable support for no compression */
76 return 0; /* GPR_FALSE */
79 grpc_channel_args* grpc_channel_args_compression_algorithm_set_state(
80 grpc_channel_args** a, grpc_compression_algorithm algorithm, int state) {
81 int* states_arg = nullptr;
82 grpc_channel_args* result = *a;
83 const int states_arg_found =
84 find_compression_algorithm_states_bitset(*a, &states_arg);
86 if (grpc_channel_args_get_compression_algorithm(*a) == algorithm &&
88 const char* algo_name = nullptr;
89 GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algo_name) != 0);
91 "Tried to disable default compression algorithm '%s'. The "
92 "operation has been ignored.",
94 } else if (states_arg_found) {
96 GPR_BITSET((unsigned*)states_arg, algorithm);
97 } else if (algorithm != GRPC_COMPRESS_NONE) {
98 GPR_BITCLEAR((unsigned*)states_arg, algorithm);
101 /* create a new arg */
103 tmp.type = GRPC_ARG_INTEGER;
104 tmp.key = (char*)GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET;
105 /* all enabled by default */
106 tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
108 GPR_BITSET((unsigned*)&tmp.value.integer, algorithm);
109 } else if (algorithm != GRPC_COMPRESS_NONE) {
110 GPR_BITCLEAR((unsigned*)&tmp.value.integer, algorithm);
112 result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
113 grpc_channel_args_destroy(*a);
119 uint32_t grpc_channel_args_compression_algorithm_get_states(
120 const grpc_channel_args* a) {
122 if (find_compression_algorithm_states_bitset(a, &states_arg)) {
123 return static_cast<uint32_t>(*states_arg);
125 return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */