]> Cypherpunks repositories - gostls13.git/commitdiff
compress/gzip, compress/zlib: add HuffmanOnly as compression levels.
authorKlaus Post <klauspost@gmail.com>
Sun, 16 Oct 2016 20:18:01 +0000 (22:18 +0200)
committerNigel Tao <nigeltao@golang.org>
Thu, 20 Oct 2016 23:11:05 +0000 (23:11 +0000)
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 <nigeltao@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/compress/gzip/gzip.go
src/compress/zlib/writer.go
src/compress/zlib/writer_test.go

index c702c493c11ef20861d49c531e59062011465224..c70321970b94838cbcfa8b8824104d753ba169c8 100644 (file)
@@ -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)
index 3b4313a8beaf56268629dc09664c4a3d3bcc3208..1620c00c523eb0adb1cd786368e455f6bc28dd41 100644 (file)
@@ -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
index dd941655206efa36d3b6b5be6b749e64676b04c6..d5019740783f16b4a5864d04b721489d43ba068b 100644 (file)
@@ -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
                }