Drew Short 9 years ago
parent
commit
a58e44c343
  1. 6
      .gitignore
  2. 638
      FFI-tests/_ffi_test_py.c
  3. 112
      FFI-tests/ffi_test.c
  4. 20
      FFI-tests/ffi_test.py
  5. 26
      FFI-tests/ffi_test_build.py
  6. 7
      ffi/pihash.h
  7. 62
      src/cache.rs
  8. 23
      src/hash.rs
  9. 6
      src/lib.rs

6
.gitignore

@ -8,6 +8,9 @@
*.rlib
*.dll
# logging files
*.log
# Executables
*.exe
@ -22,6 +25,7 @@ Cargo.lock
# Ignore sublime workspace files
*.sublime-workspace
*.sublime-project
#Rustfmt backup files
*.rs.bk
@ -29,3 +33,5 @@ Cargo.lock
# FFI test binaries
FFI-tests/*.so
FFI-tests/*.o
FFI-tests/*.out
FFI-tests/_*.c

638
FFI-tests/_ffi_test_py.c

@ -1,638 +0,0 @@
#define _CFFI_
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
/* This part is from file 'cffi/parse_c_type.h'. It is copied at the
beginning of C sources generated by CFFI's ffi.set_source(). */
typedef void *_cffi_opcode_t;
#define _CFFI_OP(opcode, arg) (_cffi_opcode_t)(opcode | (((uintptr_t)(arg)) << 8))
#define _CFFI_GETOP(cffi_opcode) ((unsigned char)(uintptr_t)cffi_opcode)
#define _CFFI_GETARG(cffi_opcode) (((intptr_t)cffi_opcode) >> 8)
#define _CFFI_OP_PRIMITIVE 1
#define _CFFI_OP_POINTER 3
#define _CFFI_OP_ARRAY 5
#define _CFFI_OP_OPEN_ARRAY 7
#define _CFFI_OP_STRUCT_UNION 9
#define _CFFI_OP_ENUM 11
#define _CFFI_OP_FUNCTION 13
#define _CFFI_OP_FUNCTION_END 15
#define _CFFI_OP_NOOP 17
#define _CFFI_OP_BITFIELD 19
#define _CFFI_OP_TYPENAME 21
#define _CFFI_OP_CPYTHON_BLTN_V 23 // varargs
#define _CFFI_OP_CPYTHON_BLTN_N 25 // noargs
#define _CFFI_OP_CPYTHON_BLTN_O 27 // O (i.e. a single arg)
#define _CFFI_OP_CONSTANT 29
#define _CFFI_OP_CONSTANT_INT 31
#define _CFFI_OP_GLOBAL_VAR 33
#define _CFFI_OP_DLOPEN_FUNC 35
#define _CFFI_OP_DLOPEN_CONST 37
#define _CFFI_OP_GLOBAL_VAR_F 39
#define _CFFI_OP_EXTERN_PYTHON 41
#define _CFFI_PRIM_VOID 0
#define _CFFI_PRIM_BOOL 1
#define _CFFI_PRIM_CHAR 2
#define _CFFI_PRIM_SCHAR 3
#define _CFFI_PRIM_UCHAR 4
#define _CFFI_PRIM_SHORT 5
#define _CFFI_PRIM_USHORT 6
#define _CFFI_PRIM_INT 7
#define _CFFI_PRIM_UINT 8
#define _CFFI_PRIM_LONG 9
#define _CFFI_PRIM_ULONG 10
#define _CFFI_PRIM_LONGLONG 11
#define _CFFI_PRIM_ULONGLONG 12
#define _CFFI_PRIM_FLOAT 13
#define _CFFI_PRIM_DOUBLE 14
#define _CFFI_PRIM_LONGDOUBLE 15
#define _CFFI_PRIM_WCHAR 16
#define _CFFI_PRIM_INT8 17
#define _CFFI_PRIM_UINT8 18
#define _CFFI_PRIM_INT16 19
#define _CFFI_PRIM_UINT16 20
#define _CFFI_PRIM_INT32 21
#define _CFFI_PRIM_UINT32 22
#define _CFFI_PRIM_INT64 23
#define _CFFI_PRIM_UINT64 24
#define _CFFI_PRIM_INTPTR 25
#define _CFFI_PRIM_UINTPTR 26
#define _CFFI_PRIM_PTRDIFF 27
#define _CFFI_PRIM_SIZE 28
#define _CFFI_PRIM_SSIZE 29
#define _CFFI_PRIM_INT_LEAST8 30
#define _CFFI_PRIM_UINT_LEAST8 31
#define _CFFI_PRIM_INT_LEAST16 32
#define _CFFI_PRIM_UINT_LEAST16 33
#define _CFFI_PRIM_INT_LEAST32 34
#define _CFFI_PRIM_UINT_LEAST32 35
#define _CFFI_PRIM_INT_LEAST64 36
#define _CFFI_PRIM_UINT_LEAST64 37
#define _CFFI_PRIM_INT_FAST8 38
#define _CFFI_PRIM_UINT_FAST8 39
#define _CFFI_PRIM_INT_FAST16 40
#define _CFFI_PRIM_UINT_FAST16 41
#define _CFFI_PRIM_INT_FAST32 42
#define _CFFI_PRIM_UINT_FAST32 43
#define _CFFI_PRIM_INT_FAST64 44
#define _CFFI_PRIM_UINT_FAST64 45
#define _CFFI_PRIM_INTMAX 46
#define _CFFI_PRIM_UINTMAX 47
#define _CFFI__NUM_PRIM 48
#define _CFFI__UNKNOWN_PRIM (-1)
#define _CFFI__UNKNOWN_FLOAT_PRIM (-2)
#define _CFFI__UNKNOWN_LONG_DOUBLE (-3)
#define _CFFI__IO_FILE_STRUCT (-1)
struct _cffi_global_s {
const char *name;
void *address;
_cffi_opcode_t type_op;
void *size_or_direct_fn; // OP_GLOBAL_VAR: size, or 0 if unknown
// OP_CPYTHON_BLTN_*: addr of direct function
};
struct _cffi_getconst_s {
unsigned long long value;
const struct _cffi_type_context_s *ctx;
int gindex;
};
struct _cffi_struct_union_s {
const char *name;
int type_index; // -> _cffi_types, on a OP_STRUCT_UNION
int flags; // _CFFI_F_* flags below
size_t size;
int alignment;
int first_field_index; // -> _cffi_fields array
int num_fields;
};
#define _CFFI_F_UNION 0x01 // is a union, not a struct
#define _CFFI_F_CHECK_FIELDS 0x02 // complain if fields are not in the
// "standard layout" or if some are missing
#define _CFFI_F_PACKED 0x04 // for CHECK_FIELDS, assume a packed struct
#define _CFFI_F_EXTERNAL 0x08 // in some other ffi.include()
#define _CFFI_F_OPAQUE 0x10 // opaque
struct _cffi_field_s {
const char *name;
size_t field_offset;
size_t field_size;
_cffi_opcode_t field_type_op;
};
struct _cffi_enum_s {
const char *name;
int type_index; // -> _cffi_types, on a OP_ENUM
int type_prim; // _CFFI_PRIM_xxx
const char *enumerators; // comma-delimited string
};
struct _cffi_typename_s {
const char *name;
int type_index; /* if opaque, points to a possibly artificial
OP_STRUCT which is itself opaque */
};
struct _cffi_type_context_s {
_cffi_opcode_t *types;
const struct _cffi_global_s *globals;
const struct _cffi_field_s *fields;
const struct _cffi_struct_union_s *struct_unions;
const struct _cffi_enum_s *enums;
const struct _cffi_typename_s *typenames;
int num_globals;
int num_struct_unions;
int num_enums;
int num_typenames;
const char *const *includes;
int num_types;
int flags; /* future extension */
};
struct _cffi_parse_info_s {
const struct _cffi_type_context_s *ctx;
_cffi_opcode_t *output;
unsigned int output_size;
size_t error_location;
const char *error_message;
};
struct _cffi_externpy_s {
const char *name;
size_t size_of_result;
void *reserved1, *reserved2;
};
#ifdef _CFFI_INTERNAL
static int parse_c_type(struct _cffi_parse_info_s *info, const char *input);
static int search_in_globals(const struct _cffi_type_context_s *ctx,
const char *search, size_t search_len);
static int search_in_struct_unions(const struct _cffi_type_context_s *ctx,
const char *search, size_t search_len);
#endif
/* this block of #ifs should be kept exactly identical between
c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py
and cffi/_cffi_include.h */
#if defined(_MSC_VER)
# include <malloc.h> /* for alloca() */
# if _MSC_VER < 1600 /* MSVC < 2010 */
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
typedef __int8 int_least8_t;
typedef __int16 int_least16_t;
typedef __int32 int_least32_t;
typedef __int64 int_least64_t;
typedef unsigned __int8 uint_least8_t;
typedef unsigned __int16 uint_least16_t;
typedef unsigned __int32 uint_least32_t;
typedef unsigned __int64 uint_least64_t;
typedef __int8 int_fast8_t;
typedef __int16 int_fast16_t;
typedef __int32 int_fast32_t;
typedef __int64 int_fast64_t;
typedef unsigned __int8 uint_fast8_t;
typedef unsigned __int16 uint_fast16_t;
typedef unsigned __int32 uint_fast32_t;
typedef unsigned __int64 uint_fast64_t;
typedef __int64 intmax_t;
typedef unsigned __int64 uintmax_t;
# else
# include <stdint.h>
# endif
# if _MSC_VER < 1800 /* MSVC < 2013 */
typedef unsigned char _Bool;
# endif
#else
# include <stdint.h>
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux)
# include <alloca.h>
# endif
#endif
#ifdef __GNUC__
# define _CFFI_UNUSED_FN __attribute__((unused))
#else
# define _CFFI_UNUSED_FN /* nothing */
#endif
/********** CPython-specific section **********/
#ifndef PYPY_VERSION
#if PY_MAJOR_VERSION >= 3
# define PyInt_FromLong PyLong_FromLong
#endif
#define _cffi_from_c_double PyFloat_FromDouble
#define _cffi_from_c_float PyFloat_FromDouble
#define _cffi_from_c_long PyInt_FromLong
#define _cffi_from_c_ulong PyLong_FromUnsignedLong
#define _cffi_from_c_longlong PyLong_FromLongLong
#define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong
#define _cffi_to_c_double PyFloat_AsDouble
#define _cffi_to_c_float PyFloat_AsDouble
#define _cffi_from_c_int(x, type) \
(((type)-1) > 0 ? /* unsigned */ \
(sizeof(type) < sizeof(long) ? \
PyInt_FromLong((long)x) : \
sizeof(type) == sizeof(long) ? \
PyLong_FromUnsignedLong((unsigned long)x) : \
PyLong_FromUnsignedLongLong((unsigned long long)x)) : \
(sizeof(type) <= sizeof(long) ? \
PyInt_FromLong((long)x) : \
PyLong_FromLongLong((long long)x)))
#define _cffi_to_c_int(o, type) \
((type)( \
sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \
: (type)_cffi_to_c_i8(o)) : \
sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \
: (type)_cffi_to_c_i16(o)) : \
sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \
: (type)_cffi_to_c_i32(o)) : \
sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \
: (type)_cffi_to_c_i64(o)) : \
(Py_FatalError("unsupported size for type " #type), (type)0)))
#define _cffi_to_c_i8 \
((int(*)(PyObject *))_cffi_exports[1])
#define _cffi_to_c_u8 \
((int(*)(PyObject *))_cffi_exports[2])
#define _cffi_to_c_i16 \
((int(*)(PyObject *))_cffi_exports[3])
#define _cffi_to_c_u16 \
((int(*)(PyObject *))_cffi_exports[4])
#define _cffi_to_c_i32 \
((int(*)(PyObject *))_cffi_exports[5])
#define _cffi_to_c_u32 \
((unsigned int(*)(PyObject *))_cffi_exports[6])
#define _cffi_to_c_i64 \
((long long(*)(PyObject *))_cffi_exports[7])
#define _cffi_to_c_u64 \
((unsigned long long(*)(PyObject *))_cffi_exports[8])
#define _cffi_to_c_char \
((int(*)(PyObject *))_cffi_exports[9])
#define _cffi_from_c_pointer \
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10])
#define _cffi_to_c_pointer \
((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11])
#define _cffi_get_struct_layout \
not used any more
#define _cffi_restore_errno \
((void(*)(void))_cffi_exports[13])
#define _cffi_save_errno \
((void(*)(void))_cffi_exports[14])
#define _cffi_from_c_char \
((PyObject *(*)(char))_cffi_exports[15])
#define _cffi_from_c_deref \
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16])
#define _cffi_to_c \
((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17])
#define _cffi_from_c_struct \
((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18])
#define _cffi_to_c_wchar_t \
((wchar_t(*)(PyObject *))_cffi_exports[19])
#define _cffi_from_c_wchar_t \
((PyObject *(*)(wchar_t))_cffi_exports[20])
#define _cffi_to_c_long_double \
((long double(*)(PyObject *))_cffi_exports[21])
#define _cffi_to_c__Bool \
((_Bool(*)(PyObject *))_cffi_exports[22])
#define _cffi_prepare_pointer_call_argument \
((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23])
#define _cffi_convert_array_from_object \
((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24])
#define _cffi_call_python \
((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[25])
#define _CFFI_NUM_EXPORTS 26
typedef struct _ctypedescr CTypeDescrObject;
static void *_cffi_exports[_CFFI_NUM_EXPORTS];
#define _cffi_type(index) ( \
assert((((uintptr_t)_cffi_types[index]) & 1) == 0), \
(CTypeDescrObject *)_cffi_types[index])
static PyObject *_cffi_init(const char *module_name, Py_ssize_t version,
const struct _cffi_type_context_s *ctx)
{
PyObject *module, *o_arg, *new_module;
void *raw[] = {
(void *)module_name,
(void *)version,
(void *)_cffi_exports,
(void *)ctx,
};
module = PyImport_ImportModule("_cffi_backend");
if (module == NULL)
goto failure;
o_arg = PyLong_FromVoidPtr((void *)raw);
if (o_arg == NULL)
goto failure;
new_module = PyObject_CallMethod(
module, (char *)"_init_cffi_1_0_external_module", (char *)"O", o_arg);
Py_DECREF(o_arg);
Py_DECREF(module);
return new_module;
failure:
Py_XDECREF(module);
return NULL;
}
_CFFI_UNUSED_FN
static PyObject **_cffi_unpack_args(PyObject *args_tuple, Py_ssize_t expected,
const char *fnname)
{
if (PyTuple_GET_SIZE(args_tuple) != expected) {
PyErr_Format(PyExc_TypeError,
"%.150s() takes exactly %zd arguments (%zd given)",
fnname, expected, PyTuple_GET_SIZE(args_tuple));
return NULL;
}
return &PyTuple_GET_ITEM(args_tuple, 0); /* pointer to the first item,
the others follow */
}
/********** end CPython-specific section **********/
#else
_CFFI_UNUSED_FN
static void (*_cffi_call_python)(struct _cffi_externpy_s *, char *);
#endif
#define _cffi_array_len(array) (sizeof(array) / sizeof((array)[0]))
#define _cffi_prim_int(size, sign) \
((size) == 1 ? ((sign) ? _CFFI_PRIM_INT8 : _CFFI_PRIM_UINT8) : \
(size) == 2 ? ((sign) ? _CFFI_PRIM_INT16 : _CFFI_PRIM_UINT16) : \
(size) == 4 ? ((sign) ? _CFFI_PRIM_INT32 : _CFFI_PRIM_UINT32) : \
(size) == 8 ? ((sign) ? _CFFI_PRIM_INT64 : _CFFI_PRIM_UINT64) : \
_CFFI__UNKNOWN_PRIM)
#define _cffi_prim_float(size) \
((size) == sizeof(float) ? _CFFI_PRIM_FLOAT : \
(size) == sizeof(double) ? _CFFI_PRIM_DOUBLE : \
(size) == sizeof(long double) ? _CFFI__UNKNOWN_LONG_DOUBLE : \
_CFFI__UNKNOWN_FLOAT_PRIM)
#define _cffi_check_int(got, got_nonpos, expected) \
((got_nonpos) == (expected <= 0) && \
(got) == (unsigned long long)expected)
#ifdef __cplusplus
}
#endif
/************************************************************/
#include <dlfcn.h>
/************************************************************/
static void *_cffi_types[] = {
/* 0 */ _CFFI_OP(_CFFI_OP_FUNCTION, 6), // uint64_t()(char const *)
/* 1 */ _CFFI_OP(_CFFI_OP_POINTER, 5), // char const *
/* 2 */ _CFFI_OP(_CFFI_OP_FUNCTION_END, 0),
/* 3 */ _CFFI_OP(_CFFI_OP_FUNCTION, 7), // void()(void)
/* 4 */ _CFFI_OP(_CFFI_OP_FUNCTION_END, 0),
/* 5 */ _CFFI_OP(_CFFI_OP_PRIMITIVE, 2), // char
/* 6 */ _CFFI_OP(_CFFI_OP_PRIMITIVE, 24), // uint64_t
/* 7 */ _CFFI_OP(_CFFI_OP_PRIMITIVE, 0), // void
};
static uint64_t _cffi_d_ext_get_ahash(char const * x0)
{
return ext_get_ahash(x0);
}
#ifndef PYPY_VERSION
static PyObject *
_cffi_f_ext_get_ahash(PyObject *self, PyObject *arg0)
{
char const * x0;
Py_ssize_t datasize;
uint64_t result;
datasize = _cffi_prepare_pointer_call_argument(
_cffi_type(1), arg0, (char **)&x0);
if (datasize != 0) {
if (datasize < 0)
return NULL;
x0 = (char const *)alloca((size_t)datasize);
memset((void *)x0, 0, (size_t)datasize);
if (_cffi_convert_array_from_object((char *)x0, _cffi_type(1), arg0) < 0)
return NULL;
}
Py_BEGIN_ALLOW_THREADS
_cffi_restore_errno();
{ result = ext_get_ahash(x0); }
_cffi_save_errno();
Py_END_ALLOW_THREADS
(void)self; /* unused */
return _cffi_from_c_int(result, uint64_t);
}
#else
# define _cffi_f_ext_get_ahash _cffi_d_ext_get_ahash
#endif
static uint64_t _cffi_d_ext_get_dhash(char const * x0)
{
return ext_get_dhash(x0);
}
#ifndef PYPY_VERSION
static PyObject *
_cffi_f_ext_get_dhash(PyObject *self, PyObject *arg0)
{
char const * x0;
Py_ssize_t datasize;
uint64_t result;
datasize = _cffi_prepare_pointer_call_argument(
_cffi_type(1), arg0, (char **)&x0);
if (datasize != 0) {
if (datasize < 0)
return NULL;
x0 = (char const *)alloca((size_t)datasize);
memset((void *)x0, 0, (size_t)datasize);
if (_cffi_convert_array_from_object((char *)x0, _cffi_type(1), arg0) < 0)
return NULL;
}
Py_BEGIN_ALLOW_THREADS
_cffi_restore_errno();
{ result = ext_get_dhash(x0); }
_cffi_save_errno();
Py_END_ALLOW_THREADS
(void)self; /* unused */
return _cffi_from_c_int(result, uint64_t);
}
#else
# define _cffi_f_ext_get_dhash _cffi_d_ext_get_dhash
#endif
static uint64_t _cffi_d_ext_get_phash(char const * x0)
{
return ext_get_phash(x0);
}
#ifndef PYPY_VERSION
static PyObject *
_cffi_f_ext_get_phash(PyObject *self, PyObject *arg0)
{
char const * x0;
Py_ssize_t datasize;
uint64_t result;
datasize = _cffi_prepare_pointer_call_argument(
_cffi_type(1), arg0, (char **)&x0);
if (datasize != 0) {
if (datasize < 0)
return NULL;
x0 = (char const *)alloca((size_t)datasize);
memset((void *)x0, 0, (size_t)datasize);
if (_cffi_convert_array_from_object((char *)x0, _cffi_type(1), arg0) < 0)
return NULL;
}
Py_BEGIN_ALLOW_THREADS
_cffi_restore_errno();
{ result = ext_get_phash(x0); }
_cffi_save_errno();
Py_END_ALLOW_THREADS
(void)self; /* unused */
return _cffi_from_c_int(result, uint64_t);
}
#else
# define _cffi_f_ext_get_phash _cffi_d_ext_get_phash
#endif
static void _cffi_d_init(void)
{
init();
}
#ifndef PYPY_VERSION
static PyObject *
_cffi_f_init(PyObject *self, PyObject *noarg)
{
Py_BEGIN_ALLOW_THREADS
_cffi_restore_errno();
{ init(); }
_cffi_save_errno();
Py_END_ALLOW_THREADS
(void)self; /* unused */
(void)noarg; /* unused */
Py_INCREF(Py_None);
return Py_None;
}
#else
# define _cffi_f_init _cffi_d_init
#endif
static void _cffi_d_teardown(void)
{
teardown();
}
#ifndef PYPY_VERSION
static PyObject *
_cffi_f_teardown(PyObject *self, PyObject *noarg)
{
Py_BEGIN_ALLOW_THREADS
_cffi_restore_errno();
{ teardown(); }
_cffi_save_errno();
Py_END_ALLOW_THREADS
(void)self; /* unused */
(void)noarg; /* unused */
Py_INCREF(Py_None);
return Py_None;
}
#else
# define _cffi_f_teardown _cffi_d_teardown
#endif
static const struct _cffi_global_s _cffi_globals[] = {
{ "ext_get_ahash", (void *)_cffi_f_ext_get_ahash, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_O, 0), (void *)_cffi_d_ext_get_ahash },
{ "ext_get_dhash", (void *)_cffi_f_ext_get_dhash, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_O, 0), (void *)_cffi_d_ext_get_dhash },
{ "ext_get_phash", (void *)_cffi_f_ext_get_phash, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_O, 0), (void *)_cffi_d_ext_get_phash },
{ "init", (void *)_cffi_f_init, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_N, 3), (void *)_cffi_d_init },
{ "teardown", (void *)_cffi_f_teardown, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_N, 3), (void *)_cffi_d_teardown },
};
static const struct _cffi_type_context_s _cffi_type_context = {
_cffi_types,
_cffi_globals,
NULL, /* no fields */
NULL, /* no struct_unions */
NULL, /* no enums */
NULL, /* no typenames */
5, /* num_globals */
0, /* num_struct_unions */
0, /* num_enums */
0, /* num_typenames */
NULL, /* no includes */
8, /* num_types */
0, /* flags */
};
#ifdef PYPY_VERSION
PyMODINIT_FUNC
_cffi_pypyinit__ffi_test_py(const void *p[])
{
p[0] = (const void *)0x2601;
p[1] = &_cffi_type_context;
}
# ifdef _MSC_VER
PyMODINIT_FUNC
# if PY_MAJOR_VERSION >= 3
PyInit__ffi_test_py(void) { return NULL; }
# else
init_ffi_test_py(void) { }
# endif
# endif
#elif PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC
PyInit__ffi_test_py(void)
{
return _cffi_init("_ffi_test_py", 0x2601, &_cffi_type_context);
}
#else
PyMODINIT_FUNC
init_ffi_test_py(void)
{
_cffi_init("_ffi_test_py", 0x2601, &_cffi_type_context);
}
#endif

