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 | |
Yash Tibrewal | bc460fa | 2017-10-02 17:42:41 -0700 | [diff] [blame] | 27 | #include "src/core/lib/transport/static_metadata.h" |
| 28 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
Muxi Yan | 130e070 | 2017-09-01 18:02:51 -0700 | [diff] [blame] | 33 | typedef struct grpc_stream_compression_vtable grpc_stream_compression_vtable; |
| 34 | |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 35 | /* Stream compression/decompression context */ |
| 36 | typedef struct grpc_stream_compression_context { |
Muxi Yan | 130e070 | 2017-09-01 18:02:51 -0700 | [diff] [blame] | 37 | const grpc_stream_compression_vtable *vtable; |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 38 | } grpc_stream_compression_context; |
| 39 | |
| 40 | typedef enum grpc_stream_compression_method { |
Muxi Yan | 130e070 | 2017-09-01 18:02:51 -0700 | [diff] [blame] | 41 | 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 Yan | f9ff554 | 2017-07-12 14:02:27 -0700 | [diff] [blame] | 45 | GRPC_STREAM_COMPRESSION_METHOD_COUNT |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 46 | } grpc_stream_compression_method; |
| 47 | |
| 48 | typedef enum grpc_stream_compression_flush { |
Muxi Yan | fbd06f7 | 2017-07-08 10:54:46 -0700 | [diff] [blame] | 49 | GRPC_STREAM_COMPRESSION_FLUSH_NONE = 0, |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 50 | GRPC_STREAM_COMPRESSION_FLUSH_SYNC, |
Muxi Yan | fbd06f7 | 2017-07-08 10:54:46 -0700 | [diff] [blame] | 51 | GRPC_STREAM_COMPRESSION_FLUSH_FINISH, |
| 52 | GRPC_STREAM_COMPRESSION_FLUSH_COUNT |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 53 | } grpc_stream_compression_flush; |
| 54 | |
Muxi Yan | 130e070 | 2017-09-01 18:02:51 -0700 | [diff] [blame] | 55 | struct grpc_stream_compression_vtable { |
Muxi Yan | 03600fc | 2017-09-05 13:42:44 -0700 | [diff] [blame] | 56 | 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 Yan | 130e070 | 2017-09-01 18:02:51 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 68 | /** |
| 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 Yan | fbd06f7 | 2017-07-08 10:54:46 -0700 | [diff] [blame] | 74 | * |
| 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 Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 80 | */ |
| 81 | bool 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 | */ |
| 93 | bool 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 | */ |
| 103 | grpc_stream_compression_context *grpc_stream_compression_context_create( |
| 104 | grpc_stream_compression_method method); |
| 105 | |
| 106 | /** |
| 107 | * Destroys a stream compression context. |
| 108 | */ |
| 109 | void grpc_stream_compression_context_destroy( |
| 110 | grpc_stream_compression_context *ctx); |
| 111 | |
Muxi Yan | 130e070 | 2017-09-01 18:02:51 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Parse stream compression method based on algorithm name |
| 114 | */ |
| 115 | int grpc_stream_compression_method_parse( |
| 116 | grpc_slice value, bool is_compress, grpc_stream_compression_method *method); |
| 117 | |
Yash Tibrewal | a7e6d65 | 2017-09-20 18:56:37 -0700 | [diff] [blame] | 118 | #ifdef __cplusplus |
| 119 | } |
| 120 | #endif |
| 121 | |
Muxi Yan | c1f837c | 2017-05-04 18:17:13 -0700 | [diff] [blame] | 122 | #endif |