This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
/
lz.go
206 lines (159 loc) · 5.29 KB
/
lz.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package compression
import (
"bytes"
"encoding/binary"
"math"
"github.com/pierrec/lz4"
"go.ytsaurus.tech/library/go/core/xerrors"
)
type CodecLz4 struct{}
func (c *CodecLz4) Compress(block []byte) ([]byte, error) {
return lz4BlockCompress(block, lz4.CompressBlock)
}
func (c *CodecLz4) Decompress(block []byte) ([]byte, error) {
return lz4BlockDecompress(block, lz4.UncompressBlock)
}
func (c *CodecLz4) GetID() CodecID {
return CodecIDLz4
}
type CodecLz4HighCompression struct{}
func (c *CodecLz4HighCompression) Compress(block []byte) ([]byte, error) {
return lz4BlockCompress(block, func(src, dst []byte, hashTable []int) (_ int, err error) {
return lz4.CompressBlockHC(src, dst, 0)
})
}
func (c *CodecLz4HighCompression) Decompress(block []byte) ([]byte, error) {
return lz4BlockDecompress(block, lz4.UncompressBlock)
}
func (c *CodecLz4HighCompression) GetID() CodecID {
return CodecIDLz4HighCompression
}
type lz4HeaderSignature uint32
const (
lz4HeaderSignatureV1 lz4HeaderSignature = (1 << 30) + 1
lz4HeaderSignatureV2 lz4HeaderSignature = (1 << 30) + 2
)
type lz4Header struct {
Signature lz4HeaderSignature
Size uint32
}
const maxLz4BlockSize = 1 << 30
type lz4BlockHeader struct {
CompressedSize uint32
UncompressedSize uint32
}
type lz4CompressFunc func(src, dst []byte, hashTable []int) (_ int, err error)
var lz4SignatureV1MaxLength = math.MaxInt32
func lz4BlockCompress(block []byte, compress lz4CompressFunc) ([]byte, error) {
if len(block) == 0 {
return nil, nil
}
var out bytes.Buffer
h := lz4Header{
Signature: lz4HeaderSignatureV1,
Size: uint32(len(block)),
}
useExtendedHeader := len(block) > lz4SignatureV1MaxLength
if useExtendedHeader {
h.Signature = lz4HeaderSignatureV2
h.Size = 0
}
if err := binary.Write(&out, binary.LittleEndian, uint32(h.Signature)); err != nil {
return nil, err
}
if err := binary.Write(&out, binary.LittleEndian, h.Size); err != nil {
return nil, err
}
if useExtendedHeader {
if err := binary.Write(&out, binary.LittleEndian, uint64(len(block))); err != nil {
return nil, err
}
}
for inputOffset, inputSize := 0, len(block); inputSize > 0; {
blockSize := inputSize
if blockSize > maxLz4BlockSize {
blockSize = maxLz4BlockSize
}
compressedBlockSizeBound := blockSize
if compressedBlockSizeBound < lz4.CompressBlockBound(blockSize) {
compressedBlockSizeBound = lz4.CompressBlockBound(blockSize)
}
uncompressedBlock := block[inputOffset : inputOffset+blockSize]
compressedBlock := make([]byte, compressedBlockSizeBound)
compressedSize, err := compress(uncompressedBlock, compressedBlock, nil)
if err != nil {
return nil, err
}
blockHeader := lz4BlockHeader{
CompressedSize: uint32(compressedSize),
UncompressedSize: uint32(blockSize),
}
if err := binary.Write(&out, binary.LittleEndian, blockHeader.CompressedSize); err != nil {
return nil, err
}
if err := binary.Write(&out, binary.LittleEndian, blockHeader.UncompressedSize); err != nil {
return nil, err
}
if _, err := out.Write(compressedBlock[:compressedSize]); err != nil {
return nil, err
}
inputOffset += blockSize
inputSize -= blockSize
}
return out.Bytes(), nil
}
type lz4DecompressFunc func(src, dst []byte) (int, error)
func lz4BlockDecompress(block []byte, decompress lz4DecompressFunc) ([]byte, error) {
if len(block) == 0 {
return nil, nil
}
if len(block) < 8 {
return nil, xerrors.Errorf("unable to decode header: invalid length: %d", len(block))
}
var out bytes.Buffer
h := lz4Header{
Signature: lz4HeaderSignature(binary.LittleEndian.Uint32(block[:4])),
Size: binary.LittleEndian.Uint32(block[4:8]),
}
inputOffset := 8
totalUncompressedSize := uint64(h.Size)
if h.Signature == lz4HeaderSignatureV2 {
if len(block) < 16 {
return nil, xerrors.Errorf("unable to decode v2 header: invalid length: %d", len(block))
}
totalUncompressedSize = binary.LittleEndian.Uint64(block[8:16])
inputOffset += 8
}
for inputOffset < len(block) {
if len(block) < inputOffset+8 {
return nil, xerrors.Errorf("unable to decode block header: invalid length")
}
blockHeader := lz4BlockHeader{
CompressedSize: binary.LittleEndian.Uint32(block[inputOffset : inputOffset+4]),
UncompressedSize: binary.LittleEndian.Uint32(block[inputOffset+4 : inputOffset+8]),
}
inputOffset += 8
if len(block) < inputOffset+int(blockHeader.CompressedSize) {
return nil, xerrors.Errorf("not enough data to decode block: header expects %d bytes, got %d",
blockHeader.CompressedSize, len(block)-inputOffset)
}
compressedBlock := block[inputOffset : inputOffset+int(blockHeader.CompressedSize)]
uncompressedBlock := make([]byte, blockHeader.UncompressedSize)
uncompressedSize, err := decompress(compressedBlock, uncompressedBlock)
if err != nil {
return nil, err
}
if uncompressedSize != int(blockHeader.UncompressedSize) {
return nil, xerrors.Errorf("uncompressed block size mismatch: expected %d, got %d",
blockHeader.UncompressedSize, uncompressedSize)
}
if _, err := out.Write(uncompressedBlock); err != nil {
return nil, err
}
inputOffset += int(blockHeader.CompressedSize)
}
if uint64(out.Len()) != totalUncompressedSize {
return nil, xerrors.Errorf("uncompressed data size mismatch: expected %d, got %d", out.Len(), h.Size)
}
return out.Bytes(), nil
}