def test_it(self): pydffi.dlopen(_HERE + '/library.so') FFI = pydffi.FFI() CU = FFI.cdef('#include "{}/library.h"'.format(_HERE)) foo = CU.types.Foo(bar=1) CU.funcs.by_ptr(FFI.ptr(foo)) self.assertEqual(foo.bar, 2);
def __init__(self, llvm_root): pydffi.dlopen(os.path.join(llvm_root, "lib", "libLLVM.so")) self.FFI=pydffi.FFI(includeDirs=[os.path.join(llvm_root, "include")]) self.CU=self.FFI.cdef(''' #include <llvm-c/Target.h> #include <llvm-c/Disassembler.h> size_t disasm(LLVMDisasmContextRef Disasm, uint8_t const* In, size_t InLen, uint64_t Addr, char* Out, size_t OutLen) { return LLVMDisasmInstruction(Disasm, (uint8_t*)In, InLen, Addr, Out, OutLen); } ''') self.CU.funcs.LLVMInitializeX86Disassembler() self.CU.funcs.LLVMInitializeX86Target() self.CU.funcs.LLVMInitializeX86TargetInfo() self.CU.funcs.LLVMInitializeX86TargetMC() self.llvm_disasm = self.CU.funcs.LLVMCreateDisasm("x86_64-pc-linux-gnu", (self.FFI.VoidPtrTy)(), self.FFI.Int(0), (self.FFI.VoidPtrTy)(), (self.FFI.VoidPtrTy)()) if int(self.llvm_disasm) == 0: raise RuntimeError("unable to create an LLVM disassembler engine") # Set Intel syntax self.CU.funcs.LLVMSetDisasmOptions(self.llvm_disasm, 4) self.LLVMDisasmInstruction = self.CU.funcs.disasm self.tmpbuf = self.FFI.arrayType(self.FFI.CharTy, 256)()
import pydffi import sys import os import sage import random if len(sys.argv) <= 1: print("Usage: %s path/to/libsss.so" % sys.argv[0], file=sys.stderr) sys.exit(1) pydffi.dlopen(sys.argv[1]) FFI=pydffi.FFI() CU=FFI.cdef(''' #include <stdint.h> void sss_create_keyshares_impl(uint8_t *out, const uint8_t* key, const uint8_t* poly, // was uint32_t* uint8_t n, uint8_t k); int sss_decrypt(uint8_t *out, uint8_t const* in, uint8_t const* key); ''') LEN_KEY=32 LEN_POLY=32 LEN_KEYSHARE=32+1 NSHARES=3 NPOLYS = NSHARES-1 LEN_POLYS=LEN_POLY*NPOLYS LEN_KEYSHARES=LEN_KEYSHARE*NSHARES
import pydffi import sys pydffi.dlopen("/usr/lib/x86_64-linux-gnu/libarchive.so") D = pydffi.FFI() CU = D.cdef(''' #include <archive.h> #include <archive_entry.h> ''') funcs = CU.funcs archive_read_next_header = funcs.archive_read_next_header archive_entry_pathname_utf8 = funcs.archive_entry_pathname_utf8 archive_read_data_skip = funcs.archive_read_data_skip a = funcs.archive_read_new() funcs.archive_read_support_filter_all(a) funcs.archive_read_support_format_all(a) r = funcs.archive_read_open_filename(a, sys.argv[1], 10240) if r != 0: raise RuntimeError("unable to open archive") entry = D.ptr(CU.types.archive_entry)() while archive_read_next_header(a, D.ptr(entry)) == 0: pathname = archive_entry_pathname_utf8(entry) print(pathname.cstr.tobytes().decode("utf8")) archive_read_data_skip(a) funcs.archive_read_free(a)
import math import pydffi pydffi.dlopen("/usr/lib/x86_64-linux-gnu/libfftw3.so") FFI = pydffi.FFI() FFT = FFI.cdef("#include <fftw3.h>") # Adapted from https://github.com/undees/fftw-example/blob/master/fftw_example.c NUM_POINTS = 64 fftw_complex = FFT.types.fftw_complex signal = FFI.arrayType(fftw_complex, NUM_POINTS)(); result = FFI.arrayType(fftw_complex, NUM_POINTS)(); def acquire_from_somewhere(signal): for i in range(NUM_POINTS): theta = float(i) / float(NUM_POINTS) * math.pi; signal[i][0] = 1.0 * math.cos(10.0 * theta) + \ 0.5 * math.cos(25.0 * theta); signal[i][1] = 1.0 * math.sin(10.0 * theta) + \ 0.5 * math.sin(25.0 * theta); def do_something_with(result): for i in range(NUM_POINTS): mag = math.sqrt(result[i][0] * result[i][0] + \ result[i][1] * result[i][1]); print("%0.4f" % mag); plan = FFT.funcs.fftw_plan_dft_1d(NUM_POINTS, signal, result, -1, 1<<6)
# REQUIRES: linux && libarchive # RUN: tar cf %t.tar %s # RUN: %python "%s" "%t.tar" | %FileCheck "%s" # CHECK: archive.py import pydffi import ctypes.util import sys pydffi.dlopen(ctypes.util.find_library("archive")) D = pydffi.FFI() CU = D.cdef(''' #include <archive.h> #include <archive_entry.h> ''') funcs = CU.funcs archive_read_next_header = funcs.archive_read_next_header archive_entry_pathname_utf8 = funcs.archive_entry_pathname_utf8 archive_read_data_skip = funcs.archive_read_data_skip a = funcs.archive_read_new() funcs.archive_read_support_filter_all(a) funcs.archive_read_support_format_all(a) r = funcs.archive_read_open_filename(a, sys.argv[1], 10240) if r != 0: raise RuntimeError("unable to open archive") entry = pydffi.ptr(CU.types.archive_entry)() while archive_read_next_header(a, pydffi.ptr(entry)) == 0: pathname = archive_entry_pathname_utf8(entry)
# REQUIRES: linux && libfftw3 # RUN: %python "%s" import math import ctypes.util import pydffi pydffi.dlopen(ctypes.util.find_library("fftw3")) FFI = pydffi.FFI() FFT = FFI.cdef("#include <fftw3.h>") # Adapted from https://github.com/undees/fftw-example/blob/master/fftw_example.c NUM_POINTS = 64 fftw_complex = FFT.types.fftw_complex signal = FFI.arrayType(fftw_complex, NUM_POINTS)() result = FFI.arrayType(fftw_complex, NUM_POINTS)() def acquire_from_somewhere(signal): for i in range(NUM_POINTS): theta = float(i) / float(NUM_POINTS) * math.pi signal[i][0] = 1.0 * math.cos(10.0 * theta) + \ 0.5 * math.cos(25.0 * theta) signal[i][1] = 1.0 * math.sin(10.0 * theta) + \ 0.5 * math.sin(25.0 * theta) def do_something_with(result): for i in range(NUM_POINTS):