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.

206 lines
6.2KB

  1. /*
  2. * Carla binary utils
  3. * Copyright (C) 2014-2023 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 "CarlaScopeUtils.hpp"
  21. #if defined(BUILDING_CARLA)
  22. # include "water/files/FileInputStream.h"
  23. #elif defined(CARLA_UTILS_USE_QT)
  24. # include <QtCore/QFile>
  25. # include <QtCore/QString>
  26. #endif
  27. #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
  28. # include <magic.h>
  29. # ifdef CARLA_OS_MAC
  30. # include "CarlaMacUtils.hpp"
  31. # endif
  32. #endif
  33. CARLA_BACKEND_START_NAMESPACE
  34. #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
  35. // --------------------------------------------------------------------------------------------------------------------
  36. class CarlaMagic
  37. {
  38. public:
  39. CarlaMagic()
  40. : fMagic(magic_open(MAGIC_SYMLINK)),
  41. fLoadedOk(false)
  42. {
  43. CARLA_SAFE_ASSERT_RETURN(fMagic != nullptr,);
  44. fLoadedOk = magic_load(fMagic, std::getenv("CARLA_MAGIC_FILE")) == 0;
  45. }
  46. ~CarlaMagic()
  47. {
  48. if (fMagic != nullptr)
  49. magic_close(fMagic);
  50. }
  51. const char* getFileDescription(const char* const filename) const
  52. {
  53. if (fMagic == nullptr || ! fLoadedOk)
  54. return nullptr;
  55. return magic_file(fMagic, filename);
  56. }
  57. private:
  58. const magic_t fMagic;
  59. bool fLoadedOk;
  60. CARLA_PREVENT_HEAP_ALLOCATION
  61. CARLA_DECLARE_NON_COPYABLE(CarlaMagic)
  62. };
  63. #endif
  64. // --------------------------------------------------------------------------------------------------------------------
  65. static inline
  66. BinaryType getBinaryTypeFromFile(const char* const filename)
  67. {
  68. carla_debug("getBinaryTypeFromFile(\"%s\")", filename);
  69. if (filename == nullptr || filename[0] == '\0')
  70. return BINARY_NATIVE;
  71. #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
  72. static const CarlaMagic magic;
  73. const char* const output(magic.getFileDescription(filename));
  74. if (output != nullptr && output[0] != '\0')
  75. {
  76. if (std::strstr(output, "MS Windows") != nullptr)
  77. if (std::strstr(output, "PE32 executable") != nullptr || std::strstr(output, "PE32+ executable") != nullptr)
  78. return (std::strstr(output, "x86-64") != nullptr)
  79. ? BINARY_WIN64
  80. : BINARY_WIN32;
  81. if (std::strstr(output, "MS-DOS executable, MZ for MS-DOS") != nullptr)
  82. return BINARY_WIN32;
  83. if (std::strstr(output, "ELF") != nullptr)
  84. return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr)
  85. ? BINARY_POSIX64
  86. : BINARY_POSIX32;
  87. #ifdef CARLA_OS_MAC
  88. if (std::strcmp(output, "directory") == 0)
  89. if (const char* const binary = findBinaryInBundle(filename))
  90. return getBinaryTypeFromFile(binary);
  91. if (std::strstr(output, "Mach-O universal binary") != nullptr)
  92. {
  93. // This is tricky, binary actually contains multiple architectures
  94. // We just assume what architectures are more important, and check for them first
  95. if (std::strstr(output, "x86_64") != nullptr)
  96. return BINARY_POSIX64;
  97. if (std::strstr(output, "arm64") != nullptr)
  98. return BINARY_POSIX64;
  99. if (std::strstr(output, "i386") != nullptr)
  100. return BINARY_POSIX32;
  101. if (std::strstr(output, "ppc") != nullptr)
  102. return BINARY_OTHER;
  103. }
  104. carla_debug("getBinaryTypeFromFile(\"%s\") - have output:\n%s", filename, output);
  105. #endif
  106. return BINARY_NATIVE;
  107. }
  108. #endif
  109. #if defined(BUILDING_CARLA) || defined(CARLA_UTILS_USE_QT)
  110. #if defined(CARLA_UTILS_USE_QT)
  111. QFile file(QString::fromUtf8(filename));
  112. CARLA_SAFE_ASSERT_RETURN(file.open(QIODevice::ReadOnly), BINARY_NATIVE);
  113. #else
  114. using water::File;
  115. using water::FileInputStream;
  116. CarlaScopedPointer<FileInputStream> stream(File(filename).createInputStream());
  117. CARLA_SAFE_ASSERT_RETURN(stream != nullptr && ! stream->failedToOpen(), BINARY_NATIVE);
  118. #endif
  119. // ----------------------------------------------------------------------------------------------------------------
  120. // binary type code based on Ardour's dll_info function
  121. // See https://github.com/Ardour/ardour/blob/master/libs/ardour/plugin_manager.cc#L867,L925
  122. // Copyright (C) 2000-2006 Paul Davis
  123. #if defined(CARLA_UTILS_USE_QT)
  124. char buf[68];
  125. if (file.read(buf, 68) != 68)
  126. #else
  127. uint8_t buf[68];
  128. if (stream->read(buf, 68) != 68)
  129. #endif
  130. return BINARY_NATIVE;
  131. if (buf[0] != 'M' && buf[1] != 'Z')
  132. return BINARY_NATIVE;
  133. const int32_t* const pe_hdr_off_ptr = (int32_t*)&buf[60];
  134. const int32_t pe_hdr_off = *pe_hdr_off_ptr;
  135. #if defined(CARLA_UTILS_USE_QT)
  136. if (! file.seek(pe_hdr_off))
  137. #else
  138. if (! stream->setPosition(pe_hdr_off))
  139. #endif
  140. return BINARY_NATIVE;
  141. #if defined(CARLA_UTILS_USE_QT)
  142. if (file.read(buf, 6) != 6)
  143. #else
  144. if (stream->read(buf, 6) != 6)
  145. #endif
  146. return BINARY_NATIVE;
  147. if (buf[0] != 'P' && buf[1] != 'E')
  148. return BINARY_NATIVE;
  149. const uint16_t* const type_ptr = (uint16_t*)&buf[4];
  150. const uint16_t type = *type_ptr;
  151. switch (type)
  152. {
  153. case 0x014c:
  154. return BINARY_WIN32;
  155. case 0x8664:
  156. return BINARY_WIN64;
  157. default:
  158. return BINARY_NATIVE;
  159. }
  160. #else
  161. return BINARY_NATIVE;
  162. #endif
  163. }
  164. // --------------------------------------------------------------------------------------------------------------------
  165. CARLA_BACKEND_END_NAMESPACE
  166. #endif // CARLA_BINARY_UTILS_HPP_INCLUDED