/*** Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: DeflateCompressor.hpp Date: 2022-2-15 Author: Reece ***/ #pragma once #include "zlib.h" namespace Aurora::Compression { struct ZIPDeflate : BaseStream { CompressInfo meta; AuInt8 bits_; ZIPDeflate(const CompressInfo &meta, AuInt8 bits) : meta(meta), BaseStream(meta.uInternalStreamSize), bits_(bits) {} ~ZIPDeflate() { if (this->init_) { deflateEnd(&this->ctx_); } } bool Init(const AuSPtr &pReader) override { this->pReader_ = pReader; if (!this->IsValid()) { SysPushErrorMem(); return false; } this->ctx_.next_in = this->din_; auto ret = deflateInit2(&this->ctx_, meta.uCompressionLevel, Z_DEFLATED, this->bits_, 8, Z_DEFAULT_STRATEGY); if (ret < Z_OK) { SysPushErrorMem("Error: {}", ret); return false; } this->init_ = true; this->SetArray(this->din_); return true; } AuStreamReadWrittenPair_t Ingest_s(AuUInt32 input) override { AuUInt32 done {}, read {}; if (!this->pReader_) { return {}; } while (read < input) { read += IngestForInPointer(this->pReader_, this->ctx_.next_in, this->ctx_.avail_in, input - read); if (!this->ctx_.avail_in) { return {read, done}; } do { this->ctx_.avail_out = AuArraySize(this->dout_); this->ctx_.next_out = this->dout_; if (!this->ctx_.avail_out) { break; } auto ret = deflate(&this->ctx_, Z_NO_FLUSH); if (ret < Z_OK) { SysPushErrorIO("Error: {}", zError(ret)); this->pReader_.reset(); return AuMakePair(read, 0); } auto have = AuArraySize(this->dout_) - this->ctx_.avail_out; done += have; if (!Write(reinterpret_cast(this->dout_), have)) { this->pReader_.reset(); SysPushErrorIO("Compression Out of Overhead"); return AuMakePair(read, 0); } } while (this->ctx_.avail_out == 0); } return {read, done}; } bool Flush() override { return RunFlush(Z_SYNC_FLUSH); } bool Finish() override { return RunFlush(Z_FINISH); } bool RunFlush(int type) { if (!this->pReader_) { return false; } while (this->ctx_.avail_in) { do { this->ctx_.avail_out = AuArraySize(this->dout_); this->ctx_.next_out = this->dout_; auto ret = deflate(&this->ctx_, Z_FULL_FLUSH); if (ret < Z_OK) { SysPushErrorIO("Error: {}", zError(ret)); this->pReader_.reset(); return false; } auto have = AuArraySize(this->dout_) - this->ctx_.avail_out; if (!Write(reinterpret_cast(this->dout_), have)) { this->pReader_.reset(); SysPushErrorIO("Compression Out of Overhead"); return false; } } while (this->ctx_.avail_out == 0); } if (!this->ctx_.avail_in) { do { this->ctx_.avail_out = AuArraySize(this->dout_); this->ctx_.next_out = this->dout_; auto ret = deflate(&this->ctx_, type); if (ret < Z_OK) { SysPushErrorIO("Error: {}", zError(ret)); this->pReader_.reset(); return false; } auto have = AuArraySize(this->dout_) - this->ctx_.avail_out; if (!Write(reinterpret_cast(this->dout_), have)) { this->pReader_.reset(); SysPushErrorIO("Compression Out of Overhead"); return false; } } while (this->ctx_.avail_out == 0); return true; } return true; } private: AuSPtr pReader_; z_stream ctx_ {}; bool init_ {}; unsigned char din_[kChunkSize]; unsigned char dout_[kChunkSize]; }; }