Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / 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 grpc_channel_args_get_compression_algorithm(
36     const grpc_channel_args* a) {
37   size_t i;
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);
43       break;
44     }
45   }
46   return GRPC_COMPRESS_NONE;
47 }
48
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);
52   grpc_arg tmp;
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);
57 }
58
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
61  * 0. */
62 static int find_compression_algorithm_states_bitset(const grpc_channel_args* a,
63                                                     int** states_arg) {
64   if (a != nullptr) {
65     size_t i;
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,
69                   a->args[i].key)) {
70         *states_arg = &a->args[i].value.integer;
71         **states_arg |= 0x1; /* forcefully enable support for no compression */
72         return 1;
73       }
74     }
75   }
76   return 0; /* GPR_FALSE */
77 }
78
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);
85
86   if (grpc_channel_args_get_compression_algorithm(*a) == algorithm &&
87       state == 0) {
88     const char* algo_name = nullptr;
89     GPR_ASSERT(grpc_compression_algorithm_name(algorithm, &algo_name) != 0);
90     gpr_log(GPR_ERROR,
91             "Tried to disable default compression algorithm '%s'. The "
92             "operation has been ignored.",
93             algo_name);
94   } else if (states_arg_found) {
95     if (state != 0) {
96       GPR_BITSET((unsigned*)states_arg, algorithm);
97     } else if (algorithm != GRPC_COMPRESS_NONE) {
98       GPR_BITCLEAR((unsigned*)states_arg, algorithm);
99     }
100   } else {
101     /* create a new arg */
102     grpc_arg tmp;
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;
107     if (state != 0) {
108       GPR_BITSET((unsigned*)&tmp.value.integer, algorithm);
109     } else if (algorithm != GRPC_COMPRESS_NONE) {
110       GPR_BITCLEAR((unsigned*)&tmp.value.integer, algorithm);
111     }
112     result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
113     grpc_channel_args_destroy(*a);
114     *a = result;
115   }
116   return result;
117 }
118
119 uint32_t grpc_channel_args_compression_algorithm_get_states(
120     const grpc_channel_args* a) {
121   int* states_arg;
122   if (find_compression_algorithm_states_bitset(a, &states_arg)) {
123     return static_cast<uint32_t>(*states_arg);
124   } else {
125     return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */
126   }
127 }