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-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 "CarlaJuceUtils.hpp"
  23. # include "juce_audio_graph/juce_audio_graph.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 juce2::File;
  65. using juce2::FileInputStream;
  66. ScopedPointer<FileInputStream> stream(File(filename).createInputStream());
  67. CARLA_SAFE_ASSERT_RETURN(stream != nullptr || stream->failedToOpen(), BINARY_NATIVE);
  68. // -------------------------------------------------------------------
  69. // binary type code based on Ardour's dll_info function
  70. // See https://github.com/Ardour/ardour/blob/master/libs/ardour/plugin_manager.cc#L867,L925
  71. // Copyright (C) 2000-2006 Paul Davis
  72. uint8_t buf[68];
  73. if (stream->read(buf, 68) != 68)
  74. return BINARY_NATIVE;
  75. if (buf[0] != 'M' && buf[1] != 'Z')
  76. return BINARY_NATIVE;
  77. const int32_t* const pe_hdr_off_ptr = (int32_t*)&buf[60];
  78. const int32_t pe_hdr_off = *pe_hdr_off_ptr;
  79. if (! stream->setPosition(pe_hdr_off))
  80. return BINARY_NATIVE;
  81. if (stream->read(buf, 6) != 6)
  82. return BINARY_NATIVE;
  83. if (buf[0] != 'P' && buf[1] != 'E')
  84. return BINARY_NATIVE;
  85. const uint16_t* const type_ptr = (uint16_t*)&buf[4];
  86. const uint16_t type = *type_ptr;
  87. switch (type)
  88. {
  89. case 0x014c:
  90. return BINARY_WIN32;
  91. case 0x8664:
  92. return BINARY_WIN64;
  93. default:
  94. return BINARY_NATIVE;
  95. }
  96. #elif defined(HAVE_LIBMAGIC)
  97. static const CarlaMagic magic;
  98. const char* const output(magic.getFileDescription(filename));
  99. if (output == nullptr || output[0] == '\0')
  100. return BINARY_NATIVE;
  101. if (std::strstr(output, "MS Windows") != nullptr)
  102. if (std::strstr(output, "PE32 executable") != nullptr || std::strstr(output, "PE32+ executable") != nullptr)
  103. return (std::strstr(output, "x86-64") != nullptr)
  104. ? BINARY_WIN64
  105. : BINARY_WIN32;
  106. if (std::strstr(output, "ELF") != nullptr)
  107. return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr)
  108. ? BINARY_POSIX64
  109. : BINARY_POSIX32;
  110. #endif
  111. return BINARY_NATIVE;
  112. }
  113. // -----------------------------------------------------------------------
  114. CARLA_BACKEND_END_NAMESPACE
  115. #endif // CARLA_BINARY_UTILS_HPP_INCLUDED