112
FFI-tests/ffi_test.c

@ -2,27 +2,109 @@
#include <stdlib.h>
#include <stdint.h>
#include <dlfcn.h>
#include <string.h>
/* function declaration */
void print_ustr_bytes(const char []);
int main() {
uint64_t (*ext_get_ahash)(const char *);
uint64_t (*ext_get_dhash)(const char *);
uint64_t (*ext_get_phash)(const char *);
void *lib;
imglib = dlopen("./libpihash.so", RTLD_LAZY);
if ( imglib != NULL ) {
// declaration for the external functions used
void (*init)();
void (*teardown)();
uint64_t (*get_ahash)(const char *);
uint64_t (*get_dhash)(const char *);
uint64_t (*get_phash)(const char *);
}
//test image locations
static const char largePathStr[] = u8"test_images/sample_01_large.jpg";
static const char mediumPathStr[] = u8"test_images/sample_01_medium.jpg";
static const char smallPathStr[] = u8"test_images/sample_01_small.jpg";
static const char image1PathStr[] = u8"test_images/sample_01_";
static const char image2PathStr[] = u8"test_images/sample_02_";
static const char image3PathStr[] = u8"test_images/sample_03_";
// Array of pointers to the base image paths to test
//static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr };
static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr };
static const int imageSetSize = 3;
// designators for the image sizes
static const char largeImageSizeStr[] = u8"large";
static const char mediumImageSizeStr[] = u8"medium";
static const char smallImageSizeStr[] = u8"small";
// Array of pointers to the images sizes
static const char *imageSizesSet[] = { largeImageSizeStr, mediumImageSizeStr, smallImageSizeStr };
static int imageSizesSetSize = 3;
// Image extension
static const char imageExtensionStr[] = u8".jpg";
// Pointers that the external function call need
const char *largePathPtr = &largePathStr[0];
const char *mediumPathPtr = &mediumPathStr[0];
const char *smallPathPtr = &smallPathStr[0];
// Loading the external library
lib = dlopen("./libpihash.so", RTLD_LAZY);
//Registering the external functions
*(void **)(&init) = dlsym(lib,"init");
*(void **)(&teardown) = dlsym(lib,"teardown");
*(void **)(&get_ahash) = dlsym(lib,"ext_get_ahash");
*(void **)(&get_dhash) = dlsym(lib,"ext_get_dhash");
*(void **)(&get_phash) = dlsym(lib,"ext_get_phash");
// Init the shared library
init();
//temp buffer for the path
char *imagePathBuffer = malloc(100);
// loop over the images and sizes to test
for (int i = 0; i < imageSetSize; i++) {
for (int j = 0; j < imageSizesSetSize; j++) {
// Make sure the buffer is clean before using it
memset(imagePathBuffer,0,100);
// Getting the correct path
strcat(imagePathBuffer, imagesSet[i]);
strcat(imagePathBuffer, imageSizesSet[j]);
strcat(imagePathBuffer, imageExtensionStr);
//printf("Path: %s\n", imagePath);
printf("Large_Test_AHash: ", largeA);
printf("Large_Test_DHash: ", largeD);
printf("Large_Test_PHash: ", largeP);
printf("Medium_Test_AHash: ", mediumA);
printf("Medium_Test_DHash: ", mediumD);
printf("Medium_Test_PHash: ", mediumP);
printf("Small_Test_AHash: ", smallA);
printf("Small_Test_DHash: ", smallD);
printf("Small_Test_PHash: ", smallP);
// Visually proving that the bytes stored are the correct representation
//print_ustr_bytes(imagePath);
printf("Image: %s\n",imagePathBuffer);
// Printing information about the hashes of the provided images
uint64_t imageAhash = get_ahash(imagePathBuffer);
uint64_t imageDhash = get_dhash(imagePathBuffer);
uint64_t imagePhash = get_phash(imagePathBuffer);
printf("ahash: %llu \n", imageAhash);
printf("dhash: %llu \n", imageDhash);
printf("phash: %llu \n", imagePhash);
}
}
//cleanup and close the buffer
memset(imagePathBuffer,0,100);
free(imagePathBuffer);
// Closing the shared library reference
if (lib != NULL ) dlclose(lib);
return EXIT_SUCCESS;
}
void print_ustr_bytes(const char str[]) {
int strLen = strlen(str);
//printf("Length: %u \n",strLen*2);
char *strBuf = malloc(strLen*4);
for(int i = 0; i <= strLen; i++) {
sprintf(&strBuf[i*4], "\\x%02X", str[i]);
}
printf("String: '%s' -> Bytes: '%s'\n" , str, strBuf);
memset(strBuf,0,strLen*4);
free(strBuf);
}

