2020-05-25 07:09:21 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pierrec/lz4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// compress compresses bytes using lz4.
|
|
|
|
func compress(source []byte) ([]byte, error) {
|
2020-06-18 08:32:38 +00:00
|
|
|
dest := make([]byte, lz4.CompressBlockBound(len(source)))
|
|
|
|
size, err := lz4.CompressBlock(source, dest, nil)
|
2020-05-25 07:09:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-18 08:32:38 +00:00
|
|
|
return dest[:size], nil
|
2020-05-25 07:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decompress decompresses bytes using lz4.
|
|
|
|
func decompress(source []byte) ([]byte, error) {
|
2020-06-18 08:32:38 +00:00
|
|
|
maxSize := len(source) * 255
|
|
|
|
if maxSize > PayloadMaxSize {
|
|
|
|
maxSize = PayloadMaxSize
|
|
|
|
}
|
|
|
|
dest := make([]byte, maxSize)
|
|
|
|
size, err := lz4.UncompressBlock(source, dest)
|
2020-05-25 07:09:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-18 08:32:38 +00:00
|
|
|
return dest[:size], nil
|
2020-05-25 07:09:21 +00:00
|
|
|
}
|