I'm using cgo to call a function in a dynamic library whose signature looks like this:
int decompress(int, const uint8_t *, size_t, uint8_t *, size_t);
Here is my go code:
// #include statements here
import "C"
import (
    "unsafe"
)
func Decompress(comp_type int, data string, expected_size int) []byte {
    compressedData := C.CString(data)
    defer C.free(unsafe.Pointer(compressedData))
    compressedDataSize := C.ulong(len(data))
    decompressedData := C.malloc(C.ulong(C.sizeof_char * expected_size))
    defer C.free(unsafe.Pointer(decompressedData))
    decompressedDataSize := C.ulong(expected_size)
    ret_val := C.XpressDecompress(C.int(comp_type),
                                  (*C.uchar) (compressedData),
                                  compressedDataSize,
                                  (*C.uchar) (decompressedData),
                                  &decompressedDataSize)
    decompressed := C.GoBytes(decompressedData, C.int(decompressedDataSize))
    return decompressed
}
However, I'm getting this error when I try to build: 
cannot convert compressedData (type *_Ctype_char) to type *_Ctype_uchar
What is the best way for me to convert the input string into a const unsigned char*?
The following worked:
compressedData := unsafe.Pointer(data)
and
(*C.uchar) (compressedData)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With