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
|
package flatex
import (
"io"
"sync"
"git.sr.ht/~runxiyu/furgit/internal/bufpool"
)
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
},
}
func Decompress(src []byte) (bufpool.Buffer, int, error) {
return DecompressSized(src, 0)
}
func DecompressSized(src []byte, sizeHint int) (bufpool.Buffer, int, error) {
d := bufferDecompressorPool.Get().(*bufferDecompressor)
defer bufferDecompressorPool.Put(d)
if err := d.inflater.reset(src); err != nil {
return bufpool.Buffer{}, 0, err
}
out := bufpool.Borrow(sizeHint)
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.window.readFlush()
}
}
}
|