diff options
| author | 2025-11-22 08:00:00 +0800 | |
|---|---|---|
| committer | 2025-11-22 08:00:00 +0800 | |
| commit | 9b453a7cca6bb258a8ca939dc9696fabd77b1b7c (patch) | |
| tree | 27f28df16a5f00f79021f9bd4d6d540435c9d2f8 /internal/zlibx | |
| parent | flatex: Remove the stale readByte(s) wrappers and just directly index the buffer (diff) | |
| signature | No signature | |
zlib, flatex: Remove code related to dicts
Git never uses them
Diffstat (limited to 'internal/zlibx')
| -rw-r--r-- | internal/zlibx/decompress.go | 34 | ||||
| -rw-r--r-- | internal/zlibx/decompress_test.go | 49 | ||||
| -rw-r--r-- | internal/zlibx/reader.go | 38 |
3 files changed, 15 insertions, 106 deletions
diff --git a/internal/zlibx/decompress.go b/internal/zlibx/decompress.go index 68a92587..df4a34be 100644 --- a/internal/zlibx/decompress.go +++ b/internal/zlibx/decompress.go @@ -9,28 +9,11 @@ import ( "git.sr.ht/~runxiyu/furgit/internal/flatex" ) -// Decompress inflates the provided zlib wrapped stream and returns the -// uncompressed data inside a pooled bufpool.Buffer. func Decompress(src []byte) (*bufpool.Buffer, error) { return DecompressSized(src, 0) } -// DecompressSized inflates the provided zlib stream, using sizeHint to -// preallocate the output buffer when known (e.g. packfile entries). func DecompressSized(src []byte, sizeHint int) (*bufpool.Buffer, error) { - return DecompressDictSized(src, nil, sizeHint) -} - -// DecompressDict is like Decompress but accepts a preset dictionary. The -// dictionary must match the checksum embedded in the stream if the dictionary -// flag is present. -func DecompressDict(src []byte, dict []byte) (*bufpool.Buffer, error) { - return DecompressDictSized(src, dict, 0) -} - -// DecompressDictSized is like DecompressDict but allows providing an expected -// uncompressed size to avoid buffer growth copies. -func DecompressDictSized(src []byte, dict []byte, sizeHint int) (*bufpool.Buffer, error) { if len(src) < 6 { return nil, io.ErrUnexpectedEOF } @@ -42,19 +25,8 @@ func DecompressDictSized(src []byte, dict []byte, sizeHint int) (*bufpool.Buffer } offset := 2 - haveDict := flg&0x20 != 0 - if haveDict { - if len(src) < offset+4 { - return nil, io.ErrUnexpectedEOF - } - if dict == nil { - return nil, ErrDictionary - } - checksum := binary.BigEndian.Uint32(src[offset : offset+4]) - if checksum != adler32.Checksum(dict) { - return nil, ErrDictionary - } - offset += 4 + if flg&0x20 != 0 { + return nil, ErrHeader } if len(src[offset:]) < 4 { @@ -62,7 +34,7 @@ func DecompressDictSized(src []byte, dict []byte, sizeHint int) (*bufpool.Buffer } deflateData := src[offset:] - out, consumed, err := flatex.DecompressDictSized(deflateData, dict, sizeHint) + out, consumed, err := flatex.DecompressSized(deflateData, sizeHint) if err != nil { return nil, err } diff --git a/internal/zlibx/decompress_test.go b/internal/zlibx/decompress_test.go index 3dfc07a5..8dcc6c02 100644 --- a/internal/zlibx/decompress_test.go +++ b/internal/zlibx/decompress_test.go @@ -6,21 +6,10 @@ import ( "testing" ) -func compressZlib(t *testing.T, payload, dict []byte) []byte { +func compressZlib(t *testing.T, payload []byte) []byte { t.Helper() var buf bytes.Buffer - var ( - w *stdzlib.Writer - err error - ) - if dict != nil { - w, err = stdzlib.NewWriterLevelDict(&buf, stdzlib.DefaultCompression, dict) - } else { - w = stdzlib.NewWriter(&buf) - } - if err != nil { - t.Fatalf("NewWriter: %v", err) - } + w := stdzlib.NewWriter(&buf) if _, err := w.Write(payload); err != nil { t.Fatalf("Write: %v", err) } @@ -32,7 +21,7 @@ func compressZlib(t *testing.T, payload, dict []byte) []byte { func TestDecompress(t *testing.T) { payload := []byte("hello, zlib world!") - compressed := compressZlib(t, payload, nil) + compressed := compressZlib(t, payload) out, err := Decompress(compressed) if err != nil { @@ -45,37 +34,9 @@ func TestDecompress(t *testing.T) { } } -func TestDecompressDict(t *testing.T) { - dict := []byte("git dictionary for zlib") - payload := append([]byte(nil), dict...) - payload = append(payload, []byte(" -- extended body -- extended body")...) - compressed := compressZlib(t, payload, dict) - - out, err := DecompressDict(compressed, dict) - if err != nil { - t.Fatalf("DecompressDict: %v", err) - } - defer out.Release() - - if !bytes.Equal(out.Bytes(), payload) { - t.Fatalf("unexpected payload %q", out.Bytes()) - } -} - -func TestDecompressDictMissing(t *testing.T) { - dict := []byte("preset dictionary") - payload := append([]byte(nil), dict...) - payload = append(payload, []byte(" .. more data ..")...) - compressed := compressZlib(t, payload, dict) - - if _, err := Decompress(compressed); err != ErrDictionary { - t.Fatalf("expected ErrDictionary, got %v", err) - } -} - func TestDecompressChecksumError(t *testing.T) { payload := []byte("checksum check") - compressed := compressZlib(t, payload, nil) + compressed := compressZlib(t, payload) compressed[len(compressed)-1] ^= 0xff if _, err := Decompress(compressed); err != ErrChecksum { @@ -85,7 +46,7 @@ func TestDecompressChecksumError(t *testing.T) { func TestDecompressSizedUsesHint(t *testing.T) { payload := []byte("tiny payload") - compressed := compressZlib(t, payload, nil) + compressed := compressZlib(t, payload) const hint = 1 << 20 out, err := DecompressSized(compressed, hint) diff --git a/internal/zlibx/reader.go b/internal/zlibx/reader.go index 9a4b4315..6e3a53ea 100644 --- a/internal/zlibx/reader.go +++ b/internal/zlibx/reader.go @@ -55,8 +55,6 @@ const ( var ( // ErrChecksum is returned when reading ZLIB data that has an invalid checksum. ErrChecksum = errors.New("zlib: invalid checksum") - // ErrDictionary is returned when reading ZLIB data that has an invalid dictionary. - ErrDictionary = errors.New("zlib: invalid dictionary") // ErrHeader is returned when reading ZLIB data that has an invalid header. ErrHeader = errors.New("zlib: invalid header") ) @@ -82,19 +80,12 @@ type reader struct { // data than necessary from r. // It is the caller's responsibility to call Close on the ReadCloser when done. func NewReader(r io.Reader) (io.ReadCloser, error) { - return NewReaderDict(r, nil) -} - -// NewReaderDict is like [NewReader] but uses a preset dictionary. -// NewReaderDict ignores the dictionary if the compressed data does not refer to it. -// If the compressed data refers to a different dictionary, NewReaderDict returns [ErrDictionary]. -func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error) { v := pool.Get() z, ok := v.(*reader) if !ok { panic("zlib: pool returned unexpected type") } - err := z.Reset(r, dict) + err := z.Reset(r) if err != nil { return nil, err } @@ -147,7 +138,7 @@ func (z *reader) Close() error { return nil } -func (z *reader) Reset(r io.Reader, dict []byte) error { +func (z *reader) Reset(r io.Reader) error { *z = reader{decompressor: z.decompressor} if fr, ok := r.(flatex.Reader); ok { z.r = fr @@ -168,30 +159,15 @@ func (z *reader) Reset(r io.Reader, dict []byte) error { z.err = ErrHeader return z.err } - haveDict := z.scratch[1]&0x20 != 0 - if haveDict { - _, z.err = io.ReadFull(z.r, z.scratch[0:4]) - if z.err != nil { - if z.err == io.EOF { - z.err = io.ErrUnexpectedEOF - } - return z.err - } - checksum := binary.BigEndian.Uint32(z.scratch[:4]) - if checksum != adler32.Checksum(dict) { - z.err = ErrDictionary - return z.err - } + if z.scratch[1]&0x20 != 0 { + z.err = ErrHeader + return z.err } if z.decompressor == nil { - if haveDict { - z.decompressor = flatex.NewReaderDict(z.r, dict) - } else { - z.decompressor = flatex.NewReader(z.r) - } + z.decompressor = flatex.NewReader(z.r) } else { - z.err = z.decompressor.(flatex.Resetter).Reset(z.r, dict) + z.err = z.decompressor.(flatex.Resetter).Reset(z.r) if z.err != nil { return z.err } |
