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