Skip to content

add pgzip #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ require (
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/klauspost/pgzip v1.2.6
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/kofalt/go-memoize v0.0.0-20240506050413-9e5eb99a0f2a h1:yyeZ0oZLWgSakB9QzPuL/Kyx9kcXYblDOswXaOEx0tg=
github.com/kofalt/go-memoize v0.0.0-20240506050413-9e5eb99a0f2a/go.mod h1:EUxMohcCc4AiiO1SImzCQo3EdrEYj9Xkyrxbepg02nQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down
19 changes: 19 additions & 0 deletions pkg/mutators/single/0stdconfigbuilders.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,22 @@ func stdConfigStrings(min, max int) configBuilder {
return args, nil
}
}

func stdConfigInts(min, max int) configBuilder {
return func(args []string) (any, error) {
if len(args) < min || len(args) > max {
return nil, ErrWrongNumberOfArgs(min, max, len(args))
}

var ints []int
for _, arg := range args {
n, err := strconv.Atoi(arg)
if err != nil {
return nil, err
}
ints = append(ints, n)
}

return ints, nil
}
}
64 changes: 64 additions & 0 deletions pkg/mutators/single/pgzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package mutators

import (
"io"
"runtime"

gzip "github.com/klauspost/pgzip"

"github.com/batmac/ccat/pkg/log"
)

func init() {
singleRegister("unpgzip", unpgzip, withDescription("decompress with pgzip"),
withCategory("decompress"),
)

singleRegister("pgzip", cpgzip, withDescription("compress with pgzip (X:6 is compression level, 0-9, blockSize, blocks)"),
withCategory("compress"),
withConfigBuilder(stdConfigInts(0, 3)),
)
}

func unpgzip(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 cpgzip(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
args := config.([]int)

lvl := 6
if len(args) > 0 {
lvl = args[0]
}

log.Debugf("compression level: %d", lvl)
zw, err := gzip.NewWriterLevel(w, lvl)
if err != nil {
log.Fatal(err)
}
defer zw.Close()

switch len(args) {
case 2:
log.Debugf("setting block size: %d", args[1])
if err := zw.SetConcurrency(args[1], runtime.GOMAXPROCS(0)); err != nil {
log.Fatal(err)
}
case 3:
log.Debugf("setting block size: %d, blocks: %d", args[1], args[2])
if err := zw.SetConcurrency(args[1], args[2]); err != nil {
log.Fatal(err)
}
}

//#nosec
return io.Copy(zw, r)
}
Loading