zlib.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. // This is a derivative work based on Zlib, copyright below:
  10. /*
  11. Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
  12. This software is provided 'as-is', without any express or implied
  13. warranty. In no event will the authors be held liable for any damages
  14. arising from the use of this software.
  15. Permission is granted to anyone to use this software for any purpose,
  16. including commercial applications, and to alter it and redistribute it
  17. freely, subject to the following restrictions:
  18. 1. The origin of this software must not be misrepresented; you must not
  19. claim that you wrote the original software. If you use this software
  20. in a product, an acknowledgment in the product documentation would be
  21. appreciated but is not required.
  22. 2. Altered source versions must be plainly marked as such, and must not be
  23. misrepresented as being the original software.
  24. 3. This notice may not be removed or altered from any source distribution.
  25. Jean-loup Gailly Mark Adler
  26. jloup@gzip.org madler@alumni.caltech.edu
  27. The data format used by the zlib library is described by RFCs (Request for
  28. Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
  29. (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
  30. */
  31. #ifndef BOOST_BEAST_ZLIB_ZLIB_HPP
  32. #define BOOST_BEAST_ZLIB_ZLIB_HPP
  33. #include <boost/beast/core/detail/config.hpp>
  34. #include <cstdint>
  35. #include <cstdlib>
  36. namespace boost {
  37. namespace beast {
  38. namespace zlib {
  39. #if !defined(__MACTYPES__)
  40. using Byte = unsigned char; // 8 bits
  41. #endif
  42. using uInt = unsigned int; // 16 bits or more
  43. /* Possible values of the data_type field (though see inflate()) */
  44. enum kind
  45. {
  46. binary = 0,
  47. text = 1,
  48. unknown = 2
  49. };
  50. /** Deflate codec parameters.
  51. Objects of this type are filled in by callers and provided to the
  52. deflate codec to define the input and output areas for the next
  53. compress or decompress operation.
  54. The application must update next_in and avail_in when avail_in has dropped
  55. to zero. It must update next_out and avail_out when avail_out has dropped
  56. to zero. The application must initialize zalloc, zfree and opaque before
  57. calling the init function. All other fields are set by the compression
  58. library and must not be updated by the application.
  59. The fields total_in and total_out can be used for statistics or progress
  60. reports. After compression, total_in holds the total size of the
  61. uncompressed data and may be saved for use in the decompressor (particularly
  62. if the decompressor wants to decompress everything in a single step).
  63. */
  64. struct z_params
  65. {
  66. /** A pointer to the next input byte.
  67. If there is no more input, this may be set to `nullptr`.
  68. The application must update `next_in` and `avail_in` when
  69. `avail_in` has dropped to zero.
  70. */
  71. void const* next_in;
  72. /** The number of bytes of input available at `next_in`.
  73. If there is no more input, this should be set to zero.
  74. The application must update `next_in` and `avail_in` when
  75. `avail_in` has dropped to zero.
  76. */
  77. std::size_t avail_in;
  78. /** The total number of input bytes read so far.
  79. This field is set by the compression library and must
  80. not be updated by the application.
  81. This field can also be used for statistics or progress
  82. reports.
  83. After compression, total_in holds the total size of the
  84. uncompressed data and may be saved for use by the
  85. decompressor (particularly if the decompressor wants
  86. to decompress everything in a single step).
  87. */
  88. std::size_t total_in = 0;
  89. /** A pointer to the next output byte.
  90. The application must update `next_out` and `avail_out`
  91. when avail_out has dropped to zero.
  92. */
  93. void* next_out;
  94. /** The remaining bytes of space at `next_out`.
  95. The application must update `next_out` and `avail_out`
  96. when avail_out has dropped to zero.
  97. */
  98. std::size_t avail_out;
  99. /** The total number of bytes output so far.
  100. This field is set by the compression library and must
  101. not be updated by the application.
  102. This field can also be used for statistics or progress
  103. reports.
  104. */
  105. std::size_t total_out = 0;
  106. /** Best guess about the data type: binary or text
  107. This represents binary or text for deflate, or
  108. the decoding state for inflate.
  109. */
  110. int data_type = unknown;
  111. };
  112. /** Flush option.
  113. The allowed flush values for the @ref deflate_stream::write
  114. and @ref inflate_stream::write functions.
  115. Please refer to @ref deflate_stream::write and
  116. @ref inflate_stream::write for details.
  117. @see
  118. deflate_stream::write,
  119. inflate_stream::write
  120. */
  121. enum class Flush
  122. {
  123. // order matters
  124. /// No policy
  125. none,
  126. /// Flush all pending output on a bit boundary and hold up to seven bits
  127. block,
  128. /// Flush all pending output on a bit boundary
  129. partial,
  130. /// Flush all pending output on a byte boundary
  131. sync,
  132. /// Flush all pending output on a byte boundary and reset state
  133. full,
  134. /// Compress the input left in a single step
  135. finish,
  136. /// Flush output as in Flush::block or at the end of each deflate block header
  137. trees
  138. };
  139. /** Compression levels.
  140. The compression levels go from 0 and 9: 1 gives best speed, 9 gives
  141. best compression.
  142. Compression level 0 gives no compression at all. The input data is
  143. simply copied a block at a time.
  144. A compression level 6 is usually a default compromise between
  145. speed and compression.
  146. */
  147. enum compression
  148. {
  149. none = 0,
  150. best_speed = 1,
  151. best_size = 9,
  152. default_size = -1
  153. };
  154. /** Compression strategy.
  155. These are used when compressing streams.
  156. */
  157. enum class Strategy
  158. {
  159. /** Default strategy.
  160. This is suitable for general purpose compression, and works
  161. well in the majority of cases.
  162. */
  163. normal,
  164. /** Filtered strategy.
  165. This strategy should be used when the data be compressed
  166. is produced by a filter or predictor.
  167. */
  168. filtered,
  169. /** Huffman-only strategy.
  170. This strategy only performs Huffman encoding, without doing
  171. any string matching.
  172. */
  173. huffman,
  174. /** Run Length Encoding strategy.
  175. This strategy limits match distances to one, making it
  176. equivalent to run length encoding. This can give better
  177. performance for things like PNG image data.
  178. */
  179. rle,
  180. /** Fixed table strategy.
  181. This strategy prevents the use of dynamic Huffman codes,
  182. allowing for a simpler decoder for special applications.
  183. */
  184. fixed
  185. };
  186. } // zlib
  187. } // beast
  188. } // boost
  189. #endif