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"
25 #include "src/core/lib/slice/slice_utils.h"
27 extern const grpc_stream_compression_vtable
28 grpc_stream_compression_identity_vtable;
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,
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,
46 grpc_stream_compression_context* grpc_stream_compression_context_create(
47 grpc_stream_compression_method 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);
56 gpr_log(GPR_ERROR, "Unknown stream compression method: %d", method);
61 void grpc_stream_compression_context_destroy(
62 grpc_stream_compression_context* ctx) {
63 ctx->vtable->context_destroy(ctx);
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;
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;