Skip to content

Return length from StatelessDeflate #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This package provides various compression algorithms.

# changelog

* Feb 14, 2020: Return a length from [stateless deflate](https://pkg.go.dev/github.com/klauspost/compress/flate?tab=doc#StatelessDeflate) to indicate the bytes written. Breaking change.
* Feb 4, 2020: (v1.10.0) Add optional dictionary to [stateless deflate](https://pkg.go.dev/github.com/klauspost/compress/flate?tab=doc#StatelessDeflate). Breaking change, send `nil` for previous behaviour. [#216](https://github.com/klauspost/compress/pull/216)
* Feb 3, 2020: Fix buffer overflow on repeated small block deflate. [#218](https://github.com/klauspost/compress/pull/218)
* Jan 31, 2020: Allow copying content from an existing ZIP file without decompressing+compressing. [#214](https://github.com/klauspost/compress/pull/214)
Expand Down
4 changes: 2 additions & 2 deletions flate/flate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ func TestRegressions(t *testing.T) {
t.Run(tt.Name+"stateless", func(t *testing.T) {
// Split into two and use history...
buf := new(bytes.Buffer)
err = StatelessDeflate(buf, data1[:len(data1)/2], false, nil)
_, err = StatelessDeflate(buf, data1[:len(data1)/2], false, nil)
if err != nil {
t.Error(err)
}

// Use top half as dictionary...
dict := data1[:len(data1)/2]
err = StatelessDeflate(buf, data1[len(data1)/2:], true, dict)
_, err = StatelessDeflate(buf, data1[len(data1)/2:], true, dict)
if err != nil {
t.Error(err)
}
Expand Down
21 changes: 9 additions & 12 deletions flate/stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ func (s *statelessWriter) Close() error {
}
s.closed = true
// Emit EOF block
return StatelessDeflate(s.dst, nil, true, nil)
_, err := StatelessDeflate(s.dst, nil, true, nil)
return err
}

func (s *statelessWriter) Write(p []byte) (n int, err error) {
err = StatelessDeflate(s.dst, p, false, nil)
if err != nil {
return 0, err
}
return len(p), nil
func (s *statelessWriter) Write(p []byte) (int, error) {
return StatelessDeflate(s.dst, p, false, nil)
}

func (s *statelessWriter) Reset(w io.Writer) {
Expand Down Expand Up @@ -64,7 +61,7 @@ var bitWriterPool = sync.Pool{
// Up to 8KB of an optional dictionary can be given which is presumed to presumed to precede the block.
// Longer dictionaries will be truncated and will still produce valid output.
// Sending nil dictionary is perfectly fine.
func StatelessDeflate(out io.Writer, in []byte, eof bool, dict []byte) error {
func StatelessDeflate(out io.Writer, in []byte, eof bool, dict []byte) (int, error) {
var dst tokens
bw := bitWriterPool.Get().(*huffmanBitWriter)
bw.reset(out)
Expand All @@ -78,7 +75,7 @@ func StatelessDeflate(out io.Writer, in []byte, eof bool, dict []byte) error {
// Could be faster...
bw.writeStoredHeader(0, true)
bw.flush()
return bw.err
return 0, bw.err
}

// Truncate dict
Expand Down Expand Up @@ -108,7 +105,7 @@ func StatelessDeflate(out io.Writer, in []byte, eof bool, dict []byte) error {
if dst.n == 0 {
bw.writeStoredHeader(len(uncompressed), isEof)
if bw.err != nil {
return bw.err
return 0, bw.err
}
bw.writeBytes(uncompressed)
} else if int(dst.n) > len(uncompressed)-len(uncompressed)>>4 {
Expand All @@ -123,15 +120,15 @@ func StatelessDeflate(out io.Writer, in []byte, eof bool, dict []byte) error {
dst.Reset()
}
if bw.err != nil {
return bw.err
return 0, bw.err
}
}
if !eof {
// Align, only a stored block can do that.
bw.writeStoredHeader(0, false)
}
bw.flush()
return bw.err
return len(in), bw.err
}

func hashSL(u uint32) uint32 {
Expand Down
4 changes: 2 additions & 2 deletions gzip/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (z *Writer) Write(p []byte) (int, error) {
z.size += uint32(len(p))
z.digest = crc32.Update(z.digest, crc32.IEEETable, p)
if z.level == StatelessCompression {
return len(p), flate.StatelessDeflate(z.w, p, false, nil)
return flate.StatelessDeflate(z.w, p, false, nil)
}
n, z.err = z.compressor.Write(p)
return n, z.err
Expand Down Expand Up @@ -255,7 +255,7 @@ func (z *Writer) Close() error {
}
}
if z.level == StatelessCompression {
z.err = flate.StatelessDeflate(z.w, nil, true, nil)
_, z.err = flate.StatelessDeflate(z.w, nil, true, nil)
} else {
z.err = z.compressor.Close()
}
Expand Down