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 4.0KB

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