Audio plugin host https://kx.studio/carla
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.

178 lines
5.2KB

  1. /*
  2. * Carla binary utils
  3. * Copyright (C) 2014-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_BINARY_UTILS_HPP_INCLUDED
  18. #define CARLA_BINARY_UTILS_HPP_INCLUDED
  19. #include "CarlaBackend.h"
  20. #include "CarlaUtils.hpp"
  21. #include "water/files/FileInputStream.h"
  22. #ifdef HAVE_LIBMAGIC
  23. # include <magic.h>
  24. # ifdef CARLA_OS_MAC
  25. # include "CarlaMacUtils.hpp"
  26. # endif
  27. #endif
  28. CARLA_BACKEND_START_NAMESPACE
  29. #ifdef HAVE_LIBMAGIC
  30. // --------------------------------------------------------------------------------------------------------------------
  31. class CarlaMagic
  32. {
  33. public:
  34. CarlaMagic()
  35. : fMagic(magic_open(MAGIC_SYMLINK)),
  36. fLoadedOk(false)
  37. {
  38. CARLA_SAFE_ASSERT_RETURN(fMagic != nullptr,);
  39. fLoadedOk = magic_load(fMagic, std::getenv("CARLA_MAGIC_FILE")) == 0;
  40. }
  41. ~CarlaMagic()
  42. {
  43. if (fMagic != nullptr)
  44. magic_close(fMagic);
  45. }
  46. const char* getFileDescription(const char* const filename) const
  47. {
  48. if (fMagic == nullptr || ! fLoadedOk)
  49. return nullptr;
  50. return magic_file(fMagic, filename);
  51. }
  52. private:
  53. const magic_t fMagic;
  54. bool fLoadedOk;
  55. CARLA_PREVENT_HEAP_ALLOCATION
  56. CARLA_DECLARE_NON_COPY_CLASS(CarlaMagic)
  57. };
  58. #endif
  59. // --------------------------------------------------------------------------------------------------------------------
  60. static inline
  61. BinaryType getBinaryTypeFromFile(const char* const filename)
  62. {
  63. carla_debug("getBinaryTypeFromFile(\"%s\")", filename);
  64. if (filename == nullptr || filename[0] == '\0')
  65. return BINARY_NATIVE;
  66. #ifdef HAVE_LIBMAGIC
  67. static const CarlaMagic magic;
  68. const char* const output(magic.getFileDescription(filename));
  69. if (output != nullptr && output[0] != '\0')
  70. {
  71. if (std::strstr(output, "MS Windows") != nullptr)
  72. if (std::strstr(output, "PE32 executable") != nullptr || std::strstr(output, "PE32+ executable") != nullptr)
  73. return (std::strstr(output, "x86-64") != nullptr)
  74. ? BINARY_WIN64
  75. : BINARY_WIN32;
  76. if (std::strstr(output, "MS-DOS executable, MZ for MS-DOS") != nullptr)
  77. return BINARY_WIN32;
  78. if (std::strstr(output, "ELF") != nullptr)
  79. return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr)
  80. ? BINARY_POSIX64
  81. : BINARY_POSIX32;
  82. # ifdef CARLA_OS_MAC
  83. if (std::strcmp(output, "directory") == 0)
  84. if (const char* const binary = findBinaryInBundle(filename))
  85. return getBinaryTypeFromFile(binary);
  86. if (std::strstr(output, "Mach-O universal binary") != nullptr)
  87. {
  88. // This is tricky, binary actually contains multiple architectures
  89. // We just assume what architectures are more important, and check for them first
  90. if (std::strstr(output, "x86_64") != nullptr)
  91. return BINARY_POSIX64;
  92. if (std::strstr(output, "i386"))
  93. return BINARY_POSIX32;
  94. if (std::strstr(output, "ppc"))
  95. return BINARY_OTHER;
  96. }
  97. carla_debug("getBinaryTypeFromFile(\"%s\") - have output:\n%s", filename, output);
  98. # endif
  99. return BINARY_NATIVE;
  100. }
  101. #endif
  102. using water::File;
  103. using water::FileInputStream;
  104. ScopedPointer<FileInputStream> stream(File(filename).createInputStream());
  105. CARLA_SAFE_ASSERT_RETURN(stream != nullptr && ! stream->failedToOpen(), BINARY_NATIVE);
  106. // ----------------------------------------------------------------------------------------------------------------
  107. // binary type code based on Ardour's dll_info function
  108. // See https://github.com/Ardour/ardour/blob/master/libs/ardour/plugin_manager.cc#L867,L925
  109. // Copyright (C) 2000-2006 Paul Davis
  110. uint8_t buf[68];
  111. if (stream->read(buf, 68) != 68)
  112. return BINARY_NATIVE;
  113. if (buf[0] != 'M' && buf[1] != 'Z')
  114. return BINARY_NATIVE;
  115. const int32_t* const pe_hdr_off_ptr = (int32_t*)&buf[60];
  116. const int32_t pe_hdr_off = *pe_hdr_off_ptr;
  117. if (! stream->setPosition(pe_hdr_off))
  118. return BINARY_NATIVE;
  119. if (stream->read(buf, 6) != 6)
  120. return BINARY_NATIVE;
  121. if (buf[0] != 'P' && buf[1] != 'E')
  122. return BINARY_NATIVE;
  123. const uint16_t* const type_ptr = (uint16_t*)&buf[4];
  124. const uint16_t type = *type_ptr;
  125. switch (type)
  126. {
  127. case 0x014c:
  128. return BINARY_WIN32;
  129. case 0x8664:
  130. return BINARY_WIN64;
  131. default:
  132. return BINARY_NATIVE;
  133. }
  134. }
  135. // --------------------------------------------------------------------------------------------------------------------
  136. CARLA_BACKEND_END_NAMESPACE
  137. #endif // CARLA_BINARY_UTILS_HPP_INCLUDED