37 #ifndef BOOST_BEAST_ZLIB_DETAIL_INFLATE_STREAM_HPP 38 #define BOOST_BEAST_ZLIB_DETAIL_INFLATE_STREAM_HPP 46 #include <boost/throw_exception.hpp> 66 template<
class =
void>
void doClear();
67 template<
class =
void>
void doReset(
int windowBits);
152 static std::uint16_t constexpr kEnoughLens = 852;
153 static std::uint16_t constexpr kEnoughDists = 592;
154 static std::uint16_t constexpr kEnough = kEnoughLens + kEnoughDists;
159 code
const* distcode;
172 template<
class =
void>
184 template<
class =
void>
189 template<
class =
void>
193 template<
class =
void>
201 unsigned dmax_ = 32768U;
218 unsigned short lens_[320];
219 unsigned short work_[288];
220 code codes_[kEnough];
221 code *next_ = codes_;
226 code
const* lencode_ = codes_ ;
227 code
const* distcode_ = codes_;
239 if(windowBits < 8 || windowBits > 15)
240 BOOST_THROW_EXCEPTION(std::domain_error{
241 "windowBits out of range"});
242 w_.reset(windowBits);
268 std::uint8_t const*
>(zs.
next_in);
288 if( (r.
out.
used() && mode_ < BAD &&
298 zs.
data_type = bi_.size() + (last_ ? 64 : 0) +
299 (mode_ == TYPE ? 128 : 0) +
300 (mode_ == LEN_ || mode_ == COPY_ ? 256 : 0);
374 length_ = v & 0xffff;
375 if(length_ != ((v >> 16) ^ 0xffff))
418 if(nlen_ > 286 || ndist_ > 30)
426 static std::array<std::uint8_t, 19> constexpr order = {{
427 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}};
428 while(have_ < ncode_)
432 bi_.read(lens_[order[have_]], 3);
435 while(have_ < order.size())
436 lens_[order[have_++]] = 0;
441 inflate_table(build::codes, &lens_[0],
442 order.size(), &next_, &lenbits_, work_, ec);
455 while(have_ < nlen_ + ndist_)
460 bi_.peek(v, lenbits_);
461 auto cp = &lencode_[v];
465 lens_[have_++] = cp->val;
479 len = lens_[have_ - 1];
483 else if(cp->val == 17)
501 if(have_ + copy > nlen_ + ndist_)
503 std::fill(&lens_[have_], &lens_[have_ + copy], len);
520 inflate_table(build::lens, &lens_[0],
521 nlen_, &next_, &lenbits_, work_, ec);
529 inflate_table(build::dists, lens_ + nlen_,
530 ndist_, &next_, &distbits_, work_, ec);
564 bi_.peek(v, lenbits_);
565 auto cp = &lencode_[v];
566 if(cp->op && (cp->op & 0xf0) == 0)
569 if(! bi_.fill(prev->bits + prev->op, r.
in.
next, r.
in.
last))
571 bi_.peek(v, prev->bits + prev->op);
572 cp = &lencode_[prev->val + (v >> prev->bits)];
573 bi_.drop(prev->bits + cp->bits);
574 back_ += prev->bits + cp->bits;
595 extra_ = cp->op & 15;
619 bi_.peek(v, distbits_);
620 auto cp = &distcode_[v];
621 if((cp->op & 0xf0) == 0)
624 if(! bi_.fill(prev->bits + prev->op, r.
in.
next, r.
in.
last))
626 bi_.peek(v, prev->bits + prev->op);
627 cp = &distcode_[prev->val + (v >> prev->bits)];
628 bi_.drop(prev->bits + cp->bits);
629 back_ += prev->bits + cp->bits;
639 extra_ = cp->op & 15;
654 #ifdef INFLATE_STRICT 668 auto offset =
static_cast<std::uint16_t
>(
670 if(offset > w_.size())
674 w_.read(r.
out.
next, offset, n);
681 auto in = r.
out.
next - offset_;
696 auto const v =
static_cast<std::uint8_t
>(length_);
715 BOOST_THROW_EXCEPTION(std::logic_error{
762 std::uint16_t
const* base;
763 std::uint16_t
const* extra;
765 std::uint16_t count[15+1];
766 std::uint16_t offs[15+1];
769 static std::uint16_t constexpr lbase[31] = {
770 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
771 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
774 static std::uint16_t constexpr lext[31] = {
775 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
776 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78};
779 static std::uint16_t constexpr dbase[32] = {
780 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
781 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
782 8193, 12289, 16385, 24577, 0, 0};
785 static std::uint16_t constexpr dext[32] = {
786 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
787 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
788 28, 28, 29, 29, 64, 64};
822 for (len = 0; len <= 15; len++)
824 for (sym = 0; sym < codes; sym++)
829 for (max = 15; max >= 1; max--)
836 here.op = (std::uint8_t)64;
837 here.bits = (std::uint8_t)1;
838 here.val = (std::uint16_t)0;
844 for (min = 1; min < max; min++)
852 for (len = 1; len <= 15; len++)
862 if (left > 0 && (type == build::codes || max != 1))
870 for (len = 1; len < 15; len++)
871 offs[len + 1] = offs[len] + count[len];
874 for (sym = 0; sym < codes; sym++)
876 work[offs[lens[sym]]++] = (std::uint16_t)sym;
936 low = (unsigned)(-1);
940 auto const not_enough = []
942 BOOST_THROW_EXCEPTION(std::logic_error{
943 "insufficient output size when inflating tables"});
947 if ((type == build::lens && used > kEnoughLens) ||
948 (type == build::dists && used > kEnoughDists))
955 here.bits = (std::uint8_t)(len - drop);
956 if ((
int)(work[sym]) < end)
958 here.op = (std::uint8_t)0;
959 here.val = work[sym];
961 else if ((
int)(work[sym]) > end)
963 here.op = (std::uint8_t)(extra[work[sym]]);
964 here.val = base[work[sym]];
968 here.op = (std::uint8_t)(32 + 64);
973 incr = 1U << (len - drop);
979 next[(huff >> drop) + fill] = here;
983 incr = 1U << (len - 1);
996 if (--(count[len]) == 0)
998 if (len == max)
break;
999 len = lens[work[sym]];
1003 if (len > root && (huff & mask) != low)
1014 left = (int)(1 << curr);
1015 while (curr + drop < max)
1017 left -= count[curr + drop];
1018 if (left <= 0)
break;
1025 if ((type == build::lens && used > kEnoughLens) ||
1026 (type == build::dists && used > kEnoughDists))
1027 return not_enough();
1031 (*table)[low].op = (std::uint8_t)curr;
1032 (*table)[low].bits = (std::uint8_t)root;
1033 (*table)[low].val = (std::uint16_t)(next - *table);
1043 here.bits = (std::uint8_t)(len - drop);
1055 get_fixed_tables() ->
1058 struct fixed_codes : codes
1070 std::uint16_t lens[320];
1071 std::uint16_t work[288];
1073 std::fill(&lens[ 0], &lens[144], std::uint16_t{8});
1074 std::fill(&lens[144], &lens[256], std::uint16_t{9});
1075 std::fill(&lens[256], &lens[280], std::uint16_t{7});
1076 std::fill(&lens[280], &lens[288], std::uint16_t{8});
1080 auto next = &len_[0];
1081 inflate_table(build::lens,
1082 lens, 288, &next, &lenbits, work, ec);
1084 BOOST_THROW_EXCEPTION(std::logic_error{ec.message()});
1095 auto next = &dist_[0];
1096 std::fill(&lens[0], &lens[32], std::uint16_t{5});
1097 inflate_table(build::dists,
1098 lens, 32, &next, &distbits, work, ec);
1100 BOOST_THROW_EXCEPTION(std::logic_error{ec.message()});
1105 static fixed_codes
const fc;
1114 auto const fc = get_fixed_tables();
1115 lencode_ = fc.lencode;
1116 lenbits_ = fc.lenbits;
1117 distcode_ = fc.distcode;
1118 distbits_ = fc.distbits;
1173 unsigned char const* last;
1178 unsigned const lmask =
1179 (1U << lenbits_) - 1;
1180 unsigned const dmask =
1181 (1U << distbits_) - 1;
1192 auto cp = &lencode_[bi_.peek_fast() & lmask];
1195 op = (unsigned)(cp->op);
1199 *r.
out.
next++ = (
unsigned char)(cp->val);
1204 len = (unsigned)(cp->val);
1210 len += (unsigned)bi_.peek_fast() & ((1U << op) - 1);
1215 cp = &distcode_[bi_.peek_fast() & dmask];
1218 op = (unsigned)(cp->op);
1222 dist = (unsigned)(cp->val);
1230 dist += (unsigned)bi_.peek_fast() & ((1U << op) - 1);
1231 #ifdef INFLATE_STRICT 1252 auto const n =
clamp(len, op);
1267 else if((op & 64) == 0)
1270 cp = &distcode_[cp->val + (bi_.peek_fast() & ((1U << op) - 1))];
1280 else if((op & 64) == 0)
1283 cp = &lencode_[cp->val + (bi_.peek_fast() & ((1U << op) - 1))];
iter_t first
Definition: ranges.hpp:58
Invalid distance too far back.
iter_t last
Definition: ranges.hpp:59
int data_type
Definition: zlib.hpp:109
std::size_t avail() const
Definition: ranges.hpp:78
void doClear()
Definition: inflate_stream.hpp:257
Definition: async_result.hpp:20
std::size_t total_out
Definition: zlib.hpp:107
Over-subscribed length code.
Too many length or distance symbols.
range< true > in
Definition: ranges.hpp:84
inflate_stream()
Definition: inflate_stream.hpp:61
Flush
Definition: zlib.hpp:114
Definition: bitstream.hpp:49
Invalid literal/length code.
void doReset()
Definition: inflate_stream.hpp:71
std::size_t total_in
Definition: zlib.hpp:95
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
void const * next_in
Definition: zlib.hpp:85
void * next_out
Definition: zlib.hpp:99
Definition: ranges.hpp:48
void reset(int bits)
Definition: window.hpp:92
int bits() const
Definition: window.hpp:61
error
Definition: error.hpp:50
Definition: inflate_stream.hpp:58
iter_t next
Definition: ranges.hpp:60
std::size_t avail_out
Definition: zlib.hpp:103
void doWrite(z_params &zs, Flush flush, error_code &ec)
Definition: inflate_stream.hpp:264
std::size_t avail_in
Definition: zlib.hpp:91
U clamp(U u, V v)
Definition: ranges.hpp:92
std::size_t used() const
Definition: ranges.hpp:71
range< false > out
Definition: ranges.hpp:85
Invalid stored block length.
Definition: window.hpp:51
Missing end of block code.
Invalid bit length repeat.