Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/inflate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,12 @@ export class Inflate {
if (e !== UNDERFLOW) throw e;
this.ip = ip; this.bitbuf = buf; this.bitcnt = cnt;
}
if (this.stage === Stage.Done) {
// whole bytes still buffered belong to whatever follows the stream
this.ip -= this.bitcnt >> 3;
this.bitbuf = this.bitcnt = 0;
}
// give back whole buffered bytes, as zlib does, so a caller's remaining count stays exact
const back = Math.min(this.bitcnt >> 3, this.ip);
this.ip -= back;
this.bitcnt -= back << 3;
this.bitbuf &= (1 << this.bitcnt) - 1;
if (this.stage === Stage.Done) this.bitbuf = this.bitcnt = 0;
this.finishCall();
return [this.ip, this.op - off];
}
Expand Down
19 changes: 19 additions & 0 deletions test/zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ test("inflate stops at the end of the stream and leaves the bytes after it", ()
assert.equal(read, packed.length);
});

test("inflate reads exactly the stream when the output is small", () => {
const data = sample(300, 14);
const packed = deflateRawSync(data);
const input = Buffer.concat([packed, Buffer.from([1, 2, 3, 4, 5])]);
for (const size of [1, 2, 3, 4, 5, 8]) {
const z = new Inflate(true);
const out = new Uint8Array(size);
let pos = 0, total = 0;
while (!z.finished) {
const [read, written] = z.run(input.subarray(pos), out, 0, size);
assert.ok(read > 0 || written > 0, `no progress at size ${size}`);
pos += read;
total += written;
}
assert.equal(total, data.length);
assert.equal(pos, packed.length, `size ${size}`);
}
});

test("inflate sees the end of the stream when the output fills on the last byte", () => {
for (const wrap of [true, false]) {
for (const size of [1, 10, 5000]) {
Expand Down
Loading