stream_compression.h 4.6 KB

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