Skip to content

Commit 1529e16

Browse files
authored
add pgzip (#736)
1 parent f49a723 commit 1529e16

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ require (
104104
github.com/hashicorp/go-version v1.7.0 // indirect
105105
github.com/json-iterator/go v1.1.12 // indirect
106106
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
107+
github.com/klauspost/pgzip v1.2.6
107108
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
108109
github.com/mattn/go-runewidth v0.0.15 // indirect
109110
github.com/minio/md5-simd v1.1.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
208208
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
209209
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
210210
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
211+
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
212+
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
211213
github.com/kofalt/go-memoize v0.0.0-20240506050413-9e5eb99a0f2a h1:yyeZ0oZLWgSakB9QzPuL/Kyx9kcXYblDOswXaOEx0tg=
212214
github.com/kofalt/go-memoize v0.0.0-20240506050413-9e5eb99a0f2a/go.mod h1:EUxMohcCc4AiiO1SImzCQo3EdrEYj9Xkyrxbepg02nQ=
213215
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=

pkg/mutators/single/0stdconfigbuilders.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,22 @@ func stdConfigStrings(min, max int) configBuilder {
7171
return args, nil
7272
}
7373
}
74+
75+
func stdConfigInts(min, max int) configBuilder {
76+
return func(args []string) (any, error) {
77+
if len(args) < min || len(args) > max {
78+
return nil, ErrWrongNumberOfArgs(min, max, len(args))
79+
}
80+
81+
var ints []int
82+
for _, arg := range args {
83+
n, err := strconv.Atoi(arg)
84+
if err != nil {
85+
return nil, err
86+
}
87+
ints = append(ints, n)
88+
}
89+
90+
return ints, nil
91+
}
92+
}

pkg/mutators/single/pgzip.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package mutators
2+
3+
import (
4+
"io"
5+
"runtime"
6+
7+
gzip "github.com/klauspost/pgzip"
8+
9+
"github.com/batmac/ccat/pkg/log"
10+
)
11+
12+
func init() {
13+
singleRegister("unpgzip", unpgzip, withDescription("decompress with pgzip"),
14+
withCategory("decompress"),
15+
)
16+
17+
singleRegister("pgzip", cpgzip, withDescription("compress with pgzip (X:6 is compression level, 0-9, blockSize, blocks)"),
18+
withCategory("compress"),
19+
withConfigBuilder(stdConfigInts(0, 3)),
20+
)
21+
}
22+
23+
func unpgzip(w io.WriteCloser, r io.ReadCloser, _ any) (int64, error) {
24+
zr, err := gzip.NewReader(r)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
defer zr.Close()
29+
30+
//#nosec
31+
return io.Copy(w, zr)
32+
}
33+
34+
func cpgzip(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
35+
args := config.([]int)
36+
37+
lvl := 6
38+
if len(args) > 0 {
39+
lvl = args[0]
40+
}
41+
42+
log.Debugf("compression level: %d", lvl)
43+
zw, err := gzip.NewWriterLevel(w, lvl)
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
defer zw.Close()
48+
49+
switch len(args) {
50+
case 2:
51+
log.Debugf("setting block size: %d", args[1])
52+
if err := zw.SetConcurrency(args[1], runtime.GOMAXPROCS(0)); err != nil {
53+
log.Fatal(err)
54+
}
55+
case 3:
56+
log.Debugf("setting block size: %d, blocks: %d", args[1], args[2])
57+
if err := zw.SetConcurrency(args[1], args[2]); err != nil {
58+
log.Fatal(err)
59+
}
60+
}
61+
62+
//#nosec
63+
return io.Copy(zw, r)
64+
}

0 commit comments

Comments
 (0)