mirror of
https://github.com/PretendoNetwork/nex-go.git
synced 2025-04-02 11:02:14 -04:00
prudp: swap to native Go LZO lib
This commit is contained in:
parent
6488c96db7
commit
fcf3430788
3 changed files with 11 additions and 39 deletions
|
@ -4,7 +4,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/cyberdelia/lzo"
|
||||
"github.com/rasky/go-lzo"
|
||||
)
|
||||
|
||||
// TODO - Untested. I think this works. Maybe. Verify and remove this comment
|
||||
|
@ -14,29 +14,15 @@ type LZO struct{}
|
|||
|
||||
// Compress compresses the payload using LZO
|
||||
func (l *LZO) Compress(payload []byte) ([]byte, error) {
|
||||
var compressed bytes.Buffer
|
||||
compressed := lzo.Compress1X(payload)
|
||||
|
||||
lzoWriter := lzo.NewWriter(&compressed)
|
||||
compressionRatio := len(payload)/len(compressed) + 1
|
||||
|
||||
_, err := lzoWriter.Write(payload)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
err = lzoWriter.Close()
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
compressedBytes := compressed.Bytes()
|
||||
|
||||
compressionRatio := len(payload)/len(compressedBytes) + 1
|
||||
|
||||
result := make([]byte, len(compressedBytes)+1)
|
||||
result := make([]byte, len(compressed)+1)
|
||||
|
||||
result[0] = byte(compressionRatio)
|
||||
|
||||
copy(result[1:], compressedBytes)
|
||||
copy(result[1:], compressed)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
@ -52,32 +38,18 @@ func (l *LZO) Decompress(payload []byte) ([]byte, error) {
|
|||
}
|
||||
|
||||
reader := bytes.NewReader(compressed)
|
||||
decompressed := bytes.Buffer{}
|
||||
|
||||
lzoReader, err := lzo.NewReader(reader)
|
||||
decompressed, err := lzo.Decompress1X(reader, len(compressed), 0)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
_, err = decompressed.ReadFrom(lzoReader)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
err = lzoReader.Close()
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
decompressedBytes := decompressed.Bytes()
|
||||
|
||||
ratioCheck := len(decompressedBytes)/len(compressed) + 1
|
||||
ratioCheck := len(decompressed)/len(compressed) + 1
|
||||
|
||||
if ratioCheck != int(compressionRatio) {
|
||||
return []byte{}, fmt.Errorf("Failed to decompress payload. Got bad ratio. Expected %d, got %d", compressionRatio, ratioCheck)
|
||||
}
|
||||
|
||||
return decompressedBytes, nil
|
||||
return decompressed, nil
|
||||
}
|
||||
|
||||
// Copy returns a copy of the algorithm
|
||||
|
|
2
go.mod
2
go.mod
|
@ -4,8 +4,8 @@ go 1.21
|
|||
|
||||
require (
|
||||
github.com/PretendoNetwork/plogger-go v1.0.4
|
||||
github.com/cyberdelia/lzo v1.0.0
|
||||
github.com/lxzan/gws v1.8.0
|
||||
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e
|
||||
github.com/superwhiskers/crunch/v3 v3.5.7
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
|
||||
golang.org/x/mod v0.12.0
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,7 +1,5 @@
|
|||
github.com/PretendoNetwork/plogger-go v1.0.4 h1:PF7xHw9eDRHH+RsAP9tmAE7fG0N0p6H4iPwHKnsoXwc=
|
||||
github.com/PretendoNetwork/plogger-go v1.0.4/go.mod h1:7kD6M4vPq1JL4LTuPg6kuB1OvUBOwQOtAvTaUwMbwvU=
|
||||
github.com/cyberdelia/lzo v1.0.0 h1:smmvcahczwI/VWSzZ7iikt50lubari5py3qL4hAEHII=
|
||||
github.com/cyberdelia/lzo v1.0.0/go.mod h1:UVNk6eM6Sozt1wx17TECJKuqmIY58TJOVeJxjlGGAGs=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
|
||||
|
@ -23,6 +21,8 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP
|
|||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e h1:dCWirM5F3wMY+cmRda/B1BiPsFtmzXqV9b0hLWtVBMs=
|
||||
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e/go.mod h1:9leZcVcItj6m9/CfHY5Em/iBrCz7js8LcRQGTKEEv2M=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/superwhiskers/crunch/v3 v3.5.7 h1:N9RLxaR65C36i26BUIpzPXGy2f6pQ7wisu2bawbKNqg=
|
||||
|
|
Loading…
Add table
Reference in a new issue