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.

150 lines
4.1KB

  1. /*
  2. * Carla binary utils
  3. * Copyright (C) 2014 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. #ifndef BUILD_BRIDGE
  22. # if defined(CARLA_OS_WIN)
  23. # include "juce_core/juce_core.h"
  24. # elif defined(HAVE_LIBMAGIC)
  25. # include <magic.h>
  26. # endif
  27. #endif
  28. CARLA_BACKEND_START_NAMESPACE
  29. #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE)
  30. // -----------------------------------------------------------------------
  31. class CarlaMagic
  32. {
  33. public:
  34. CarlaMagic()
  35. : fMagic(magic_open(MAGIC_SYMLINK))
  36. {
  37. CARLA_SAFE_ASSERT_RETURN(fMagic != nullptr,);
  38. magic_load(fMagic, nullptr);
  39. }
  40. ~CarlaMagic()
  41. {
  42. if (fMagic != nullptr)
  43. magic_close(fMagic);
  44. }
  45. const char* getFileDescription(const char* const filename) const
  46. {
  47. if (fMagic == nullptr)
  48. return nullptr;
  49. return magic_file(fMagic, filename);
  50. }
  51. private:
  52. const magic_t fMagic;
  53. CARLA_PREVENT_HEAP_ALLOCATION
  54. CARLA_DECLARE_NON_COPY_CLASS(CarlaMagic)
  55. };
  56. #endif
  57. // -----------------------------------------------------------------------
  58. static inline
  59. BinaryType getBinaryTypeFromFile(const char* const filename)
  60. {
  61. carla_debug("getBinaryTypeFromFile(\"%s\")", filename);
  62. if (filename == nullptr || filename[0] == '\0')
  63. return BINARY_NATIVE;
  64. #ifndef BUILD_BRIDGE
  65. # if defined(CARLA_OS_WIN)
  66. using juce::File;
  67. using juce::FileInputStream;
  68. using juce::ScopedPointer;
  69. ScopedPointer<FileInputStream> stream(File(filename).createInputStream());
  70. CARLA_SAFE_ASSERT_RETURN(stream != nullptr || stream->failedToOpen(), BINARY_NATIVE);
  71. // -------------------------------------------------------------------
  72. // binary type code based on Ardour's dll_info function
  73. // See https://github.com/Ardour/ardour/blob/master/libs/ardour/plugin_manager.cc#L867,L925
  74. // Copyright (C) 2000-2006 Paul Davis
  75. uint8_t buf[68];
  76. if (stream->read(buf, 68) != 68)
  77. return BINARY_NATIVE;
  78. if (buf[0] != 'M' && buf[1] != 'Z')
  79. return BINARY_NATIVE;
  80. const int32_t* const pe_hdr_off_ptr = (int32_t*)&buf[60];
  81. const int32_t pe_hdr_off = *pe_hdr_off_ptr;
  82. if (! stream->setPosition(pe_hdr_off))
  83. return BINARY_NATIVE;
  84. if (stream->read(buf, 6) != 6)
  85. return BINARY_NATIVE;
  86. if (buf[0] != 'P' && buf[1] != 'E')
  87. return BINARY_NATIVE;
  88. const uint16_t* const type_ptr = (uint16_t*)&buf[4];
  89. const uint16_t type = *type_ptr;
  90. switch (type)
  91. {
  92. case 0x014c:
  93. return BINARY_WIN32;
  94. case 0x8664:
  95. return BINARY_WIN64;
  96. default:
  97. return BINARY_NATIVE;
  98. }
  99. # elif defined(HAVE_LIBMAGIC)
  100. static const CarlaMagic magic;
  101. const char* const output(magic.getFileDescription(filename));
  102. if (output == nullptr || output[0] == '\0')
  103. return BINARY_NATIVE;
  104. if (std::strstr(output, "MS Windows") != nullptr)
  105. if (std::strstr(output, "PE32 executable") != nullptr || std::strstr(output, "PE32+ executable") != nullptr)
  106. return (std::strstr(output, "x86-64") != nullptr) ? BINARY_WIN64 : BINARY_WIN32;
  107. if (std::strstr(output, "ELF") != nullptr)
  108. return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr) ? BINARY_POSIX64 : BINARY_POSIX32;
  109. # endif
  110. #endif
  111. return BINARY_NATIVE;
  112. }
  113. // -----------------------------------------------------------------------
  114. CARLA_BACKEND_END_NAMESPACE
  115. #endif // CARLA_BINARY_UTILS_HPP_INCLUDED