Skip to content

Instantly share code, notes, and snippets.

@csghone
Last active May 7, 2020 07:03
Show Gist options
  • Save csghone/9044d1bc0aa62df77ccbba6d2e046eb6 to your computer and use it in GitHub Desktop.
Save csghone/9044d1bc0aa62df77ccbba6d2e046eb6 to your computer and use it in GitHub Desktop.
CFFI Python Example - Part 1/2
# Generate module `cffi_example` using `python3 generate_cffi_example_module.py`
import os
from cffi import FFI
ffi = FFI()
person_header = """
unsigned int decompress(const uint8_t* raw_data, size_t compressed_size, uint8_t* decompressed_data);
"""
ffi.set_source("cffi_example.lzo_ext", person_header + """
#include "lzo/lzo1z.h"
unsigned int decompress(const uint8_t* raw_data, size_t compressed_size, uint8_t* decompressed_data)
{
long unsigned int decompressed_length_size = 0;
lzo1z_decompress ((const unsigned char *) raw_data, compressed_size,
(unsigned char *) decompressed_data, &decompressed_length_size, NULL);
return decompressed_length_size;
}
""", libraries=["lzo2"])
ffi.cdef(person_header)
if __name__ == "__main__":
ffi.compile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment