hyperxpro/Brotli4j

Unable to compress correctly if input data does not start from 0.

deshanxiao opened this issue · 5 comments

Describe the bug
Unable to compress correctly if input data does not start from 0.

To Reproduce
Call Encoder.compress(byte[] x, int offset, int length) will compress all bytes from x (offset > 0)

encoder.push(EncoderJNI.Operation.FINISH, data.length);

data.length -> length ?

Expected behavior
It should only compress the bytes from start to start + len.

Platform (please complete the following information):
ubuntu

Additional context

What is the error? Are you getting corrupted data?

Technically, you should compress all data and then finish the encoding with length 0.

I got no error but the output array looks unexpected in length. Here is a very simple example: @hyperxpro

public class Main2 {
    static {
        Brotli4jLoader.ensureAvailability();
    }

    public static void main(String[] args) throws IOException {
        byte[] totalBytes = "abcdefg".getBytes();
        // compress "defg"
        byte[] compressedBytes = Encoder.compress(totalBytes, 3, 4);
        // len(output) == 7 not 4
        byte[] output = Decoder.decompress(compressedBytes, 0, compressedBytes.length);
    }
}

Okay, looks like a bug.

Would you like to raise a PR at https://github.com/google/brotli?