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.

193 lines
5.7KB

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