stream_compression.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H
  19. #define GRPC_CORE_LIB_COMPRESSION_STREAM_COMPRESSION_H
  20. #include <stdbool.h>
  21. #include <grpc/slice_buffer.h>
  22. #include <zlib.h>
  23. /* Stream compression/decompression context */
  24. typedef struct grpc_stream_compression_context {
  25. z_stream zs;
  26. int (*flate)(z_stream *zs, int flush);
  27. } grpc_stream_compression_context;
  28. typedef enum grpc_stream_compression_method {
  29. GRPC_STREAM_COMPRESSION_COMPRESS = 0,
  30. GRPC_STREAM_COMPRESSION_DECOMPRESS,
  31. GRPC_STREAM_COMPRESSION_METHOD_COUNT
  32. } grpc_stream_compression_method;
  33. typedef enum grpc_stream_compression_flush {
  34. GRPC_STREAM_COMPRESSION_FLUSH_NONE = 0,
  35. GRPC_STREAM_COMPRESSION_FLUSH_SYNC,
  36. GRPC_STREAM_COMPRESSION_FLUSH_FINISH,
  37. GRPC_STREAM_COMPRESSION_FLUSH_COUNT
  38. } grpc_stream_compression_flush;
  39. /**
  40. * Compress bytes provided in \a in with a given context, with an optional flush
  41. * at the end of compression. Emits at most \a max_output_size compressed bytes
  42. * into \a out. If all the bytes in input buffer \a in are depleted and \a flush
  43. * is not GRPC_STREAM_COMPRESSION_FLUSH_NONE, the corresponding flush method is
  44. * executed. The total number of bytes emitted is outputed in \a output_size.
  45. *
  46. * A SYNC flush indicates that the entire messages in \a in can be decompressed
  47. * from \a out. A FINISH flush implies a SYNC flush, and that any further
  48. * compression will not be dependent on the state of the current context and any
  49. * previous compressed bytes. It allows corresponding decompression context to
  50. * be dropped when reaching this boundary.
  51. */
  52. bool grpc_stream_compress(grpc_stream_compression_context *ctx,
  53. grpc_slice_buffer *in, grpc_slice_buffer *out,
  54. size_t *output_size, size_t max_output_size,
  55. grpc_stream_compression_flush flush);
  56. /**
  57. * Decompress bytes provided in \a in with a given context. Emits at most \a
  58. * max_output_size decompressed bytes into \a out. If decompression process
  59. * reached the end of a gzip stream, \a end_of_context is set to true; otherwise
  60. * it is set to false. The total number of bytes emitted is outputed in \a
  61. * output_size.
  62. */
  63. bool grpc_stream_decompress(grpc_stream_compression_context *ctx,
  64. grpc_slice_buffer *in, grpc_slice_buffer *out,
  65. size_t *output_size, size_t max_output_size,
  66. bool *end_of_context);
  67. /**
  68. * Creates a stream compression context. \a pending_bytes_buffer is the input
  69. * buffer for compression/decompression operations. \a method specifies whether
  70. * the context is for compression or decompression.
  71. */
  72. grpc_stream_compression_context *grpc_stream_compression_context_create(
  73. grpc_stream_compression_method method);
  74. /**
  75. * Destroys a stream compression context.
  76. */
  77. void grpc_stream_compression_context_destroy(
  78. grpc_stream_compression_context *ctx);
  79. #endif