deflate_stream.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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-2013 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_DETAIL_DEFLATE_STREAM_HPP
  32. #define BOOST_BEAST_ZLIB_DETAIL_DEFLATE_STREAM_HPP
  33. #include <boost/beast/zlib/error.hpp>
  34. #include <boost/beast/zlib/zlib.hpp>
  35. #include <boost/beast/zlib/detail/ranges.hpp>
  36. #include <boost/assert.hpp>
  37. #include <boost/config.hpp>
  38. #include <boost/optional.hpp>
  39. #include <boost/throw_exception.hpp>
  40. #include <cstdint>
  41. #include <cstdlib>
  42. #include <cstring>
  43. #include <memory>
  44. #include <stdexcept>
  45. #include <type_traits>
  46. namespace boost {
  47. namespace beast {
  48. namespace zlib {
  49. namespace detail {
  50. class deflate_stream
  51. {
  52. protected:
  53. // Upper limit on code length
  54. static std::uint8_t constexpr maxBits = 15;
  55. // Number of length codes, not counting the special END_BLOCK code
  56. static std::uint16_t constexpr lengthCodes = 29;
  57. // Number of literal bytes 0..255
  58. static std::uint16_t constexpr literals = 256;
  59. // Number of Literal or Length codes, including the END_BLOCK code
  60. static std::uint16_t constexpr lCodes = literals + 1 + lengthCodes;
  61. // Number of distance code lengths
  62. static std::uint16_t constexpr dCodes = 30;
  63. // Number of codes used to transfer the bit lengths
  64. static std::uint16_t constexpr blCodes = 19;
  65. // Number of distance codes
  66. static std::uint16_t constexpr distCodeLen = 512;
  67. // Size limit on bit length codes
  68. static std::uint8_t constexpr maxBlBits= 7;
  69. static std::uint16_t constexpr minMatch = 3;
  70. static std::uint16_t constexpr maxMatch = 258;
  71. // Can't change minMatch without also changing code, see original zlib
  72. BOOST_STATIC_ASSERT(minMatch == 3);
  73. // end of block literal code
  74. static std::uint16_t constexpr END_BLOCK = 256;
  75. // repeat previous bit length 3-6 times (2 bits of repeat count)
  76. static std::uint8_t constexpr REP_3_6 = 16;
  77. // repeat a zero length 3-10 times (3 bits of repeat count)
  78. static std::uint8_t constexpr REPZ_3_10 = 17;
  79. // repeat a zero length 11-138 times (7 bits of repeat count)
  80. static std::uint8_t constexpr REPZ_11_138 = 18;
  81. // The three kinds of block type
  82. static std::uint8_t constexpr STORED_BLOCK = 0;
  83. static std::uint8_t constexpr STATIC_TREES = 1;
  84. static std::uint8_t constexpr DYN_TREES = 2;
  85. // Maximum value for memLevel in deflateInit2
  86. static std::uint8_t constexpr max_mem_level = 9;
  87. // Default memLevel
  88. static std::uint8_t constexpr DEF_MEM_LEVEL = max_mem_level;
  89. /* Note: the deflate() code requires max_lazy >= minMatch and max_chain >= 4
  90. For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  91. meaning.
  92. */
  93. // maximum heap size
  94. static std::uint16_t constexpr HEAP_SIZE = 2 * lCodes + 1;
  95. // size of bit buffer in bi_buf
  96. static std::uint8_t constexpr Buf_size = 16;
  97. // Matches of length 3 are discarded if their distance exceeds kTooFar
  98. static std::size_t constexpr kTooFar = 4096;
  99. /* Minimum amount of lookahead, except at the end of the input file.
  100. See deflate.c for comments about the minMatch+1.
  101. */
  102. static std::size_t constexpr kMinLookahead = maxMatch + minMatch+1;
  103. /* Number of bytes after end of data in window to initialize in order
  104. to avoid memory checker errors from longest match routines
  105. */
  106. static std::size_t constexpr kWinInit = maxMatch;
  107. // Describes a single value and its code string.
  108. struct ct_data
  109. {
  110. std::uint16_t fc; // frequency count or bit string
  111. std::uint16_t dl; // parent node in tree or length of bit string
  112. bool
  113. operator==(ct_data const& rhs) const
  114. {
  115. return fc == rhs.fc && dl == rhs.dl;
  116. }
  117. };
  118. struct static_desc
  119. {
  120. ct_data const* static_tree;// static tree or NULL
  121. std::uint8_t const* extra_bits; // extra bits for each code or NULL
  122. std::uint16_t extra_base; // base index for extra_bits
  123. std::uint16_t elems; // max number of elements in the tree
  124. std::uint8_t max_length; // max bit length for the codes
  125. };
  126. struct lut_type
  127. {
  128. // Number of extra bits for each length code
  129. std::uint8_t const extra_lbits[lengthCodes] = {
  130. 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0
  131. };
  132. // Number of extra bits for each distance code
  133. std::uint8_t const extra_dbits[dCodes] = {
  134. 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13
  135. };
  136. // Number of extra bits for each bit length code
  137. std::uint8_t const extra_blbits[blCodes] = {
  138. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7
  139. };
  140. // The lengths of the bit length codes are sent in order
  141. // of decreasing probability, to avoid transmitting the
  142. // lengths for unused bit length codes.
  143. std::uint8_t const bl_order[blCodes] = {
  144. 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15
  145. };
  146. ct_data ltree[lCodes + 2];
  147. ct_data dtree[dCodes];
  148. // Distance codes. The first 256 values correspond to the distances
  149. // 3 .. 258, the last 256 values correspond to the top 8 bits of
  150. // the 15 bit distances.
  151. std::uint8_t dist_code[distCodeLen];
  152. std::uint8_t length_code[maxMatch-minMatch+1];
  153. std::uint8_t base_length[lengthCodes];
  154. std::uint16_t base_dist[dCodes];
  155. static_desc l_desc = {
  156. ltree, extra_lbits, literals+1, lCodes, maxBits
  157. };
  158. static_desc d_desc = {
  159. dtree, extra_dbits, 0, dCodes, maxBits
  160. };
  161. static_desc bl_desc =
  162. {
  163. nullptr, extra_blbits, 0, blCodes, maxBlBits
  164. };
  165. };
  166. struct tree_desc
  167. {
  168. ct_data *dyn_tree; /* the dynamic tree */
  169. int max_code; /* largest code with non zero frequency */
  170. static_desc const* stat_desc; /* the corresponding static tree */
  171. };
  172. enum block_state
  173. {
  174. need_more, /* block not completed, need more input or more output */
  175. block_done, /* block flush performed */
  176. finish_started, /* finish started, need only more output at next deflate */
  177. finish_done /* finish done, accept no more input or output */
  178. };
  179. // VFALCO This might not be needed, e.g. for zip/gzip
  180. enum StreamStatus
  181. {
  182. EXTRA_STATE = 69,
  183. NAME_STATE = 73,
  184. COMMENT_STATE = 91,
  185. HCRC_STATE = 103,
  186. BUSY_STATE = 113,
  187. FINISH_STATE = 666
  188. };
  189. /* A std::uint16_t is an index in the character window. We use short instead of int to
  190. * save space in the various tables. IPos is used only for parameter passing.
  191. */
  192. using IPos = unsigned;
  193. using self = deflate_stream;
  194. typedef block_state(self::*compress_func)(z_params& zs, Flush flush);
  195. //--------------------------------------------------------------------------
  196. lut_type const& lut_;
  197. bool inited_ = false;
  198. std::size_t buf_size_;
  199. std::unique_ptr<std::uint8_t[]> buf_;
  200. int status_; // as the name implies
  201. Byte* pending_buf_; // output still pending
  202. std::uint32_t
  203. pending_buf_size_; // size of pending_buf
  204. Byte* pending_out_; // next pending byte to output to the stream
  205. uInt pending_; // nb of bytes in the pending buffer
  206. boost::optional<Flush>
  207. last_flush_; // value of flush param for previous deflate call
  208. uInt w_size_; // LZ77 window size (32K by default)
  209. uInt w_bits_; // log2(w_size) (8..16)
  210. uInt w_mask_; // w_size - 1
  211. /* Sliding window. Input bytes are read into the second half of the window,
  212. and move to the first half later to keep a dictionary of at least wSize
  213. bytes. With this organization, matches are limited to a distance of
  214. wSize-maxMatch bytes, but this ensures that IO is always
  215. performed with a length multiple of the block size. Also, it limits
  216. the window size to 64K.
  217. To do: use the user input buffer as sliding window.
  218. */
  219. Byte *window_ = nullptr;
  220. /* Actual size of window: 2*wSize, except when the user input buffer
  221. is directly used as sliding window.
  222. */
  223. std::uint32_t window_size_;
  224. /* Link to older string with same hash index. To limit the size of this
  225. array to 64K, this link is maintained only for the last 32K strings.
  226. An index in this array is thus a window index modulo 32K.
  227. */
  228. std::uint16_t* prev_;
  229. std::uint16_t* head_; // Heads of the hash chains or 0
  230. uInt ins_h_; // hash index of string to be inserted
  231. uInt hash_size_; // number of elements in hash table
  232. uInt hash_bits_; // log2(hash_size)
  233. uInt hash_mask_; // hash_size-1
  234. /* Number of bits by which ins_h must be shifted at each input
  235. step. It must be such that after minMatch steps,
  236. the oldest byte no longer takes part in the hash key, that is:
  237. hash_shift * minMatch >= hash_bits
  238. */
  239. uInt hash_shift_;
  240. /* Window position at the beginning of the current output block.
  241. Gets negative when the window is moved backwards.
  242. */
  243. long block_start_;
  244. uInt match_length_; // length of best match
  245. IPos prev_match_; // previous match
  246. int match_available_; // set if previous match exists
  247. uInt strstart_; // start of string to insert
  248. uInt match_start_; // start of matching string
  249. uInt lookahead_; // number of valid bytes ahead in window
  250. /* Length of the best match at previous step. Matches not greater
  251. than this are discarded. This is used in the lazy match evaluation.
  252. */
  253. uInt prev_length_;
  254. /* To speed up deflation, hash chains are never searched beyond
  255. this length. A higher limit improves compression ratio but
  256. degrades the speed.
  257. */
  258. uInt max_chain_length_;
  259. /* Attempt to find a better match only when the current match is strictly
  260. smaller than this value. This mechanism is used only for compression
  261. levels >= 4.
  262. OR Insert new strings in the hash table only if the match length is not
  263. greater than this length. This saves time but degrades compression.
  264. used only for compression levels <= 3.
  265. */
  266. uInt max_lazy_match_;
  267. int level_; // compression level (1..9)
  268. Strategy strategy_; // favor or force Huffman coding
  269. // Use a faster search when the previous match is longer than this
  270. uInt good_match_;
  271. int nice_match_; // Stop searching when current match exceeds this
  272. ct_data dyn_ltree_[
  273. HEAP_SIZE]; // literal and length tree
  274. ct_data dyn_dtree_[
  275. 2*dCodes+1]; // distance tree
  276. ct_data bl_tree_[
  277. 2*blCodes+1]; // Huffman tree for bit lengths
  278. tree_desc l_desc_; // desc. for literal tree
  279. tree_desc d_desc_; // desc. for distance tree
  280. tree_desc bl_desc_; // desc. for bit length tree
  281. // number of codes at each bit length for an optimal tree
  282. std::uint16_t bl_count_[maxBits+1];
  283. // Index within the heap array of least frequent node in the Huffman tree
  284. static std::size_t constexpr kSmallest = 1;
  285. /* The sons of heap[n] are heap[2*n] and heap[2*n+1].
  286. heap[0] is not used. The same heap array is used to build all trees.
  287. */
  288. int heap_[2*lCodes+1]; // heap used to build the Huffman trees
  289. int heap_len_; // number of elements in the heap
  290. int heap_max_; // element of largest frequency
  291. // Depth of each subtree used as tie breaker for trees of equal frequency
  292. std::uint8_t depth_[2*lCodes+1];
  293. std::uint8_t *l_buf_; // buffer for literals or lengths
  294. /* Size of match buffer for literals/lengths.
  295. There are 4 reasons for limiting lit_bufsize to 64K:
  296. - frequencies can be kept in 16 bit counters
  297. - if compression is not successful for the first block, all input
  298. data is still in the window so we can still emit a stored block even
  299. when input comes from standard input. (This can also be done for
  300. all blocks if lit_bufsize is not greater than 32K.)
  301. - if compression is not successful for a file smaller than 64K, we can
  302. even emit a stored file instead of a stored block (saving 5 bytes).
  303. This is applicable only for zip (not gzip or zlib).
  304. - creating new Huffman trees less frequently may not provide fast
  305. adaptation to changes in the input data statistics. (Take for
  306. example a binary file with poorly compressible code followed by
  307. a highly compressible string table.) Smaller buffer sizes give
  308. fast adaptation but have of course the overhead of transmitting
  309. trees more frequently.
  310. - I can't count above 4
  311. */
  312. uInt lit_bufsize_;
  313. uInt last_lit_; // running index in l_buf_
  314. /* Buffer for distances. To simplify the code, d_buf_ and l_buf_
  315. have the same number of elements. To use different lengths, an
  316. extra flag array would be necessary.
  317. */
  318. std::uint16_t* d_buf_;
  319. std::uint32_t opt_len_; // bit length of current block with optimal trees
  320. std::uint32_t static_len_; // bit length of current block with static trees
  321. uInt matches_; // number of string matches in current block
  322. uInt insert_; // bytes at end of window left to insert
  323. /* Output buffer.
  324. Bits are inserted starting at the bottom (least significant bits).
  325. */
  326. std::uint16_t bi_buf_;
  327. /* Number of valid bits in bi_buf._ All bits above the last valid
  328. bit are always zero.
  329. */
  330. int bi_valid_;
  331. /* High water mark offset in window for initialized bytes -- bytes
  332. above this are set to zero in order to avoid memory check warnings
  333. when longest match routines access bytes past the input. This is
  334. then updated to the new high water mark.
  335. */
  336. std::uint32_t high_water_;
  337. //--------------------------------------------------------------------------
  338. deflate_stream()
  339. : lut_(get_lut())
  340. {
  341. }
  342. /* In order to simplify the code, particularly on 16 bit machines, match
  343. distances are limited to MAX_DIST instead of WSIZE.
  344. */
  345. std::size_t
  346. max_dist() const
  347. {
  348. return w_size_ - kMinLookahead;
  349. }
  350. void
  351. put_byte(std::uint8_t c)
  352. {
  353. pending_buf_[pending_++] = c;
  354. }
  355. void
  356. put_short(std::uint16_t w)
  357. {
  358. put_byte(w & 0xff);
  359. put_byte(w >> 8);
  360. }
  361. /* Send a value on a given number of bits.
  362. IN assertion: length <= 16 and value fits in length bits.
  363. */
  364. void
  365. send_bits(int value, int length)
  366. {
  367. if(bi_valid_ > (int)Buf_size - length)
  368. {
  369. bi_buf_ |= (std::uint16_t)value << bi_valid_;
  370. put_short(bi_buf_);
  371. bi_buf_ = (std::uint16_t)value >> (Buf_size - bi_valid_);
  372. bi_valid_ += length - Buf_size;
  373. }
  374. else
  375. {
  376. bi_buf_ |= (std::uint16_t)(value) << bi_valid_;
  377. bi_valid_ += length;
  378. }
  379. }
  380. // Send a code of the given tree
  381. void
  382. send_code(int value, ct_data const* tree)
  383. {
  384. send_bits(tree[value].fc, tree[value].dl);
  385. }
  386. /* Mapping from a distance to a distance code. dist is the
  387. distance - 1 and must not have side effects. _dist_code[256]
  388. and _dist_code[257] are never used.
  389. */
  390. std::uint8_t
  391. d_code(unsigned dist)
  392. {
  393. if(dist < 256)
  394. return lut_.dist_code[dist];
  395. return lut_.dist_code[256+(dist>>7)];
  396. }
  397. /* Update a hash value with the given input byte
  398. IN assertion: all calls to to update_hash are made with
  399. consecutive input characters, so that a running hash
  400. key can be computed from the previous key instead of
  401. complete recalculation each time.
  402. */
  403. void
  404. update_hash(uInt& h, std::uint8_t c)
  405. {
  406. h = ((h << hash_shift_) ^ c) & hash_mask_;
  407. }
  408. /* Initialize the hash table (avoiding 64K overflow for 16
  409. bit systems). prev[] will be initialized on the fly.
  410. */
  411. void
  412. clear_hash()
  413. {
  414. head_[hash_size_-1] = 0;
  415. std::memset((Byte *)head_, 0,
  416. (unsigned)(hash_size_-1)*sizeof(*head_));
  417. }
  418. /* Compares two subtrees, using the tree depth as tie breaker
  419. when the subtrees have equal frequency. This minimizes the
  420. worst case length.
  421. */
  422. bool
  423. smaller(ct_data const* tree, int n, int m)
  424. {
  425. return tree[n].fc < tree[m].fc ||
  426. (tree[n].fc == tree[m].fc &&
  427. depth_[n] <= depth_[m]);
  428. }
  429. /* Insert string str in the dictionary and set match_head to the
  430. previous head of the hash chain (the most recent string with
  431. same hash key). Return the previous length of the hash chain.
  432. If this file is compiled with -DFASTEST, the compression level
  433. is forced to 1, and no hash chains are maintained.
  434. IN assertion: all calls to to INSERT_STRING are made with
  435. consecutive input characters and the first minMatch
  436. bytes of str are valid (except for the last minMatch-1
  437. bytes of the input file).
  438. */
  439. void
  440. insert_string(IPos& hash_head)
  441. {
  442. update_hash(ins_h_, window_[strstart_ + (minMatch-1)]);
  443. hash_head = prev_[strstart_ & w_mask_] = head_[ins_h_];
  444. head_[ins_h_] = (std::uint16_t)strstart_;
  445. }
  446. //--------------------------------------------------------------------------
  447. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  448. * the desired pack level (0..9). The values given below have been tuned to
  449. * exclude worst case performance for pathological files. Better values may be
  450. * found for specific files.
  451. */
  452. struct config
  453. {
  454. std::uint16_t good_length; /* reduce lazy search above this match length */
  455. std::uint16_t max_lazy; /* do not perform lazy search above this match length */
  456. std::uint16_t nice_length; /* quit search above this match length */
  457. std::uint16_t max_chain;
  458. compress_func func;
  459. config(
  460. std::uint16_t good_length_,
  461. std::uint16_t max_lazy_,
  462. std::uint16_t nice_length_,
  463. std::uint16_t max_chain_,
  464. compress_func func_)
  465. : good_length(good_length_)
  466. , max_lazy(max_lazy_)
  467. , nice_length(nice_length_)
  468. , max_chain(max_chain_)
  469. , func(func_)
  470. {
  471. }
  472. };
  473. static
  474. config
  475. get_config(std::size_t level)
  476. {
  477. switch(level)
  478. {
  479. // good lazy nice chain
  480. case 0: return { 0, 0, 0, 0, &self::deflate_stored}; // store only
  481. case 1: return { 4, 4, 8, 4, &self::deflate_fast}; // max speed, no lazy matches
  482. case 2: return { 4, 5, 16, 8, &self::deflate_fast};
  483. case 3: return { 4, 6, 32, 32, &self::deflate_fast};
  484. case 4: return { 4, 4, 16, 16, &self::deflate_slow}; // lazy matches
  485. case 5: return { 8, 16, 32, 32, &self::deflate_slow};
  486. case 6: return { 8, 16, 128, 128, &self::deflate_slow};
  487. case 7: return { 8, 32, 128, 256, &self::deflate_slow};
  488. case 8: return { 32, 128, 258, 1024, &self::deflate_slow};
  489. default:
  490. case 9: return { 32, 258, 258, 4096, &self::deflate_slow}; // max compression
  491. }
  492. }
  493. void
  494. maybe_init()
  495. {
  496. if(! inited_)
  497. init();
  498. }
  499. template<class Unsigned>
  500. static
  501. Unsigned
  502. bi_reverse(Unsigned code, unsigned len);
  503. BOOST_BEAST_DECL
  504. static
  505. void
  506. gen_codes(ct_data *tree, int max_code, std::uint16_t *bl_count);
  507. BOOST_BEAST_DECL
  508. static
  509. lut_type const&
  510. get_lut();
  511. BOOST_BEAST_DECL void doReset (int level, int windowBits, int memLevel, Strategy strategy);
  512. BOOST_BEAST_DECL void doReset ();
  513. BOOST_BEAST_DECL void doClear ();
  514. BOOST_BEAST_DECL std::size_t doUpperBound (std::size_t sourceLen) const;
  515. BOOST_BEAST_DECL void doTune (int good_length, int max_lazy, int nice_length, int max_chain);
  516. BOOST_BEAST_DECL void doParams (z_params& zs, int level, Strategy strategy, error_code& ec);
  517. BOOST_BEAST_DECL void doWrite (z_params& zs, boost::optional<Flush> flush, error_code& ec);
  518. BOOST_BEAST_DECL void doDictionary (Byte const* dict, uInt dictLength, error_code& ec);
  519. BOOST_BEAST_DECL void doPrime (int bits, int value, error_code& ec);
  520. BOOST_BEAST_DECL void doPending (unsigned* value, int* bits);
  521. BOOST_BEAST_DECL void init ();
  522. BOOST_BEAST_DECL void lm_init ();
  523. BOOST_BEAST_DECL void init_block ();
  524. BOOST_BEAST_DECL void pqdownheap (ct_data const* tree, int k);
  525. BOOST_BEAST_DECL void pqremove (ct_data const* tree, int& top);
  526. BOOST_BEAST_DECL void gen_bitlen (tree_desc *desc);
  527. BOOST_BEAST_DECL void build_tree (tree_desc *desc);
  528. BOOST_BEAST_DECL void scan_tree (ct_data *tree, int max_code);
  529. BOOST_BEAST_DECL void send_tree (ct_data *tree, int max_code);
  530. BOOST_BEAST_DECL int build_bl_tree ();
  531. BOOST_BEAST_DECL void send_all_trees (int lcodes, int dcodes, int blcodes);
  532. BOOST_BEAST_DECL void compress_block (ct_data const* ltree, ct_data const* dtree);
  533. BOOST_BEAST_DECL int detect_data_type ();
  534. BOOST_BEAST_DECL void bi_windup ();
  535. BOOST_BEAST_DECL void bi_flush ();
  536. BOOST_BEAST_DECL void copy_block (char *buf, unsigned len, int header);
  537. BOOST_BEAST_DECL void tr_init ();
  538. BOOST_BEAST_DECL void tr_align ();
  539. BOOST_BEAST_DECL void tr_flush_bits ();
  540. BOOST_BEAST_DECL void tr_stored_block (char *bu, std::uint32_t stored_len, int last);
  541. BOOST_BEAST_DECL void tr_tally_dist (std::uint16_t dist, std::uint8_t len, bool& flush);
  542. BOOST_BEAST_DECL void tr_tally_lit (std::uint8_t c, bool& flush);
  543. BOOST_BEAST_DECL void tr_flush_block (z_params& zs, char *buf, std::uint32_t stored_len, int last);
  544. BOOST_BEAST_DECL void fill_window (z_params& zs);
  545. BOOST_BEAST_DECL void flush_pending (z_params& zs);
  546. BOOST_BEAST_DECL void flush_block (z_params& zs, bool last);
  547. BOOST_BEAST_DECL int read_buf (z_params& zs, Byte *buf, unsigned size);
  548. BOOST_BEAST_DECL uInt longest_match (IPos cur_match);
  549. BOOST_BEAST_DECL block_state f_stored (z_params& zs, Flush flush);
  550. BOOST_BEAST_DECL block_state f_fast (z_params& zs, Flush flush);
  551. BOOST_BEAST_DECL block_state f_slow (z_params& zs, Flush flush);
  552. BOOST_BEAST_DECL block_state f_rle (z_params& zs, Flush flush);
  553. BOOST_BEAST_DECL block_state f_huff (z_params& zs, Flush flush);
  554. block_state
  555. deflate_stored(z_params& zs, Flush flush)
  556. {
  557. return f_stored(zs, flush);
  558. }
  559. block_state
  560. deflate_fast(z_params& zs, Flush flush)
  561. {
  562. return f_fast(zs, flush);
  563. }
  564. block_state
  565. deflate_slow(z_params& zs, Flush flush)
  566. {
  567. return f_slow(zs, flush);
  568. }
  569. block_state
  570. deflate_rle(z_params& zs, Flush flush)
  571. {
  572. return f_rle(zs, flush);
  573. }
  574. block_state
  575. deflate_huff(z_params& zs, Flush flush)
  576. {
  577. return f_huff(zs, flush);
  578. }
  579. };
  580. //--------------------------------------------------------------------------
  581. // Reverse the first len bits of a code
  582. template<class Unsigned>
  583. Unsigned
  584. deflate_stream::
  585. bi_reverse(Unsigned code, unsigned len)
  586. {
  587. BOOST_STATIC_ASSERT(std::is_unsigned<Unsigned>::value);
  588. BOOST_ASSERT(len <= 8 * sizeof(unsigned));
  589. Unsigned res = 0;
  590. do
  591. {
  592. res |= code & 1;
  593. code >>= 1;
  594. res <<= 1;
  595. }
  596. while(--len > 0);
  597. return res >> 1;
  598. }
  599. } // detail
  600. } // zlib
  601. } // beast
  602. } // boost
  603. #ifdef BOOST_BEAST_HEADER_ONLY
  604. #include <boost/beast/zlib/detail/deflate_stream.ipp>
  605. #endif
  606. #endif