1 /* 2 * 3 * Copyright 2017 gRPC authors. 4 * 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 */ 18 19 #ifndef GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H 20 #define GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <stdbool.h> 25 26 #include <grpc/slice_buffer.h> 27 #include <zlib.h> 28 29 #include "src/core/lib/transport/static_metadata.h" 30 31 typedef struct grpc_stream_compression_vtable grpc_stream_compression_vtable; 32 33 /* Stream compression/decompression context */ 34 typedef struct grpc_stream_compression_context { 35 const grpc_stream_compression_vtable* vtable; 36 } grpc_stream_compression_context; 37 38 typedef enum grpc_stream_compression_method { 39 GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS = 0, 40 GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS, 41 GRPC_STREAM_COMPRESSION_GZIP_COMPRESS, 42 GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS, 43 GRPC_STREAM_COMPRESSION_METHOD_COUNT 44 } grpc_stream_compression_method; 45 46 typedef enum grpc_stream_compression_flush { 47 GRPC_STREAM_COMPRESSION_FLUSH_NONE = 0, 48 GRPC_STREAM_COMPRESSION_FLUSH_SYNC, 49 GRPC_STREAM_COMPRESSION_FLUSH_FINISH, 50 GRPC_STREAM_COMPRESSION_FLUSH_COUNT 51 } grpc_stream_compression_flush; 52 53 struct grpc_stream_compression_vtable { 54 bool (*compress)(grpc_stream_compression_context* ctx, grpc_slice_buffer* in, 55 grpc_slice_buffer* out, size_t* output_size, 56 size_t max_output_size, grpc_stream_compression_flush flush); 57 bool (*decompress)(grpc_stream_compression_context* ctx, 58 grpc_slice_buffer* in, grpc_slice_buffer* out, 59 size_t* output_size, size_t max_output_size, 60 bool* end_of_context); 61 grpc_stream_compression_context* (*context_create)( 62 grpc_stream_compression_method method); 63 void (*context_destroy)(grpc_stream_compression_context* ctx); 64 }; 65 66 /** 67 * Compress bytes provided in \a in with a given context, with an optional flush 68 * at the end of compression. Emits at most \a max_output_size compressed bytes 69 * into \a out. If all the bytes in input buffer \a in are depleted and \a flush 70 * is not GRPC_STREAM_COMPRESSION_FLUSH_NONE, the corresponding flush method is 71 * executed. The total number of bytes emitted is outputed in \a output_size. 72 * 73 * A SYNC flush indicates that the entire messages in \a in can be decompressed 74 * from \a out. A FINISH flush implies a SYNC flush, and that any further 75 * compression will not be dependent on the state of the current context and any 76 * previous compressed bytes. It allows corresponding decompression context to 77 * be dropped when reaching this boundary. 78 */ 79 bool grpc_stream_compress(grpc_stream_compression_context* ctx, 80 grpc_slice_buffer* in, grpc_slice_buffer* out, 81 size_t* output_size, size_t max_output_size, 82 grpc_stream_compression_flush flush); 83 84 /** 85 * Decompress bytes provided in \a in with a given context. Emits at most \a 86 * max_output_size decompressed bytes into \a out. If decompression process 87 * reached the end of a gzip stream, \a end_of_context is set to true; otherwise 88 * it is set to false. The total number of bytes emitted is outputed in \a 89 * output_size. 90 */ 91 bool grpc_stream_decompress(grpc_stream_compression_context* ctx, 92 grpc_slice_buffer* in, grpc_slice_buffer* out, 93 size_t* output_size, size_t max_output_size, 94 bool* end_of_context); 95 96 /** 97 * Creates a stream compression context. \a pending_bytes_buffer is the input 98 * buffer for compression/decompression operations. \a method specifies whether 99 * the context is for compression or decompression. 100 */ 101 grpc_stream_compression_context* grpc_stream_compression_context_create( 102 grpc_stream_compression_method method); 103 104 /** 105 * Destroys a stream compression context. 106 */ 107 void grpc_stream_compression_context_destroy( 108 grpc_stream_compression_context* ctx); 109 110 /** 111 * Parse stream compression method based on algorithm name 112 */ 113 int grpc_stream_compression_method_parse( 114 grpc_slice value, bool is_compress, grpc_stream_compression_method* method); 115 116 #endif 117