Back to home page

EIC code displayed by LXR

 
 

    


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

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-2013 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_INFLATE_STREAM_IPP
0038 #define BOOST_BEAST_ZLIB_DETAIL_INFLATE_STREAM_IPP
0039 
0040 #include <boost/beast/zlib/detail/inflate_stream.hpp>
0041 #include <boost/throw_exception.hpp>
0042 #include <array>
0043 
0044 namespace boost {
0045 namespace beast {
0046 namespace zlib {
0047 namespace detail {
0048 
0049 void
0050 inflate_stream::
0051 doClear()
0052 {
0053 }
0054 
0055 void
0056 inflate_stream::
0057 doReset(int windowBits)
0058 {
0059     if(windowBits < 8 || windowBits > 15)
0060         BOOST_THROW_EXCEPTION(std::domain_error{
0061             "windowBits out of range"});
0062     w_.reset(windowBits);
0063 
0064     bi_.flush();
0065     mode_ = HEAD;
0066     last_ = 0;
0067     dmax_ = 32768U;
0068     lencode_ = codes_;
0069     distcode_ = codes_;
0070     next_ = codes_;
0071     back_ = -1;
0072 }
0073 
0074 void
0075 inflate_stream::
0076 doWrite(z_params& zs, Flush flush, error_code& ec)
0077 {
0078     ranges r;
0079     r.in.first = static_cast<
0080         std::uint8_t const*>(zs.next_in);
0081     r.in.last = r.in.first + zs.avail_in;
0082     r.in.next = r.in.first;
0083     r.out.first = static_cast<
0084         std::uint8_t*>(zs.next_out);
0085     r.out.last = r.out.first + zs.avail_out;
0086     r.out.next = r.out.first;
0087 
0088     auto const done =
0089         [&]
0090         {
0091             /*
0092                Return from inflate(), updating the total counts and the check value.
0093                If there was no progress during the inflate() call, return a buffer
0094                error.  Call updatewindow() to create and/or update the window state.
0095                Note: a memory error from inflate() is non-recoverable.
0096              */
0097 
0098 
0099             // VFALCO TODO Don't allocate update the window unless necessary
0100             if(/*wsize_ ||*/ (r.out.used() && mode_ < BAD &&
0101                     (mode_ < CHECK || flush != Flush::finish)))
0102                 w_.write(r.out.first, r.out.used());
0103 
0104             zs.next_in = r.in.next;
0105             zs.avail_in = r.in.avail();
0106             zs.next_out = r.out.next;
0107             zs.avail_out = r.out.avail();
0108             zs.total_in += r.in.used();
0109             zs.total_out += r.out.used();
0110             zs.data_type = bi_.size() + (last_ ? 64 : 0) +
0111                 (mode_ == TYPE ? 128 : 0) +
0112                 (mode_ == LEN_ || mode_ == COPY_ ? 256 : 0);
0113 
0114             if(((! r.in.used() && ! r.out.used()) ||
0115                     flush == Flush::finish) && ! ec)
0116             {
0117                 BOOST_BEAST_ASSIGN_EC(ec, error::need_buffers);
0118             }
0119         };
0120     auto const err =
0121         [&](error e)
0122         {
0123             BOOST_BEAST_ASSIGN_EC(ec, e);
0124             mode_ = BAD;
0125         };
0126 
0127     if(mode_ == TYPE)
0128         mode_ = TYPEDO;
0129 
0130     for(;;)
0131     {
0132         switch(mode_)
0133         {
0134         case HEAD:
0135             mode_ = TYPEDO;
0136             break;
0137 
0138         case TYPE:
0139             if(flush == Flush::block || flush == Flush::trees)
0140                 return done();
0141             // fall through
0142 
0143         case TYPEDO:
0144         {
0145             if(last_)
0146             {
0147                 bi_.flush_byte();
0148                 mode_ = CHECK;
0149                 break;
0150             }
0151             if(! bi_.fill(3, r.in.next, r.in.last))
0152                 return done();
0153             std::uint8_t v;
0154             bi_.read(v, 1);
0155             last_ = v != 0;
0156             bi_.read(v, 2);
0157             switch(v)
0158             {
0159             case 0:
0160                 // uncompressed block
0161                 mode_ = STORED;
0162                 break;
0163             case 1:
0164                 // fixed Huffman table
0165                 fixedTables();
0166                 mode_ = LEN_;             /* decode codes */
0167                 if(flush == Flush::trees)
0168                     return done();
0169                 break;
0170             case 2:
0171                 // dynamic Huffman table
0172                 mode_ = TABLE;
0173                 break;
0174 
0175             default:
0176                 return err(error::invalid_block_type);
0177             }
0178             break;
0179         }
0180 
0181         case STORED:
0182         {
0183             bi_.flush_byte();
0184             std::uint32_t v;
0185             if(! bi_.fill(32, r.in.next, r.in.last))
0186                 return done();
0187             bi_.peek(v, 32);
0188             length_ = v & 0xffff;
0189             if(length_ != ((v >> 16) ^ 0xffff))
0190                 return err(error::invalid_stored_length);
0191             // flush instead of read, otherwise
0192             // undefined right shift behavior.
0193             bi_.flush();
0194             mode_ = COPY_;
0195             if(flush == Flush::trees)
0196                 return done();
0197             BOOST_FALLTHROUGH;
0198         }
0199 
0200         case COPY_:
0201             mode_ = COPY;
0202             BOOST_FALLTHROUGH;
0203 
0204         case COPY:
0205         {
0206             auto copy = length_;
0207             if(copy == 0)
0208             {
0209                 mode_ = TYPE;
0210                 break;
0211             }
0212             copy = clamp(copy, r.in.avail());
0213             copy = clamp(copy, r.out.avail());
0214             if(copy == 0)
0215                 return done();
0216             std::memcpy(r.out.next, r.in.next, copy);
0217             r.in.next += copy;
0218             r.out.next += copy;
0219             length_ -= copy;
0220             break;
0221         }
0222 
0223         case TABLE:
0224             if(! bi_.fill(5 + 5 + 4, r.in.next, r.in.last))
0225                 return done();
0226             bi_.read(nlen_, 5);
0227             nlen_ += 257;
0228             bi_.read(ndist_, 5);
0229             ndist_ += 1;
0230             bi_.read(ncode_, 4);
0231             ncode_ += 4;
0232             if(nlen_ > 286 || ndist_ > 30)
0233                 return err(error::too_many_symbols);
0234             have_ = 0;
0235             mode_ = LENLENS;
0236             BOOST_FALLTHROUGH;
0237 
0238         case LENLENS:
0239         {
0240             static std::array<std::uint8_t, 19> constexpr order = {{
0241                 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}};
0242             while(have_ < ncode_)
0243             {
0244                 if(! bi_.fill(3, r.in.next, r.in.last))
0245                     return done();
0246                 bi_.read(lens_[order[have_]], 3);
0247                 ++have_;
0248             }
0249             while(have_ < order.size())
0250                 lens_[order[have_++]] = 0;
0251 
0252             next_ = &codes_[0];
0253             lencode_ = next_;
0254             lenbits_ = 7;
0255             inflate_table(build::codes, &lens_[0],
0256                 order.size(), &next_, &lenbits_, work_, ec);
0257             if(ec)
0258             {
0259                 mode_ = BAD;
0260                 break;
0261             }
0262             have_ = 0;
0263             mode_ = CODELENS;
0264             BOOST_FALLTHROUGH;
0265         }
0266 
0267         case CODELENS:
0268         {
0269             while(have_ < nlen_ + ndist_)
0270             {
0271                 std::uint16_t v;
0272                 if(! bi_.fill(lenbits_, r.in.next, r.in.last))
0273                     return done();
0274                 bi_.peek(v, lenbits_);
0275                 auto cp = &lencode_[v];
0276                 if(cp->val < 16)
0277                 {
0278                     bi_.drop(cp->bits);
0279                     lens_[have_++] = cp->val;
0280                 }
0281                 else
0282                 {
0283                     std::uint16_t len;
0284                     std::uint16_t copy;
0285                     if(cp->val == 16)
0286                     {
0287                         if(! bi_.fill(cp->bits + 2, r.in.next, r.in.last))
0288                             return done();
0289                         bi_.drop(cp->bits);
0290                         if(have_ == 0)
0291                             return err(error::invalid_bit_length_repeat);
0292                         bi_.read(copy, 2);
0293                         len = lens_[have_ - 1];
0294                         copy += 3;
0295 
0296                     }
0297                     else if(cp->val == 17)
0298                     {
0299                         if(! bi_.fill(cp->bits + 3, r.in.next, r.in.last))
0300                             return done();
0301                         bi_.drop(cp->bits);
0302                         bi_.read(copy, 3);
0303                         len = 0;
0304                         copy += 3;
0305                     }
0306                     else
0307                     {
0308                         if(! bi_.fill(cp->bits + 7, r.in.next, r.in.last))
0309                             return done();
0310                         bi_.drop(cp->bits);
0311                         bi_.read(copy, 7);
0312                         len = 0;
0313                         copy += 11;
0314                     }
0315                     if(have_ + copy > nlen_ + ndist_)
0316                         return err(error::invalid_bit_length_repeat);
0317                     std::fill(&lens_[have_], &lens_[have_ + copy], len);
0318                     have_ += copy;
0319                     copy = 0;
0320                 }
0321             }
0322             // handle error breaks in while
0323             if(mode_ == BAD)
0324                 break;
0325             // check for end-of-block code (better have one)
0326             if(lens_[256] == 0)
0327                 return err(error::missing_eob);
0328             /* build code tables -- note: do not change the lenbits or distbits
0329                values here (9 and 6) without reading the comments in inftrees.hpp
0330                concerning the kEnough constants, which depend on those values */
0331             next_ = &codes_[0];
0332             lencode_ = next_;
0333             lenbits_ = 9;
0334             inflate_table(build::lens, &lens_[0],
0335                 nlen_, &next_, &lenbits_, work_, ec);
0336             if(ec)
0337             {
0338                 mode_ = BAD;
0339                 return;
0340             }
0341             distcode_ = next_;
0342             distbits_ = 6;
0343             inflate_table(build::dists, lens_ + nlen_,
0344                 ndist_, &next_, &distbits_, work_, ec);
0345             if(ec)
0346             {
0347                 mode_ = BAD;
0348                 return;
0349             }
0350             mode_ = LEN_;
0351             if(flush == Flush::trees)
0352                 return done();
0353             BOOST_FALLTHROUGH;
0354         }
0355 
0356         case LEN_:
0357             mode_ = LEN;
0358             BOOST_FALLTHROUGH;
0359 
0360         case LEN:
0361         {
0362             if(r.in.avail() >= 6 && r.out.avail() >= 258)
0363             {
0364                 inflate_fast(r, ec);
0365                 if(ec)
0366                 {
0367                     mode_ = BAD;
0368                     return;
0369                 }
0370                 if(mode_ == TYPE)
0371                     back_ = -1;
0372                 break;
0373             }
0374             if(! bi_.fill(lenbits_, r.in.next, r.in.last))
0375                 return done();
0376             std::uint16_t v;
0377             back_ = 0;
0378             bi_.peek(v, lenbits_);
0379             auto cp = &lencode_[v];
0380             if(cp->op && (cp->op & 0xf0) == 0)
0381             {
0382                 auto prev = cp;
0383                 if(! bi_.fill(prev->bits + prev->op, r.in.next, r.in.last))
0384                     return done();
0385                 bi_.peek(v, prev->bits + prev->op);
0386                 cp = &lencode_[prev->val + (v >> prev->bits)];
0387                 bi_.drop(prev->bits + cp->bits);
0388                 back_ += prev->bits + cp->bits;
0389             }
0390             else
0391             {
0392                 bi_.drop(cp->bits);
0393                 back_ += cp->bits;
0394             }
0395             length_ = cp->val;
0396             if(cp->op == 0)
0397             {
0398                 mode_ = LIT;
0399                 break;
0400             }
0401             if(cp->op & 32)
0402             {
0403                 back_ = -1;
0404                 mode_ = TYPE;
0405                 break;
0406             }
0407             if(cp->op & 64)
0408                 return err(error::invalid_literal_length);
0409             extra_ = cp->op & 15;
0410             mode_ = LENEXT;
0411             BOOST_FALLTHROUGH;
0412         }
0413 
0414         case LENEXT:
0415             if(extra_)
0416             {
0417                 if(! bi_.fill(extra_, r.in.next, r.in.last))
0418                     return done();
0419                 std::uint16_t v;
0420                 bi_.read(v, extra_);
0421                 length_ += v;
0422                 back_ += extra_;
0423             }
0424             was_ = length_;
0425             mode_ = DIST;
0426             BOOST_FALLTHROUGH;
0427 
0428         case DIST:
0429         {
0430             if(! bi_.fill(distbits_, r.in.next, r.in.last))
0431                 return done();
0432             std::uint16_t v;
0433             bi_.peek(v, distbits_);
0434             auto cp = &distcode_[v];
0435             if((cp->op & 0xf0) == 0)
0436             {
0437                 auto prev = cp;
0438                 if(! bi_.fill(prev->bits + prev->op, r.in.next, r.in.last))
0439                     return done();
0440                 bi_.peek(v, prev->bits + prev->op);
0441                 cp = &distcode_[prev->val + (v >> prev->bits)];
0442                 bi_.drop(prev->bits + cp->bits);
0443                 back_ += prev->bits + cp->bits;
0444             }
0445             else
0446             {
0447                 bi_.drop(cp->bits);
0448                 back_ += cp->bits;
0449             }
0450             if(cp->op & 64)
0451                 return err(error::invalid_distance_code);
0452             offset_ = cp->val;
0453             extra_ = cp->op & 15;
0454             mode_ = DISTEXT;
0455             BOOST_FALLTHROUGH;
0456         }
0457 
0458         case DISTEXT:
0459             if(extra_)
0460             {
0461                 std::uint16_t v;
0462                 if(! bi_.fill(extra_, r.in.next, r.in.last))
0463                     return done();
0464                 bi_.read(v, extra_);
0465                 offset_ += v;
0466                 back_ += extra_;
0467             }
0468 #ifdef INFLATE_STRICT
0469             if(offset_ > dmax_)
0470                 return err(error::invalid_distance);
0471 #endif
0472             mode_ = MATCH;
0473             BOOST_FALLTHROUGH;
0474 
0475         case MATCH:
0476         {
0477             if(! r.out.avail())
0478                 return done();
0479             if(offset_ > r.out.used())
0480             {
0481                 // copy from window
0482                 auto offset = static_cast<std::uint16_t>(
0483                     offset_ - r.out.used());
0484                 if(offset > w_.size())
0485                     return err(error::invalid_distance);
0486                 auto const n = clamp(clamp(
0487                     length_, offset), r.out.avail());
0488                 w_.read(r.out.next, offset, n);
0489                 r.out.next += n;
0490                 length_ -= n;
0491             }
0492             else
0493             {
0494                 // copy from output
0495                 auto in = r.out.next - offset_;
0496                 auto n = clamp(length_, r.out.avail());
0497                 length_ -= n;
0498                 while(n--)
0499                     *r.out.next++ = *in++;
0500             }
0501             if(length_ == 0)
0502                 mode_ = LEN;
0503             break;
0504         }
0505 
0506         case LIT:
0507         {
0508             if(! r.out.avail())
0509                 return done();
0510             auto const v = static_cast<std::uint8_t>(length_);
0511             *r.out.next++ = v;
0512             mode_ = LEN;
0513             break;
0514         }
0515 
0516         case CHECK:
0517             mode_ = DONE;
0518             BOOST_FALLTHROUGH;
0519 
0520         case DONE:
0521         {
0522             BOOST_BEAST_ASSIGN_EC(ec, error::end_of_stream);
0523             return done();
0524         }
0525 
0526         case BAD:
0527             return done();
0528 
0529         case SYNC:
0530         default:
0531             BOOST_THROW_EXCEPTION(std::logic_error{
0532                 "stream error"});
0533         }
0534     }
0535 }
0536 
0537 //------------------------------------------------------------------------------
0538 
0539 /*  Build a set of tables to decode the provided canonical Huffman code.
0540     The code lengths are lens[0..codes-1].  The result starts at *table,
0541     whose indices are 0..2^bits-1.  work is a writable array of at least
0542     lens shorts, which is used as a work area.  type is the type of code
0543     to be generated, build::codes, build::lens, or build::dists.  On
0544     return, zero is success, -1 is an invalid code, and +1 means that
0545     kEnough isn't enough.  table on return points to the next available
0546     entry's address.  bits is the requested root table index bits, and
0547     on return it is the actual root table index bits.  It will differ if
0548     the request is greater than the longest code or if it is less than
0549     the shortest code.
0550 */
0551 void
0552 inflate_stream::
0553 inflate_table(
0554     build type,
0555     std::uint16_t* lens,
0556     std::size_t codes,
0557     code** table,
0558     unsigned *bits,
0559     std::uint16_t* work,
0560     error_code& ec)
0561 {
0562     unsigned len;                   // a code's length in bits
0563     unsigned sym;                   // index of code symbols
0564     unsigned min, max;              // minimum and maximum code lengths
0565     unsigned root;                  // number of index bits for root table
0566     unsigned curr;                  // number of index bits for current table
0567     unsigned drop;                  // code bits to drop for sub-table
0568     int left;                       // number of prefix codes available
0569     unsigned used;                  // code entries in table used
0570     unsigned huff;                  // Huffman code
0571     unsigned incr;                  // for incrementing code, index
0572     unsigned fill;                  // index for replicating entries
0573     unsigned low;                   // low bits for current root entry
0574     unsigned mask;                  // mask for low root bits
0575     code here;                      // table entry for duplication
0576     code *next;                     // next available space in table
0577     std::uint16_t const* base;      // base value table to use
0578     std::uint16_t const* extra;     // extra bits table to use
0579     unsigned match;                 // use base and extra for symbol >= match
0580     std::uint16_t count[15+1];      // number of codes of each length
0581     std::uint16_t offs[15+1];       // offsets in table for each length
0582 
0583     // Length codes 257..285 base
0584     static std::uint16_t constexpr lbase[31] = {
0585         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
0586         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
0587 
0588     // Length codes 257..285 extra
0589     static std::uint16_t constexpr lext[31] = {
0590         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
0591         19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202};
0592 
0593     // Distance codes 0..29 base
0594     static std::uint16_t constexpr dbase[32] = {
0595         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
0596         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
0597         8193, 12289, 16385, 24577, 0, 0};
0598 
0599     // Distance codes 0..29 extra
0600     static std::uint16_t constexpr dext[32] = {
0601         16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
0602         23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
0603         28, 28, 29, 29, 64, 64};
0604 
0605     /*
0606        Process a set of code lengths to create a canonical Huffman code.  The
0607        code lengths are lens[0..codes-1].  Each length corresponds to the
0608        symbols 0..codes-1.  The Huffman code is generated by first sorting the
0609        symbols by length from short to long, and retaining the symbol order
0610        for codes with equal lengths.  Then the code starts with all zero bits
0611        for the first code of the shortest length, and the codes are integer
0612        increments for the same length, and zeros are appended as the length
0613        increases.  For the deflate format, these bits are stored backwards
0614        from their more natural integer increment ordering, and so when the
0615        decoding tables are built in the large loop below, the integer codes
0616        are incremented backwards.
0617 
0618        This routine assumes, but does not check, that all of the entries in
0619        lens[] are in the range 0..15.  The caller must assure this.
0620        1..15 is interpreted as that code length.  zero means that that
0621        symbol does not occur in this code.
0622 
0623        The codes are sorted by computing a count of codes for each length,
0624        creating from that a table of starting indices for each length in the
0625        sorted table, and then entering the symbols in order in the sorted
0626        table.  The sorted table is work[], with that space being provided by
0627        the caller.
0628 
0629        The length counts are used for other purposes as well, i.e. finding
0630        the minimum and maximum length codes, determining if there are any
0631        codes at all, checking for a valid set of lengths, and looking ahead
0632        at length counts to determine sub-table sizes when building the
0633        decoding tables.
0634      */
0635 
0636     /* accumulate lengths for codes (assumes lens[] all in 0..15) */
0637     for (len = 0; len <= 15; len++)
0638         count[len] = 0;
0639     for (sym = 0; sym < codes; sym++)
0640         count[lens[sym]]++;
0641 
0642     /* bound code lengths, force root to be within code lengths */
0643     root = *bits;
0644     for (max = 15; max >= 1; max--)
0645         if (count[max] != 0)
0646             break;
0647     if (root > max)
0648         root = max;
0649     if (max == 0)
0650     {                     /* no symbols to code at all */
0651         here.op = (std::uint8_t)64;    /* invalid code marker */
0652         here.bits = (std::uint8_t)1;
0653         here.val = (std::uint16_t)0;
0654         *(*table)++ = here;             /* make a table to force an error */
0655         *(*table)++ = here;
0656         *bits = 1;
0657         return;       /* no symbols, but wait for decoding to report error */
0658     }
0659     for (min = 1; min < max; min++)
0660         if (count[min] != 0)
0661             break;
0662     if (root < min)
0663         root = min;
0664 
0665     /* check for an over-subscribed or incomplete set of lengths */
0666     left = 1;
0667     for (len = 1; len <= 15; len++)
0668     {
0669         left <<= 1;
0670         left -= count[len];
0671         if (left < 0)
0672         {
0673             BOOST_BEAST_ASSIGN_EC(ec, error::over_subscribed_length);
0674             return;
0675         }
0676     }
0677     if (left > 0 && (type == build::codes || max != 1))
0678     {
0679         BOOST_BEAST_ASSIGN_EC(ec, error::incomplete_length_set);
0680         return;
0681     }
0682 
0683     /* generate offsets into symbol table for each length for sorting */
0684     offs[1] = 0;
0685     for (len = 1; len < 15; len++)
0686         offs[len + 1] = offs[len] + count[len];
0687 
0688     /* sort symbols by length, by symbol order within each length */
0689     for (sym = 0; sym < codes; sym++)
0690         if (lens[sym] != 0)
0691             work[offs[lens[sym]]++] = (std::uint16_t)sym;
0692 
0693     /*
0694        Create and fill in decoding tables.  In this loop, the table being
0695        filled is at next and has curr index bits.  The code being used is huff
0696        with length len.  That code is converted to an index by dropping drop
0697        bits off of the bottom.  For codes where len is less than drop + curr,
0698        those top drop + curr - len bits are incremented through all values to
0699        fill the table with replicated entries.
0700 
0701        root is the number of index bits for the root table.  When len exceeds
0702        root, sub-tables are created pointed to by the root entry with an index
0703        of the low root bits of huff.  This is saved in low to check for when a
0704        new sub-table should be started.  drop is zero when the root table is
0705        being filled, and drop is root when sub-tables are being filled.
0706 
0707        When a new sub-table is needed, it is necessary to look ahead in the
0708        code lengths to determine what size sub-table is needed.  The length
0709        counts are used for this, and so count[] is decremented as codes are
0710        entered in the tables.
0711 
0712        used keeps track of how many table entries have been allocated from the
0713        provided *table space.  It is checked for build::lens and DIST tables against
0714        the constants kEnoughLens and kEnoughDists to guard against changes in
0715        the initial root table size constants.  See the comments in inftrees.hpp
0716        for more information.
0717 
0718        sym increments through all symbols, and the loop terminates when
0719        all codes of length max, i.e. all codes, have been processed.  This
0720        routine permits incomplete codes, so another loop after this one fills
0721        in the rest of the decoding tables with invalid code markers.
0722      */
0723 
0724     /* set up for code type */
0725     switch (type)
0726     {
0727     case build::codes:
0728         base = extra = work;    /* dummy value--not used */
0729         match = 20;
0730         break;
0731     case build::lens:
0732         base = lbase;
0733         extra = lext;
0734         match = 257;
0735         break;
0736     default:            /* build::dists */
0737         base = dbase;
0738         extra = dext;
0739         match = 0;
0740     }
0741 
0742     /* initialize state for loop */
0743     huff = 0;                   /* starting code */
0744     sym = 0;                    /* starting code symbol */
0745     len = min;                  /* starting code length */
0746     next = *table;              /* current table to fill in */
0747     curr = root;                /* current table index bits */
0748     drop = 0;                   /* current bits to drop from code for index */
0749     low = (unsigned)(-1);       /* trigger new sub-table when len > root */
0750     used = 1U << root;          /* use root table entries */
0751     mask = used - 1;            /* mask for comparing low */
0752 
0753     auto const not_enough = []
0754     {
0755         BOOST_THROW_EXCEPTION(std::logic_error{
0756             "insufficient output size when inflating tables"});
0757     };
0758 
0759     // check available table space
0760     if ((type == build::lens && used > kEnoughLens) ||
0761             (type == build::dists && used > kEnoughDists))
0762         return not_enough();
0763 
0764     /* process all codes and make table entries */
0765     for (;;)
0766     {
0767         /* create table entry */
0768         here.bits = (std::uint8_t)(len - drop);
0769         if (work[sym] + 1U < match)
0770         {
0771             here.op = (std::uint8_t)0;
0772             here.val = work[sym];
0773         }
0774         else if (work[sym] >= match)
0775         {
0776             here.op = (std::uint8_t)(extra[work[sym] - match]);
0777             here.val = base[work[sym] - match];
0778         }
0779         else
0780         {
0781             here.op = (std::uint8_t)(32 + 64);         /* end of block */
0782             here.val = 0;
0783         }
0784 
0785         /* replicate for those indices with low len bits equal to huff */
0786         incr = 1U << (len - drop);
0787         fill = 1U << curr;
0788         min = fill;                 /* save offset to next table */
0789         do
0790         {
0791             fill -= incr;
0792             next[(huff >> drop) + fill] = here;
0793         } while (fill != 0);
0794 
0795         /* backwards increment the len-bit code huff */
0796         incr = 1U << (len - 1);
0797         while (huff & incr)
0798             incr >>= 1;
0799         if (incr != 0)
0800         {
0801             huff &= incr - 1;
0802             huff += incr;
0803         }
0804         else
0805             huff = 0;
0806 
0807         /* go to next symbol, update count, len */
0808         sym++;
0809         if (--(count[len]) == 0)
0810         {
0811             if (len == max) break;
0812             len = lens[work[sym]];
0813         }
0814 
0815         /* create new sub-table if needed */
0816         if (len > root && (huff & mask) != low)
0817         {
0818             /* if first time, transition to sub-tables */
0819             if (drop == 0)
0820                 drop = root;
0821 
0822             /* increment past last table */
0823             next += min;            /* here min is 1 << curr */
0824 
0825             /* determine length of next table */
0826             curr = len - drop;
0827             left = (int)(1 << curr);
0828             while (curr + drop < max)
0829             {
0830                 left -= count[curr + drop];
0831                 if (left <= 0) break;
0832                 curr++;
0833                 left <<= 1;
0834             }
0835 
0836             /* check for enough space */
0837             used += 1U << curr;
0838             if ((type == build::lens && used > kEnoughLens) ||
0839                     (type == build::dists && used > kEnoughDists))
0840                 return not_enough();
0841 
0842             /* point entry in root table to sub-table */
0843             low = huff & mask;
0844             (*table)[low].op = (std::uint8_t)curr;
0845             (*table)[low].bits = (std::uint8_t)root;
0846             (*table)[low].val = (std::uint16_t)(next - *table);
0847         }
0848     }
0849 
0850     /* fill in remaining table entry if code is incomplete (guaranteed to have
0851        at most one remaining entry, since if the code is incomplete, the
0852        maximum code length that was allowed to get this far is one bit) */
0853     if (huff != 0)
0854     {
0855         here.op = 64;   // invalid code marker
0856         here.bits = (std::uint8_t)(len - drop);
0857         here.val = 0;
0858         next[huff] = here;
0859     }
0860 
0861     *table += used;
0862     *bits = root;
0863 }
0864 
0865 auto
0866 inflate_stream::
0867 get_fixed_tables() ->
0868     codes const&
0869 {
0870     struct fixed_codes : codes
0871     {
0872         code len_[512];
0873         code dist_[32];
0874 
0875         fixed_codes()
0876         {
0877             lencode = len_;
0878             lenbits = 9;
0879             distcode = dist_;
0880             distbits = 5;
0881 
0882             std::uint16_t lens[320];
0883             std::uint16_t work[288];
0884 
0885             std::fill(&lens[  0], &lens[144], std::uint16_t{8});
0886             std::fill(&lens[144], &lens[256], std::uint16_t{9});
0887             std::fill(&lens[256], &lens[280], std::uint16_t{7});
0888             std::fill(&lens[280], &lens[288], std::uint16_t{8});
0889 
0890             {
0891                 error_code ec;
0892                 auto next = &len_[0];
0893                 inflate_table(build::lens,
0894                     lens, 288, &next, &lenbits, work, ec);
0895                 if(ec)
0896                     BOOST_THROW_EXCEPTION(std::logic_error{ec.message()});
0897             }
0898 
0899             // VFALCO These fixups are from ZLib
0900             len_[ 99].op = 64;
0901             len_[227].op = 64;
0902             len_[355].op = 64;
0903             len_[483].op = 64;
0904 
0905             {
0906                 error_code ec;
0907                 auto next = &dist_[0];
0908                 std::fill(&lens[0], &lens[32], std::uint16_t{5});
0909                 inflate_table(build::dists,
0910                     lens, 32, &next, &distbits, work, ec);
0911                 if(ec)
0912                     BOOST_THROW_EXCEPTION(std::logic_error{ec.message()});
0913             }
0914         }
0915     };
0916 
0917     static fixed_codes const fc;
0918     return fc;
0919 }
0920 
0921 void
0922 inflate_stream::
0923 fixedTables()
0924 {
0925     auto const fc = get_fixed_tables();
0926     lencode_ = fc.lencode;
0927     lenbits_ = fc.lenbits;
0928     distcode_ = fc.distcode;
0929     distbits_ = fc.distbits;
0930 }
0931 
0932 /*
0933    Decode literal, length, and distance codes and write out the resulting
0934    literal and match bytes until either not enough input or output is
0935    available, an end-of-block is encountered, or a data error is encountered.
0936    When large enough input and output buffers are supplied to inflate(), for
0937    example, a 16K input buffer and a 64K output buffer, more than 95% of the
0938    inflate execution time is spent in this routine.
0939 
0940    Entry assumptions:
0941 
0942         state->mode_ == LEN
0943         zs.avail_in >= 6
0944         zs.avail_out >= 258
0945         start >= zs.avail_out
0946         state->bits_ < 8
0947 
0948    On return, state->mode_ is one of:
0949 
0950         LEN -- ran out of enough output space or enough available input
0951         TYPE -- reached end of block code, inflate() to interpret next block
0952         BAD -- error in block data
0953 
0954    Notes:
0955 
0956     - The maximum input bits used by a length/distance pair is 15 bits for the
0957       length code, 5 bits for the length extra, 15 bits for the distance code,
0958       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
0959       Therefore if zs.avail_in >= 6, then there is enough input to avoid
0960       checking for available input while decoding.
0961 
0962     - The maximum bytes that a single length/distance pair can output is 258
0963       bytes, which is the maximum length that can be coded.  inflate_fast()
0964       requires zs.avail_out >= 258 for each loop to avoid checking for
0965       output space.
0966 
0967   inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
0968    - Using bit fields for code structure
0969    - Different op definition to avoid & for extra bits (do & for table bits)
0970    - Three separate decoding do-loops for direct, window, and wnext == 0
0971    - Special case for distance > 1 copies to do overlapped load and store copy
0972    - Explicit branch predictions (based on measured branch probabilities)
0973    - Deferring match copy and interspersed it with decoding subsequent codes
0974    - Swapping literal/length else
0975    - Swapping window/direct else
0976    - Larger unrolled copy loops (three is about right)
0977    - Moving len -= 3 statement into middle of loop
0978  */
0979 void
0980 inflate_stream::
0981 inflate_fast(ranges& r, error_code& ec)
0982 {
0983     unsigned char const* last;  // have enough input while in < last
0984     unsigned char *end;         // while out < end, enough space available
0985     std::size_t op;             // code bits, operation, extra bits, or window position, window bytes to copy
0986     unsigned len;               // match length, unused bytes
0987     unsigned dist;              // match distance
0988     unsigned const lmask =
0989         (1U << lenbits_) - 1;   // mask for first level of length codes
0990     unsigned const dmask =
0991         (1U << distbits_) - 1;  // mask for first level of distance codes
0992 
0993     last = r.in.next + (r.in.avail() - 5);
0994     end = r.out.next + (r.out.avail() - 257);
0995 
0996     /* decode literals and length/distances until end-of-block or not enough
0997        input data or output space */
0998     do
0999     {
1000         if(bi_.size() < 15)
1001             bi_.fill_16(r.in.next);
1002         auto cp = &lencode_[bi_.peek_fast() & lmask];
1003     dolen:
1004         bi_.drop(cp->bits);
1005         op = (unsigned)(cp->op);
1006         if(op == 0)
1007         {
1008             // literal
1009             *r.out.next++ = (unsigned char)(cp->val);
1010         }
1011         else if(op & 16)
1012         {
1013             // length base
1014             len = (unsigned)(cp->val);
1015             op &= 15; // number of extra bits
1016             if(op)
1017             {
1018                 if(bi_.size() < op)
1019                     bi_.fill_8(r.in.next);
1020                 len += (unsigned)bi_.peek_fast() & ((1U << op) - 1);
1021                 bi_.drop(op);
1022             }
1023             if(bi_.size() < 15)
1024                 bi_.fill_16(r.in.next);
1025             cp = &distcode_[bi_.peek_fast() & dmask];
1026         dodist:
1027             bi_.drop(cp->bits);
1028             op = (unsigned)(cp->op);
1029             if(op & 16)
1030             {
1031                 // distance base
1032                 dist = (unsigned)(cp->val);
1033                 op &= 15; // number of extra bits
1034                 if(bi_.size() < op)
1035                 {
1036                     bi_.fill_8(r.in.next);
1037                     if(bi_.size() < op)
1038                         bi_.fill_8(r.in.next);
1039                 }
1040                 dist += (unsigned)bi_.peek_fast() & ((1U << op) - 1);
1041 #ifdef INFLATE_STRICT
1042                 if(dist > dmax_)
1043                 {
1044                     BOOST_BEAST_ASSIGN_EC(ec, error::invalid_distance);
1045                     mode_ = BAD;
1046                     break;
1047                 }
1048 #endif
1049                 bi_.drop(op);
1050 
1051                 op = r.out.used();
1052                 if(dist > op)
1053                 {
1054                     // copy from window
1055                     op = dist - op; // distance back in window
1056                     if(op > w_.size())
1057                     {
1058                         BOOST_BEAST_ASSIGN_EC(ec, error::invalid_distance);
1059                         mode_ = BAD;
1060                         break;
1061                     }
1062                     auto const n = clamp(len, op);
1063                     w_.read(r.out.next, op, n);
1064                     r.out.next += n;
1065                     len -= n;
1066                 }
1067                 if(len > 0)
1068                 {
1069                     // copy from output
1070                     auto in = r.out.next - dist;
1071                     auto n = clamp(len, r.out.avail());
1072                     len -= n;
1073                     while(n--)
1074                         *r.out.next++ = *in++;
1075                 }
1076             }
1077             else if((op & 64) == 0)
1078             {
1079                 // 2nd level distance code
1080                 cp = &distcode_[cp->val + (bi_.peek_fast() & ((1U << op) - 1))];
1081                 goto dodist;
1082             }
1083             else
1084             {
1085                 BOOST_BEAST_ASSIGN_EC(ec, error::invalid_distance_code);
1086                 mode_ = BAD;
1087                 break;
1088             }
1089         }
1090         else if((op & 64) == 0)
1091         {
1092             // 2nd level length code
1093             cp = &lencode_[cp->val + (bi_.peek_fast() & ((1U << op) - 1))];
1094             goto dolen;
1095         }
1096         else if(op & 32)
1097         {
1098             // end-of-block
1099             mode_ = TYPE;
1100             break;
1101         }
1102         else
1103         {
1104             BOOST_BEAST_ASSIGN_EC(ec, error::invalid_literal_length);
1105             mode_ = BAD;
1106             break;
1107         }
1108     }
1109     while(r.in.next < last && r.out.next < end);
1110 
1111     // return unused bytes (on entry, bits < 8, so in won't go too far back)
1112     bi_.rewind(r.in.next);
1113 }
1114 
1115 } // detail
1116 } // zlib
1117 } // beast
1118 } // boost
1119 
1120 #endif