/* */ #include "GZipEncoder.h" #include #include #include "fmt.h" #include "DlAbortEx.h" #include "util.h" namespace aria2 { GZipEncoder::GZipEncoder() : strm_(nullptr) {} GZipEncoder::~GZipEncoder() { release(); } void GZipEncoder::init() { release(); strm_ = new z_stream(); strm_->zalloc = Z_NULL; strm_->zfree = Z_NULL; strm_->opaque = Z_NULL; strm_->avail_in = 0; strm_->next_in = Z_NULL; if (Z_OK != deflateInit2(strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY)) { throw DL_ABORT_EX("Initializing z_stream failed."); } } void GZipEncoder::release() { if (strm_) { deflateEnd(strm_); delete strm_; strm_ = nullptr; } } std::string GZipEncoder::encode(const unsigned char* in, size_t length, int flush) { strm_->avail_in = length; strm_->next_in = const_cast(in); std::string out; std::array outbuf; while (1) { strm_->avail_out = outbuf.size(); strm_->next_out = outbuf.data(); int ret = ::deflate(strm_, flush); if (ret == Z_STREAM_ERROR) { throw DL_ABORT_EX(fmt("libz::deflate() failed. cause:%s", strm_->msg)); } size_t produced = outbuf.size() - strm_->avail_out; out.append(&outbuf[0], &outbuf[produced]); if (strm_->avail_out > 0) { break; } } return out; } std::string GZipEncoder::str() { internalBuf_ += encode(nullptr, 0, Z_FINISH); return internalBuf_; } GZipEncoder& GZipEncoder::operator<<(const char* s) { internalBuf_ += encode(reinterpret_cast(s), strlen(s)); return *this; } GZipEncoder& GZipEncoder::operator<<(const std::string& s) { internalBuf_ += encode(reinterpret_cast(s.data()), s.size()); return *this; } GZipEncoder& GZipEncoder::operator<<(int64_t i) { std::string s = util::itos(i); (*this) << s; return *this; } GZipEncoder& GZipEncoder::write(const char* s, size_t length) { internalBuf_ += encode(reinterpret_cast(s), length); return *this; } } // namespace aria2