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