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.

154 lines
4.3KB

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