/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <zlib.h>
#include <cstdlib>

namespace Extensions
{
	static unsigned char* STBZlibCompressor(unsigned char* data, int data_len, int* out_len, int quality)
	{
		uLong maxCompressedSize = compressBound(data_len);
		void* outData = malloc(maxCompressedSize);
		if (!outData)
		{
			*out_len = 0;
			return nullptr;
		}
		int result = compress2(static_cast<Bytef*>(outData), &maxCompressedSize, data, data_len, Z_BEST_COMPRESSION);
		if (result != Z_OK)
		{
			free(outData);
			*out_len = 0;
			return nullptr;
		}
		*out_len = static_cast<int>(maxCompressedSize);
		return static_cast<unsigned char*>(outData);
	}
}