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.

CarlaBinaryUtils.hpp 5.1KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, "ELF") != nullptr)
  77. return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr)
  78. ? BINARY_POSIX64
  79. : BINARY_POSIX32;
  80. # ifdef CARLA_OS_MAC
  81. if (std::strcmp(output, "directory") == 0)
  82. if (const char* const binary = findBinaryInBundle(filename))
  83. return getBinaryTypeFromFile(binary);
  84. if (std::strstr(output, "Mach-O universal binary") != nullptr)
  85. {
  86. // This is tricky, binary actually contains multiple architectures
  87. // We just assume what architectures are more important, and check for them first
  88. if (std::strstr(output, "x86_64") != nullptr)
  89. return BINARY_POSIX64;
  90. if (std::strstr(output, "i386"))
  91. return BINARY_POSIX32;
  92. if (std::strstr(output, "ppc"))
  93. return BINARY_OTHER;
  94. }
  95. carla_stdout("getBinaryTypeFromFile(\"%s\") - have output:\n%s", filename, output);
  96. # endif
  97. return BINARY_NATIVE;
  98. }
  99. #endif
  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. // ----------------------------------------------------------------------------------------------------------------
  105. // binary type code based on Ardour's dll_info function
  106. // See https://github.com/Ardour/ardour/blob/master/libs/ardour/plugin_manager.cc#L867,L925
  107. // Copyright (C) 2000-2006 Paul Davis
  108. uint8_t buf[68];
  109. if (stream->read(buf, 68) != 68)
  110. return BINARY_NATIVE;
  111. if (buf[0] != 'M' && buf[1] != 'Z')
  112. return BINARY_NATIVE;
  113. const int32_t* const pe_hdr_off_ptr = (int32_t*)&buf[60];
  114. const int32_t pe_hdr_off = *pe_hdr_off_ptr;
  115. if (! stream->setPosition(pe_hdr_off))
  116. return BINARY_NATIVE;
  117. if (stream->read(buf, 6) != 6)
  118. return BINARY_NATIVE;
  119. if (buf[0] != 'P' && buf[1] != 'E')
  120. return BINARY_NATIVE;
  121. const uint16_t* const type_ptr = (uint16_t*)&buf[4];
  122. const uint16_t type = *type_ptr;
  123. switch (type)
  124. {
  125. case 0x014c:
  126. return BINARY_WIN32;
  127. case 0x8664:
  128. return BINARY_WIN64;
  129. default:
  130. return BINARY_NATIVE;
  131. }
  132. }
  133. // --------------------------------------------------------------------------------------------------------------------
  134. CARLA_BACKEND_END_NAMESPACE
  135. #endif // CARLA_BINARY_UTILS_HPP_INCLUDED