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