77173 {
77174 type = MiniZStreamType::MINIZ_TYPE_INFLATE;
77175
77176
77177 while (compressed_size > 0) {
77178
77179 if (compressed_size < GZIP_HEADER_MINSIZE) {
77180 FormatException("Failed to decompress GZIP block: compressed size is less than gzip header size");
77181 }
77182 auto gzip_hdr = reinterpret_cast<const unsigned char *>(compressed_data);
77183 if (gzip_hdr[0] != 0x1F || gzip_hdr[1] != 0x8B || gzip_hdr[2] != GZIP_COMPRESSION_DEFLATE ||
77184 gzip_hdr[3] & GZIP_FLAG_UNSUPPORTED) {
77185 FormatException("Input is invalid/unsupported GZIP stream");
77186 }
77187 compressed_data += GZIP_HEADER_MINSIZE;
77188 compressed_size -= GZIP_HEADER_MINSIZE;
77189
77190
77191 auto mz_ret = mz_inflateInit2(&stream, -MZ_DEFAULT_WINDOW_BITS);
77192 if (mz_ret != duckdb_miniz::MZ_OK) {
77193 FormatException("Failed to initialize miniz", mz_ret);
77194 }
77195
77196
77197 stream.next_in = reinterpret_cast<const unsigned char *>(compressed_data);
77198 stream.avail_in = static_cast<unsigned int>(compressed_size);
77199 stream.next_out = reinterpret_cast<unsigned char *>(out_data);
77200 stream.avail_out = static_cast<unsigned int>(out_size);
77201
77202
77203 mz_ret = mz_inflate(&stream, duckdb_miniz::MZ_FINISH);
77204 if (mz_ret != duckdb_miniz::MZ_OK && mz_ret != duckdb_miniz::MZ_STREAM_END) {
77205 FormatException("Failed to decompress GZIP block", mz_ret);
77206 }
77207 mz_inflateEnd(&stream);
77208
77209
77210 compressed_data += GZIP_FOOTER_SIZE + stream.total_in;
77211 compressed_size -= GZIP_FOOTER_SIZE + stream.total_in;
77212 out_data += stream.total_out;
77213 out_size -= stream.total_out;
77214
77215 ResetStreamInternal();
77216 }
77217 }