Skip to content

Commit

Permalink
move zlib stream classes to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Nov 1, 2024
1 parent 9b0101f commit 12455d3
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 221 deletions.
2 changes: 2 additions & 0 deletions vstgui/uidescription/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ set(${target}_sources
compresseduidescription.h
cstream.cpp
cstream.h
cstream_zlib.cpp
cstream_zlib.h
delegationcontroller.h
doc.h
icontentprovider.h
Expand Down
222 changes: 1 addition & 221 deletions vstgui/uidescription/compresseduidescription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,13 @@

#include "../lib/cresourcedescription.h"
#include "compresseduidescription.h"
#include "cstream.h"
#include "cstream_zlib.h"
#include "uicontentprovider.h"
#include <array>

//------------------------------------------------------------------------
namespace VSTGUI {

namespace {
#if _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4334)
#endif
#define MINIZ_NO_STDIO
#define MINIZ_NO_ARCHIVE_APIS
#define MINIZ_NO_ARCHIVE_WRITING_APIS
#include "../thirdparty/miniz/miniz.c"
#if _MSC_VER
#pragma warning(pop)
#endif

using z_stream = mz_stream;
};

//-----------------------------------------------------------------------------
class ZLibInputStream : public InputStream
{
public:
ZLibInputStream (ByteOrder byteOrder = kNativeByteOrder);
~ZLibInputStream ();

bool open (InputStream& stream);

bool operator>> (std::string& string) override { return false; }
uint32_t readRaw (void* buffer, uint32_t size) override;

protected:
std::unique_ptr<z_stream> zstream;
InputStream* stream {nullptr};
std::array<Bytef, 4096> internalBuffer;
};

//-----------------------------------------------------------------------------
class ZLibOutputStream : public OutputStream
{
public:
ZLibOutputStream (ByteOrder byteOrder = kNativeByteOrder);
~ZLibOutputStream ();

bool open (OutputStream& stream, int32_t compressionLevel = 6);
bool close ();

bool operator<< (const std::string& str) override
{
return writeRaw (str.data (), static_cast<uint32_t> (str.size ())) == str.size ();
}
uint32_t writeRaw (const void* buffer, uint32_t size) override;

protected:
std::unique_ptr<z_stream> zstream;
OutputStream* stream {nullptr};
std::array<Bytef, 4096> internalBuffer;
};

//------------------------------------------------------------------------
class ZLibInputContentProvider : public IContentProvider
{
Expand Down Expand Up @@ -208,169 +152,5 @@ bool CompressedUIDescription::save (UTF8StringPtr filename, int32_t flags,
return result;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
ZLibInputStream::ZLibInputStream (ByteOrder byteOrder) : InputStream (byteOrder)
{
}

//-----------------------------------------------------------------------------
ZLibInputStream::~ZLibInputStream ()
{
if (zstream)
inflateEnd (zstream.get ());
}

//-----------------------------------------------------------------------------
bool ZLibInputStream::open (InputStream& _stream)
{
if (zstream != nullptr || stream != nullptr)
return false;
stream = &_stream;

auto read = stream->readRaw (internalBuffer.data (), static_cast<uint32_t> (internalBuffer.size ()));
if (read == 0 || read == kStreamIOError)
return false;

zstream = std::unique_ptr<z_stream> (new z_stream);
memset (zstream.get (), 0, sizeof (z_stream));

zstream->next_in = internalBuffer.data ();
zstream->avail_in = read;

if (inflateInit (zstream.get ()) != Z_OK)
{
zstream = nullptr;
}

return zstream != nullptr;
}

//-----------------------------------------------------------------------------
uint32_t ZLibInputStream::readRaw (void* buffer, uint32_t size)
{
if (!zstream || !buffer)
return kStreamIOError;
zstream->next_out = static_cast<Bytef*> (buffer);
zstream->avail_out = size;
while (zstream->avail_out > 0)
{
if (zstream->avail_in == 0)
{
auto read = stream->readRaw (internalBuffer.data (), static_cast<uint32_t> (internalBuffer.size ()));
if (read > 0 && read != kStreamIOError)
{
zstream->next_in = internalBuffer.data ();
zstream->avail_in = read;
}
}
auto zres = inflate (zstream.get (), Z_SYNC_FLUSH);
if (zres == Z_STREAM_END)
{
return size - zstream->avail_out;
}
else if (zres != Z_OK)
return kStreamIOError;
}
return size;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
ZLibOutputStream::ZLibOutputStream (ByteOrder byteOrder) : OutputStream (byteOrder)
{
}

//-----------------------------------------------------------------------------
ZLibOutputStream::~ZLibOutputStream ()
{
close ();
}

//-----------------------------------------------------------------------------
bool ZLibOutputStream::open (OutputStream& _stream, int32_t compressionLevel)
{
if (zstream != nullptr || stream != nullptr)
return false;
stream = &_stream;

zstream = std::unique_ptr<z_stream> (new z_stream);
memset (zstream.get (), 0, sizeof (z_stream));

if (deflateInit (zstream.get (), compressionLevel) != Z_OK)
{
zstream = nullptr;
return false;
}
return true;
}

//-----------------------------------------------------------------------------
bool ZLibOutputStream::close ()
{
bool result = true;
if (zstream)
{
zstream->next_in = nullptr;
zstream->avail_in = 0;
while (true)
{
zstream->next_out = internalBuffer.data ();
zstream->avail_out = static_cast<unsigned int> (internalBuffer.size ());
auto zres = deflate (zstream.get (), Z_FINISH);
if (zres != Z_OK && zres != Z_BUF_ERROR && zres != Z_STREAM_END)
{
result = false;
break;
}
else if (zstream->avail_out != internalBuffer.size ())
{
auto written = stream->writeRaw (internalBuffer.data (),
static_cast<uint32_t> (internalBuffer.size () - zstream->avail_out));
if (written != internalBuffer.size () - zstream->avail_out)
{
result = false;
break;
}
}
if (zres == Z_STREAM_END)
break;
}
deflateEnd (zstream.get ());
zstream = nullptr;
}
return result;
}

//-----------------------------------------------------------------------------
uint32_t ZLibOutputStream::writeRaw (const void* buffer, uint32_t size)
{
if (!zstream)
return kStreamIOError;
zstream->next_in = const_cast<Bytef*> (static_cast<const Bytef*> (buffer));
zstream->avail_in = size;
while (zstream->avail_in > 0)
{
zstream->next_out = internalBuffer.data ();
zstream->avail_out = static_cast<unsigned int> (internalBuffer.size ());
auto zres = deflate (zstream.get (), Z_NO_FLUSH);
if (zres == Z_STREAM_ERROR)
{
return kStreamIOError;
}
if (zstream->avail_out != internalBuffer.size ())
{
auto written = stream->writeRaw (
internalBuffer.data (),
static_cast<uint32_t> (internalBuffer.size () - zstream->avail_out));
if (written != internalBuffer.size () - zstream->avail_out)
return kStreamIOError;
}
}
return size;
}

//------------------------------------------------------------------------
} // VSTGUI
Loading

0 comments on commit 12455d3

Please sign in to comment.