ZLib and Mojo help needed #1861
-
Hello! Can somebody help me with zlib compression with Mojo ffi module? I don't really get C, because I came mostly from Python and C# and I don't really understand how to work with C types. ZLib docs says (https://arc.net/l/quote/shbynbdx): ZEXTERN int ZEXPORT compress(Bytef *dest, uLongf *destLen,
const Bytef *source, uLong sourceLen); I don't understand how to pass to zlib a pointer to variable that should be modified, aka I've tried my best and still doesn't get it - code below runs perfectly, but compressed is still empty after all: from sys import ffi, external_call
alias Bytef = Pointer[SIMD[DType.int8, 1]]
alias zlib_compress_type = fn(_out: Bytef, _out_len: Pointer[Int64], _in: Bytef, _in_len: Int64) -> NoneType
fn main() raises:
var handle = ffi.DLHandle('/Users/toiletsandpaper/Documents/mojo_test/zlib-1.3.1/libz.dylib')
var zlib_compress = handle.get_function[zlib_compress_type]('compress')
var text: StringRef = StringRef("some text")
var compressed: StringRef = StringRef("")
var comp_len = Pointer[Int64].alloc(1)
_ = zlib_compress(
compressed.data._as_scalar_pointer(),
comp_len,
text.data._as_scalar_pointer(),
len(text),
)
print(text)
print(compressed) Asking for help - I really need an example of how to call |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
So, with the help of @jackos in Discord
|
Beta Was this translation helpful? Give feedback.
So, with the help of @jackos in Discord
I've managed to finally wrote it: https://gist.github.com/toiletsandpaper/591dcd934d028440cafea22d5690d19f