forked from diskfs/go-diskfs
/
compressor.go
351 lines (329 loc) · 8.83 KB
/
compressor.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package squashfs
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"io/ioutil"
"github.com/pierrec/lz4"
"github.com/ulikunitz/xz"
"github.com/ulikunitz/xz/lzma"
)
// Compressor defines a compressor. Fulfilled by various implementations in this package
type Compressor interface {
compress([]byte) ([]byte, error)
decompress([]byte) ([]byte, error)
loadOptions([]byte) error
optionsBytes() []byte
flavour() compression
}
// CompressorLzma lzma compression
type CompressorLzma struct {
}
func (c CompressorLzma) compress(in []byte) ([]byte, error) {
var b bytes.Buffer
lz, err := lzma.NewWriter(&b)
if err != nil {
return nil, fmt.Errorf("Error creating lzma compressor: %v", err)
}
if _, err := lz.Write(in); err != nil {
return nil, err
}
if err := lz.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (c CompressorLzma) decompress(in []byte) ([]byte, error) {
b := bytes.NewReader(in)
lz, err := lzma.NewReader(b)
if err != nil {
return nil, fmt.Errorf("Error creating lzma decompressor: %v", err)
}
p, err := ioutil.ReadAll(lz)
if err != nil {
return nil, fmt.Errorf("Error decompressing: %v", err)
}
return p, nil
}
func (c CompressorLzma) loadOptions(b []byte) error {
// lzma has no supported optiosn
return nil
}
func (c CompressorLzma) optionsBytes() []byte {
return []byte{}
}
func (c CompressorLzma) flavour() compression {
return compressionLzma
}
type GzipStrategy uint16
// gzip strategy options
const (
GzipDefault GzipStrategy = 0x1
GzipFiltered = 0x2
GzipHuffman = 0x4
GzipRunLengthEncoded = 0x8
GzipFixed = 0x10
)
// CompressorGzip gzip compression
type CompressorGzip struct {
CompressionLevel uint32
WindowSize uint16
Strategies map[GzipStrategy]bool
}
func (c CompressorGzip) compress(in []byte) ([]byte, error) {
var b bytes.Buffer
gz, err := zlib.NewWriterLevel(&b, int(c.CompressionLevel))
if err != nil {
return nil, fmt.Errorf("Error creating gzip compressor: %v", err)
}
if _, err := gz.Write(in); err != nil {
return nil, err
}
if err := gz.Flush(); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (c CompressorGzip) decompress(in []byte) ([]byte, error) {
b := bytes.NewReader(in)
gz, err := zlib.NewReader(b)
if err != nil {
return nil, fmt.Errorf("Error creating gzip decompressor: %v", err)
}
p, err := ioutil.ReadAll(gz)
if err != nil {
return nil, fmt.Errorf("Error decompressing: %v", err)
}
return p, nil
}
func (c CompressorGzip) loadOptions(b []byte) error {
expected := 8
if len(b) != expected {
return fmt.Errorf("Cannot parse gzip options, received %d bytes expected %d", len(b), expected)
}
c.CompressionLevel = binary.LittleEndian.Uint32(b[0:4])
c.WindowSize = binary.LittleEndian.Uint16(b[4:6])
strategies := map[GzipStrategy]bool{}
flags := binary.LittleEndian.Uint16(b[6:8])
for _, strategy := range []GzipStrategy{GzipDefault, GzipFiltered, GzipHuffman, GzipRunLengthEncoded, GzipFixed} {
if flags&uint16(strategy) == uint16(strategy) {
strategies[strategy] = true
}
}
c.Strategies = strategies
return nil
}
func (c CompressorGzip) optionsBytes() []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint32(b[0:4], c.CompressionLevel)
binary.LittleEndian.PutUint16(b[4:6], c.WindowSize)
var flags uint16
for _, strategy := range []GzipStrategy{GzipDefault, GzipFiltered, GzipHuffman, GzipRunLengthEncoded, GzipFixed} {
if c.Strategies[strategy] {
flags |= uint16(strategy)
}
}
binary.LittleEndian.PutUint16(b[6:8], flags)
return b
}
func (c CompressorGzip) flavour() compression {
return compressionGzip
}
// XzFilter filter for xz compression
type XzFilter uint32
// xz filter options
const (
XzFilterX86 XzFilter = 0x1
XzFilterPowerPC = 0x2
XzFilterIA64 = 0x4
XzFilterArm = 0x8
XzFilterArmThumb = 0x10
XzFilterSparc = 0x20
)
// CompressorXz xz compression
type CompressorXz struct {
DictionarySize uint32
ExecutableFilters map[XzFilter]bool
}
func (c CompressorXz) compress(in []byte) ([]byte, error) {
var b bytes.Buffer
config := xz.WriterConfig{
DictCap: int(c.DictionarySize),
}
xz, err := config.NewWriter(&b)
if err != nil {
return nil, fmt.Errorf("Error creating xz compressor: %v", err)
}
if _, err := xz.Write(in); err != nil {
return nil, err
}
if err := xz.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (c CompressorXz) decompress(in []byte) ([]byte, error) {
b := bytes.NewReader(in)
xz, err := xz.NewReader(b)
if err != nil {
return nil, fmt.Errorf("Error creating xz decompressor: %v", err)
}
p, err := ioutil.ReadAll(xz)
if err != nil {
return nil, fmt.Errorf("Error decompressing: %v", err)
}
return p, nil
}
func (c CompressorXz) loadOptions(b []byte) error {
expected := 8
if len(b) != expected {
return fmt.Errorf("Cannot parse xz options, received %d bytes expected %d", len(b), expected)
}
c.DictionarySize = binary.LittleEndian.Uint32(b[0:4])
filters := map[XzFilter]bool{}
flags := binary.LittleEndian.Uint32(b[4:8])
for _, filter := range []XzFilter{XzFilterX86, XzFilterPowerPC, XzFilterIA64, XzFilterArm, XzFilterArmThumb, XzFilterSparc} {
if flags&uint32(filter) == uint32(filter) {
filters[filter] = true
}
}
c.ExecutableFilters = filters
return nil
}
func (c CompressorXz) optionsBytes() []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint32(b[0:4], c.DictionarySize)
var flags uint32
for _, filter := range []XzFilter{XzFilterX86, XzFilterPowerPC, XzFilterIA64, XzFilterArm, XzFilterArmThumb, XzFilterSparc} {
if c.ExecutableFilters[filter] {
flags |= uint32(filter)
}
}
binary.LittleEndian.PutUint32(b[4:8], flags)
return b
}
func (c CompressorXz) flavour() compression {
return compressionXz
}
// lz4 compression
type lz4Flag uint32
const (
lz4HighCompression lz4Flag = 0x1
)
const (
lz4version1 uint32 = 1
)
// CompressorLz4 lz4 compression
type CompressorLz4 struct {
version uint32
flags map[lz4Flag]bool
}
func (c CompressorLz4) compress(in []byte) ([]byte, error) {
var b bytes.Buffer
lz := lz4.NewWriter(&b)
if _, err := lz.Write(in); err != nil {
return nil, err
}
if err := lz.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (c CompressorLz4) decompress(in []byte) ([]byte, error) {
b := bytes.NewReader(in)
lz := lz4.NewReader(b)
p, err := ioutil.ReadAll(lz)
if err != nil {
return nil, fmt.Errorf("Error decompressing: %v", err)
}
return p, nil
}
func (c CompressorLz4) loadOptions(b []byte) error {
expected := 8
if len(b) != expected {
return fmt.Errorf("Cannot parse lz4 options, received %d bytes expected %d", len(b), expected)
}
version := binary.LittleEndian.Uint32(b[0:4])
if version != lz4version1 {
return fmt.Errorf("Compressed with lz4 version %d, only support %d", version, lz4version1)
}
c.version = version
flagMap := map[lz4Flag]bool{}
flags := binary.LittleEndian.Uint32(b[4:8])
for _, f := range []lz4Flag{lz4HighCompression} {
if flags&uint32(f) == uint32(f) {
flagMap[f] = true
}
}
c.flags = flagMap
return nil
}
func (c CompressorLz4) optionsBytes() []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint32(b[0:4], c.version)
var flags uint32
for _, f := range []lz4Flag{lz4HighCompression} {
if c.flags[f] {
flags |= uint32(f)
}
}
binary.LittleEndian.PutUint32(b[4:8], flags)
return b
}
func (c CompressorLz4) flavour() compression {
return compressionLz4
}
// CompressorZstd zstd compression
type CompressorZstd struct {
level uint32
}
const (
zstdMinLevel uint32 = 1
zstdMaxLevel uint32 = 22
)
func (c CompressorZstd) loadOptions(b []byte) error {
expected := 4
if len(b) != expected {
return fmt.Errorf("Cannot parse zstd options, received %d bytes expected %d", len(b), expected)
}
level := binary.LittleEndian.Uint32(b[0:4])
if level < zstdMinLevel || level > zstdMaxLevel {
return fmt.Errorf("zstd compression level requested %d, must be at least %d and not more thann %d", level, zstdMinLevel, zstdMaxLevel)
}
c.level = level
return nil
}
func (c CompressorZstd) optionsBytes() []byte {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b[0:4], c.level)
return b
}
func (c CompressorZstd) flavour() compression {
return compressionZstd
}
func newCompressor(flavour compression) (Compressor, error) {
var c Compressor
switch flavour {
case compressionNone:
c = nil
case compressionGzip:
c = &CompressorGzip{}
case compressionLzma:
c = &CompressorLzma{}
case compressionLzo:
return nil, fmt.Errorf("LZO compression not yet supported")
case compressionXz:
c = &CompressorXz{}
case compressionLz4:
c = &CompressorLz4{}
case compressionZstd:
return nil, fmt.Errorf("zstd compression not yet supported")
default:
return nil, fmt.Errorf("Unknown compression type: %d", flavour)
}
return c, nil
}