Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Muxi Yan | 163d8d6 | 2017-06-30 09:53:37 -0700 | [diff] [blame] | 3 | * Copyright 2017 gRPC authors. |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 4 | * |
Muxi Yan | 163d8d6 | 2017-06-30 09:53:37 -0700 | [diff] [blame] | 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 |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 8 | * |
Muxi Yan | 163d8d6 | 2017-06-30 09:53:37 -0700 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 10 | * |
Muxi Yan | 163d8d6 | 2017-06-30 09:53:37 -0700 | [diff] [blame] | 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. |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Muxi Yan | 163d8d6 | 2017-06-30 09:53:37 -0700 | [diff] [blame] | 19 | #ifndef GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H |
| 20 | #define GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 21 | |
| 22 | #include <stdbool.h> |
| 23 | |
| 24 | #include <grpc/slice_buffer.h> |
| 25 | #include <zlib.h> |
| 26 | |
| 27 | /* Stream compression/decompression context */ |
| 28 | typedef struct grpc_stream_compression_context { |
| 29 | z_stream zs; |
| 30 | int (*flate)(z_stream *zs, int flush); |
| 31 | } grpc_stream_compression_context; |
| 32 | |
| 33 | typedef enum grpc_stream_compression_method { |
Muxi Yan | fbd06f7 | 2017-07-08 10:54:46 -0700 | [diff] [blame] | 34 | GRPC_STREAM_COMPRESSION_COMPRESS = 0, |
Muxi Yan | f9ff554 | 2017-07-12 14:02:27 -0700 | [diff] [blame] | 35 | GRPC_STREAM_COMPRESSION_DECOMPRESS, |
| 36 | GRPC_STREAM_COMPRESSION_METHOD_COUNT |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 37 | } grpc_stream_compression_method; |
| 38 | |
| 39 | typedef enum grpc_stream_compression_flush { |
Muxi Yan | fbd06f7 | 2017-07-08 10:54:46 -0700 | [diff] [blame] | 40 | GRPC_STREAM_COMPRESSION_FLUSH_NONE = 0, |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 41 | GRPC_STREAM_COMPRESSION_FLUSH_SYNC, |
Muxi Yan | fbd06f7 | 2017-07-08 10:54:46 -0700 | [diff] [blame] | 42 | GRPC_STREAM_COMPRESSION_FLUSH_FINISH, |
| 43 | GRPC_STREAM_COMPRESSION_FLUSH_COUNT |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 44 | } 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 Yan | fbd06f7 | 2017-07-08 10:54:46 -0700 | [diff] [blame] | 52 | * |
| 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 Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 58 | */ |
| 59 | bool 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 | */ |
| 71 | bool 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 | */ |
| 81 | grpc_stream_compression_context *grpc_stream_compression_context_create( |
| 82 | grpc_stream_compression_method method); |
| 83 | |
| 84 | /** |
| 85 | * Destroys a stream compression context. |
| 86 | */ |
| 87 | void grpc_stream_compression_context_destroy( |
| 88 | grpc_stream_compression_context *ctx); |
| 89 | |
| 90 | #endif |