mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use vector instead of manual memory management for decompressing text files
parent
d074ab909c
commit
9fe9e4d398
|
@ -654,54 +654,38 @@ void TextfileWindow::ScrollToLine(size_t line)
|
||||||
/**
|
/**
|
||||||
* Do an in-memory gunzip operation. This works on a raw deflate stream,
|
* Do an in-memory gunzip operation. This works on a raw deflate stream,
|
||||||
* or a file with gzip or zlib header.
|
* or a file with gzip or zlib header.
|
||||||
* @param bufp A pointer to a buffer containing the input data. This
|
* @param input Buffer containing the input data.
|
||||||
* buffer will be freed and replaced by a buffer containing
|
* @return Decompressed buffer.
|
||||||
* the uncompressed data.
|
|
||||||
* @param sizep A pointer to the buffer size. Before the call, the value
|
|
||||||
* pointed to should contain the size of the input buffer.
|
|
||||||
* After the call, it contains the size of the uncompressed
|
|
||||||
* data.
|
|
||||||
*
|
*
|
||||||
* When decompressing fails, *bufp is set to nullptr and *sizep to 0. The
|
* When decompressing fails, an empty buffer is returned.
|
||||||
* compressed buffer passed in is still freed in this case.
|
|
||||||
*/
|
*/
|
||||||
static void Gunzip(uint8_t **bufp, size_t *sizep)
|
static std::vector<char> Gunzip(std::span<char> input)
|
||||||
{
|
{
|
||||||
static const int BLOCKSIZE = 8192;
|
static const int BLOCKSIZE = 8192;
|
||||||
uint8_t *buf = nullptr;
|
std::vector<char> output;
|
||||||
size_t alloc_size = 0;
|
|
||||||
z_stream z;
|
|
||||||
int res;
|
|
||||||
|
|
||||||
|
z_stream z;
|
||||||
memset(&z, 0, sizeof(z));
|
memset(&z, 0, sizeof(z));
|
||||||
z.next_in = *bufp;
|
z.next_in = reinterpret_cast<Bytef *>(input.data());
|
||||||
z.avail_in = (uInt)*sizep;
|
z.avail_in = static_cast<uInt>(input.size());
|
||||||
|
|
||||||
/* window size = 15, add 32 to enable gzip or zlib header processing */
|
/* window size = 15, add 32 to enable gzip or zlib header processing */
|
||||||
res = inflateInit2(&z, 15 + 32);
|
int res = inflateInit2(&z, 15 + 32);
|
||||||
/* Z_BUF_ERROR just means we need more space */
|
/* Z_BUF_ERROR just means we need more space */
|
||||||
while (res == Z_OK || (res == Z_BUF_ERROR && z.avail_out == 0)) {
|
while (res == Z_OK || (res == Z_BUF_ERROR && z.avail_out == 0)) {
|
||||||
/* When we get here, we're either just starting, or
|
/* When we get here, we're either just starting, or
|
||||||
* inflate is out of output space - allocate more */
|
* inflate is out of output space - allocate more */
|
||||||
alloc_size += BLOCKSIZE;
|
|
||||||
z.avail_out += BLOCKSIZE;
|
z.avail_out += BLOCKSIZE;
|
||||||
buf = ReallocT(buf, alloc_size);
|
output.resize(output.size() + BLOCKSIZE);
|
||||||
z.next_out = buf + alloc_size - z.avail_out;
|
z.next_out = reinterpret_cast<Bytef *>(&*output.end() - z.avail_out);
|
||||||
res = inflate(&z, Z_FINISH);
|
res = inflate(&z, Z_FINISH);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(*bufp);
|
|
||||||
inflateEnd(&z);
|
inflateEnd(&z);
|
||||||
|
if (res != Z_STREAM_END) return {};
|
||||||
|
|
||||||
if (res == Z_STREAM_END) {
|
output.resize(output.size() - z.avail_out);
|
||||||
*bufp = buf;
|
return output;
|
||||||
*sizep = alloc_size - z.avail_out;
|
|
||||||
} else {
|
|
||||||
/* Something went wrong */
|
|
||||||
*bufp = nullptr;
|
|
||||||
*sizep = 0;
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -710,52 +694,36 @@ static void Gunzip(uint8_t **bufp, size_t *sizep)
|
||||||
/**
|
/**
|
||||||
* Do an in-memory xunzip operation. This works on a .xz or (legacy)
|
* Do an in-memory xunzip operation. This works on a .xz or (legacy)
|
||||||
* .lzma file.
|
* .lzma file.
|
||||||
* @param bufp A pointer to a buffer containing the input data. This
|
* @param input Buffer containing the input data.
|
||||||
* buffer will be freed and replaced by a buffer containing
|
* @return Decompressed buffer.
|
||||||
* the uncompressed data.
|
|
||||||
* @param sizep A pointer to the buffer size. Before the call, the value
|
|
||||||
* pointed to should contain the size of the input buffer.
|
|
||||||
* After the call, it contains the size of the uncompressed
|
|
||||||
* data.
|
|
||||||
*
|
*
|
||||||
* When decompressing fails, *bufp is set to nullptr and *sizep to 0. The
|
* When decompressing fails, an empty buffer is returned.
|
||||||
* compressed buffer passed in is still freed in this case.
|
|
||||||
*/
|
*/
|
||||||
static void Xunzip(uint8_t **bufp, size_t *sizep)
|
static std::vector<char> Xunzip(std::span<char> input)
|
||||||
{
|
{
|
||||||
static const int BLOCKSIZE = 8192;
|
static const int BLOCKSIZE = 8192;
|
||||||
uint8_t *buf = nullptr;
|
std::vector<char> output;
|
||||||
size_t alloc_size = 0;
|
|
||||||
lzma_stream z = LZMA_STREAM_INIT;
|
|
||||||
int res;
|
|
||||||
|
|
||||||
z.next_in = *bufp;
|
lzma_stream z = LZMA_STREAM_INIT;
|
||||||
z.avail_in = *sizep;
|
z.next_in = reinterpret_cast<uint8_t *>(input.data());
|
||||||
|
z.avail_in = input.size();
|
||||||
|
|
||||||
res = lzma_auto_decoder(&z, UINT64_MAX, LZMA_CONCATENATED);
|
int res = lzma_auto_decoder(&z, UINT64_MAX, LZMA_CONCATENATED);
|
||||||
/* Z_BUF_ERROR just means we need more space */
|
/* Z_BUF_ERROR just means we need more space */
|
||||||
while (res == LZMA_OK || (res == LZMA_BUF_ERROR && z.avail_out == 0)) {
|
while (res == LZMA_OK || (res == LZMA_BUF_ERROR && z.avail_out == 0)) {
|
||||||
/* When we get here, we're either just starting, or
|
/* When we get here, we're either just starting, or
|
||||||
* inflate is out of output space - allocate more */
|
* inflate is out of output space - allocate more */
|
||||||
alloc_size += BLOCKSIZE;
|
|
||||||
z.avail_out += BLOCKSIZE;
|
z.avail_out += BLOCKSIZE;
|
||||||
buf = ReallocT(buf, alloc_size);
|
output.resize(output.size() + BLOCKSIZE);
|
||||||
z.next_out = buf + alloc_size - z.avail_out;
|
z.next_out = reinterpret_cast<uint8_t *>(&*output.end() - z.avail_out);
|
||||||
res = lzma_code(&z, LZMA_FINISH);
|
res = lzma_code(&z, LZMA_FINISH);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(*bufp);
|
|
||||||
lzma_end(&z);
|
lzma_end(&z);
|
||||||
|
if (res != LZMA_STREAM_END) return {};
|
||||||
|
|
||||||
if (res == LZMA_STREAM_END) {
|
output.resize(output.size() - z.avail_out);
|
||||||
*bufp = buf;
|
return output;
|
||||||
*sizep = alloc_size - z.avail_out;
|
|
||||||
} else {
|
|
||||||
/* Something went wrong */
|
|
||||||
*bufp = nullptr;
|
|
||||||
*sizep = 0;
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -779,28 +747,26 @@ static void Xunzip(uint8_t **bufp, size_t *sizep)
|
||||||
/* Early return on empty files. */
|
/* Early return on empty files. */
|
||||||
if (filesize == 0) return;
|
if (filesize == 0) return;
|
||||||
|
|
||||||
char *buf = MallocT<char>(filesize);
|
std::vector<char> buf;
|
||||||
size_t read = fread(buf, 1, filesize, handle);
|
buf.resize(filesize);
|
||||||
|
size_t read = fread(buf.data(), 1, buf.size(), handle);
|
||||||
fclose(handle);
|
fclose(handle);
|
||||||
|
|
||||||
if (read != filesize) {
|
if (read != buf.size()) return;
|
||||||
free(buf);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(WITH_ZLIB)
|
#if defined(WITH_ZLIB)
|
||||||
/* In-place gunzip */
|
/* In-place gunzip */
|
||||||
if (textfile.ends_with(".gz")) Gunzip((uint8_t**)&buf, &filesize);
|
if (textfile.ends_with(".gz")) buf = Gunzip(buf);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(WITH_LIBLZMA)
|
#if defined(WITH_LIBLZMA)
|
||||||
/* In-place xunzip */
|
/* In-place xunzip */
|
||||||
if (textfile.ends_with(".xz")) Xunzip((uint8_t**)&buf, &filesize);
|
if (textfile.ends_with(".xz")) buf = Xunzip(buf);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (buf == nullptr) return;
|
if (buf.empty()) return;
|
||||||
|
|
||||||
std::string_view sv_buf(buf, filesize);
|
std::string_view sv_buf(buf.data(), buf.size());
|
||||||
|
|
||||||
/* Check for the byte-order-mark, and skip it if needed. */
|
/* Check for the byte-order-mark, and skip it if needed. */
|
||||||
if (sv_buf.starts_with("\ufeff")) sv_buf.remove_prefix(3);
|
if (sv_buf.starts_with("\ufeff")) sv_buf.remove_prefix(3);
|
||||||
|
@ -813,7 +779,6 @@ static void Xunzip(uint8_t **bufp, size_t *sizep)
|
||||||
|
|
||||||
/* Process the loaded text into lines, and do any further parsing needed. */
|
/* Process the loaded text into lines, and do any further parsing needed. */
|
||||||
this->LoadText(sv_buf);
|
this->LoadText(sv_buf);
|
||||||
free(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue