61 lines
2.2 KiB
C++
61 lines
2.2 KiB
C++
// IAEngine: 2D Game Engine by IA
|
|
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#include <IAEngine/Utils.hpp>
|
|
|
|
#include <zlib.h>
|
|
|
|
namespace ia::iae
|
|
{
|
|
Vector<UINT8> Utils::Inflate(IN PCUINT8 data, IN SIZE_T dataSize)
|
|
{
|
|
STATIC UINT8 TMP_BUFFER[16384];
|
|
Vector<UINT8> result;
|
|
result.reserve(ia_min((SIZE_T)(dataSize * 3), sizeof(TMP_BUFFER)));
|
|
z_stream stream;
|
|
stream.zalloc = Z_NULL;
|
|
stream.zfree = Z_NULL;
|
|
stream.opaque = Z_NULL;
|
|
stream.avail_in = 0;
|
|
stream.next_in = Z_NULL;
|
|
if (inflateInit(&stream) != Z_OK)
|
|
THROW_UNKNOWN("Inflate failed: init zlib inflate");
|
|
stream.avail_in = dataSize;
|
|
stream.next_in = (Bytef *) data;
|
|
while (true) {
|
|
stream.avail_out = sizeof(TMP_BUFFER);
|
|
stream.next_out = TMP_BUFFER;
|
|
const auto r = inflate(&stream, Z_SYNC_FLUSH);
|
|
result.insert(result.end(), sizeof(TMP_BUFFER) - stream.avail_out, TMP_BUFFER);
|
|
if (r == Z_STREAM_END)
|
|
break;
|
|
else if (r != Z_OK)
|
|
THROW_INVALID_DATA("Inflate failed: zlib inflation");
|
|
}
|
|
inflateEnd(&stream);
|
|
return result;
|
|
}
|
|
|
|
Vector<UINT8> Utils::Deflate(IN PCUINT8 data, IN SIZE_T dataSize)
|
|
{
|
|
Vector<UINT8> result;
|
|
auto deflateBound = compressBound(dataSize);
|
|
result.resize(deflateBound);
|
|
compress(result.data(), &deflateBound, data, dataSize);
|
|
result.resize(deflateBound);
|
|
return result;
|
|
}
|
|
} // namespace ia::iae
|