aboutsummaryrefslogtreecommitdiff
path: root/internal/zlibx
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-20 19:06:13 +0800
committerGravatar Runxi Yu2026-02-20 19:07:14 +0800
commitaa513c069c1418734aea894dc944e27c6a78a3bb (patch)
tree687f0a11bb550fa088fd82a98ceb8979bbc35f69 /internal/zlibx
parentComment on prior reverts removing the pack writing API (diff)
signatureNo signature
Delete everything, I'm redesigning this.
I'll stop using a flat package and make things much more modular. And also experiment with streaming APIs so large blobs don't OOM us.
Diffstat (limited to 'internal/zlibx')
-rw-r--r--internal/zlibx/LICENSE27
-rw-r--r--internal/zlibx/constants.go52
-rw-r--r--internal/zlibx/decompress.go54
-rw-r--r--internal/zlibx/decompress_test.go170
4 files changed, 0 insertions, 303 deletions
diff --git a/internal/zlibx/LICENSE b/internal/zlibx/LICENSE
deleted file mode 100644
index 2a7cf70d..00000000
--- a/internal/zlibx/LICENSE
+++ /dev/null
@@ -1,27 +0,0 @@
-Copyright 2009 The Go Authors.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are
-met:
-
- * Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above
-copyright notice, this list of conditions and the following disclaimer
-in the documentation and/or other materials provided with the
-distribution.
- * Neither the name of Google LLC nor the names of its
-contributors may be used to endorse or promote products derived from
-this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/internal/zlibx/constants.go b/internal/zlibx/constants.go
deleted file mode 100644
index 161e3458..00000000
--- a/internal/zlibx/constants.go
+++ /dev/null
@@ -1,52 +0,0 @@
-// Copyright 2009 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-/*
-Package zlibx implements reading of zlib format compressed data,
-as specified in RFC 1950.
-
-This package differs from the standard library's compress/zlib package
-in that it pools readers to reduce allocations. Writing is unsupported.
-
-THis package will likely be refactorered much more for our specific
-use case of only doing full decompressions to byte slices.
-
-Note that closing the reader causes it to be returned to a pool for
-reuse. Therefore, the caller must not retain references to the
-reader after closing it; in the standard library's compress/zlib package,
-it is legal to Reset a closed reader and continue using it; that is
-not allowed here, so there is simply no Resetter interface.
-
-The implementation provides filters that uncompress during reading
-and compress during writing. For example, to write compressed data
-to a buffer:
-
- var b bytes.Buffer
- w := zlib.NewWriter(&b)
- w.Write([]byte("hello, world\n"))
- w.Close()
-
-and to read that data back:
-
- r, err := zlib.NewReader(&b)
- io.Copy(os.Stdout, r)
- r.Close()
-*/
-package zlibx
-
-import (
- "errors"
-)
-
-const (
- zlibDeflate = 8
- zlibMaxWindow = 7
-)
-
-var (
- // ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
- ErrChecksum = errors.New("zlib: invalid checksum")
- // ErrHeader is returned when reading ZLIB data that has an invalid header.
- ErrHeader = errors.New("zlib: invalid header")
-)
diff --git a/internal/zlibx/decompress.go b/internal/zlibx/decompress.go
deleted file mode 100644
index 126c1fcb..00000000
--- a/internal/zlibx/decompress.go
+++ /dev/null
@@ -1,54 +0,0 @@
-package zlibx
-
-import (
- "encoding/binary"
- "io"
-
- "codeberg.org/lindenii/furgit/internal/adler32"
- "codeberg.org/lindenii/furgit/internal/bufpool"
- "codeberg.org/lindenii/furgit/internal/flatex"
-)
-
-func Decompress(src []byte) (bufpool.Buffer, error) {
- out, _, err := DecompressSized(src, 0)
- return out, err
-}
-
-func DecompressSized(src []byte, sizeHint int) (buf bufpool.Buffer, consumed int, err error) {
- if len(src) < 6 {
- return bufpool.Buffer{}, 0, io.ErrUnexpectedEOF
- }
-
- cmf := src[0]
- flg := src[1]
- if (cmf&0x0f != zlibDeflate) || (cmf>>4 > zlibMaxWindow) || (binary.BigEndian.Uint16(src[:2])%31 != 0) {
- return bufpool.Buffer{}, 0, ErrHeader
- }
-
- offset := 2
- if flg&0x20 != 0 {
- return bufpool.Buffer{}, 0, ErrHeader
- }
-
- if len(src[offset:]) < 4 {
- return bufpool.Buffer{}, 0, io.ErrUnexpectedEOF
- }
-
- deflateData := src[offset:]
- out, consumed, err := flatex.DecompressSized(deflateData, sizeHint)
- if err != nil {
- return bufpool.Buffer{}, 0, err
- }
-
- checksumPos := offset + consumed
- if checksumPos+4 > len(src) {
- out.Release()
- return bufpool.Buffer{}, 0, io.ErrUnexpectedEOF
- }
- expected := binary.BigEndian.Uint32(src[checksumPos : checksumPos+4])
- if expected != adler32.Checksum(out.Bytes()) {
- out.Release()
- return bufpool.Buffer{}, 0, ErrChecksum
- }
- return out, checksumPos + 4, nil
-}
diff --git a/internal/zlibx/decompress_test.go b/internal/zlibx/decompress_test.go
deleted file mode 100644
index bea348d2..00000000
--- a/internal/zlibx/decompress_test.go
+++ /dev/null
@@ -1,170 +0,0 @@
-package zlibx
-
-import (
- "bytes"
- stdzlib "compress/zlib"
- "crypto/rand"
- "testing"
-)
-
-func compressZlib(t *testing.T, payload []byte) []byte {
- t.Helper()
- var buf bytes.Buffer
- w := stdzlib.NewWriter(&buf)
- if _, err := w.Write(payload); err != nil {
- t.Fatalf("Write: %v", err)
- }
- if err := w.Close(); err != nil {
- t.Fatalf("Close: %v", err)
- }
- return buf.Bytes()
-}
-
-func TestDecompress(t *testing.T) {
- makeRand := func(n int) []byte {
- b := make([]byte, n)
- if _, err := rand.Read(b); err != nil {
- t.Fatalf("rand.Read: %v", err)
- }
- return b
- }
-
- type tc struct {
- name string
- payload []byte
- }
-
- tests := []tc{
- {
- name: "simple-hello",
- payload: []byte("hello, zlib world!"),
- },
- {
- name: "empty",
- payload: []byte{},
- },
- {
- name: "single-byte",
- payload: []byte{0x42},
- },
- {
- name: "all-zero-1k",
- payload: bytes.Repeat([]byte{0}, 1024),
- },
- {
- name: "all-FF-1k",
- payload: bytes.Repeat([]byte{0xFF}, 1024),
- },
- {
- name: "ascii-repeated-pattern",
- payload: bytes.Repeat([]byte("ABC123!"), 500),
- },
- {
- name: "binary-structured",
- payload: []byte{
- 0x00, 0x01, 0x02, 0x03,
- 0x10, 0x20, 0x30, 0x40,
- 0xFF, 0xEE, 0xDD, 0xCC,
- },
- },
- {
- name: "1k-crypto-random",
- payload: makeRand(1024),
- },
- {
- name: "32k-crypto-random",
- payload: makeRand(32 * 1024),
- },
- {
- name: "256k-crypto-random",
- payload: makeRand(256 * 1024),
- },
- {
- name: "highly-compressible-large",
- payload: bytes.Repeat([]byte("AAAAAAAAAAAAAAAAAAAA"), 50_000),
- },
- {
- name: "json",
- payload: []byte(`{"name":"test","values":[1,2,3,4],"deep":{"x":123,"y":"abc"}}`),
- },
- {
- name: "html",
- payload: []byte("<html><body><h1>Title</h1><p>Paragraph</p></body></html>"),
- },
- {
- name: "alternating-binary-pattern",
- payload: func() []byte {
- b := make([]byte, 4096)
- for i := 0; i < len(b); i++ {
- if i%2 == 0 {
- b[i] = 0xAA
- } else {
- b[i] = 0x55
- }
- }
- return b
- }(),
- },
- {
- name: "large-repetitive-words",
- payload: bytes.Repeat([]byte("the quick brown fox jumps over the lazy dog\n"), 4000),
- },
- {
- name: "unicode",
- payload: []byte("我不知道该说点啥就随便打点字吧🤷‍♀️"),
- },
- {
- name: "multi-meg-random-2MB",
- payload: makeRand(2 * 1024 * 1024),
- },
- {
- name: "multi-meg-random-16MB",
- payload: makeRand(16 * 1024 * 1024),
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- compressed := compressZlib(t, tt.payload)
-
- out, err := Decompress(compressed)
- if err != nil {
- t.Fatalf("Decompress: %v", err)
- }
- defer out.Release()
-
- if !bytes.Equal(out.Bytes(), tt.payload) {
- t.Fatalf("payload mismatch: got %d bytes, want %d", len(out.Bytes()), len(tt.payload))
- }
- })
- }
-}
-
-func TestDecompressChecksumError(t *testing.T) {
- payload := []byte("checksum check")
- compressed := compressZlib(t, payload)
- compressed[len(compressed)-1] ^= 0xff
-
- if _, err := Decompress(compressed); err != ErrChecksum {
- t.Fatalf("expected ErrChecksum, got %v", err)
- }
-}
-
-func TestDecompressSizedUsesHint(t *testing.T) {
- payload := []byte("tiny payload")
- compressed := compressZlib(t, payload)
-
- const hint = 1 << 20
- out, _, err := DecompressSized(compressed, hint)
- if err != nil {
- t.Fatalf("DecompressSized: %v", err)
- }
- defer out.Release()
-
- if !bytes.Equal(out.Bytes(), payload) {
- t.Fatalf("unexpected payload %q", out.Bytes())
- }
- if cap(out.Bytes()) < hint {
- t.Fatalf("expected capacity >= %d, got %d", hint, cap(out.Bytes()))
- }
-}