]> Cypherpunks repositories - gostls13.git/commitdiff
compress/flate: make compression level 0 consistent
authorKlaus Post <klauspost@gmail.com>
Sat, 15 Oct 2016 12:37:19 +0000 (14:37 +0200)
committerJoe Tsai <thebrokentoaster@gmail.com>
Thu, 27 Oct 2016 00:58:30 +0000 (00:58 +0000)
Tests for determinism was not working as intended since io.Copybuffer
uses the io.WriterTo if available.

This exposed that level 0 (no compression) changed output
based on the number of writes and buffers given to the
writer.

Previously, Write would emit a new raw block (BTYPE=00) for
every non-empty call to Write.

This CL fixes it such that a raw block is only emitted upon
the following conditions:
  * A full window is obtained (every 65535 bytes)
  * Flush is called
  * Close is called

Change-Id: I807f866d97e2db7820f11febab30a96266a6cbf1
Reviewed-on: https://go-review.googlesource.com/31174
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
src/compress/flate/deflate.go
src/compress/flate/writer_test.go

index 7a805235d21bf3d7b4020e35c599e0c928ab82e2..97265b3ca27ad091af42e7bf2ff120dd5e54f277 100644 (file)
@@ -521,10 +521,10 @@ func (d *compressor) fillStore(b []byte) int {
 }
 
 func (d *compressor) store() {
-       if d.windowEnd > 0 {
+       if d.windowEnd > 0 && (d.windowEnd == maxStoreBlockSize || d.sync) {
                d.err = d.writeStoredBlock(d.window[:d.windowEnd])
+               d.windowEnd = 0
        }
-       d.windowEnd = 0
 }
 
 // storeHuff compresses and stores the currently added data
index 21cd0b22eef5fe6e6185635f74de712c82b59fd9..68de48b98f6a80b5ec6a829096377aa48cc8a9b1 100644 (file)
@@ -75,7 +75,7 @@ func TestWriteError(t *testing.T) {
                        if err != nil {
                                t.Fatalf("NewWriter: level %d: %v", l, err)
                        }
-                       n, err := io.CopyBuffer(w, bytes.NewBuffer(in), copyBuffer)
+                       n, err := io.CopyBuffer(w, struct{ io.Reader }{bytes.NewBuffer(in)}, copyBuffer)
                        if err == nil {
                                t.Fatalf("Level %d: Expected an error, writer was %#v", l, ew)
                        }
@@ -142,7 +142,7 @@ func testDeterministic(i int, t *testing.T) {
        }
        // Use a very small prime sized buffer.
        cbuf := make([]byte, 787)
-       _, err = io.CopyBuffer(w, br, cbuf)
+       _, err = io.CopyBuffer(w, struct{ io.Reader }{br}, cbuf)
        if err != nil {
                t.Fatal(err)
        }
@@ -157,7 +157,7 @@ func testDeterministic(i int, t *testing.T) {
        if err != nil {
                t.Fatal(err)
        }
-       _, err = io.CopyBuffer(w2, br2, cbuf)
+       _, err = io.CopyBuffer(w2, struct{ io.Reader }{br2}, cbuf)
        if err != nil {
                t.Fatal(err)
        }