@@ -2578,28 +2578,40 @@ class gzip_compressor : public compressor {
25782578 Callback callback) override {
25792579 assert (is_valid_);
25802580
2581- auto flush = last ? Z_FINISH : Z_NO_FLUSH;
2581+ do {
2582+ constexpr size_t max_avail_in =
2583+ std::numeric_limits<decltype (strm_.avail_in )>::max ();
25822584
2583- strm_.avail_in = static_cast <decltype (strm_.avail_in )>(data_length);
2584- strm_.next_in = const_cast <Bytef *>(reinterpret_cast <const Bytef *>(data));
2585+ strm_.avail_in = static_cast <decltype (strm_.avail_in )>(
2586+ std::min (data_length, max_avail_in));
2587+ strm_.next_in =
2588+ const_cast <Bytef *>(reinterpret_cast <const Bytef *>(data));
25852589
2586- int ret = Z_OK;
2590+ data_length -= strm_.avail_in ;
2591+ data += strm_.avail_in ;
25872592
2588- std::array<char , CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
2589- do {
2590- strm_.avail_out = static_cast <uInt>(buff.size ());
2591- strm_.next_out = reinterpret_cast <Bytef *>(buff.data ());
2593+ auto flush = (last && data_length == 0 ) ? Z_FINISH : Z_NO_FLUSH;
2594+ int ret = Z_OK;
25922595
2593- ret = deflate (&strm_, flush);
2594- if (ret == Z_STREAM_ERROR) { return false ; }
2596+ std::array<char , CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
2597+ do {
2598+ strm_.avail_out = static_cast <uInt>(buff.size ());
2599+ strm_.next_out = reinterpret_cast <Bytef *>(buff.data ());
25952600
2596- if (!callback (buff.data (), buff.size () - strm_.avail_out )) {
2597- return false ;
2598- }
2599- } while (strm_.avail_out == 0 );
2601+ ret = deflate (&strm_, flush);
2602+ if (ret == Z_STREAM_ERROR) { return false ; }
2603+
2604+ if (!callback (buff.data (), buff.size () - strm_.avail_out )) {
2605+ return false ;
2606+ }
2607+ } while (strm_.avail_out == 0 );
2608+
2609+ assert ((flush == Z_FINISH && ret == Z_STREAM_END) ||
2610+ (flush == Z_NO_FLUSH && ret == Z_OK));
2611+ assert (strm_.avail_in == 0 );
2612+
2613+ } while (data_length > 0 );
26002614
2601- assert ((last && ret == Z_STREAM_END) || (!last && ret == Z_OK));
2602- assert (strm_.avail_in == 0 );
26032615 return true ;
26042616 }
26052617
@@ -2633,28 +2645,41 @@ class gzip_decompressor : public decompressor {
26332645
26342646 int ret = Z_OK;
26352647
2636- strm_.avail_in = static_cast <decltype (strm_.avail_in )>(data_length);
2637- strm_.next_in = const_cast <Bytef *>(reinterpret_cast <const Bytef *>(data));
2648+ do {
2649+ constexpr size_t max_avail_in =
2650+ std::numeric_limits<decltype (strm_.avail_in )>::max ();
2651+
2652+ strm_.avail_in = static_cast <decltype (strm_.avail_in )>(
2653+ std::min (data_length, max_avail_in));
2654+ strm_.next_in =
2655+ const_cast <Bytef *>(reinterpret_cast <const Bytef *>(data));
2656+
2657+ data_length -= strm_.avail_in ;
2658+ data += strm_.avail_in ;
2659+
2660+ std::array<char , CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
2661+ while (strm_.avail_in > 0 ) {
2662+ strm_.avail_out = static_cast <uInt>(buff.size ());
2663+ strm_.next_out = reinterpret_cast <Bytef *>(buff.data ());
2664+
2665+ ret = inflate (&strm_, Z_NO_FLUSH);
2666+ assert (ret != Z_STREAM_ERROR);
2667+ switch (ret) {
2668+ case Z_NEED_DICT:
2669+ case Z_DATA_ERROR:
2670+ case Z_MEM_ERROR: inflateEnd (&strm_); return false ;
2671+ }
26382672
2639- std::array<char , CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
2640- while (strm_.avail_in > 0 ) {
2641- strm_.avail_out = static_cast <uInt>(buff.size ());
2642- strm_.next_out = reinterpret_cast <Bytef *>(buff.data ());
2643-
2644- ret = inflate (&strm_, Z_NO_FLUSH);
2645- assert (ret != Z_STREAM_ERROR);
2646- switch (ret) {
2647- case Z_NEED_DICT:
2648- case Z_DATA_ERROR:
2649- case Z_MEM_ERROR: inflateEnd (&strm_); return false ;
2673+ if (!callback (buff.data (), buff.size () - strm_.avail_out )) {
2674+ return false ;
2675+ }
26502676 }
26512677
2652- if (!callback (buff.data (), buff.size () - strm_.avail_out )) {
2653- return false ;
2654- }
2655- }
2678+ if (ret != Z_OK && ret != Z_STREAM_END) return false ;
26562679
2657- return ret == Z_OK || ret == Z_STREAM_END;
2680+ } while (data_length > 0 );
2681+
2682+ return true ;
26582683 }
26592684
26602685private:
0 commit comments