From: Klaus Post Date: Sun, 16 Oct 2016 20:18:01 +0000 (+0200) Subject: compress/gzip, compress/zlib: add HuffmanOnly as compression levels. X-Git-Tag: go1.8beta1~733 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c9517b1ffe7c99a3fd2a748fab51645bd674ad75;p=gostls13.git compress/gzip, compress/zlib: add HuffmanOnly as compression levels. This exposes HuffmanOnly in zlib and gzip packages, which is currently unavailable. Change-Id: If5d103bbc8b5fce2f5d740fd103a235c5d1ed7cd Reviewed-on: https://go-review.googlesource.com/31186 Reviewed-by: Nigel Tao Reviewed-by: Joe Tsai Run-TryBot: Joe Tsai TryBot-Result: Gobot Gobot --- diff --git a/src/compress/gzip/gzip.go b/src/compress/gzip/gzip.go index c702c493c1..c70321970b 100644 --- a/src/compress/gzip/gzip.go +++ b/src/compress/gzip/gzip.go @@ -19,6 +19,7 @@ const ( BestSpeed = flate.BestSpeed BestCompression = flate.BestCompression DefaultCompression = flate.DefaultCompression + HuffmanOnly = flate.HuffmanOnly ) // A Writer is an io.WriteCloser. @@ -52,11 +53,11 @@ func NewWriter(w io.Writer) *Writer { // NewWriterLevel is like NewWriter but specifies the compression level instead // of assuming DefaultCompression. // -// The compression level can be DefaultCompression, NoCompression, or any -// integer value between BestSpeed and BestCompression inclusive. The error -// returned will be nil if the level is valid. +// The compression level can be DefaultCompression, NoCompression, HuffmanOnly +// or any integer value between BestSpeed and BestCompression inclusive. +// The error returned will be nil if the level is valid. func NewWriterLevel(w io.Writer, level int) (*Writer, error) { - if level < DefaultCompression || level > BestCompression { + if level < HuffmanOnly || level > BestCompression { return nil, fmt.Errorf("gzip: invalid compression level: %d", level) } z := new(Writer) diff --git a/src/compress/zlib/writer.go b/src/compress/zlib/writer.go index 3b4313a8be..1620c00c52 100644 --- a/src/compress/zlib/writer.go +++ b/src/compress/zlib/writer.go @@ -19,6 +19,7 @@ const ( BestSpeed = flate.BestSpeed BestCompression = flate.BestCompression DefaultCompression = flate.DefaultCompression + HuffmanOnly = flate.HuffmanOnly ) // A Writer takes data written to it and writes the compressed @@ -47,9 +48,9 @@ func NewWriter(w io.Writer) *Writer { // NewWriterLevel is like NewWriter but specifies the compression level instead // of assuming DefaultCompression. // -// The compression level can be DefaultCompression, NoCompression, or any -// integer value between BestSpeed and BestCompression inclusive. The error -// returned will be nil if the level is valid. +// The compression level can be DefaultCompression, NoCompression, HuffmanOnly +// or any integer value between BestSpeed and BestCompression inclusive. +// The error returned will be nil if the level is valid. func NewWriterLevel(w io.Writer, level int) (*Writer, error) { return NewWriterLevelDict(w, level, nil) } @@ -60,7 +61,7 @@ func NewWriterLevel(w io.Writer, level int) (*Writer, error) { // The dictionary may be nil. If not, its contents should not be modified until // the Writer is closed. func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error) { - if level < DefaultCompression || level > BestCompression { + if level < HuffmanOnly || level > BestCompression { return nil, fmt.Errorf("zlib: invalid compression level: %d", level) } return &Writer{ @@ -99,7 +100,7 @@ func (z *Writer) writeHeader() (err error) { // The next bit, FDICT, is set if a dictionary is given. // The final five FCHECK bits form a mod-31 checksum. switch z.level { - case 0, 1: + case -2, 0, 1: z.scratch[1] = 0 << 6 case 2, 3, 4, 5: z.scratch[1] = 1 << 6 diff --git a/src/compress/zlib/writer_test.go b/src/compress/zlib/writer_test.go index dd94165520..d501974078 100644 --- a/src/compress/zlib/writer_test.go +++ b/src/compress/zlib/writer_test.go @@ -147,6 +147,7 @@ func TestWriter(t *testing.T) { tag := fmt.Sprintf("#%d", i) testLevelDict(t, tag, b, DefaultCompression, "") testLevelDict(t, tag, b, NoCompression, "") + testLevelDict(t, tag, b, HuffmanOnly, "") for level := BestSpeed; level <= BestCompression; level++ { testLevelDict(t, tag, b, level, "") } @@ -157,6 +158,7 @@ func TestWriterBig(t *testing.T) { for i, fn := range filenames { testFileLevelDict(t, fn, DefaultCompression, "") testFileLevelDict(t, fn, NoCompression, "") + testFileLevelDict(t, fn, HuffmanOnly, "") for level := BestSpeed; level <= BestCompression; level++ { testFileLevelDict(t, fn, level, "") if level >= 1 && testing.Short() && testenv.Builder() == "" { @@ -174,6 +176,7 @@ func TestWriterDict(t *testing.T) { for i, fn := range filenames { testFileLevelDict(t, fn, DefaultCompression, dictionary) testFileLevelDict(t, fn, NoCompression, dictionary) + testFileLevelDict(t, fn, HuffmanOnly, dictionary) for level := BestSpeed; level <= BestCompression; level++ { testFileLevelDict(t, fn, level, dictionary) if level >= 1 && testing.Short() && testenv.Builder() == "" { @@ -191,8 +194,10 @@ func TestWriterReset(t *testing.T) { for _, fn := range filenames { testFileLevelDictReset(t, fn, NoCompression, nil) testFileLevelDictReset(t, fn, DefaultCompression, nil) + testFileLevelDictReset(t, fn, HuffmanOnly, nil) testFileLevelDictReset(t, fn, NoCompression, []byte(dictionary)) testFileLevelDictReset(t, fn, DefaultCompression, []byte(dictionary)) + testFileLevelDictReset(t, fn, HuffmanOnly, []byte(dictionary)) if testing.Short() { break }