This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lz4.go
82 lines (65 loc) · 1.95 KB
/
lz4.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package lz4
import (
"encoding/binary"
"fmt"
"io"
"sync"
"github.com/pierrec/lz4/v4"
)
// Decompress an lz4-java block. The data returned is only safe to use until the next operation
func Decompress(data io.Reader) ([]byte, error) {
var header [21]byte
_, err := data.Read(header[:])
if err != nil {
return nil, err
}
magicValue := string(header[:8])
if magicValue != magic {
return nil, fmt.Errorf("invalid magic value")
}
compressedLength := binary.LittleEndian.Uint32(header[9:13])
decompressedLength := binary.LittleEndian.Uint32(header[13:17])
token := header[8]
compressionMethod := token & 0xf0
switch compressionMethod {
case methodLZ4:
var compressed = compressedBuffers.Get().([]byte)
if len(compressed) < int(compressedLength) {
compressed = make([]byte, compressedLength)
}
defer compressedBuffers.Put(compressed)
var decompressed = decompressedBuffers.Get().([]byte)
if len(decompressed) < int(decompressedLength) {
decompressed = make([]byte, decompressedLength)
}
defer decompressedBuffers.Put(decompressed)
if _, err := data.Read(compressed[:compressedLength]); err != nil {
return nil, err
}
_, err = lz4.UncompressBlock(compressed[:compressedLength], decompressed[:decompressedLength])
return decompressed[:decompressedLength], err
case methodUncompressed:
var compressed = compressedBuffers.Get().([]byte)
if len(compressed) < int(compressedLength) {
compressed = make([]byte, compressedLength)
}
defer compressedBuffers.Put(compressed)
if _, err := data.Read(compressed); err != nil {
return nil, err
}
return compressed[:compressedLength], nil
default:
return nil, fmt.Errorf("unknown compression method %d", compressionMethod)
}
}
var decompressedBuffers = sync.Pool{
New: func() any { return make([]byte, 0) },
}
var compressedBuffers = sync.Pool{
New: func() any { return make([]byte, 0) },
}
const magic = "LZ4Block"
const (
methodUncompressed = 1 << (iota + 4)
methodLZ4
)