blob: 844dff81a3b05bbf4dad89f2b63fb64a0a866689 [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
27/* Stream compression/decompression context */
28typedef struct grpc_stream_compression_context {
29 z_stream zs;
30 int (*flate)(z_stream *zs, int flush);
31} grpc_stream_compression_context;
32
33typedef enum grpc_stream_compression_method {
Muxi Yanfbd06f72017-07-08 10:54:46 -070034 GRPC_STREAM_COMPRESSION_COMPRESS = 0,
Muxi Yanf9ff5542017-07-12 14:02:27 -070035 GRPC_STREAM_COMPRESSION_DECOMPRESS,
36 GRPC_STREAM_COMPRESSION_METHOD_COUNT
Muxi Yanc1f837c2017-05-04 18:17:13 -070037} grpc_stream_compression_method;
38
39typedef enum grpc_stream_compression_flush {
Muxi Yanfbd06f72017-07-08 10:54:46 -070040 GRPC_STREAM_COMPRESSION_FLUSH_NONE = 0,
Muxi Yanc1f837c2017-05-04 18:17:13 -070041 GRPC_STREAM_COMPRESSION_FLUSH_SYNC,
Muxi Yanfbd06f72017-07-08 10:54:46 -070042 GRPC_STREAM_COMPRESSION_FLUSH_FINISH,
43 GRPC_STREAM_COMPRESSION_FLUSH_COUNT
Muxi Yanc1f837c2017-05-04 18:17:13 -070044} grpc_stream_compression_flush;
45
46/**
47 * Compress bytes provided in \a in with a given context, with an optional flush
48 * at the end of compression. Emits at most \a max_output_size compressed bytes
49 * into \a out. If all the bytes in input buffer \a in are depleted and \a flush
50 * is not GRPC_STREAM_COMPRESSION_FLUSH_NONE, the corresponding flush method is
51 * executed. The total number of bytes emitted is outputed in \a output_size.
Muxi Yanfbd06f72017-07-08 10:54:46 -070052 *
53 * A SYNC flush indicates that the entire messages in \a in can be decompressed
54 * from \a out. A FINISH flush implies a SYNC flush, and that any further
55 * compression will not be dependent on the state of the current context and any
56 * previous compressed bytes. It allows corresponding decompression context to
57 * be dropped when reaching this boundary.
Muxi Yanc1f837c2017-05-04 18:17:13 -070058 */
59bool grpc_stream_compress(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 grpc_stream_compression_flush flush);
63
64/**
65 * Decompress bytes provided in \a in with a given context. Emits at most \a
66 * max_output_size decompressed bytes into \a out. If decompression process
67 * reached the end of a gzip stream, \a end_of_context is set to true; otherwise
68 * it is set to false. The total number of bytes emitted is outputed in \a
69 * output_size.
70 */
71bool grpc_stream_decompress(grpc_stream_compression_context *ctx,
72 grpc_slice_buffer *in, grpc_slice_buffer *out,
73 size_t *output_size, size_t max_output_size,
74 bool *end_of_context);
75
76/**
77 * Creates a stream compression context. \a pending_bytes_buffer is the input
78 * buffer for compression/decompression operations. \a method specifies whether
79 * the context is for compression or decompression.
80 */
81grpc_stream_compression_context *grpc_stream_compression_context_create(
82 grpc_stream_compression_method method);
83
84/**
85 * Destroys a stream compression context.
86 */
87void grpc_stream_compression_context_destroy(
88 grpc_stream_compression_context *ctx);
89
90#endif