3 * Copyright 2017 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>
21 #include "src/core/lib/compression/stream_compression_identity.h"
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
26 #include "src/core/lib/slice/slice_internal.h"
28 #define OUTPUT_BLOCK_SIZE (1024)
30 /* Singleton context used for all identity streams. */
31 static grpc_stream_compression_context identity_ctx = {
32 &grpc_stream_compression_identity_vtable};
34 static void grpc_stream_compression_pass_through(grpc_slice_buffer* in,
35 grpc_slice_buffer* out,
37 size_t max_output_size) {
38 if (max_output_size >= in->length) {
40 *output_size = in->length;
42 grpc_slice_buffer_move_into(in, out);
45 *output_size = max_output_size;
47 grpc_slice_buffer_move_first(in, max_output_size, out);
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*/) {
58 grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
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) {
69 grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
71 *end_of_context = false;
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. */
85 static void grpc_stream_compression_context_destroy_identity(
86 grpc_stream_compression_context* /*ctx*/) {}
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};