diff --git a/internal/native/cgo_read.go b/internal/native/cgo_read.go index 8a27755..87cb8fb 100644 --- a/internal/native/cgo_read.go +++ b/internal/native/cgo_read.go @@ -15,13 +15,12 @@ import "C" import ( "io" "log" - "unsafe" ) //export cgo_read_buffer func cgo_read_buffer( bufferIndex C.int, - bufPtr unsafe.Pointer, + bufPtr *byte, length C.int, ) C.int { goLength := int(length) diff --git a/internal/native/cgo_write.go b/internal/native/cgo_write.go index efc1394..16085f6 100644 --- a/internal/native/cgo_write.go +++ b/internal/native/cgo_write.go @@ -11,12 +11,11 @@ package native #include "bsdiff.h" */ import "C" -import "unsafe" //export cgo_write_buffer func cgo_write_buffer( bufferIndex C.int, - dataPtr unsafe.Pointer, + dataPtr *byte, size C.int, ) C.int { buffer := writers.Get(int(bufferIndex)) diff --git a/internal/native/native.go b/internal/native/native.go index 9ef23f3..4f5c179 100644 --- a/internal/native/native.go +++ b/internal/native/native.go @@ -28,12 +28,12 @@ func bytesToUint8PtrAndSize(bytes []byte) (ptr *C.uint8_t, size C.int64_t) { return } -func cPtrToSlice(ptr unsafe.Pointer, size int) []byte { +func cPtrToSlice(ptr *byte, size int) []byte { var slice []byte sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice)) sliceHeader.Cap = size sliceHeader.Len = size - sliceHeader.Data = uintptr(ptr) + sliceHeader.Data = uintptr(unsafe.Pointer(ptr)) return slice } diff --git a/internal/native/native_internal_test.go b/internal/native/native_internal_test.go new file mode 100644 index 0000000..8a3acef --- /dev/null +++ b/internal/native/native_internal_test.go @@ -0,0 +1,38 @@ +// Copyright (c) 2017-2025 Carl Kittelberger. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE.txt file. + +//go:build cgo && go1.20 +// +build cgo,go1.20 + +package native + +import ( + "crypto/rand" + "testing" + "unsafe" + + "github.com/stretchr/testify/assert" +) + +const testBytesLen = 512 * 1024 + +func makeTestBytes() []byte { + b := make([]byte, testBytesLen) + _, _ = rand.Read(b) + return b +} + +func Test_bytesToUint8PtrAndSize(t *testing.T) { + b := makeTestBytes() + ptr, size := bytesToUint8PtrAndSize(b) + assert.EqualValues(t, &b[0], ptr) + assert.EqualValues(t, testBytesLen, size) +} + +func Test_cPtrToSlice(t *testing.T) { + b := makeTestBytes() + bytePtr := unsafe.SliceData(b) + b2 := cPtrToSlice(bytePtr, testBytesLen) + assert.Equal(t, b, b2) +}