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.

151 lines
4.1KB

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