-
Notifications
You must be signed in to change notification settings - Fork 2
/
stdCompress.go
84 lines (75 loc) · 1.96 KB
/
stdCompress.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
package mutators
import (
"compress/bzip2"
"compress/gzip"
"compress/zlib"
"io"
"github.com/batmac/ccat/pkg/log"
)
func init() {
singleRegister("ungzip", ungzip, withDescription("decompress gzip data"),
withCategory("decompress"),
)
singleRegister("unbzip2", bunzip2, withDescription("decompress bzip2 data"),
withCategory("decompress"),
)
singleRegister("unzlib", unzlib, withDescription("decompress zlib data"),
withCategory("decompress"),
)
singleRegister("gzip", cgzip, withDescription("compress to gzip data (X:6 is compression level, 0-9)"),
withCategory("compress"),
withConfigBuilder(stdConfigUint64WithDefault(^uint64(0))),
)
singleRegister("zlib", czlib, withDescription("compress to zlib data (X:6 is compression level, 0-9)"),
withCategory("compress"),
withConfigBuilder(stdConfigUint64WithDefault(^uint64(0))),
)
}
func ungzip(w io.WriteCloser, r io.ReadCloser, _ any) (int64, error) {
zr, err := gzip.NewReader(r)
if err != nil {
log.Fatal(err)
}
defer zr.Close()
//#nosec
return io.Copy(w, zr)
}
func bunzip2(w io.WriteCloser, r io.ReadCloser, _ any) (int64, error) {
bzr := bzip2.NewReader(r)
if bzr == nil {
log.Fatal("bzip2 decompressor failed to init")
}
//#nosec
return io.Copy(w, bzr)
}
func unzlib(w io.WriteCloser, r io.ReadCloser, _ any) (int64, error) {
z, err := zlib.NewReader(r)
if err != nil {
log.Fatal(err)
}
defer z.Close()
//#nosec
return io.Copy(w, z)
}
func cgzip(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
lvl := int(config.(uint64))
// log.Printf("compression level: %d", lvl)
zw, err := gzip.NewWriterLevel(w, lvl)
if err != nil {
log.Fatal(err)
}
defer zw.Close()
//#nosec
return io.Copy(zw, r)
}
func czlib(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
lvl := int(config.(uint64))
log.Debugf("compression level: %d", lvl)
zw, err := zlib.NewWriterLevel(w, lvl)
if err != nil {
log.Fatal(err)
}
defer zw.Close()
//#nosec
return io.Copy(zw, r)
}