Skip to content

Commit

Permalink
add pgzip
Browse files Browse the repository at this point in the history
  • Loading branch information
batmac committed Sep 17, 2024
1 parent f49a723 commit f32eeb4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
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)
}

0 comments on commit f32eeb4

Please sign in to comment.