File indexing completed on 2025-01-18 09:29:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
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
0063 static std::uint8_t constexpr maxBits = 15;
0064
0065
0066 static std::uint16_t constexpr lengthCodes = 29;
0067
0068
0069 static std::uint16_t constexpr literals = 256;
0070
0071
0072 static std::uint16_t constexpr lCodes = literals + 1 + lengthCodes;
0073
0074
0075 static std::uint16_t constexpr dCodes = 30;
0076
0077
0078 static std::uint16_t constexpr blCodes = 19;
0079
0080
0081 static std::uint16_t constexpr distCodeLen = 512;
0082
0083
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
0090 BOOST_STATIC_ASSERT(minMatch == 3);
0091
0092
0093 static std::uint16_t constexpr END_BLOCK = 256;
0094
0095
0096 static std::uint8_t constexpr REP_3_6 = 16;
0097
0098
0099 static std::uint8_t constexpr REPZ_3_10 = 17;
0100
0101
0102 static std::uint8_t constexpr REPZ_11_138 = 18;
0103
0104
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
0110 static std::uint8_t constexpr max_mem_level = 9;
0111
0112
0113 static std::uint8_t constexpr DEF_MEM_LEVEL = max_mem_level;
0114
0115
0116
0117
0118
0119
0120
0121 static std::uint16_t constexpr HEAP_SIZE = 2 * lCodes + 1;
0122
0123
0124 static std::uint8_t constexpr Buf_size = 16;
0125
0126
0127 static std::size_t constexpr kTooFar = 4096;
0128
0129
0130
0131
0132 static std::size_t constexpr kMinLookahead = maxMatch + minMatch+1;
0133
0134
0135
0136
0137 static std::size_t constexpr kWinInit = maxMatch;
0138
0139
0140 struct ct_data
0141 {
0142 std::uint16_t fc;
0143 std::uint16_t dl;
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;
0155 std::uint8_t const* extra_bits;
0156 std::uint16_t extra_base;
0157 std::uint16_t elems;
0158 std::uint8_t max_length;
0159 };
0160
0161 struct lut_type
0162 {
0163
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
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
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
0179
0180
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
0190
0191
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;
0217 int max_code;
0218 static_desc const* stat_desc;
0219 };
0220
0221 enum block_state
0222 {
0223 need_more,
0224 block_done,
0225 finish_started,
0226 finish_done
0227 };
0228
0229
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
0241
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_;
0257 Byte* pending_buf_;
0258 std::uint32_t
0259 pending_buf_size_;
0260 Byte* pending_out_;
0261 uInt pending_;
0262 boost::optional<Flush>
0263 last_flush_;
0264
0265 uInt w_size_;
0266 uInt w_bits_;
0267 uInt w_mask_;
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277 Byte *window_ = nullptr;
0278
0279
0280
0281
0282 std::uint32_t window_size_;
0283
0284
0285
0286
0287
0288 std::uint16_t* prev_;
0289
0290 std::uint16_t* head_;
0291
0292 uInt ins_h_;
0293 uInt hash_size_;
0294 uInt hash_bits_;
0295 uInt hash_mask_;
0296
0297
0298
0299
0300
0301
0302 uInt hash_shift_;
0303
0304
0305
0306
0307 long block_start_;
0308
0309 uInt match_length_;
0310 IPos prev_match_;
0311 int match_available_;
0312 uInt strstart_;
0313 uInt match_start_;
0314 uInt lookahead_;
0315
0316
0317
0318
0319 uInt prev_length_;
0320
0321
0322
0323
0324
0325 uInt max_chain_length_;
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 uInt max_lazy_match_;
0336
0337 int level_;
0338 Strategy strategy_;
0339
0340
0341 uInt good_match_;
0342
0343 int nice_match_;
0344
0345 ct_data dyn_ltree_[
0346 HEAP_SIZE];
0347 ct_data dyn_dtree_[
0348 2*dCodes+1];
0349 ct_data bl_tree_[
0350 2*blCodes+1];
0351
0352 tree_desc l_desc_;
0353 tree_desc d_desc_;
0354 tree_desc bl_desc_;
0355
0356
0357 std::uint16_t bl_count_[maxBits+1];
0358
0359
0360 static std::size_t constexpr kSmallest = 1;
0361
0362
0363
0364
0365
0366 int heap_[2*lCodes+1];
0367 int heap_len_;
0368 int heap_max_;
0369
0370
0371 std::uint8_t depth_[2*lCodes+1];
0372
0373 std::uint8_t *sym_buf_;
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393 uInt lit_bufsize_;
0394
0395 uInt sym_next_;
0396 uInt sym_end_;
0397
0398 std::uint32_t opt_len_;
0399 std::uint32_t static_len_;
0400 uInt matches_;
0401 uInt insert_;
0402
0403
0404
0405
0406 std::uint16_t bi_buf_;
0407
0408
0409
0410
0411 int bi_valid_;
0412
0413
0414
0415
0416
0417
0418 std::uint32_t high_water_;
0419
0420
0421
0422 deflate_stream()
0423 : lut_(get_lut())
0424 {
0425 }
0426
0427
0428
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
0450
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
0470 void
0471 send_code(int value, ct_data const* tree)
0472 {
0473 send_bits(tree[value].fc, tree[value].dl);
0474 }
0475
0476
0477
0478
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
0489
0490
0491
0492
0493
0494 void
0495 update_hash(uInt& h, std::uint8_t c)
0496 {
0497 h = ((h << hash_shift_) ^ c) & hash_mask_;
0498 }
0499
0500
0501
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
0512
0513
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
0524
0525
0526
0527
0528
0529
0530
0531
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
0544
0545
0546
0547
0548 struct config
0549 {
0550 std::uint16_t good_length;
0551 std::uint16_t max_lazy;
0552 std::uint16_t nice_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
0578 case 0: return { 0, 0, 0, 0, &self::deflate_stored};
0579 case 1: return { 4, 4, 8, 4, &self::deflate_fast};
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};
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};
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
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 }
0715 }
0716 }
0717 }
0718
0719 #ifdef BOOST_BEAST_HEADER_ONLY
0720 #include <boost/beast/zlib/detail/deflate_stream.ipp>
0721 #endif
0722
0723 #endif