Advertisement
pochti_da

Untitled

Nov 2nd, 2020
2,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <limits.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <dirent.h>
  11. #include <dlfcn.h>
  12. #include <stdint.h>
  13.  
  14. enum
  15. {
  16.     ARGS_SIZE = 64 / sizeof(uint64_t),
  17.     DOUBLE = 'd',
  18.     STRING = 's',
  19.     INT = 'i',
  20.     VOID = 'v'
  21. };
  22.  
  23. typedef unsigned char uchar;
  24.  
  25. void
  26. parse_args(uchar *args, char *argv[], int ind, char *sign)
  27. {
  28.     for (sign += 1; *sign; ++sign) {
  29.         char type = *sign;
  30.  
  31.         if (type == DOUBLE) {
  32.             double temp;
  33.             sscanf(argv[ind], "%lf", &temp);
  34.            
  35.             memcpy(args, &temp, sizeof(temp));
  36.             args += sizeof(temp);
  37.         } else if (type == STRING) {
  38.             char *temp = argv[ind];
  39.  
  40.             memcpy(args, &temp, sizeof(temp));
  41.             args += sizeof(temp);
  42.         } else if (type == INT) {
  43.             int temp;
  44.             sscanf(argv[ind], "%d", &temp);
  45.  
  46.             memcpy(args, &temp, sizeof(temp));
  47.             args += sizeof(temp);
  48.         }
  49.  
  50.         ++ind;
  51.     }
  52. }
  53.  
  54. int
  55. main(int argc, char *argv[])
  56. {
  57.     void *handle = dlopen(argv[1], RTLD_LAZY);
  58.     if (handle == NULL) {
  59.         fprintf(stderr, "Error error...\n");
  60.         return 1;
  61.     }
  62.  
  63.     void *sym = dlsym(handle, argv[2]);
  64.     if (sym == NULL) {
  65.         fprintf(stderr, "Error error...\n");
  66.         return 1;
  67.     }
  68.  
  69.     uint64_t args[ARGS_SIZE];
  70.     parse_args((void *)args, argv, 4, argv[3]);
  71.  
  72.     char type = argv[3][0];
  73.     if (type == VOID) {
  74.         void (*func)(uint64_t, uint64_t, uint64_t, uint64_t,
  75.                 uint64_t, uint64_t, uint64_t, uint64_t) = sym;
  76.         func(args[0], args[1], args[2], args[3],
  77.                 args[4], args[5], args[6], args[7]);
  78.     } else if (type == DOUBLE) {
  79.         double (*func)(uint64_t, uint64_t, uint64_t, uint64_t,
  80.                 uint64_t, uint64_t, uint64_t, uint64_t) = sym;
  81.  
  82.         double res = func(args[0], args[1], args[2], args[3],
  83.                 args[4], args[5], args[6], args[7]);
  84.         printf("%.10g\n", res);
  85.     } else if (type == INT) {
  86.         int (*func)(uint64_t, uint64_t, uint64_t, uint64_t,
  87.                 uint64_t, uint64_t, uint64_t, uint64_t) = sym;
  88.  
  89.         int res = func(args[0], args[1], args[2], args[3],
  90.                 args[4], args[5], args[6], args[7]);
  91.         printf("%d\n", res);
  92.     } else {
  93.         char *(*func)(uint64_t, uint64_t, uint64_t, uint64_t,
  94.                 uint64_t, uint64_t, uint64_t, uint64_t) = sym;
  95.  
  96.         char *str = func(args[0], args[1], args[2], args[3],
  97.                 args[4], args[5], args[6], args[7]);
  98.         printf("%s\n", str);
  99.     }
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement