• 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/log.h>
22 
23 #include "src/core/lib/compression/stream_compression.h"
24 #include "src/core/lib/compression/stream_compression_gzip.h"
25 #include "src/core/lib/slice/slice_utils.h"
26 
27 extern const grpc_stream_compression_vtable
28     grpc_stream_compression_identity_vtable;
29 
grpc_stream_compress(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)30 bool grpc_stream_compress(grpc_stream_compression_context* ctx,
31                           grpc_slice_buffer* in, grpc_slice_buffer* out,
32                           size_t* output_size, size_t max_output_size,
33                           grpc_stream_compression_flush flush) {
34   return ctx->vtable->compress(ctx, in, out, output_size, max_output_size,
35                                flush);
36 }
37 
grpc_stream_decompress(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)38 bool grpc_stream_decompress(grpc_stream_compression_context* ctx,
39                             grpc_slice_buffer* in, grpc_slice_buffer* out,
40                             size_t* output_size, size_t max_output_size,
41                             bool* end_of_context) {
42   return ctx->vtable->decompress(ctx, in, out, output_size, max_output_size,
43                                  end_of_context);
44 }
45 
grpc_stream_compression_context_create(grpc_stream_compression_method method)46 grpc_stream_compression_context* grpc_stream_compression_context_create(
47     grpc_stream_compression_method method) {
48   switch (method) {
49     case GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS:
50     case GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS:
51       return grpc_stream_compression_identity_vtable.context_create(method);
52     case GRPC_STREAM_COMPRESSION_GZIP_COMPRESS:
53     case GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS:
54       return grpc_stream_compression_gzip_vtable.context_create(method);
55     default:
56       gpr_log(GPR_ERROR, "Unknown stream compression method: %d", method);
57       return nullptr;
58   }
59 }
60 
grpc_stream_compression_context_destroy(grpc_stream_compression_context * ctx)61 void grpc_stream_compression_context_destroy(
62     grpc_stream_compression_context* ctx) {
63   ctx->vtable->context_destroy(ctx);
64 }
65 
grpc_stream_compression_method_parse(grpc_slice value,bool is_compress,grpc_stream_compression_method * method)66 int grpc_stream_compression_method_parse(
67     grpc_slice value, bool is_compress,
68     grpc_stream_compression_method* method) {
69   if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_IDENTITY)) {
70     *method = is_compress ? GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS
71                           : GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS;
72     return 1;
73   } else if (grpc_slice_eq_static_interned(value, GRPC_MDSTR_GZIP)) {
74     *method = is_compress ? GRPC_STREAM_COMPRESSION_GZIP_COMPRESS
75                           : GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS;
76     return 1;
77   } else {
78     return 0;
79   }
80 }
81