blob: 6ee3ac196642b78b88e161433c5d8ee55b59ffef [file] [log] [blame]
Muxi Yanc1f837c2017-05-04 18:17:13 -07001/*
2 *
Muxi Yan163d8d62017-06-30 09:53:37 -07003 * Copyright 2017 gRPC authors.
Muxi Yanc1f837c2017-05-04 18:17:13 -07004 *
Muxi Yan163d8d62017-06-30 09:53:37 -07005 * 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
Muxi Yanc1f837c2017-05-04 18:17:13 -07008 *
Muxi Yan163d8d62017-06-30 09:53:37 -07009 * http://www.apache.org/licenses/LICENSE-2.0
Muxi Yanc1f837c2017-05-04 18:17:13 -070010 *
Muxi Yan163d8d62017-06-30 09:53:37 -070011 * 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.
Muxi Yanc1f837c2017-05-04 18:17:13 -070016 *
17 */
18
Muxi Yan163d8d62017-06-30 09:53:37 -070019#ifndef GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H
20#define GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H
Muxi Yanc1f837c2017-05-04 18:17:13 -070021
22#include <stdbool.h>
23
24#include <grpc/slice_buffer.h>
25#include <zlib.h>
26
Yash Tibrewalbc460fa2017-10-02 17:42:41 -070027#include "src/core/lib/transport/static_metadata.h"
28
Yash Tibrewala7e6d652017-09-20 18:56:37 -070029#ifdef __cplusplus
30extern "C" {
31#endif
32
Muxi Yan130e0702017-09-01 18:02:51 -070033typedef struct grpc_stream_compression_vtable grpc_stream_compression_vtable;
34
Muxi Yanc1f837c2017-05-04 18:17:13 -070035/* Stream compression/decompression context */
36typedef struct grpc_stream_compression_context {
Muxi Yan130e0702017-09-01 18:02:51 -070037 const grpc_stream_compression_vtable *vtable;
Muxi Yanc1f837c2017-05-04 18:17:13 -070038} grpc_stream_compression_context;
39
40typedef enum grpc_stream_compression_method {
Muxi Yan130e0702017-09-01 18:02:51 -070041 GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS = 0,
42 GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS,
43 GRPC_STREAM_COMPRESSION_GZIP_COMPRESS,
44 GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS,
Muxi Yanf9ff5542017-07-12 14:02:27 -070045 GRPC_STREAM_COMPRESSION_METHOD_COUNT
Muxi Yanc1f837c2017-05-04 18:17:13 -070046} grpc_stream_compression_method;
47
48typedef enum grpc_stream_compression_flush {
Muxi Yanfbd06f72017-07-08 10:54:46 -070049 GRPC_STREAM_COMPRESSION_FLUSH_NONE = 0,
Muxi Yanc1f837c2017-05-04 18:17:13 -070050 GRPC_STREAM_COMPRESSION_FLUSH_SYNC,
Muxi Yanfbd06f72017-07-08 10:54:46 -070051 GRPC_STREAM_COMPRESSION_FLUSH_FINISH,
52 GRPC_STREAM_COMPRESSION_FLUSH_COUNT
Muxi Yanc1f837c2017-05-04 18:17:13 -070053} grpc_stream_compression_flush;
54
Muxi Yan130e0702017-09-01 18:02:51 -070055struct grpc_stream_compression_vtable {
Muxi Yan03600fc2017-09-05 13:42:44 -070056 bool (*compress)(grpc_stream_compression_context *ctx, grpc_slice_buffer *in,
57 grpc_slice_buffer *out, size_t *output_size,
58 size_t max_output_size, grpc_stream_compression_flush flush);
59 bool (*decompress)(grpc_stream_compression_context *ctx,
60 grpc_slice_buffer *in, grpc_slice_buffer *out,
61 size_t *output_size, size_t max_output_size,
62 bool *end_of_context);
63 grpc_stream_compression_context *(*context_create)(
64 grpc_stream_compression_method method);
65 void (*context_destroy)(grpc_stream_compression_context *ctx);
Muxi Yan130e0702017-09-01 18:02:51 -070066};
67
Muxi Yanc1f837c2017-05-04 18:17:13 -070068/**
69 * Compress bytes provided in \a in with a given context, with an optional flush
70 * at the end of compression. Emits at most \a max_output_size compressed bytes
71 * into \a out. If all the bytes in input buffer \a in are depleted and \a flush
72 * is not GRPC_STREAM_COMPRESSION_FLUSH_NONE, the corresponding flush method is
73 * executed. The total number of bytes emitted is outputed in \a output_size.
Muxi Yanfbd06f72017-07-08 10:54:46 -070074 *
75 * A SYNC flush indicates that the entire messages in \a in can be decompressed
76 * from \a out. A FINISH flush implies a SYNC flush, and that any further
77 * compression will not be dependent on the state of the current context and any
78 * previous compressed bytes. It allows corresponding decompression context to
79 * be dropped when reaching this boundary.
Muxi Yanc1f837c2017-05-04 18:17:13 -070080 */
81bool grpc_stream_compress(grpc_stream_compression_context *ctx,
82 grpc_slice_buffer *in, grpc_slice_buffer *out,
83 size_t *output_size, size_t max_output_size,
84 grpc_stream_compression_flush flush);
85
86/**
87 * Decompress bytes provided in \a in with a given context. Emits at most \a
88 * max_output_size decompressed bytes into \a out. If decompression process
89 * reached the end of a gzip stream, \a end_of_context is set to true; otherwise
90 * it is set to false. The total number of bytes emitted is outputed in \a
91 * output_size.
92 */
93bool grpc_stream_decompress(grpc_stream_compression_context *ctx,
94 grpc_slice_buffer *in, grpc_slice_buffer *out,
95 size_t *output_size, size_t max_output_size,
96 bool *end_of_context);
97
98/**
99 * Creates a stream compression context. \a pending_bytes_buffer is the input
100 * buffer for compression/decompression operations. \a method specifies whether
101 * the context is for compression or decompression.
102 */
103grpc_stream_compression_context *grpc_stream_compression_context_create(
104 grpc_stream_compression_method method);
105
106/**
107 * Destroys a stream compression context.
108 */
109void grpc_stream_compression_context_destroy(
110 grpc_stream_compression_context *ctx);
111
Muxi Yan130e0702017-09-01 18:02:51 -0700112/**
113 * Parse stream compression method based on algorithm name
114 */
115int grpc_stream_compression_method_parse(
116 grpc_slice value, bool is_compress, grpc_stream_compression_method *method);
117
Yash Tibrewala7e6d652017-09-20 18:56:37 -0700118#ifdef __cplusplus
119}
120#endif
121
Muxi Yanc1f837c2017-05-04 18:17:13 -0700122#endif