mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-28 23:27:36 +00:00
08cc04c3d5
part of #904
30 lines
699 B
Go
30 lines
699 B
Go
package network
|
|
|
|
import (
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
|
"github.com/pierrec/lz4"
|
|
)
|
|
|
|
// compress compresses bytes using lz4.
|
|
func compress(source []byte) ([]byte, error) {
|
|
dest := make([]byte, lz4.CompressBlockBound(len(source)))
|
|
size, err := lz4.CompressBlock(source, dest, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dest[:size], nil
|
|
}
|
|
|
|
// decompress decompresses bytes using lz4.
|
|
func decompress(source []byte) ([]byte, error) {
|
|
maxSize := len(source) * 255
|
|
if maxSize > payload.MaxSize {
|
|
maxSize = payload.MaxSize
|
|
}
|
|
dest := make([]byte, maxSize)
|
|
size, err := lz4.UncompressBlock(source, dest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return dest[:size], nil
|
|
}
|