64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"io"
|
|
"log"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
"github.com/songgao/water/waterutil"
|
|
)
|
|
|
|
func Compress(writer io.Writer, data [][]byte) {
|
|
|
|
// encode gob
|
|
enc := gob.NewEncoder(writer)
|
|
|
|
log.Printf("send to target %s source bytes: %d", waterutil.IPv4Destination(data[0]), len(data[0]))
|
|
err := enc.Encode(data)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
// zstd compress
|
|
zenc, err := zstd.NewWriter(writer)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
err = zenc.Flush()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
}
|
|
|
|
func Decompress(data []byte) (frames [][]byte) {
|
|
var err error
|
|
|
|
log.Printf("get data bytes len: %d", len(data))
|
|
|
|
var buf = bytes.NewBuffer(data)
|
|
var zdec *zstd.Decoder
|
|
zdec, err = zstd.NewReader(buf)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
zdec.Close()
|
|
|
|
dec := gob.NewDecoder(buf)
|
|
err = dec.Decode(&frames)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func SwapBytes(frame1 []byte, start1 int, frame2 []byte, start2 int, size int) {
|
|
var temp []byte = make([]byte, size)
|
|
copy(temp, frame1[start1:start1+size])
|
|
copy(frame1[start1:start1+size], frame2[start2:start2+size])
|
|
copy(frame2[start2:start2+size], temp)
|
|
}
|