kspalaiologos / bzip3

A better and stronger spiritual successor to BZip2.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to decompress original file with length between 57 and 64

vcoracolombo opened this issue · comments

Hello

There seems to be a logic error in the compress/decompress code that can lead to failure do decompress a file if it has between 57 and 64 bytes.

How to reproduce:

$ echo -ne "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" > /tmp/a
$ bzip3 -e /tmp/a /tmp/a.bz3
$ bzip3 -d /tmp/a.bz3 -c
Failed to decode a block: Malformed header

Cause

I'm not 100% sure how to fix this correctly, but I think the logic issue is in the decompression.
During compression, it checks in bz3_encode_block if the original size is less than 64. If so, it writes a special value -1 and returns data_size + 8. This result is then saved in the bz3 file.
When the file is being decompressed, in bz3_decode_block, this value is checked to see if is greater than 64. See that when compressing the logic tested if the original was less than 64, but in decompression the new size is tested. I think that it should test the old size (orig_size). Otherwise a block with e.g. size 60 will add to 68 in data_size and fail the check.