Problem interfacing with C library (QuickLZ)

Hello fellow camels!

I’ve recently fell in love with OCaml and use it on my free time a lot lately.

Are there any good sources to learn how the Ctypes library works? I’m trying to interact with a c library called QuickLZ as there doesn’t seem to be a native way to interact with files that are QuickLZ compressed (GitHub - RT-Thread-packages/quicklz: the world's fastest compression library).

I’m able to interact with most functions within the library but when it comes to more advanced calls that takes c structs I’m getting stuck. How do I send a qlz_state_decompress struct to the function?

Mainly it is this function I am not able to interact with:

size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state)

Are there any good sources that I could read to be able to read to further my understanding on how to interact with c-libraries?


1 Like

Maybe this example may help you

module InterfaceInfo = struct
  let init_func = funptr @@ repr @-> repr @->> void
  let finalize_func = funptr @@ repr @-> repr @->> void

  type t
  let t : t structure typ = structure "GInterfaceInfo"
  let interface_init = field t "interface_init" init_func
  let interface_finalize = field t "interface_finalize" finalize_func
  let interface_data = field t "interface_data" repr
  let () = seal t

Here is how you define the shape of a C structure, (just in case repr is void *) and here is how you use it.

 let interface_info = make InterfaceInfo.t in
    setf interface_info InterfaceInfo.interface_init init;
    setf interface_info InterfaceInfo.interface_finalize interface_default_finalize;
    setf interface_info InterfaceInfo.interface_data null;

    do_something param1 param2 (addr interface_info)

Obviously, there is no need to create a separate module to define a structure that is just an example

1 Like