101arrowz / fflate

High performance (de)compression in an 8kB package
https://101arrowz.github.io/fflate
MIT License
2.21k stars 77 forks source link

Report the offset at which "Unexpected EOF" error occured #221

Open LorisSigrist opened 2 weeks ago

LorisSigrist commented 2 weeks ago

What can't you do right now? I'm trying to process a buffer with sections that are compressed with deflate. I know where these sections start, but have no idea how long they are. I need to skip past these sections so I can continue processing the buffer.

If there were a way to know at which offset EOF happened I would be able to skip past these sections.

Example Buffer:

| plaintext data I want to read | deflated data of unknown length | plaintext data I want to read  | 

An optimal solution Two possibilities:

How is this done by other libraries? pako (which I'm currently using) doesn't fail when hitting the EOF. Instead it emits the decompressed data & has a value you can read the tells you how many bytes have remained unprocessed.

Eg. If you have a 150byte buffer and the first 100 are deflated-data, it will emit the decompressed data & tell you there are 50 bytes remaining.

101arrowz commented 1 week ago

The last time I tried something like this fflate just decompresses up to the end of the DEFLATE stream and no further, meaning it wouldn't provide an EOF error at all. I might be mistaken or maybe you're doing something differently than I'm imagining - could you send a code snippet I can use to reproduce the EOF error?

Regardless, you are right that there isn't a way to figure out how many compressed bytes have been read after decompression completes. This should be addressable in the streaming API without much difficulty, but it will not be added to the standard buffer-based API (to keep that API simple and bloat-free). Will look into it for the next release.