1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-16 19:43:06 +01:00

Part of #11159: Optimize util_zlib_deflate return (#12111)

This commit is contained in:
frutiemax
2020-07-04 08:21:50 -04:00
committed by GitHub
parent 55f0f34fe0
commit bee0b4b82d
3 changed files with 15 additions and 17 deletions

View File

@@ -1625,9 +1625,10 @@ uint8_t* Network::save_for_network(size_t& out_size, const std::vector<const Obj
const void* data = ms.GetData();
int32_t size = ms.GetLength();
uint8_t* compressed = util_zlib_deflate(static_cast<const uint8_t*>(data), size, &out_size);
if (compressed != nullptr)
auto compressed = util_zlib_deflate(static_cast<const uint8_t*>(data), size);
if (compressed != std::nullopt)
{
out_size = compressed->size();
header = reinterpret_cast<uint8_t*>(_strdup("open2_sv6_zlib"));
size_t header_len = strlen(reinterpret_cast<char*>(header)) + 1; // account for null terminator
header = static_cast<uint8_t*>(realloc(header, header_len + out_size));
@@ -1637,11 +1638,10 @@ uint8_t* Network::save_for_network(size_t& out_size, const std::vector<const Obj
}
else
{
std::memcpy(&header[header_len], compressed, out_size);
std::memcpy(&header[header_len], compressed->data(), out_size);
out_size += header_len;
log_verbose("Sending map of size %u bytes, compressed to %u bytes", size, out_size);
}
free(compressed);
}
else
{

View File

@@ -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;
}

View File

@@ -14,6 +14,8 @@
#include <cstdio>
#include <ctime>
#include <optional>
#include <vector>
int32_t squaredmetres_to_squaredfeet(int32_t squaredMetres);
int32_t metres_to_feet(int32_t metres);
@@ -52,7 +54,7 @@ bool str_is_null_or_empty(const char* str);
uint32_t util_rand();
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);
uint8_t* util_zlib_inflate(uint8_t* data, size_t data_in_size, size_t* data_out_size);
bool util_gzip_compress(FILE* source, FILE* dest);