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 <grpc/support/log.h>
23 #include "src/core/lib/compression/stream_compression.h"
24 #include "src/core/lib/compression/stream_compression_gzip.h"
26 extern const grpc_stream_compression_vtable
27 grpc_stream_compression_identity_vtable;
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,
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,
45 grpc_stream_compression_context* grpc_stream_compression_context_create(
46 grpc_stream_compression_method 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);
55 gpr_log(GPR_ERROR, "Unknown stream compression method: %d", method);
60 void grpc_stream_compression_context_destroy(
61 grpc_stream_compression_context* ctx) {
62 ctx->vtable->context_destroy(ctx);
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;
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;