Skip to content

Commit

Permalink
fix: lzfse.EncodeBuffer to have same format as DecodeBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Jul 31, 2024
1 parent dacd2c0 commit 00e6b37
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -47,4 +47,4 @@ func main() {

## License

MIT Copyright (c) 2019-2022 blacktop
MIT Copyright (c) 2019-2024 blacktop
35 changes: 22 additions & 13 deletions lzfse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
})
Expand All @@ -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)
Expand All @@ -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)
Expand Down
26 changes: 19 additions & 7 deletions lzfse_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
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)
}
testDecodeLZVNBuffer(t, encBuff, wantBuf)
}

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)
}
Expand Down

0 comments on commit 00e6b37

Please sign in to comment.