Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:35

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 // This is a derivative work based on Zlib, copyright below:
0010 /*
0011     Copyright (C) 1995-2018 Jean-loup Gailly and Mark Adler
0012 
0013     This software is provided 'as-is', without any express or implied
0014     warranty.  In no event will the authors be held liable for any damages
0015     arising from the use of this software.
0016 
0017     Permission is granted to anyone to use this software for any purpose,
0018     including commercial applications, and to alter it and redistribute it
0019     freely, subject to the following restrictions:
0020 
0021     1. The origin of this software must not be misrepresented; you must not
0022        claim that you wrote the original software. If you use this software
0023        in a product, an acknowledgment in the product documentation would be
0024        appreciated but is not required.
0025     2. Altered source versions must be plainly marked as such, and must not be
0026        misrepresented as being the original software.
0027     3. This notice may not be removed or altered from any source distribution.
0028 
0029     Jean-loup Gailly        Mark Adler
0030     jloup@gzip.org          madler@alumni.caltech.edu
0031 
0032     The data format used by the zlib library is described by RFCs (Request for
0033     Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
0034     (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
0035 */
0036 
0037 #ifndef BOOST_BEAST_ZLIB_DETAIL_DEFLATE_STREAM_HPP
0038 #define BOOST_BEAST_ZLIB_DETAIL_DEFLATE_STREAM_HPP
0039 
0040 #include <boost/beast/zlib/error.hpp>
0041 #include <boost/beast/zlib/zlib.hpp>
0042 #include <boost/beast/zlib/detail/ranges.hpp>
0043 #include <boost/assert.hpp>
0044 #include <boost/config.hpp>
0045 #include <boost/optional.hpp>
0046 #include <boost/throw_exception.hpp>
0047 #include <cstdint>
0048 #include <cstdlib>
0049 #include <cstring>
0050 #include <memory>
0051 #include <stdexcept>
0052 #include <type_traits>
0053 
0054 namespace boost {
0055 namespace beast {
0056 namespace zlib {
0057 namespace detail {
0058 
0059 class deflate_stream
0060 {
0061 protected:
0062     // Upper limit on code length
0063     static std::uint8_t constexpr maxBits = 15;
0064 
0065     // Number of length codes, not counting the special END_BLOCK code
0066     static std::uint16_t constexpr lengthCodes = 29;
0067 
0068     // Number of literal bytes 0..255
0069     static std::uint16_t constexpr literals = 256;
0070 
0071     // Number of Literal or Length codes, including the END_BLOCK code
0072     static std::uint16_t constexpr lCodes = literals + 1 + lengthCodes;
0073 
0074     // Number of distance code lengths
0075     static std::uint16_t constexpr dCodes = 30;
0076 
0077     // Number of codes used to transfer the bit lengths
0078     static std::uint16_t constexpr blCodes = 19;
0079 
0080     // Number of distance codes
0081     static std::uint16_t constexpr distCodeLen = 512;
0082 
0083     // Size limit on bit length codes
0084     static std::uint8_t constexpr maxBlBits= 7;
0085 
0086     static std::uint16_t constexpr minMatch = 3;
0087     static std::uint16_t constexpr maxMatch = 258;
0088 
0089     // Can't change minMatch without also changing code, see original zlib
0090     BOOST_STATIC_ASSERT(minMatch == 3);
0091 
0092     // end of block literal code
0093     static std::uint16_t constexpr END_BLOCK = 256;
0094 
0095     // repeat previous bit length 3-6 times (2 bits of repeat count)
0096     static std::uint8_t constexpr REP_3_6 = 16;
0097 
0098     // repeat a zero length 3-10 times  (3 bits of repeat count)
0099     static std::uint8_t constexpr REPZ_3_10 = 17;
0100 
0101     // repeat a zero length 11-138 times  (7 bits of repeat count)
0102     static std::uint8_t constexpr REPZ_11_138 = 18;
0103 
0104     // The three kinds of block type
0105     static std::uint8_t constexpr STORED_BLOCK = 0;
0106     static std::uint8_t constexpr STATIC_TREES = 1;
0107     static std::uint8_t constexpr DYN_TREES    = 2;
0108 
0109     // Maximum value for memLevel in deflateInit2
0110     static std::uint8_t constexpr max_mem_level = 9;
0111 
0112     // Default memLevel
0113     static std::uint8_t constexpr DEF_MEM_LEVEL = max_mem_level;
0114 
0115     /*  Note: the deflate() code requires max_lazy >= minMatch and max_chain >= 4
0116         For deflate_fast() (levels <= 3) good is ignored and lazy has a different
0117         meaning.
0118     */
0119 
0120     // maximum heap size
0121     static std::uint16_t constexpr HEAP_SIZE = 2 * lCodes + 1;
0122 
0123     // size of bit buffer in bi_buf
0124     static std::uint8_t constexpr Buf_size = 16;
0125 
0126     // Matches of length 3 are discarded if their distance exceeds kTooFar
0127     static std::size_t constexpr kTooFar = 4096;
0128 
0129     /*  Minimum amount of lookahead, except at the end of the input file.
0130         See deflate.c for comments about the minMatch+1.
0131     */
0132     static std::size_t constexpr kMinLookahead = maxMatch + minMatch+1;
0133 
0134     /*  Number of bytes after end of data in window to initialize in order
0135         to avoid memory checker errors from longest match routines
0136     */
0137     static std::size_t constexpr kWinInit = maxMatch;
0138 
0139     // Describes a single value and its code string.
0140     struct ct_data
0141     {
0142         std::uint16_t fc; // frequency count or bit string
0143         std::uint16_t dl; // parent node in tree or length of bit string
0144 
0145         bool
0146         operator==(ct_data const& rhs) const
0147         {
0148             return fc == rhs.fc && dl == rhs.dl;
0149         }
0150     };
0151 
0152     struct static_desc
0153     {
0154         ct_data const*      static_tree;// static tree or NULL
0155         std::uint8_t const* extra_bits; // extra bits for each code or NULL
0156         std::uint16_t       extra_base; // base index for extra_bits
0157         std::uint16_t       elems;      //  max number of elements in the tree
0158         std::uint8_t        max_length; // max bit length for the codes
0159     };
0160 
0161     struct lut_type
0162     {
0163         // Number of extra bits for each length code
0164         std::uint8_t const extra_lbits[lengthCodes] = {
0165             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
0166         };
0167 
0168         // Number of extra bits for each distance code
0169         std::uint8_t const extra_dbits[dCodes] = {
0170             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
0171         };
0172 
0173         // Number of extra bits for each bit length code
0174         std::uint8_t const extra_blbits[blCodes] = {
0175             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7
0176         };
0177 
0178         // The lengths of the bit length codes are sent in order
0179         // of decreasing probability, to avoid transmitting the
0180         // lengths for unused bit length codes.
0181         std::uint8_t const bl_order[blCodes] = {
0182             16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15
0183         };
0184 
0185         ct_data ltree[lCodes + 2];
0186 
0187         ct_data dtree[dCodes];
0188 
0189         // Distance codes. The first 256 values correspond to the distances
0190         // 3 .. 258, the last 256 values correspond to the top 8 bits of
0191         // the 15 bit distances.
0192         std::uint8_t dist_code[distCodeLen];
0193 
0194         std::uint8_t length_code[maxMatch-minMatch+1];
0195 
0196         std::uint8_t base_length[lengthCodes];
0197 
0198         std::uint16_t base_dist[dCodes];
0199 
0200         static_desc l_desc = {
0201             ltree, extra_lbits, literals+1, lCodes, maxBits
0202         };
0203 
0204         static_desc d_desc = {
0205             dtree, extra_dbits, 0, dCodes, maxBits
0206         };
0207 
0208         static_desc bl_desc =
0209         {
0210             nullptr, extra_blbits, 0, blCodes, maxBlBits
0211         };
0212     };
0213 
0214     struct tree_desc
0215     {
0216         ct_data *dyn_tree;           /* the dynamic tree */
0217         int     max_code;            /* largest code with non zero frequency */
0218         static_desc const* stat_desc; /* the corresponding static tree */
0219     };
0220 
0221     enum block_state
0222     {
0223         need_more,      /* block not completed, need more input or more output */
0224         block_done,     /* block flush performed */
0225         finish_started, /* finish started, need only more output at next deflate */
0226         finish_done     /* finish done, accept no more input or output */
0227     };
0228 
0229     // VFALCO This might not be needed, e.g. for zip/gzip
0230     enum StreamStatus
0231     {
0232         EXTRA_STATE = 69,
0233         NAME_STATE = 73,
0234         COMMENT_STATE = 91,
0235         HCRC_STATE = 103,
0236         BUSY_STATE = 113,
0237         FINISH_STATE = 666
0238     };
0239 
0240     /* A std::uint16_t is an index in the character window. We use short instead of int to
0241      * save space in the various tables. IPos is used only for parameter passing.
0242      */
0243     using IPos = unsigned;
0244 
0245     using self = deflate_stream;
0246     typedef block_state(self::*compress_func)(z_params& zs, Flush flush);
0247 
0248     //--------------------------------------------------------------------------
0249 
0250     lut_type const& lut_;
0251 
0252     bool inited_ = false;
0253     std::size_t buf_size_;
0254     std::unique_ptr<std::uint8_t[]> buf_;
0255 
0256     int status_;                    // as the name implies
0257     Byte* pending_buf_;             // output still pending
0258     std::uint32_t
0259         pending_buf_size_;          // size of pending_buf
0260     Byte* pending_out_;             // next pending byte to output to the stream
0261     uInt pending_;                  // nb of bytes in the pending buffer
0262     boost::optional<Flush>
0263         last_flush_;                // value of flush param for previous deflate call
0264 
0265     uInt w_size_;                   // LZ77 window size (32K by default)
0266     uInt w_bits_;                   // log2(w_size)  (8..16)
0267     uInt w_mask_;                   // w_size - 1
0268 
0269     /*  Sliding window. Input bytes are read into the second half of the window,
0270         and move to the first half later to keep a dictionary of at least wSize
0271         bytes. With this organization, matches are limited to a distance of
0272         wSize-maxMatch bytes, but this ensures that IO is always
0273         performed with a length multiple of the block size. Also, it limits
0274         the window size to 64K.
0275         To do: use the user input buffer as sliding window.
0276     */
0277     Byte *window_ = nullptr;
0278 
0279     /*  Actual size of window: 2*wSize, except when the user input buffer
0280         is directly used as sliding window.
0281     */
0282     std::uint32_t window_size_;
0283 
0284     /*  Link to older string with same hash index. To limit the size of this
0285         array to 64K, this link is maintained only for the last 32K strings.
0286         An index in this array is thus a window index modulo 32K.
0287     */
0288     std::uint16_t* prev_;
0289 
0290     std::uint16_t* head_;           // Heads of the hash chains or 0
0291 
0292     uInt  ins_h_;                   // hash index of string to be inserted
0293     uInt  hash_size_;               // number of elements in hash table
0294     uInt  hash_bits_;               // log2(hash_size)
0295     uInt  hash_mask_;               // hash_size-1
0296 
0297     /*  Number of bits by which ins_h must be shifted at each input
0298         step. It must be such that after minMatch steps,
0299         the oldest byte no longer takes part in the hash key, that is:
0300         hash_shift * minMatch >= hash_bits
0301     */
0302     uInt hash_shift_;
0303 
0304     /*  Window position at the beginning of the current output block.
0305         Gets negative when the window is moved backwards.
0306     */
0307     long block_start_;
0308 
0309     uInt match_length_;             // length of best match
0310     IPos prev_match_;               // previous match
0311     int match_available_;           // set if previous match exists
0312     uInt strstart_;                 // start of string to insert
0313     uInt match_start_;              // start of matching string
0314     uInt lookahead_;                // number of valid bytes ahead in window
0315 
0316     /*  Length of the best match at previous step. Matches not greater
0317         than this are discarded. This is used in the lazy match evaluation.
0318     */
0319     uInt prev_length_;
0320 
0321     /*  To speed up deflation, hash chains are never searched beyond
0322         this length. A higher limit improves compression ratio but
0323         degrades the speed.
0324     */
0325     uInt max_chain_length_;
0326 
0327     /*  Attempt to find a better match only when the current match is strictly
0328         smaller than this value. This mechanism is used only for compression
0329         levels >= 4.
0330 
0331         OR Insert new strings in the hash table only if the match length is not
0332         greater than this length. This saves time but degrades compression.
0333         used only for compression levels <= 3.
0334     */
0335     uInt max_lazy_match_;
0336 
0337     int level_;                     // compression level (1..9)
0338     Strategy strategy_;             // favor or force Huffman coding
0339 
0340     // Use a faster search when the previous match is longer than this
0341     uInt good_match_;
0342 
0343     int nice_match_;                // Stop searching when current match exceeds this
0344 
0345     ct_data dyn_ltree_[
0346         HEAP_SIZE];                 // literal and length tree
0347     ct_data dyn_dtree_[
0348         2*dCodes+1];                // distance tree
0349     ct_data bl_tree_[
0350         2*blCodes+1];               // Huffman tree for bit lengths
0351 
0352     tree_desc l_desc_;              // desc. for literal tree
0353     tree_desc d_desc_;              // desc. for distance tree
0354     tree_desc bl_desc_;             // desc. for bit length tree
0355 
0356     // number of codes at each bit length for an optimal tree
0357     std::uint16_t bl_count_[maxBits+1];
0358 
0359     // Index within the heap array of least frequent node in the Huffman tree
0360     static std::size_t constexpr kSmallest = 1;
0361 
0362     /*  The sons of heap[n] are heap[2*n] and heap[2*n+1].
0363         heap[0] is not used. The same heap array is used to build all trees.
0364     */
0365 
0366     int heap_[2*lCodes+1];          // heap used to build the Huffman trees
0367     int heap_len_;                  // number of elements in the heap
0368     int heap_max_;                  // element of largest frequency
0369 
0370     // Depth of each subtree used as tie breaker for trees of equal frequency
0371     std::uint8_t depth_[2*lCodes+1];
0372 
0373     std::uint8_t *sym_buf_;           // buffer for distances and literals or lengths
0374 
0375     /*  Size of match buffer for literals/lengths.
0376         There are 4 reasons for limiting lit_bufsize to 64K:
0377           - frequencies can be kept in 16 bit counters
0378           - if compression is not successful for the first block, all input
0379             data is still in the window so we can still emit a stored block even
0380             when input comes from standard input.  (This can also be done for
0381             all blocks if lit_bufsize is not greater than 32K.)
0382           - if compression is not successful for a file smaller than 64K, we can
0383             even emit a stored file instead of a stored block (saving 5 bytes).
0384             This is applicable only for zip (not gzip or zlib).
0385           - creating new Huffman trees less frequently may not provide fast
0386             adaptation to changes in the input data statistics. (Take for
0387             example a binary file with poorly compressible code followed by
0388             a highly compressible string table.) Smaller buffer sizes give
0389             fast adaptation but have of course the overhead of transmitting
0390             trees more frequently.
0391           - I can't count above 4
0392     */
0393     uInt lit_bufsize_;
0394 
0395     uInt sym_next_;      /* running index in sym_buf */
0396     uInt sym_end_;       /* symbol table full when sym_next reaches this */
0397 
0398     std::uint32_t opt_len_;         // bit length of current block with optimal trees
0399     std::uint32_t static_len_;      // bit length of current block with static trees
0400     uInt matches_;                  // number of string matches in current block
0401     uInt insert_;                   // bytes at end of window left to insert
0402 
0403     /*  Output buffer.
0404         Bits are inserted starting at the bottom (least significant bits).
0405      */
0406     std::uint16_t bi_buf_;
0407 
0408     /*  Number of valid bits in bi_buf._  All bits above the last valid
0409         bit are always zero.
0410     */
0411     int bi_valid_;
0412 
0413     /*  High water mark offset in window for initialized bytes -- bytes
0414         above this are set to zero in order to avoid memory check warnings
0415         when longest match routines access bytes past the input.  This is
0416         then updated to the new high water mark.
0417     */
0418     std::uint32_t high_water_;
0419 
0420     //--------------------------------------------------------------------------
0421 
0422     deflate_stream()
0423         : lut_(get_lut())
0424     {
0425     }
0426 
0427     /*  In order to simplify the code, particularly on 16 bit machines, match
0428         distances are limited to MAX_DIST instead of WSIZE.
0429     */
0430     std::size_t
0431     max_dist() const
0432     {
0433         return w_size_ - kMinLookahead;
0434     }
0435 
0436     void
0437     put_byte(std::uint8_t c)
0438     {
0439         pending_buf_[pending_++] = c;
0440     }
0441 
0442     void
0443     put_short(std::uint16_t w)
0444     {
0445         put_byte(w & 0xff);
0446         put_byte(w >> 8);
0447     }
0448 
0449     /*  Send a value on a given number of bits.
0450         IN assertion: length <= 16 and value fits in length bits.
0451     */
0452     void
0453     send_bits(int value, int length)
0454     {
0455         if(bi_valid_ > (int)Buf_size - length)
0456         {
0457             bi_buf_ |= (std::uint16_t)value << bi_valid_;
0458             put_short(bi_buf_);
0459             bi_buf_ = (std::uint16_t)value >> (Buf_size - bi_valid_);
0460             bi_valid_ += length - Buf_size;
0461         }
0462         else
0463         {
0464             bi_buf_ |= (std::uint16_t)(value) << bi_valid_;
0465             bi_valid_ += length;
0466         }
0467     }
0468 
0469     // Send a code of the given tree
0470     void
0471     send_code(int value, ct_data const* tree)
0472     {
0473         send_bits(tree[value].fc, tree[value].dl);
0474     }
0475 
0476     /*  Mapping from a distance to a distance code. dist is the
0477         distance - 1 and must not have side effects. _dist_code[256]
0478         and _dist_code[257] are never used.
0479     */
0480     std::uint8_t
0481     d_code(unsigned dist)
0482     {
0483         if(dist < 256)
0484             return lut_.dist_code[dist];
0485         return lut_.dist_code[256+(dist>>7)];
0486     }
0487 
0488     /*  Update a hash value with the given input byte
0489         IN  assertion: all calls to to update_hash are made with
0490             consecutive input characters, so that a running hash
0491             key can be computed from the previous key instead of
0492             complete recalculation each time.
0493     */
0494     void
0495     update_hash(uInt& h, std::uint8_t c)
0496     {
0497         h = ((h << hash_shift_) ^ c) & hash_mask_;
0498     }
0499 
0500     /*  Initialize the hash table (avoiding 64K overflow for 16
0501         bit systems). prev[] will be initialized on the fly.
0502     */
0503     void
0504     clear_hash()
0505     {
0506         head_[hash_size_-1] = 0;
0507         std::memset((Byte *)head_, 0,
0508             (unsigned)(hash_size_-1)*sizeof(*head_));
0509     }
0510 
0511     /*  Compares two subtrees, using the tree depth as tie breaker
0512         when the subtrees have equal frequency. This minimizes the
0513         worst case length.
0514     */
0515     bool
0516     smaller(ct_data const* tree, int n, int m)
0517     {
0518         return tree[n].fc < tree[m].fc ||
0519             (tree[n].fc == tree[m].fc &&
0520                 depth_[n] <= depth_[m]);
0521     }
0522 
0523     /*  Insert string str in the dictionary and set match_head to the
0524         previous head of the hash chain (the most recent string with
0525         same hash key). Return the previous length of the hash chain.
0526         If this file is compiled with -DFASTEST, the compression level
0527         is forced to 1, and no hash chains are maintained.
0528         IN  assertion: all calls to to INSERT_STRING are made with
0529             consecutive input characters and the first minMatch
0530             bytes of str are valid (except for the last minMatch-1
0531             bytes of the input file).
0532     */
0533     void
0534     insert_string(IPos& hash_head)
0535     {
0536         update_hash(ins_h_, window_[strstart_ + (minMatch-1)]);
0537         hash_head = prev_[strstart_ & w_mask_] = head_[ins_h_];
0538         head_[ins_h_] = (std::uint16_t)strstart_;
0539     }
0540 
0541     //--------------------------------------------------------------------------
0542 
0543     /* Values for max_lazy_match, good_match and max_chain_length, depending on
0544      * the desired pack level (0..9). The values given below have been tuned to
0545      * exclude worst case performance for pathological files. Better values may be
0546      * found for specific files.
0547      */
0548     struct config
0549     {
0550        std::uint16_t good_length; /* reduce lazy search above this match length */
0551        std::uint16_t max_lazy;    /* do not perform lazy search above this match length */
0552        std::uint16_t nice_length; /* quit search above this match length */
0553        std::uint16_t max_chain;
0554        compress_func func;
0555 
0556        config(
0557                std::uint16_t good_length_,
0558                std::uint16_t max_lazy_,
0559                std::uint16_t nice_length_,
0560                std::uint16_t max_chain_,
0561                compress_func func_)
0562            : good_length(good_length_)
0563            , max_lazy(max_lazy_)
0564            , nice_length(nice_length_)
0565            , max_chain(max_chain_)
0566            , func(func_)
0567        {
0568        }
0569     };
0570 
0571     static
0572     config
0573     get_config(std::size_t level)
0574     {
0575         switch(level)
0576         {
0577         //              good lazy nice chain
0578         case 0: return {  0,   0,   0,    0, &self::deflate_stored}; // store only
0579         case 1: return {  4,   4,   8,    4, &self::deflate_fast};   // max speed, no lazy matches
0580         case 2: return {  4,   5,  16,    8, &self::deflate_fast};
0581         case 3: return {  4,   6,  32,   32, &self::deflate_fast};
0582         case 4: return {  4,   4,  16,   16, &self::deflate_slow};   // lazy matches
0583         case 5: return {  8,  16,  32,   32, &self::deflate_slow};
0584         case 6: return {  8,  16, 128,  128, &self::deflate_slow};
0585         case 7: return {  8,  32, 128,  256, &self::deflate_slow};
0586         case 8: return { 32, 128, 258, 1024, &self::deflate_slow};
0587         default:
0588         case 9: return { 32, 258, 258, 4096, &self::deflate_slow};    // max compression
0589         }
0590     }
0591 
0592     void
0593     maybe_init()
0594     {
0595         if(! inited_)
0596             init();
0597     }
0598 
0599     template<class Unsigned>
0600     static
0601     Unsigned
0602     bi_reverse(Unsigned code, unsigned len);
0603 
0604     BOOST_BEAST_DECL
0605     static
0606     void
0607     gen_codes(ct_data *tree, int max_code, std::uint16_t *bl_count);
0608 
0609     BOOST_BEAST_DECL
0610     static
0611     lut_type const&
0612     get_lut();
0613 
0614     BOOST_BEAST_DECL void doReset             (int level, int windowBits, int memLevel, Strategy strategy);
0615     BOOST_BEAST_DECL void doReset             ();
0616     BOOST_BEAST_DECL void doClear             ();
0617     BOOST_BEAST_DECL std::size_t doUpperBound (std::size_t sourceLen) const;
0618     BOOST_BEAST_DECL void doTune              (int good_length, int max_lazy, int nice_length, int max_chain);
0619     BOOST_BEAST_DECL void doParams            (z_params& zs, int level, Strategy strategy, error_code& ec);
0620     BOOST_BEAST_DECL void doWrite             (z_params& zs, boost::optional<Flush> flush, error_code& ec);
0621     BOOST_BEAST_DECL void doDictionary        (Byte const* dict, uInt dictLength, error_code& ec);
0622     BOOST_BEAST_DECL void doPrime             (int bits, int value, error_code& ec);
0623     BOOST_BEAST_DECL void doPending           (unsigned* value, int* bits);
0624 
0625     BOOST_BEAST_DECL void init                ();
0626     BOOST_BEAST_DECL void lm_init             ();
0627     BOOST_BEAST_DECL void init_block          ();
0628     BOOST_BEAST_DECL void pqdownheap          (ct_data const* tree, int k);
0629     BOOST_BEAST_DECL void pqremove            (ct_data const* tree, int& top);
0630     BOOST_BEAST_DECL void gen_bitlen          (tree_desc *desc);
0631     BOOST_BEAST_DECL void build_tree          (tree_desc *desc);
0632     BOOST_BEAST_DECL void scan_tree           (ct_data *tree, int max_code);
0633     BOOST_BEAST_DECL void send_tree           (ct_data *tree, int max_code);
0634     BOOST_BEAST_DECL int  build_bl_tree       ();
0635     BOOST_BEAST_DECL void send_all_trees      (int lcodes, int dcodes, int blcodes);
0636     BOOST_BEAST_DECL void compress_block      (ct_data const* ltree, ct_data const* dtree);
0637     BOOST_BEAST_DECL int  detect_data_type    ();
0638     BOOST_BEAST_DECL void bi_windup           ();
0639     BOOST_BEAST_DECL void bi_flush            ();
0640     BOOST_BEAST_DECL void copy_block          (char *buf, unsigned len, int header);
0641 
0642     BOOST_BEAST_DECL void tr_init             ();
0643     BOOST_BEAST_DECL void tr_align            ();
0644     BOOST_BEAST_DECL void tr_flush_bits       ();
0645     BOOST_BEAST_DECL void tr_stored_block     (char *bu, std::uint32_t stored_len, int last);
0646     BOOST_BEAST_DECL void tr_tally_dist       (std::uint16_t dist, std::uint8_t len, bool& flush);
0647     BOOST_BEAST_DECL void tr_tally_lit        (std::uint8_t c, bool& flush);
0648 
0649     BOOST_BEAST_DECL void tr_flush_block      (z_params& zs, char *buf, std::uint32_t stored_len, int last);
0650     BOOST_BEAST_DECL void fill_window         (z_params& zs);
0651     BOOST_BEAST_DECL void flush_pending       (z_params& zs);
0652     BOOST_BEAST_DECL void flush_block         (z_params& zs, bool last);
0653     BOOST_BEAST_DECL int  read_buf            (z_params& zs, Byte *buf, unsigned size);
0654     BOOST_BEAST_DECL uInt longest_match       (IPos cur_match);
0655 
0656     BOOST_BEAST_DECL block_state f_stored     (z_params& zs, Flush flush);
0657     BOOST_BEAST_DECL block_state f_fast       (z_params& zs, Flush flush);
0658     BOOST_BEAST_DECL block_state f_slow       (z_params& zs, Flush flush);
0659     BOOST_BEAST_DECL block_state f_rle        (z_params& zs, Flush flush);
0660     BOOST_BEAST_DECL block_state f_huff       (z_params& zs, Flush flush);
0661 
0662     block_state
0663     deflate_stored(z_params& zs, Flush flush)
0664     {
0665         return f_stored(zs, flush);
0666     }
0667 
0668     block_state
0669     deflate_fast(z_params& zs, Flush flush)
0670     {
0671         return f_fast(zs, flush);
0672     }
0673 
0674     block_state
0675     deflate_slow(z_params& zs, Flush flush)
0676     {
0677         return f_slow(zs, flush);
0678     }
0679 
0680     block_state
0681     deflate_rle(z_params& zs, Flush flush)
0682     {
0683         return f_rle(zs, flush);
0684     }
0685 
0686     block_state
0687     deflate_huff(z_params& zs, Flush flush)
0688     {
0689         return f_huff(zs, flush);
0690     }
0691 };
0692 
0693 //--------------------------------------------------------------------------
0694 
0695 // Reverse the first len bits of a code
0696 template<class Unsigned>
0697 Unsigned
0698 deflate_stream::
0699 bi_reverse(Unsigned code, unsigned len)
0700 {
0701     BOOST_STATIC_ASSERT(std::is_unsigned<Unsigned>::value);
0702     BOOST_ASSERT(len <= 8 * sizeof(unsigned));
0703     Unsigned res = 0;
0704     do
0705     {
0706         res |= code & 1;
0707         code >>= 1;
0708         res <<= 1;
0709     }
0710     while(--len > 0);
0711     return res >> 1;
0712 }
0713 
0714 } // detail
0715 } // zlib
0716 } // beast
0717 } // boost
0718 
0719 #ifdef BOOST_BEAST_HEADER_ONLY
0720 #include <boost/beast/zlib/detail/deflate_stream.ipp>
0721 #endif
0722 
0723 #endif