mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-09-23 14:53:52 +00:00
Source code export placeholder
This commit is contained in:
@@ -9,6 +9,8 @@
|
||||
#include "zlib.h"
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
static voidpf ZlibAllocShim(voidpf opaque, uInt items, uInt size)
|
||||
@@ -22,66 +24,189 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
namespace PortabilityLayer
|
||||
bool PortabilityLayer::DeflateCodec::DecompressStream(GpIOStream *stream, size_t inSize, void *outBuffer, size_t outSize)
|
||||
{
|
||||
bool DeflateCodec::DecompressStream(GpIOStream *stream, size_t inSize, void *outBuffer, size_t outSize)
|
||||
z_stream zstream;
|
||||
zstream.zalloc = ZlibAllocShim;
|
||||
zstream.zfree = ZlibFreeShim;
|
||||
zstream.opaque = MemoryManager::GetInstance();
|
||||
|
||||
if (inflateInit2(&zstream, -15) != Z_OK)
|
||||
return false;
|
||||
|
||||
const size_t bufferSize = 1024;
|
||||
uint8_t buffer[1024];
|
||||
|
||||
zstream.avail_out = outSize;
|
||||
zstream.next_out = static_cast<Bytef*>(outBuffer);
|
||||
zstream.avail_in = 0;
|
||||
zstream.next_in = buffer;
|
||||
|
||||
bool failed = false;
|
||||
for (;;)
|
||||
{
|
||||
z_stream zstream;
|
||||
zstream.zalloc = ZlibAllocShim;
|
||||
zstream.zfree = ZlibFreeShim;
|
||||
zstream.opaque = MemoryManager::GetInstance();
|
||||
|
||||
if (inflateInit2(&zstream, -15) != Z_OK)
|
||||
return false;
|
||||
|
||||
const size_t bufferSize = 1024;
|
||||
uint8_t buffer[1024];
|
||||
|
||||
zstream.avail_out = outSize;
|
||||
zstream.next_out = static_cast<Bytef*>(outBuffer);
|
||||
zstream.avail_in = 0;
|
||||
zstream.next_in = buffer;
|
||||
|
||||
bool failed = false;
|
||||
for (;;)
|
||||
if (zstream.avail_in == 0)
|
||||
{
|
||||
if (zstream.avail_in == 0)
|
||||
const size_t sizeToRead = (bufferSize < inSize) ? bufferSize : inSize;
|
||||
|
||||
if (sizeToRead == 0)
|
||||
{
|
||||
const size_t sizeToRead = (bufferSize < inSize) ? bufferSize : inSize;
|
||||
|
||||
if (sizeToRead == 0)
|
||||
{
|
||||
// Ran out of input
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (stream->Read(buffer, sizeToRead) != sizeToRead)
|
||||
{
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
zstream.avail_in = sizeToRead;
|
||||
zstream.next_in = buffer;
|
||||
}
|
||||
|
||||
int result = inflate(&zstream, Z_NO_FLUSH);
|
||||
if (result == Z_STREAM_END)
|
||||
{
|
||||
failed = (zstream.avail_out != 0);
|
||||
// Ran out of input
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (result != Z_OK)
|
||||
if (stream->Read(buffer, sizeToRead) != sizeToRead)
|
||||
{
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
zstream.avail_in = sizeToRead;
|
||||
zstream.next_in = buffer;
|
||||
}
|
||||
|
||||
inflateEnd(&zstream);
|
||||
int result = inflate(&zstream, Z_NO_FLUSH);
|
||||
if (result == Z_STREAM_END)
|
||||
{
|
||||
failed = (zstream.avail_out != 0);
|
||||
break;
|
||||
}
|
||||
|
||||
return !failed;
|
||||
if (result != Z_OK)
|
||||
{
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inflateEnd(&zstream);
|
||||
|
||||
return !failed;
|
||||
}
|
||||
|
||||
namespace PortabilityLayer
|
||||
{
|
||||
class DeflateContextImpl final : public DeflateContext
|
||||
{
|
||||
public:
|
||||
DeflateContextImpl(GpIOStream *stream, int compressionLevel);
|
||||
~DeflateContextImpl();
|
||||
|
||||
bool Init();
|
||||
|
||||
void Destroy() override;
|
||||
bool Append(const void *buffer, size_t size) override;
|
||||
|
||||
bool Flush() override;
|
||||
|
||||
private:
|
||||
GpIOStream *m_ioStream;
|
||||
z_stream m_zStream;
|
||||
int m_compressionLevel;
|
||||
bool m_streamInitialized;
|
||||
|
||||
uint8_t m_flushBuffer[1024];
|
||||
};
|
||||
}
|
||||
|
||||
PortabilityLayer::DeflateContextImpl::DeflateContextImpl(GpIOStream *stream, int compressionLevel)
|
||||
: m_ioStream(stream)
|
||||
, m_compressionLevel(compressionLevel)
|
||||
, m_streamInitialized(false)
|
||||
{
|
||||
memset(&m_zStream, 0, sizeof(m_zStream));
|
||||
}
|
||||
|
||||
PortabilityLayer::DeflateContextImpl::~DeflateContextImpl()
|
||||
{
|
||||
if (m_streamInitialized)
|
||||
deflateEnd(&m_zStream);
|
||||
}
|
||||
|
||||
bool PortabilityLayer::DeflateContextImpl::Init()
|
||||
{
|
||||
m_zStream.zalloc = ZlibAllocShim;
|
||||
m_zStream.zfree = ZlibFreeShim;
|
||||
m_zStream.opaque = MemoryManager::GetInstance();
|
||||
|
||||
if (deflateInit2(&m_zStream, m_compressionLevel, Z_DEFLATED, -15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
|
||||
return false;
|
||||
|
||||
m_streamInitialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void PortabilityLayer::DeflateContextImpl::Destroy()
|
||||
{
|
||||
this->~DeflateContextImpl();
|
||||
PortabilityLayer::MemoryManager::GetInstance()->Release(this);
|
||||
}
|
||||
|
||||
bool PortabilityLayer::DeflateContextImpl::Append(const void *buffer, size_t size)
|
||||
{
|
||||
m_zStream.avail_out = sizeof(m_flushBuffer);
|
||||
m_zStream.next_out = m_flushBuffer;
|
||||
|
||||
m_zStream.next_in = reinterpret_cast<Bytef*>(const_cast<void*>(buffer));
|
||||
m_zStream.avail_in = size;
|
||||
|
||||
while (m_zStream.avail_in > 0)
|
||||
{
|
||||
if (deflate(&m_zStream, Z_NO_FLUSH) != Z_OK)
|
||||
return false;
|
||||
|
||||
if (m_zStream.avail_out != sizeof(m_flushBuffer))
|
||||
{
|
||||
size_t amountOut = sizeof(m_flushBuffer) - m_zStream.avail_out;
|
||||
if (m_ioStream->Write(m_flushBuffer, amountOut) != amountOut)
|
||||
return false;
|
||||
|
||||
m_zStream.avail_out = sizeof(m_flushBuffer);
|
||||
m_zStream.next_out = m_flushBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PortabilityLayer::DeflateContextImpl::Flush()
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
const int deflateCode = deflate(&m_zStream, Z_FINISH);
|
||||
if (deflateCode != Z_OK && deflateCode != Z_STREAM_END)
|
||||
return false;
|
||||
|
||||
if (m_zStream.avail_out != sizeof(m_flushBuffer))
|
||||
{
|
||||
size_t amountOut = sizeof(m_flushBuffer) - m_zStream.avail_out;
|
||||
if (m_ioStream->Write(m_flushBuffer, amountOut) != amountOut)
|
||||
return false;
|
||||
|
||||
m_zStream.avail_out = sizeof(m_flushBuffer);
|
||||
m_zStream.next_out = m_flushBuffer;
|
||||
}
|
||||
|
||||
if (deflateCode == Z_STREAM_END)
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PortabilityLayer::DeflateContext *PortabilityLayer::DeflateContext::Create(GpIOStream *stream, int compressionLevel)
|
||||
{
|
||||
void *storage = PortabilityLayer::MemoryManager::GetInstance()->Alloc(sizeof(PortabilityLayer::DeflateContextImpl));
|
||||
if (!storage)
|
||||
return nullptr;
|
||||
|
||||
DeflateContextImpl *obj = new (storage) DeflateContextImpl(stream, compressionLevel);
|
||||
if (!obj->Init())
|
||||
{
|
||||
obj->Destroy();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@@ -6,6 +6,17 @@ class GpIOStream;
|
||||
|
||||
namespace PortabilityLayer
|
||||
{
|
||||
class DeflateContext
|
||||
{
|
||||
public:
|
||||
static DeflateContext *Create(GpIOStream *stream, int compressionLevel);
|
||||
|
||||
virtual void Destroy() = 0;
|
||||
virtual bool Append(const void *buffer, size_t size) = 0;
|
||||
|
||||
virtual bool Flush() = 0;
|
||||
};
|
||||
|
||||
class DeflateCodec
|
||||
{
|
||||
public:
|
||||
|
@@ -34,6 +34,9 @@ namespace PortabilityLayer
|
||||
LEUInt32_t m_uncompressedSize;
|
||||
LEUInt16_t m_fileNameLength;
|
||||
LEUInt16_t m_extraFieldLength;
|
||||
|
||||
// File name
|
||||
// Extra field
|
||||
};
|
||||
|
||||
struct ZipCentralDirectoryFileHeader
|
||||
|
Reference in New Issue
Block a user