Advertisement
my5t3ry

Untitled

Feb 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1.  
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/sendfile.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13.  
  14. #include <magic.h>
  15.  
  16. #include "../config.h"
  17. #include "../common/common.h"
  18.  
  19. void replace_magic(void *mem_so, size_t mem_sz,
  20.                    const char *magic, const char *replacement);
  21.  
  22. int main(int argc, char **argv)
  23. {
  24.   int arch = 32;
  25.   magic_t magic;
  26.   char dll_real_path[PATH_MAX];
  27.   char wineprefix_real_path[PATH_MAX];
  28.  
  29.   if (argc != 3 && argc != 4) {
  30.     fprintf(stderr, "usage: %s <vst.dll> <vst.so> [<wine-prefix>]\n", argv[0]);
  31.     return 2;
  32.   }
  33.  
  34.   int has_wineprefix = argc == 4;
  35.  
  36.   struct stat st_dll;
  37.   if (stat(argv[1], &st_dll) ||
  38.       !realpath(argv[1], dll_real_path)) {
  39.     fprintf(stderr, "%s: %m\n", argv[1]);
  40.     return 1;
  41.   }
  42.  
  43.   struct stat st_tpl;
  44.   if (stat(VST_BRIDGE_TPL_PATH, &st_tpl)) {
  45.     fprintf(stderr, "%s: %m\n", VST_BRIDGE_TPL_PATH);
  46.     return 1;
  47.   }
  48.  
  49.   if (has_wineprefix) {
  50.     struct stat st_wineprefix;
  51.  
  52.     if (stat(argv[3], &st_wineprefix) ||
  53.         !realpath(argv[3], wineprefix_real_path)) {
  54.       fprintf(stderr, "%s: %m\n", argv[3]);
  55.       return 1;
  56.     }
  57.  
  58.     if (!S_ISDIR(st_wineprefix.st_mode)) {
  59.       fprintf(stderr, "%s: %s\n", argv[3], strerror(ENOTDIR));
  60.       return 1;
  61.     }
  62.   }
  63.  
  64.   magic = magic_open(MAGIC_NONE);
  65.   if (!magic)
  66.     fprintf(stderr, "failed to initialize magic\n");
  67.   else {
  68.     magic_load(magic, NULL);
  69.     if (strstr(magic_file(magic, dll_real_path), "80386"))
  70.       arch = 32;
  71.     else if (strstr(magic_file(magic, dll_real_path), "x86-64"))
  72.       arch = 64;
  73.     printf("%s: detected %d bits dll\n", dll_real_path, arch);
  74.     magic_close(magic);
  75.   }
  76.  
  77.   // copy file
  78.   int fd_tpl = open(VST_BRIDGE_TPL_PATH, O_RDONLY, 0644);
  79.   if (fd_tpl < 0) {
  80.     fprintf(stderr, "%s: %m\n", VST_BRIDGE_TPL_PATH);
  81.     return 1;
  82.   }
  83.  
  84.   int fd_so = open(argv[2], O_CREAT | O_TRUNC | O_RDWR, 0755);
  85.   if (fd_so < 0) {
  86.     fprintf(stderr, "%s: %m\n", argv[2]);
  87.     return 1;
  88.   }
  89.  
  90.   if (fchmod(fd_so, 0755))
  91.     fprintf(stderr, "chmod(%s, 0755): %m\n", argv[2]);
  92.  
  93.   if (sendfile(fd_so, fd_tpl, NULL, st_tpl.st_size) != st_tpl.st_size) {
  94.     fprintf(stderr, "copy %s to %s: %m\n", VST_BRIDGE_TPL_PATH, argv[2]);
  95.     return 1;
  96.   }
  97.  
  98.   close(fd_tpl);
  99.  
  100.   void *mem_so = mmap(NULL, st_tpl.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  101.                       fd_so, 0);
  102.   if (mem_so == MAP_FAILED) {
  103.     fprintf(stderr, "mmap(%s): %m\n", argv[2]);
  104.     return 1;
  105.   }
  106.  
  107.   replace_magic(mem_so, st_tpl.st_size, VST_BRIDGE_TPL_DLL, dll_real_path);
  108.   replace_magic(mem_so, st_tpl.st_size, VST_BRIDGE_TPL_HOST, arch == 32 ? VST_BRIDGE_HOST32_PATH : VST_BRIDGE_HOST64_PATH);
  109.   if (has_wineprefix)
  110.     replace_magic(mem_so, st_tpl.st_size, VST_BRIDGE_TPL_WINEPREFIX, wineprefix_real_path);
  111.  
  112.   munmap(mem_so, st_tpl.st_size);
  113.  
  114.   close(fd_so);
  115.  
  116.   return 0;
  117. }
  118.  
  119. void replace_magic(void *mem_so, size_t mem_sz,
  120.                    const char *magic, const char *replacement)
  121. {
  122.   char pattern[PATH_MAX];
  123.  
  124.   // Just to make sure we replace only values of PATH_MAX length.
  125.   memset(pattern, 0, sizeof(pattern));
  126.   strcpy(pattern, magic);
  127.  
  128.   void *pos = memmem(mem_so, mem_sz, pattern, sizeof(pattern));
  129.   if (!pos) {
  130.     fprintf(stderr, "`%s' magic not found in plugin\n", magic);
  131.     exit(1);
  132.   }
  133.  
  134.   // Remove all the rubbish. Probably will never matter.
  135.   memset(pattern, 0, sizeof(pattern));
  136.   strcpy(pattern, replacement);
  137.  
  138.   memcpy(pos, pattern, sizeof(pattern));
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement