Browse Source

Working FFI C code

Proved that the python code is messed up somehow. C code calling the shared library works just fine.
develop
Drew Short 9 years ago
parent
commit
77f1a6623f
  1. 78
      FFI-tests/ffi_test.c

78
FFI-tests/ffi_test.c

@ -2,41 +2,71 @@
#include <stdlib.h>
#include <stdint.h>
#include <dlfcn.h>
#include <string.h>
/* function declaration */
void print_str_bytes(const char []);
int main() {
void *lib;
uint64_t (*ext_get_ahash)(const char *);
uint64_t (*ext_get_dhash)(const char *);
uint64_t (*ext_get_phash)(const char *);
uint64_t (*get_ahash)(const char *);
uint64_t (*get_dhash)(const char *);
uint64_t (*get_phash)(const char *);
static const char largePathStr[] = { 0x74,0x65,0x73,0x74,0x5F,0x69,0x6D,0x61,0x67,0x65,0x73,0x2F,0x73,0x61,0x6D,0x70,0x6C,0x65,0x5F,0x30,0x31,0x5F,0x6C,0x61,0x72,0x67,0x65,0x2E,0x6A,0x70,0x67,0x00 };
//print_str_bytes(largePathStr);
static const char mediumPathStr[] = { 0x74,0x65,0x73,0x74,0x5F,0x69,0x6D,0x61,0x67,0x65,0x73,0x2F,0x73,0x61,0x6D,0x70,0x6C,0x65,0x5F,0x30,0x31,0x5F,0x6D,0x65,0x64,0x69,0x75,0x6D,0x2E,0x6A,0x70,0x67,0x00 };
//print_str_bytes(mediumPathStr);
static const char smallPathStr[] = { 0x74,0x65,0x73,0x74,0x5F,0x69,0x6D,0x61,0x67,0x65,0x73,0x2F,0x73,0x61,0x6D,0x70,0x6C,0x65,0x5F,0x30,0x31,0x5F,0x73,0x6D,0x61,0x6C,0x6C,0x2E,0x6A,0x70,0x67,0x00 };
//print_str_bytes(smallPathStr);
static const char largePath[] = u8"test_images/sample_01_large.jpg";
static const char mediumPath[] = u8"test_images/sample_01_medium.jpg";
static const char smallPath[] = u8"test_images/sample_01_small.jpg";
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);
uint64_t largeA = ext_get_ahash(*largePath);
uint64_t largeD = ext_get_dhash(*largePath);
uint64_t largeP = ext_get_phash(*largePath);
//Registering the external functions
*(void **)(&get_ahash) = dlsym(lib,"ext_get_ahash");
*(void **)(&get_dhash) = dlsym(lib,"ext_get_dhash");
*(void **)(&get_phash) = dlsym(lib,"ext_get_phash");
uint64_t mediumA = ext_get_ahash(*mediumPath);
uint64_t mediumD = ext_get_dhash(*mediumPath);
uint64_t mediumP = ext_get_phash(*mediumPath);
uint64_t largeA = get_ahash(largePathPtr);
uint64_t largeD = get_dhash(largePathPtr);
uint64_t largeP = get_phash(largePathPtr);
uint64_t smallA = ext_get_ahash(*smallPath);
uint64_t smallD = ext_get_dhash(*smallPath);
uint64_t smallP = ext_get_phash(*smallPath);
uint64_t mediumA = get_ahash(mediumPathPtr);
uint64_t mediumD = get_dhash(mediumPathPtr);
uint64_t mediumP = get_phash(mediumPathPtr);
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);
uint64_t smallA = get_ahash(smallPathPtr);
uint64_t smallD = get_dhash(smallPathPtr);
uint64_t smallP = get_phash(smallPathPtr);
printf("Large_Test_AHash: %llu \n", largeA);
printf("Large_Test_DHash: %llu \n", largeD);
printf("Large_Test_PHash: %llu \n", largeP);
printf("Medium_Test_AHash: %llu \n", mediumA);
printf("Medium_Test_DHash: %llu \n", mediumD);
printf("Medium_Test_PHash: %llu \n", mediumP);
printf("Small_Test_AHash: %llu \n", smallA);
printf("Small_Test_DHash: %llu \n", smallD);
printf("Small_Test_PHash: %llu \n", smallP);
if (lib != NULL ) dlclose(lib);
return EXIT_SUCCESS;
}
void print_str_bytes(const char str[]) {
int strLen = strlen(str);
printf("Length: %u \n",strLen*2);
char* strBuf = (char*) malloc(strLen);
for(int i = 0; i <= strLen; i++) {
int j = i * 2;
printf("%c",str[i]);
sprintf(&strBuf[j], "%02X", str[i]);
}
printf("\nBytes: %s \n" , strBuf);
}
Loading…
Cancel
Save