From 00e6b378b1d2199dcd569d3c153ac6b29d24b5bb Mon Sep 17 00:00:00 2001 From: blacktop Date: Wed, 31 Jul 2024 11:27:54 -0600 Subject: [PATCH] fix: lzfse.EncodeBuffer to have same format as DecodeBuffer --- README.md | 8 ++++---- lzfse.go | 35 ++++++++++++++++++++++------------- lzfse_test.go | 26 +++++++++++++++++++------- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2d69be0..8996c33 100644 --- a/README.md +++ b/README.md @@ -18,22 +18,22 @@ go get github.com/blacktop/lzfse-cgo ```golang import ( - "io/ioutil" "log" + "os" "github.com/blacktop/lzfse-cgo" ) func main() { - dat, err := ioutil.ReadFile("encoded.file") + dat, err := os.ReadFile("encoded.file") if err != nil { log.Fatal(fmt.Errorf("failed to read compressed file: %v", err)) } decompressed = lzfse.DecodeBuffer(dat) - err = ioutil.WriteFile("decoded.file", decompressed, 0644) + err = os.WriteFile("decoded.file", decompressed, 0644) if err != nil { log.Fatal(fmt.Errorf("failed to decompress file: %v", err)) } @@ -47,4 +47,4 @@ func main() { ## License -MIT Copyright (c) 2019-2022 blacktop +MIT Copyright (c) 2019-2024 blacktop diff --git a/lzfse.go b/lzfse.go index bae9f04..421904b 100644 --- a/lzfse.go +++ b/lzfse.go @@ -36,7 +36,7 @@ size_t lzvn_decode_buffer(void *__restrict dst, size_t dst_size, import "C" import ( "bytes" - "io/ioutil" + "os" "sync" "testing" "unsafe" @@ -73,15 +73,15 @@ func EncodeScratchSize() uint { } // EncodeBuffer function as declared in lzfse.h:87 -func EncodeBuffer(dstBuffer []byte, dstSize uint, srcBuffer string, srcSize uint, scratchBuffer unsafe.Pointer) uint { +func EncodeBuffer(srcBuffer []byte) []byte { + csrcBuffer, _ := unpackPUint8String(string(srcBuffer)) + csrcSize, _ := (C.size_t)(len(srcBuffer)), cgoAllocsUnknown + dstBuffer := make([]byte, len(srcBuffer)*2) cdstBuffer, _ := (*C.uint8_t)(unsafe.Pointer((*sliceHeader)(unsafe.Pointer(&dstBuffer)).Data)), cgoAllocsUnknown - cdstSize, _ := (C.size_t)(dstSize), cgoAllocsUnknown - csrcBuffer, _ := unpackPUint8String(srcBuffer) - csrcSize, _ := (C.size_t)(srcSize), cgoAllocsUnknown - cscratchBuffer, _ := scratchBuffer, cgoAllocsUnknown - __ret := C.lzfse_encode_buffer(cdstBuffer, cdstSize, csrcBuffer, csrcSize, cscratchBuffer) - __v := (uint)(__ret) - return __v + cdstSize, _ := (C.size_t)(len(dstBuffer)), cgoAllocsUnknown + __ret := C.lzfse_encode_buffer(cdstBuffer, cdstSize, csrcBuffer, csrcSize, nil) + out_size := (C.size_t)(__ret) + return dstBuffer[:out_size] } // DecodeScratchSize function as declared in lzfse.h:94 @@ -151,8 +151,17 @@ func DecodeLZVNBuffer(encBuf, decBuf []byte) uint { func testDecodeBuffer(t *testing.T, encBuf, wantBuf []byte) { t.Run("README", func(t *testing.T) { - if got := DecodeBuffer(encBuf); !bytes.Contains(got, wantBuf) { - ioutil.WriteFile("fail.out", got, 0755) + if got := DecodeBuffer(encBuf); !bytes.Equal(got, wantBuf) { + os.WriteFile("fail.out", got, 0755) + t.Errorf("DecodeBuffer() = %v, want %v", got, wantBuf) + } + }) +} + +func testEncodeBuffer(t *testing.T, encBuf, wantBuf []byte) { + t.Run("README", func(t *testing.T) { + if got := EncodeBuffer(encBuf); !bytes.Equal(got, wantBuf) { + os.WriteFile("fail.out", got, 0755) t.Errorf("DecodeBuffer() = %v, want %v", got, wantBuf) } }) @@ -162,7 +171,7 @@ func testDecodeLZVNBuffer(t *testing.T, encBuf, wantBuf []byte) { t.Run("test/lzvn_enc.bin", func(t *testing.T) { got := make([]byte, 68608) if DecodeLZVNBuffer(encBuf, got); !bytes.Contains(got, wantBuf) { - if err := ioutil.WriteFile("fail.out", got, 0755); err != nil { + if err := os.WriteFile("fail.out", got, 0755); err != nil { t.Errorf("failed to write fail.out: %v", err) } t.Errorf("DecodeLZVNBuffer() = %v, want %v", got, wantBuf) @@ -174,7 +183,7 @@ func testEncodeLZVNBuffer(t *testing.T, srcBuf, wantBuf []byte) { t.Run("test/lzvn_dec.bin", func(t *testing.T) { got := make([]byte, len(srcBuf)*4) if EncodeLZVNBuffer(srcBuf, got); !bytes.Contains(got, wantBuf) { - if err := ioutil.WriteFile("fail.out", got, 0755); err != nil { + if err := os.WriteFile("fail.out", got, 0755); err != nil { t.Errorf("failed to write fail.out: %v", err) } t.Errorf("EncodeLZVNBuffer() = %v, want %v", got, wantBuf) diff --git a/lzfse_test.go b/lzfse_test.go index 7666314..345a52a 100644 --- a/lzfse_test.go +++ b/lzfse_test.go @@ -1,28 +1,40 @@ package lzfse import ( - "io/ioutil" + "os" "testing" ) func TestDecodeBuffer(t *testing.T) { - wantBuf, err := ioutil.ReadFile("test/dec.bin") + wantBuf, err := os.ReadFile("test/dec.bin") if err != nil { t.Errorf("failed to read test file 'test/dec.bin': %v", err) } - encBuff, err := ioutil.ReadFile("test/enc.bin") + encBuff, err := os.ReadFile("test/enc.bin") if err != nil { t.Errorf("failed to read test file 'test/enc.bin': %v", err) } testDecodeBuffer(t, encBuff, wantBuf) } +func TestEncodeBuffer(t *testing.T) { + wantBuf, err := os.ReadFile("test/enc.bin") + if err != nil { + t.Errorf("failed to read test file 'test/enc.bin': %v", err) + } + decBuff, err := os.ReadFile("test/dec.bin") + if err != nil { + t.Errorf("failed to read test file 'test/dec.bin': %v", err) + } + testEncodeBuffer(t, decBuff, wantBuf) +} + func TestDecodeLZVNBuffer(t *testing.T) { - wantBuf, err := ioutil.ReadFile("test/lzvn_dec.bin") + wantBuf, err := os.ReadFile("test/lzvn_dec.bin") if err != nil { t.Errorf("failed to read test file 'test/lzvn_dec.bin': %v", err) } - encBuff, err := ioutil.ReadFile("test/lzvn_enc.bin") + encBuff, err := os.ReadFile("test/lzvn_enc.bin") if err != nil { t.Errorf("failed to read test file 'test/lzvn_enc.bin': %v", err) } @@ -30,11 +42,11 @@ func TestDecodeLZVNBuffer(t *testing.T) { } func TestEncodeLZVNBuffer(t *testing.T) { - wantBuf, err := ioutil.ReadFile("test/lzvn_enc.bin") + wantBuf, err := os.ReadFile("test/lzvn_enc.bin") if err != nil { t.Errorf("failed to read test file 'test/lzvn_enc.bin': %v", err) } - srcBuff, err := ioutil.ReadFile("test/lzvn_dec.bin") + srcBuff, err := os.ReadFile("test/lzvn_dec.bin") if err != nil { t.Errorf("failed to read test file 'test/lzvn_dec.bin': %v", err) }