2020-05-25 07:09:21 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2020-06-15 18:13:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2020-05-25 07:09:21 +00:00
|
|
|
"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
|
2020-06-15 18:13:32 +00:00
|
|
|
if maxSize > payload.MaxSize {
|
|
|
|
maxSize = payload.MaxSize
|
2020-06-18 08:32:38 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|