20
FFI-tests/ffi_test.py

@ -1,7 +1,6 @@
#!/usr/bin/env python3
from ctypes import *
from _ffi_test_py import ffi, lib
large_image1_path = "test_images/sample_01_large.jpg".encode(encoding="utf-8")
medium_image1_path = "test_images/sample_01_medium.jpg".encode(encoding="utf-8")
@ -17,8 +16,19 @@ small_image3_path = "test_images/sample_03_small.jpg".encode(encoding="utf-8")
test_images=[large_image1_path, medium_image1_path, small_image1_path,large_image2_path, medium_image2_path, small_image2_path,large_image3_path, medium_image3_path, small_image3_path]
def unsigned64(number):
return c_ulonglong(number).value
print("starting ffi test")
# Load the shared library
lib = cdll.LoadLibrary("libpihash.so")
# Setting the ctypes return type references for the foreign functions
lib.ext_get_ahash.restype = c_ulonglong
lib.ext_get_dhash.restype = c_ulonglong
lib.ext_get_phash.restype = c_ulonglong
#initialize the library
lib.init()
@ -27,10 +37,10 @@ lib.init()
#print('\\x'+'\\x'.join('{:02x}'.format(x) for x in large_image_path))
for image in test_images:
print("Get hashes for {}", image)
print("AHash: {}",lib.ext_get_ahash(image) & 0xffffffffffffffff)
print("DHash: {}",lib.ext_get_dhash(image) & 0xffffffffffffffff)
print("PHash: {}",lib.ext_get_phash(image) & 0xffffffffffffffff)
print("Requesting hashes for: %s"% image)
print("ahash: %i"% unsigned64(lib.ext_get_ahash(image)))
print("dhash: %i"% unsigned64(lib.ext_get_dhash(image)))
print("phash: %i"% unsigned64(lib.ext_get_phash(image)))
# Do cleanup
#lib.teardown()

