telegabber/e2ee/omemo/libsignal/buffer.go
2026-07-28 00:38:46 -04:00

50 lines
1.4 KiB
Go

package libsignal
/*
#include <signal_protocol.h>
*/
import "C"
import "unsafe"
// bytesToBuffer copies a Go byte slice into a newly allocated signal_buffer.
// Ownership of the returned buffer transfers to whichever C API consumes it
// (matching the library's own buffer-ownership convention documented in
// signal_protocol.h).
func bytesToBuffer(b []byte) *C.signal_buffer {
var ptr *C.uint8_t
if len(b) > 0 {
ptr = (*C.uint8_t)(unsafe.Pointer(&b[0]))
}
return C.signal_buffer_create(ptr, C.size_t(len(b)))
}
// bufferToBytes copies the contents of a signal_buffer into a new Go byte
// slice. It does not free or otherwise take ownership of buf.
func bufferToBytes(buf *C.signal_buffer) []byte {
if buf == nil {
return nil
}
n := C.signal_buffer_len(buf)
if n == 0 {
return []byte{}
}
return C.GoBytes(unsafe.Pointer(C.signal_buffer_const_data(buf)), C.int(n))
}
// freeBuffer frees a signal_buffer allocated by the library.
func freeBuffer(buf *C.signal_buffer) {
if buf != nil {
C.signal_buffer_free(buf)
}
}
// cBytesToGo copies a raw C byte pointer + length into a new Go byte slice.
// Used for reading uint8_t*/size_t pairs (as opposed to signal_buffer*) out
// of C structures, e.g. session_signed_pre_key signatures.
func cBytesToGo(data *C.uint8_t, length C.size_t) []byte {
if data == nil || length == 0 {
return nil
}
return C.GoBytes(unsafe.Pointer(data), C.int(length))
}