deflate_stream.hpp 16 KB

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