Skip to content

Commit

Permalink
do_lzx_decompress: handle uncompressed chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
maharmstone committed Jan 5, 2021
1 parent 83f2100 commit c346ade
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/decomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,31 @@ string do_lzx_decompress(const string_view& compdata, uint64_t size) {

ret.resize(size);

auto data = string_view(compdata.data() + ((num_chunks - 1) * sizeof(uint32_t)),
compdata.length() - ((num_chunks - 1) * sizeof(uint32_t)));

for (uint64_t i = 0; i < num_chunks; i++) {
uint64_t off = (num_chunks - 1) * sizeof(uint32_t);
if (i != 0)
off += offsets[i - 1];
uint64_t off = i == 0 ? 0 : offsets[i - 1];
uint64_t complen;

if (i == 0)
complen = num_chunks > 1 ? offsets[0] : data.length();
else if (i == num_chunks - 1)
complen = data.length() - offsets[i - 1];
else
complen = offsets[i] - offsets[i - 1];

auto err = lzx_decompress(ctx, compdata.data() + off, compdata.length() - off, ret.data() + (i * LZX_CHUNK_SIZE),
i == num_chunks - 1 ? (ret.length() - (i * LZX_CHUNK_SIZE)) : LZX_CHUNK_SIZE);
if (complen == (i == num_chunks - 1 ? (ret.length() - (i * LZX_CHUNK_SIZE)) : LZX_CHUNK_SIZE)) {
// stored uncompressed
memcpy(ret.data() + (i * LZX_CHUNK_SIZE), data.data() + off, complen);
} else {
auto err = lzx_decompress(ctx, data.data() + off, complen, ret.data() + (i * LZX_CHUNK_SIZE),
i == num_chunks - 1 ? (ret.length() - (i * LZX_CHUNK_SIZE)) : LZX_CHUNK_SIZE);

if (err != 0) {
lzx_free_decompressor(ctx);
throw formatted_error(FMT_STRING("lzx_decompress returned {}."), err);
if (err != 0) {
lzx_free_decompressor(ctx);
throw formatted_error(FMT_STRING("lzx_decompress returned {}."), err);
}
}
}

Expand Down

0 comments on commit c346ade

Please sign in to comment.