aboutsummaryrefslogtreecommitdiff
path: root/internal/flatex/decompress_bytes.go
blob: 5a660b0af4071e5dd54439ce48d7e6ca9b76e0f3 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package flatex

import (
	"io"
	"sync"

	"git.sr.ht/~runxiyu/furgit/internal/bufpool"
)

// bufferDecompressor wraps the custom slice inflater so byte-slice
// decompressions avoid repeated allocations.
type bufferDecompressor struct {
	inflater sliceInflater
}

var bufferDecompressorPool = sync.Pool{
	New: func() any {
		fixedHuffmanDecoderInit()
		d := &bufferDecompressor{}
		d.inflater.bits = new([maxNumLit + maxNumDist]int)
		d.inflater.codebits = new([numCodes]int)
		return d
	},
}

// Decompress inflates the provided DEFLATE stream and returns the full output
// in a pooled bufpool.Buffer along with the number of consumed bytes from src.
func Decompress(src []byte) (bufpool.Buffer, int, error) {
	return DecompressDict(src, nil)
}

// DecompressDict inflates the provided DEFLATE stream using dict as the preset
// dictionary and returns the full output in a pooled bufpool.Buffer. The second
// returned value reports how many bytes of src were consumed.
func DecompressDict(src []byte, dict []byte) (bufpool.Buffer, int, error) {
	d := bufferDecompressorPool.Get().(*bufferDecompressor)
	defer bufferDecompressorPool.Put(d)

	if err := d.inflater.reset(src, dict); err != nil {
		return bufpool.Buffer{}, 0, err
	}

	out := bufpool.Borrow(bufpool.DefaultBufferCap)
	out.Resize(0)

	for {
		if len(d.inflater.toRead) > 0 {
			out.Append(d.inflater.toRead)
			d.inflater.toRead = nil
			continue
		}
		if d.inflater.err != nil {
			if d.inflater.err == io.EOF {
				return out, d.inflater.pos, nil
			}
			out.Release()
			return bufpool.Buffer{}, 0, d.inflater.err
		}
		d.inflater.step(&d.inflater)
		if d.inflater.err != nil && len(d.inflater.toRead) == 0 {
			d.inflater.toRead = d.inflater.dict.readFlush()
		}
	}
}