mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-29 01:35:06 +01:00
@@ -586,34 +586,30 @@ uint8_t* util_zlib_inflate(uint8_t* data, size_t data_in_size, size_t* data_out_
|
||||
* @brief Deflates input using zlib
|
||||
* @param data Data to be compressed
|
||||
* @param data_in_size Size of data to be compressed
|
||||
* @param data_out_size Pointer to a variable where output size will be written
|
||||
* @return Returns a pointer to memory holding compressed data or NULL on failure.
|
||||
* @note It is caller's responsibility to free() the returned pointer once done with it.
|
||||
* @return Returns an optional std::vector of bytes, which is equal to std::nullopt when deflate has failed
|
||||
*/
|
||||
uint8_t* util_zlib_deflate(const uint8_t* data, size_t data_in_size, size_t* data_out_size)
|
||||
std::optional<std::vector<uint8_t>> util_zlib_deflate(const uint8_t* data, size_t data_in_size)
|
||||
{
|
||||
int32_t ret = Z_OK;
|
||||
uLongf out_size = static_cast<uLongf>(*data_out_size);
|
||||
uLongf out_size = 0;
|
||||
uLong buffer_size = compressBound(static_cast<uLong>(data_in_size));
|
||||
uint8_t* buffer = static_cast<uint8_t*>(malloc(buffer_size));
|
||||
std::vector<uint8_t> buffer(buffer_size);
|
||||
do
|
||||
{
|
||||
if (ret == Z_BUF_ERROR)
|
||||
{
|
||||
buffer_size *= 2;
|
||||
out_size = buffer_size;
|
||||
buffer = static_cast<uint8_t*>(realloc(buffer, buffer_size));
|
||||
buffer.resize(buffer_size);
|
||||
}
|
||||
else if (ret == Z_STREAM_ERROR)
|
||||
{
|
||||
log_error("Your build is shipped with broken zlib. Please use the official build.");
|
||||
free(buffer);
|
||||
return nullptr;
|
||||
return std::nullopt;
|
||||
}
|
||||
ret = compress(buffer, &out_size, data, static_cast<uLong>(data_in_size));
|
||||
ret = compress(buffer.data(), &out_size, data, static_cast<uLong>(data_in_size));
|
||||
} while (ret != Z_OK);
|
||||
*data_out_size = out_size;
|
||||
buffer = static_cast<uint8_t*>(realloc(buffer, *data_out_size));
|
||||
buffer.resize(out_size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user