Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / lib / compression / stream_compression_identity.cc
1 /*
2  *
3  * Copyright 2017 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 "src/core/lib/compression/stream_compression_identity.h"
22
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25
26 #include "src/core/lib/slice/slice_internal.h"
27
28 #define OUTPUT_BLOCK_SIZE (1024)
29
30 /* Singleton context used for all identity streams. */
31 static grpc_stream_compression_context identity_ctx = {
32     &grpc_stream_compression_identity_vtable};
33
34 static void grpc_stream_compression_pass_through(grpc_slice_buffer* in,
35                                                  grpc_slice_buffer* out,
36                                                  size_t* output_size,
37                                                  size_t max_output_size) {
38   if (max_output_size >= in->length) {
39     if (output_size) {
40       *output_size = in->length;
41     }
42     grpc_slice_buffer_move_into(in, out);
43   } else {
44     if (output_size) {
45       *output_size = max_output_size;
46     }
47     grpc_slice_buffer_move_first(in, max_output_size, out);
48   }
49 }
50
51 static bool grpc_stream_compress_identity(
52     grpc_stream_compression_context* ctx, grpc_slice_buffer* in,
53     grpc_slice_buffer* out, size_t* output_size, size_t max_output_size,
54     grpc_stream_compression_flush /*flush*/) {
55   if (ctx == nullptr) {
56     return false;
57   }
58   grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
59   return true;
60 }
61
62 static bool grpc_stream_decompress_identity(
63     grpc_stream_compression_context* ctx, grpc_slice_buffer* in,
64     grpc_slice_buffer* out, size_t* output_size, size_t max_output_size,
65     bool* end_of_context) {
66   if (ctx == nullptr) {
67     return false;
68   }
69   grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
70   if (end_of_context) {
71     *end_of_context = false;
72   }
73   return true;
74 }
75
76 static grpc_stream_compression_context*
77 grpc_stream_compression_context_create_identity(
78     grpc_stream_compression_method method) {
79   GPR_ASSERT(method == GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS ||
80              method == GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS);
81   /* No context needed in this case. Use fake context instead. */
82   return &identity_ctx;
83 }
84
85 static void grpc_stream_compression_context_destroy_identity(
86     grpc_stream_compression_context* /*ctx*/) {}
87
88 const grpc_stream_compression_vtable grpc_stream_compression_identity_vtable = {
89     grpc_stream_compress_identity, grpc_stream_decompress_identity,
90     grpc_stream_compression_context_create_identity,
91     grpc_stream_compression_context_destroy_identity};