You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <dlfcn.h>
  5. #include <string.h>
  6. /* function declaration */
  7. void print_ustr_bytes(const char []);
  8. int main() {
  9. void *lib;
  10. void *lib_struct;
  11. // declaration for the external functions used
  12. void *(*init)(const char *);
  13. void (*free)();
  14. uint64_t (*get_ahash)(void *, const char *);
  15. uint64_t (*get_dhash)(void *, const char *);
  16. uint64_t (*get_phash)(void *, const char *);
  17. //test image locations
  18. static const char image1PathStr[] = u8"../test_images/sample_01_";
  19. static const char image2PathStr[] = u8"../test_images/sample_02_";
  20. static const char image3PathStr[] = u8"../test_images/sample_03_";
  21. static const char image4PathStr[] = u8"../test_images/sample_04_";
  22. // Array of pointers to the base image paths to test
  23. //static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr };
  24. static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr, image4PathStr };
  25. static const int imageSetSize = 4;
  26. // designators for the image sizes
  27. static const char largeImageSizeStr[] = u8"large";
  28. static const char mediumImageSizeStr[] = u8"medium";
  29. static const char smallImageSizeStr[] = u8"small";
  30. // Array of pointers to the images sizes
  31. static const char *imageSizesSet[] = { largeImageSizeStr, mediumImageSizeStr, smallImageSizeStr };
  32. static int imageSizesSetSize = 3;
  33. // Image extension
  34. static const char imageExtensionStr[] = u8".jpg";
  35. // Loading the external library
  36. lib = dlopen("./libpihash.so", RTLD_LAZY);
  37. //Registering the external functions
  38. *(void **)(&init) = dlsym(lib,"ext_init");
  39. *(void **)(&free) = dlsym(lib,"ext_free");
  40. *(void **)(&get_ahash) = dlsym(lib,"ext_get_ahash");
  41. *(void **)(&get_dhash) = dlsym(lib,"ext_get_dhash");
  42. *(void **)(&get_phash) = dlsym(lib,"ext_get_phash");
  43. // Init the shared library
  44. lib_struct = init(u8"./.hash_cache");
  45. //temp buffer for the path
  46. char *imagePathBuffer = malloc(100);
  47. // loop over the images and sizes to test
  48. for (int i = 0; i < imageSetSize; i++) {
  49. for (int j = 0; j < imageSizesSetSize; j++) {
  50. // Make sure the buffer is clean before using it
  51. memset(imagePathBuffer,0,100);
  52. // Getting the correct path
  53. strcat(imagePathBuffer, imagesSet[i]);
  54. strcat(imagePathBuffer, imageSizesSet[j]);
  55. strcat(imagePathBuffer, imageExtensionStr);
  56. //printf("Path: %s\n", imagePath);
  57. // Visually proving that the bytes stored are the correct representation
  58. //print_ustr_bytes(imagePath);
  59. printf("Image: %s\n",imagePathBuffer);
  60. // Printing information about the hashes of the provided images
  61. uint64_t imageAhash = get_ahash(lib_struct, imagePathBuffer);
  62. uint64_t imageDhash = get_dhash(lib_struct, imagePathBuffer);
  63. uint64_t imagePhash = get_phash(lib_struct, imagePathBuffer);
  64. printf("ahash: %llu \n", imageAhash);
  65. printf("dhash: %llu \n", imageDhash);
  66. printf("phash: %llu \n", imagePhash);
  67. }
  68. }
  69. //cleanup and close the buffer
  70. memset(imagePathBuffer,0,100);
  71. free(imagePathBuffer);
  72. // Closing the shared library reference
  73. if (lib != NULL ) dlclose(lib);
  74. return EXIT_SUCCESS;
  75. }
  76. void print_ustr_bytes(const char str[]) {
  77. int strLen = strlen(str);
  78. //printf("Length: %u \n",strLen*2);
  79. char *strBuf = malloc(strLen*4);
  80. for(int i = 0; i <= strLen; i++) {
  81. sprintf(&strBuf[i*4], "\\x%02X", str[i]);
  82. }
  83. printf("String: '%s' -> Bytes: '%s'\n" , str, strBuf);
  84. memset(strBuf,0,strLen*4);
  85. free(strBuf);
  86. }