26
FFI-tests/ffi_test_build.py

@ -1,26 +0,0 @@
#!/usr/bin/env python3
# A simple test script to confirm the C FFI for the rust library is working
from cffi import FFI
ffi = FFI()
ffi.set_source("_ffi_test_py",
"""
#include <dlfcn.h>
""",
libraries=["pihash"],
library_dirs=["."]
)
ffi.cdef("""
void init();
void teardown();
uint64_t ext_get_ahash(const char *);
uint64_t ext_get_dhash(const char *);
uint64_t ext_get_phash(const char *);
""")
if __name__ == "__main__":
ffi.compile()

7
ffi/pihash.h

@ -0,0 +1,7 @@
#include <stdint.h>
void init();
void teardown();
uint64_t ext_get_ahash(const char *);
uint64_t ext_get_dhash(const char *);
uint64_t ext_get_phash(const char *);

62
src/cache.rs

@ -47,7 +47,10 @@ fn get_file_hash(path: &Path) -> Result<String, Error> {
/**
* Put an image buffer in the cache
*/
pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer<image::Luma<u8>, Vec<u8>>) {
pub fn put_image_in_cache(path: &Path,
size: u32,
image: &ImageBuffer<image::Luma<u8>, Vec<u8>>)
-> Result<bool, Error> {
let hash = get_file_hash(&path);
match hash {
Ok(sha1) => {
@ -61,11 +64,18 @@ pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer<image::Lum
// Save the file into the cache
match image.save(cached_path) {
Ok(_) => {}
Err(e) => println!("Error: {}", e),
Err(e) => {
println!("Error: {}", e);
return Err(e);
}
}
Err(e) => println!("Error: {}", e),
}
Err(e) => {
println!("Error: {}", e);
return Err(e);
}
}
Ok(true)
}
/**
@ -74,7 +84,8 @@ pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer<image::Lum
pub fn put_matrix_in_cache(path: &Path,
size: u32,
extension: &str,
file_contents: &Vec<Vec<f64>>) {
file_contents: &Vec<Vec<f64>>)
-> Result<bool, Error> {
let hash = get_file_hash(&path);
match hash {
Ok(sha1) => {
@ -91,20 +102,29 @@ pub fn put_matrix_in_cache(path: &Path,
let desire_len = row_str.len() - 1;
row_str.truncate(desire_len);
row_str.push_str("\n");
compressor.write(&row_str.into_bytes());
try!(compressor.write(&row_str.into_bytes()));
}
let compressed_matrix = match compressor.finish() {
Ok(data) => data,
Err(e) => { println!("Unable to compress matrix data: {}", e); return },
Err(e) => {
println!("Unable to compress matrix data: {}", e);
return Err(e);
}
};
file.write(&compressed_matrix);
file.flush();
try!(file.write(&compressed_matrix));
try!(file.flush());
}
Err(e) => {
return Err(e);
}
}
Err(_) => {}
}
Err(e) => {
println!("Error: {}", e);
return Err(e);
}
Err(e) => println!("Error: {}", e),
}
Ok(true)
}
/**
@ -154,18 +174,26 @@ pub fn get_matrix_from_cache(path: &Path, size: u32, extension: &str) -> Option<
let cached_path = Path::new(&cache_path_str);
// Try to open, if it does, then we can read the image in
match File::open(&cached_path) {
Ok(mut file) => {
let mut compressed_matrix_data: Vec<u8> = Vec::new();
Ok(file) => {
let mut decoder = ZlibDecoder::new(&file);
let mut matrix_data_str = String::new();
match decoder.read_to_string(&mut matrix_data_str) {
Ok(_) => {},
Err(e) => { println!("Unable to decompress matrix: {}",e); return None }
Ok(_) => {}
Err(e) => {
println!("Unable to decompress matrix: {}", e);
return None;
}
};
// convert the matrix
let matrix: Vec<Vec<f64>> = matrix_data_str.trim().split("\n")
.map(|line| line.split(",")
.map(|f| f64::from_str(f).unwrap()).collect())
let matrix: Vec<Vec<f64>> = matrix_data_str.trim()
.split("\n")
.map(|line| {
line.split(",")
.map(|f| {
f64::from_str(f).unwrap()
})
.collect()
})
.collect();
Some(matrix)

23
src/hash.rs

@ -119,7 +119,10 @@ pub fn prepare_image<'a>(path: &'a Path,
let image = image::open(path).unwrap();
let small_image = image.resize_exact(size, size, FilterType::Lanczos3);
let grey_image = small_image.to_luma();
cache::put_image_in_cache(&path, size, &grey_image);
match cache::put_image_in_cache(&path, size, &grey_image) {
Ok(_) => {}
Err(e) => println!("Unable to store image in cache. {}", e),
};
PreparedImage {
orig_path: &*image_path,
image: grey_image,
@ -298,7 +301,9 @@ impl<'a> PerceptualHash for PHash<'a> {
// Pretty fast already, so caching doesn't make a huge difference
// Atleast compared to opening and processing the images
let mut data_matrix: Vec<Vec<f64>> = Vec::new();
match cache::get_matrix_from_cache(&Path::new(self.prepared_image.orig_path), width as u32, &"dft") {
match cache::get_matrix_from_cache(&Path::new(self.prepared_image.orig_path),
width as u32,
&"dft") {
Some(matrix) => data_matrix = matrix,
None => {
// Preparing the results
@ -308,18 +313,24 @@ impl<'a> PerceptualHash for PHash<'a> {
let pos_x = x as u32;
let pos_y = y as u32;
data_matrix[x]
.push(self.prepared_image.image.get_pixel(pos_x, pos_y).channels()[0] as f64);
.push(self.prepared_image
.image
.get_pixel(pos_x, pos_y)
.channels()[0] as f64);
}
}
// Perform the 2D DFT operation on our matrix
calculate_2d_dft(&mut data_matrix);
// Store this DFT in the cache
cache::put_matrix_in_cache(&Path::new(self.prepared_image.orig_path),
match cache::put_matrix_in_cache(&Path::new(self.prepared_image.orig_path),
width as u32,
&"dft",
&data_matrix);
},
&data_matrix) {
Ok(_) => {}
Err(e) => println!("Unable to store matrix in cache. {}", e),
};
}
}
// Only need the top left quadrant

6
src/lib.rs

@ -60,7 +60,7 @@ pub fn get_hamming_distance(hash1: u64, hash2: u64) -> u64 {
// External proxies for the get_*hash methods
#[no_mangle]
pub extern "C" fn ext_get_ahash(path_char: *const libc::c_char) -> u64 {
pub extern "C" fn ext_get_ahash(path_char: *const libc::c_char) -> libc::uint64_t {
unsafe {
let path_str = CStr::from_ptr(path_char);
let image_path = match path_str.to_str() {
@ -78,7 +78,7 @@ pub extern "C" fn ext_get_ahash(path_char: *const libc::c_char) -> u64 {
}
#[no_mangle]
pub extern "C" fn ext_get_dhash(path_char: *const libc::c_char) -> u64 {
pub extern "C" fn ext_get_dhash(path_char: *const libc::c_char) -> libc::uint64_t {
unsafe {
let path_str = CStr::from_ptr(path_char);
let image_path = match path_str.to_str() {
@ -96,7 +96,7 @@ pub extern "C" fn ext_get_dhash(path_char: *const libc::c_char) -> u64 {
}
#[no_mangle]
pub extern "C" fn ext_get_phash(path_char: *const libc::c_char) -> u64 {
pub extern "C" fn ext_get_phash(path_char: *const libc::c_char) -> libc::uint64_t {
unsafe {
let path_str = CStr::from_ptr(path_char);
let image_path = match path_str.to_str() {

Loading…
Cancel
Save