125 lines
3.3 KiB
C++
125 lines
3.3 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: BlockCompressor.cpp
|
|
Date: 2021-6-17
|
|
Author: Reece
|
|
***/
|
|
#include <RuntimeInternal.hpp>
|
|
#include "Compression.hpp"
|
|
#include "BlockCompressor.hpp"
|
|
|
|
#include "bzlib.h"
|
|
#include "zstd.h"
|
|
#include "zlib.h"
|
|
#include "lz4.h"
|
|
|
|
namespace Aurora::Compression
|
|
{
|
|
AuStreamReadWrittenPair_t BaseCompressionStream::Ingest(AuUInt32 input)
|
|
{
|
|
auto ingest = Process(input);
|
|
_count += ingest.second;
|
|
return ingest;
|
|
}
|
|
|
|
bool BaseCompressionStream::ReadByProcessedN(void *buffer, AuUInt32 minimumDeflated, AuStreamReadWrittenPair_t &pair, bool ingestUntilEOS)
|
|
{
|
|
AuUInt32 read {}, len {};
|
|
|
|
if (_count != _lastCount)
|
|
{
|
|
Flush();
|
|
}
|
|
|
|
if (ingestUntilEOS)
|
|
{
|
|
while (this->_outbuffer.RemainingBytes() < minimumDeflated)
|
|
{
|
|
auto toRead = minimumDeflated ? std::min(AuUInt32(4096), AuUInt32(minimumDeflated - this->_outbuffer.RemainingBytes())) : 4096;
|
|
if (Ingest(toRead).second == 0)
|
|
{
|
|
if (!this->_outbuffer.RemainingBytes())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
read += toRead;
|
|
}
|
|
}
|
|
|
|
len = this->_outbuffer.Read(buffer, minimumDeflated, buffer == nullptr);
|
|
pair = {read, len};
|
|
return len != 0;
|
|
}
|
|
|
|
void BaseCompressionStream::Flush()
|
|
{
|
|
ProcessFlush();
|
|
_lastCount = _count;
|
|
}
|
|
|
|
bool BaseCompressionStream::ReadByProcessedN(void *buffer, AuUInt32 minimumDeflated)
|
|
{
|
|
AuUInt32 read {}, len {};
|
|
len = this->_outbuffer.Read(buffer, minimumDeflated, buffer == nullptr);
|
|
return len != 0;
|
|
}
|
|
|
|
bool BaseCompressionStream::GoBackByProcessedN(AuUInt32 offset)
|
|
{
|
|
return this->_outbuffer.ReaderTryGoBack(offset);
|
|
}
|
|
|
|
bool BaseCompressionStream::GoForwardByProcessedN(AuUInt32 offset)
|
|
{
|
|
return this->_outbuffer.ReaderTryGoForward(offset);
|
|
}
|
|
|
|
bool BaseCompressionStream::Write(const void *a, AuUInt32 length)
|
|
{
|
|
auto written = this->_outbuffer.Write(reinterpret_cast<const AuUInt8 *>(a),
|
|
length);
|
|
|
|
if (written != length)
|
|
{
|
|
auto increase = std::max(0, (int)length - (int)this->_outbuffer.RemainingWrite());
|
|
increase += this->_outbuffer.length;
|
|
|
|
if (increase > 64 * 1024 * 1024)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!this->_outbuffer.Resize(increase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
auto remaining = length - written;
|
|
written = this->_outbuffer.Write(reinterpret_cast<const AuUInt8 *>(a) + written,
|
|
remaining);
|
|
if (written != remaining)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
AuUInt32 BaseCompressionStream::GetInternalBufferSize()
|
|
{
|
|
return this->_outbuffer.allocSize;
|
|
}
|
|
|
|
AUKN_SYM void CompressorRelease(ICompressionStream * stream)
|
|
{
|
|
SafeDelete<BaseCompressionStream *>(stream);
|
|
}
|
|
|
|
|
|
} |