deflate_stream.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #ifndef BOOST_BEAST_ZLIB_DEFLATE_STREAM_HPP
  10. #define BOOST_BEAST_ZLIB_DEFLATE_STREAM_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/zlib/error.hpp>
  13. #include <boost/beast/zlib/zlib.hpp>
  14. #include <boost/beast/zlib/detail/deflate_stream.hpp>
  15. #include <algorithm>
  16. #include <cstdlib>
  17. #include <cstdint>
  18. #include <cstring>
  19. #include <memory>
  20. namespace boost {
  21. namespace beast {
  22. namespace zlib {
  23. // This is a derivative work based on Zlib, copyright below:
  24. /*
  25. Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
  26. This software is provided 'as-is', without any express or implied
  27. warranty. In no event will the authors be held liable for any damages
  28. arising from the use of this software.
  29. Permission is granted to anyone to use this software for any purpose,
  30. including commercial applications, and to alter it and redistribute it
  31. freely, subject to the following restrictions:
  32. 1. The origin of this software must not be misrepresented; you must not
  33. claim that you wrote the original software. If you use this software
  34. in a product, an acknowledgment in the product documentation would be
  35. appreciated but is not required.
  36. 2. Altered source versions must be plainly marked as such, and must not be
  37. misrepresented as being the original software.
  38. 3. This notice may not be removed or altered from any source distribution.
  39. Jean-loup Gailly Mark Adler
  40. jloup@gzip.org madler@alumni.caltech.edu
  41. The data format used by the zlib library is described by RFCs (Request for
  42. Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
  43. (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
  44. */
  45. /** Raw deflate compressor.
  46. This is a port of zlib's "deflate" functionality to C++.
  47. */
  48. class deflate_stream
  49. : private detail::deflate_stream
  50. {
  51. public:
  52. /** Construct a default deflate stream.
  53. Upon construction, the stream settings will be set
  54. to these default values:
  55. @li `level = 6`
  56. @li `windowBits = 15`
  57. @li `memLevel = 8`
  58. @li `strategy = Strategy::normal`
  59. Although the stream is ready to be used immediately
  60. after construction, any required internal buffers are
  61. not dynamically allocated until needed.
  62. */
  63. deflate_stream()
  64. {
  65. reset(6, 15, DEF_MEM_LEVEL, Strategy::normal);
  66. }
  67. /** Reset the stream and compression settings.
  68. This function initializes the stream to the specified
  69. compression settings.
  70. Although the stream is ready to be used immediately
  71. after a reset, any required internal buffers are not
  72. dynamically allocated until needed.
  73. @note Any unprocessed input or pending output from
  74. previous calls are discarded.
  75. */
  76. void
  77. reset(
  78. int level,
  79. int windowBits,
  80. int memLevel,
  81. Strategy strategy)
  82. {
  83. doReset(level, windowBits, memLevel, strategy);
  84. }
  85. /** Reset the stream without deallocating memory.
  86. This function performs the equivalent of calling `clear`
  87. followed by `reset` with the same compression settings,
  88. without deallocating the internal buffers.
  89. @note Any unprocessed input or pending output from
  90. previous calls are discarded.
  91. */
  92. void
  93. reset()
  94. {
  95. doReset();
  96. }
  97. /** Clear the stream.
  98. This function resets the stream and frees all dynamically
  99. allocated internal buffers. The compression settings are
  100. left unchanged.
  101. @note Any unprocessed input or pending output from
  102. previous calls are discarded.
  103. */
  104. void
  105. clear()
  106. {
  107. doClear();
  108. }
  109. /** Returns the upper limit on the size of a compressed block.
  110. This function makes a conservative estimate of the maximum number
  111. of bytes needed to store the result of compressing a block of
  112. data based on the current compression level and strategy.
  113. @param sourceLen The size of the uncompressed data.
  114. @return The maximum number of resulting compressed bytes.
  115. */
  116. std::size_t
  117. upper_bound(std::size_t sourceLen) const
  118. {
  119. return doUpperBound(sourceLen);
  120. }
  121. /** Fine tune internal compression parameters.
  122. Compression parameters should only be tuned by someone who
  123. understands the algorithm used by zlib's deflate for searching
  124. for the best matching string, and even then only by the most
  125. fanatic optimizer trying to squeeze out the last compressed bit
  126. for their specific input data. Read the deflate.c source code
  127. (ZLib) for the meaning of the max_lazy, good_length, nice_length,
  128. and max_chain parameters.
  129. */
  130. void
  131. tune(
  132. int good_length,
  133. int max_lazy,
  134. int nice_length,
  135. int max_chain)
  136. {
  137. doTune(good_length, max_lazy, nice_length, max_chain);
  138. }
  139. /** Compress input and write output.
  140. This function compresses as much data as possible, and stops when
  141. the input buffer becomes empty or the output buffer becomes full.
  142. It may introduce some output latency (reading input without
  143. producing any output) except when forced to flush.
  144. In each call, one or both of these actions are performed:
  145. @li Compress more input starting at `zs.next_in` and update
  146. `zs.next_in` and `zs.avail_in` accordingly. If not all
  147. input can be processed (because there is not enough room in
  148. the output buffer), `zs.next_in` and `zs.avail_in` are updated
  149. and processing will resume at this point for the next call.
  150. @li Provide more output starting at `zs.next_out` and update
  151. `zs.next_out` and `zs.avail_out` accordingly. This action is
  152. forced if the parameter flush is not `Flush::none`. Forcing
  153. flush frequently degrades the compression ratio, so this parameter
  154. should be set only when necessary (in interactive applications).
  155. Some output may be provided even if flush is not set.
  156. Before the call, the application must ensure that at least one
  157. of the actions is possible, by providing more input and/or
  158. consuming more output, and updating `zs.avail_in` or `zs.avail_out`
  159. accordingly; `zs.avail_out` should never be zero before the call.
  160. The application can consume the compressed output when it wants,
  161. for example when the output buffer is full (`zs.avail_out == 0`),
  162. or after each call of `write`. If `write` returns no error
  163. with zero `zs.avail_out`, it must be called again after making
  164. room in the output buffer because there might be more output
  165. pending.
  166. Normally the parameter flush is set to `Flush::none`, which allows
  167. deflate to decide how much data to accumulate before producing
  168. output, in order to maximize compression.
  169. If the parameter flush is set to `Flush::sync`, all pending output
  170. is flushed to the output buffer and the output is aligned on a
  171. byte boundary, so that the decompressor can get all input data
  172. available so far. In particular `zs.avail_in` is zero after the
  173. call if enough output space has been provided before the call.
  174. Flushing may degrade compression for some compression algorithms
  175. and so it should be used only when necessary. This completes the
  176. current deflate block and follows it with an empty stored block
  177. that is three bits plus filler bits to the next byte, followed
  178. by the four bytes `{ 0x00, 0x00 0xff 0xff }`.
  179. If flush is set to `Flush::partial`, all pending output is flushed
  180. to the output buffer, but the output is not aligned to a byte
  181. boundary. All of the input data so far will be available to the
  182. decompressor, as for Z_SYNC_FLUSH. This completes the current
  183. deflate block and follows it with an empty fixed codes block that
  184. is 10 bits long. This assures that enough bytes are output in order
  185. for the decompressor to finish the block before the empty fixed
  186. code block.
  187. If flush is set to `Flush::block`, a deflate block is completed
  188. and emitted, as for `Flush::sync`, but the output is not aligned
  189. on a byte boundary, and up to seven bits of the current block are
  190. held to be written as the next byte after the next deflate block
  191. is completed. In this case, the decompressor may not be provided
  192. enough bits at this point in order to complete decompression of
  193. the data provided so far to the compressor. It may need to wait
  194. for the next block to be emitted. This is for advanced applications
  195. that need to control the emission of deflate blocks.
  196. If flush is set to `Flush::full`, all output is flushed as with
  197. `Flush::sync`, and the compression state is reset so that
  198. decompression can restart from this point if previous compressed
  199. data has been damaged or if random access is desired. Using
  200. `Flush::full` too often can seriously degrade compression.
  201. If `write` returns with `zs.avail_out == 0`, this function must
  202. be called again with the same value of the flush parameter and
  203. more output space (updated `zs.avail_out`), until the flush is
  204. complete (`write` returns with non-zero `zs.avail_out`). In the
  205. case of a `Flush::full`or `Flush::sync`, make sure that
  206. `zs.avail_out` is greater than six to avoid repeated flush markers
  207. due to `zs.avail_out == 0` on return.
  208. If the parameter flush is set to `Flush::finish`, pending input
  209. is processed, pending output is flushed and deflate returns the
  210. error `error::end_of_stream` if there was enough output space;
  211. if deflate returns with no error, this function must be called
  212. again with `Flush::finish` and more output space (updated
  213. `zs.avail_out`) but no more input data, until it returns the
  214. error `error::end_of_stream` or another error. After `write` has
  215. returned the `error::end_of_stream` error, the only possible
  216. operations on the stream are to reset or destroy.
  217. `Flush::finish` can be used immediately after initialization
  218. if all the compression is to be done in a single step. In this
  219. case, `zs.avail_out` must be at least value returned by
  220. `upper_bound` (see below). Then `write` is guaranteed to return
  221. the `error::end_of_stream` error. If not enough output space
  222. is provided, deflate will not return `error::end_of_stream`,
  223. and it must be called again as described above.
  224. `write` returns no error if some progress has been made (more
  225. input processed or more output produced), `error::end_of_stream`
  226. if all input has been consumed and all output has been produced
  227. (only when flush is set to `Flush::finish`), `error::stream_error`
  228. if the stream state was inconsistent (for example if `zs.next_in`
  229. or `zs.next_out` was `nullptr`), `error::need_buffers` if no
  230. progress is possible (for example `zs.avail_in` or `zs.avail_out`
  231. was zero). Note that `error::need_buffers` is not fatal, and
  232. `write` can be called again with more input and more output space
  233. to continue compressing.
  234. */
  235. void
  236. write(
  237. z_params& zs,
  238. Flush flush,
  239. error_code& ec)
  240. {
  241. doWrite(zs, flush, ec);
  242. }
  243. /** Update the compression level and strategy.
  244. This function dynamically updates the compression level and
  245. compression strategy. The interpretation of level and strategy
  246. is as in @ref reset. This can be used to switch between compression
  247. and straight copy of the input data, or to switch to a different kind
  248. of input data requiring a different strategy. If the compression level
  249. is changed, the input available so far is compressed with the old level
  250. (and may be flushed); the new level will take effect only at the next
  251. call of @ref write.
  252. Before the call of `params`, the stream state must be set as for a
  253. call of @ref write, since the currently available input may have to be
  254. compressed and flushed. In particular, `zs.avail_out` must be non-zero.
  255. @return `Z_OK` if success, `Z_STREAM_ERROR` if the source stream state
  256. was inconsistent or if a parameter was invalid, `error::need_buffers`
  257. if `zs.avail_out` was zero.
  258. */
  259. void
  260. params(
  261. z_params& zs,
  262. int level,
  263. Strategy strategy,
  264. error_code& ec)
  265. {
  266. doParams(zs, level, strategy, ec);
  267. }
  268. /** Return bits pending in the output.
  269. This function returns the number of bytes and bits of output
  270. that have been generated, but not yet provided in the available
  271. output. The bytes not provided would be due to the available
  272. output space having being consumed. The number of bits of output
  273. not provided are between 0 and 7, where they await more bits to
  274. join them in order to fill out a full byte. If pending or bits
  275. are `nullptr`, then those values are not set.
  276. @return `Z_OK` if success, or `Z_STREAM_ERROR` if the source
  277. stream state was inconsistent.
  278. */
  279. void
  280. pending(unsigned *value, int *bits)
  281. {
  282. doPending(value, bits);
  283. }
  284. /** Insert bits into the compressed output stream.
  285. This function inserts bits in the deflate output stream. The
  286. intent is that this function is used to start off the deflate
  287. output with the bits leftover from a previous deflate stream when
  288. appending to it. As such, this function can only be used for raw
  289. deflate, and must be used before the first `write` call after an
  290. initialization. `bits` must be less than or equal to 16, and that
  291. many of the least significant bits of `value` will be inserted in
  292. the output.
  293. @return `error::need_buffers` if there was not enough room in
  294. the internal buffer to insert the bits.
  295. */
  296. void
  297. prime(int bits, int value, error_code& ec)
  298. {
  299. return doPrime(bits, value, ec);
  300. }
  301. };
  302. /** Returns the upper limit on the size of a compressed block.
  303. This function makes a conservative estimate of the maximum number
  304. of bytes needed to store the result of compressing a block of
  305. data.
  306. @param bytes The size of the uncompressed data.
  307. @return The maximum number of resulting compressed bytes.
  308. */
  309. std::size_t
  310. deflate_upper_bound(std::size_t bytes);
  311. /* For the default windowBits of 15 and memLevel of 8, this function returns
  312. a close to exact, as well as small, upper bound on the compressed size.
  313. They are coded as constants here for a reason--if the #define's are
  314. changed, then this function needs to be changed as well. The return
  315. value for 15 and 8 only works for those exact settings.
  316. For any setting other than those defaults for windowBits and memLevel,
  317. the value returned is a conservative worst case for the maximum expansion
  318. resulting from using fixed blocks instead of stored blocks, which deflate
  319. can emit on compressed data for some combinations of the parameters.
  320. This function could be more sophisticated to provide closer upper bounds for
  321. every combination of windowBits and memLevel. But even the conservative
  322. upper bound of about 14% expansion does not seem onerous for output buffer
  323. allocation.
  324. */
  325. inline
  326. std::size_t
  327. deflate_upper_bound(std::size_t bytes)
  328. {
  329. return bytes +
  330. ((bytes + 7) >> 3) +
  331. ((bytes + 63) >> 6) + 5 +
  332. 6;
  333. }
  334. } // zlib
  335. } // beast
  336. } // boost
  337. #endif