diff --git a/src/hx/zip/zlib/ZLibCompress.cpp b/src/hx/zip/zlib/ZLibCompress.cpp index 1d11148ac..9774d2f77 100644 --- a/src/hx/zip/zlib/ZLibCompress.cpp +++ b/src/hx/zip/zlib/ZLibCompress.cpp @@ -81,11 +81,12 @@ hx::zip::Result hx::zip::zlib::ZLibCompress::execute(cpp::marshal::View hx::Throw(HX_CSTRING("ZLib Error")); } + // Per-call counts, as haxe.zip expects; total_in/total_out accumulate across calls. return Result( error == Z_STREAM_END, - static_cast(handle->total_in), - static_cast(handle->total_out)); + static_cast(src.length - handle->avail_in), + static_cast(dst.length - handle->avail_out)); } void hx::zip::zlib::ZLibCompress::setFlushMode(Flush mode) diff --git a/src/hx/zip/zlib/ZLibUncompress.cpp b/src/hx/zip/zlib/ZLibUncompress.cpp index 0ddc93706..f99c99cd2 100644 --- a/src/hx/zip/zlib/ZLibUncompress.cpp +++ b/src/hx/zip/zlib/ZLibUncompress.cpp @@ -85,11 +85,12 @@ hx::zip::Result hx::zip::zlib::ZLibUncompress::execute(cpp::marshal::View(handle->total_in), - static_cast(handle->total_out)); + static_cast(src.length - handle->avail_in), + static_cast(dst.length - handle->avail_out)); } void hx::zip::zlib::ZLibUncompress::setFlushMode(Flush mode) diff --git a/test/std/Test.hx b/test/std/Test.hx index 4923bfce3..28ce97bcc 100644 --- a/test/std/Test.hx +++ b/test/std/Test.hx @@ -154,6 +154,37 @@ class Test extends utest.Test Assert.equals(0, decompressed.compare(bytes), "Compress/Uncompress mismatch"); } + function testCompressMultipleBuffers() + { + log("Test compress/uncompress across multiple buffers"); + // Larger than the 64K default buffer so execute is called more than once. + final bytes = haxe.io.Bytes.alloc(300000); + for(i in 0...bytes.length) + bytes.set(i, (i * 7 + (i >> 9)) & 0xff); + + final compressed = Compress.run(bytes, 9); + final decompressed = Uncompress.run(compressed); + Assert.equals(bytes.length, decompressed.length, "Uncompress length"); + Assert.equals(0, decompressed.compare(bytes), "Uncompress mismatch"); + + final uncompress = new Uncompress(); + final chunk = haxe.io.Bytes.alloc(4096); + final output = new haxe.io.BytesBuffer(); + var pos = 0; + while(true) + { + final r = uncompress.execute(compressed, pos, chunk, 0); + Assert.isTrue(r.write <= chunk.length, "Per-call write exceeds buffer"); + output.addBytes(chunk, 0, r.write); + pos += r.read; + if (r.done) + break; + } + uncompress.close(); + Assert.equals(compressed.length, pos, "Per-call reads sum to input"); + Assert.equals(0, output.getBytes().compare(bytes), "Chunked uncompress mismatch"); + } + function testRegexp() {