• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/compression.h>
20 #include <grpc/impl/compression_types.h>
21 #include <grpc/slice.h>
22 #include <grpc/support/port_platform.h>
23 #include <stdint.h>
24 #include <string.h>
25 
26 #include "absl/types/optional.h"
27 #include "src/core/lib/compression/compression_internal.h"
28 #include "src/core/lib/debug/trace.h"
29 #include "src/core/lib/slice/slice_internal.h"
30 #include "src/core/util/useful.h"
31 
grpc_compression_algorithm_is_message(grpc_compression_algorithm)32 int grpc_compression_algorithm_is_message(grpc_compression_algorithm) {
33   return 1;
34 }
35 
grpc_compression_algorithm_is_stream(grpc_compression_algorithm)36 int grpc_compression_algorithm_is_stream(grpc_compression_algorithm) {
37   return 0;
38 }
39 
grpc_compression_algorithm_parse(grpc_slice name,grpc_compression_algorithm * algorithm)40 int grpc_compression_algorithm_parse(grpc_slice name,
41                                      grpc_compression_algorithm* algorithm) {
42   absl::optional<grpc_compression_algorithm> alg =
43       grpc_core::ParseCompressionAlgorithm(
44           grpc_core::StringViewFromSlice(name));
45   if (alg.has_value()) {
46     *algorithm = alg.value();
47     return 1;
48   }
49   return 0;
50 }
51 
grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,const char ** name)52 int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
53                                     const char** name) {
54   GRPC_TRACE_LOG(api, INFO)
55       << "grpc_compression_algorithm_name(algorithm=" << (int)algorithm
56       << ", name=" << name << ")";
57   const char* result = grpc_core::CompressionAlgorithmAsString(algorithm);
58   if (result != nullptr) {
59     *name = result;
60     return 1;
61   }
62   return 0;
63 }
64 
grpc_compression_algorithm_for_level(grpc_compression_level level,uint32_t accepted_encodings)65 grpc_compression_algorithm grpc_compression_algorithm_for_level(
66     grpc_compression_level level, uint32_t accepted_encodings) {
67   return grpc_core::CompressionAlgorithmSet::FromUint32(accepted_encodings)
68       .CompressionAlgorithmForLevel(level);
69 }
70 
grpc_compression_options_init(grpc_compression_options * opts)71 void grpc_compression_options_init(grpc_compression_options* opts) {
72   memset(opts, 0, sizeof(*opts));
73   // all enabled by default
74   opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
75 }
76 
grpc_compression_options_enable_algorithm(grpc_compression_options * opts,grpc_compression_algorithm algorithm)77 void grpc_compression_options_enable_algorithm(
78     grpc_compression_options* opts, grpc_compression_algorithm algorithm) {
79   grpc_core::SetBit(&opts->enabled_algorithms_bitset, algorithm);
80 }
81 
grpc_compression_options_disable_algorithm(grpc_compression_options * opts,grpc_compression_algorithm algorithm)82 void grpc_compression_options_disable_algorithm(
83     grpc_compression_options* opts, grpc_compression_algorithm algorithm) {
84   grpc_core::ClearBit(&opts->enabled_algorithms_bitset, algorithm);
85 }
86 
grpc_compression_options_is_algorithm_enabled(const grpc_compression_options * opts,grpc_compression_algorithm algorithm)87 int grpc_compression_options_is_algorithm_enabled(
88     const grpc_compression_options* opts,
89     grpc_compression_algorithm algorithm) {
90   return grpc_core::CompressionAlgorithmSet::FromUint32(
91              opts->enabled_algorithms_bitset)
92       .IsSet(algorithm);
93 }
94