• 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 flush)50 static bool grpc_stream_compress_identity(grpc_stream_compression_context* ctx,
51                                           grpc_slice_buffer* in,
52                                           grpc_slice_buffer* out,
53                                           size_t* output_size,
54                                           size_t max_output_size,
55                                           grpc_stream_compression_flush flush) {
56   if (ctx == nullptr) {
57     return false;
58   }
59   grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
60   return true;
61 }
62 
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)63 static bool grpc_stream_decompress_identity(
64     grpc_stream_compression_context* ctx, grpc_slice_buffer* in,
65     grpc_slice_buffer* out, size_t* output_size, size_t max_output_size,
66     bool* end_of_context) {
67   if (ctx == nullptr) {
68     return false;
69   }
70   grpc_stream_compression_pass_through(in, out, output_size, max_output_size);
71   if (end_of_context) {
72     *end_of_context = false;
73   }
74   return true;
75 }
76 
77 static grpc_stream_compression_context*
grpc_stream_compression_context_create_identity(grpc_stream_compression_method method)78 grpc_stream_compression_context_create_identity(
79     grpc_stream_compression_method method) {
80   GPR_ASSERT(method == GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS ||
81              method == GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS);
82   /* No context needed in this case. Use fake context instead. */
83   return &identity_ctx;
84 }
85 
grpc_stream_compression_context_destroy_identity(grpc_stream_compression_context * ctx)86 static void grpc_stream_compression_context_destroy_identity(
87     grpc_stream_compression_context* ctx) {
88   return;
89 }
90 
91 const grpc_stream_compression_vtable grpc_stream_compression_identity_vtable = {
92     grpc_stream_compress_identity, grpc_stream_decompress_identity,
93     grpc_stream_compression_context_create_identity,
94     grpc_stream_compression_context_destroy_identity};
95