| @@ -1,6 +1,6 @@ | |||
| /* | |||
| * Carla binary utils | |||
| * Copyright (C) 2014-2017 Filipe Coelho <falktx@falktx.com> | |||
| * Copyright (C) 2014-2018 Filipe Coelho <falktx@falktx.com> | |||
| * | |||
| * This program is free software; you can redistribute it and/or | |||
| * modify it under the terms of the GNU General Public License as | |||
| @@ -21,25 +21,27 @@ | |||
| #include "CarlaBackend.h" | |||
| #include "CarlaUtils.hpp" | |||
| #if defined(CARLA_OS_WIN) | |||
| # include "water/files/FileInputStream.h" | |||
| #elif defined(HAVE_LIBMAGIC) | |||
| #include "water/files/FileInputStream.h" | |||
| #ifdef HAVE_LIBMAGIC | |||
| # include <magic.h> | |||
| #endif | |||
| CARLA_BACKEND_START_NAMESPACE | |||
| #ifdef HAVE_LIBMAGIC | |||
| // ----------------------------------------------------------------------- | |||
| // -------------------------------------------------------------------------------------------------------------------- | |||
| class CarlaMagic | |||
| { | |||
| public: | |||
| CarlaMagic() | |||
| : fMagic(magic_open(MAGIC_SYMLINK)) | |||
| : fMagic(magic_open(MAGIC_SYMLINK)), | |||
| fLoadedOk(false) | |||
| { | |||
| CARLA_SAFE_ASSERT_RETURN(fMagic != nullptr,); | |||
| magic_load(fMagic, std::getenv("CARLA_MAGIC_FILE")); | |||
| fLoadedOk = magic_load(fMagic, std::getenv("CARLA_MAGIC_FILE")) == 0; | |||
| } | |||
| ~CarlaMagic() | |||
| @@ -50,7 +52,7 @@ public: | |||
| const char* getFileDescription(const char* const filename) const | |||
| { | |||
| if (fMagic == nullptr) | |||
| if (fMagic == nullptr || ! fLoadedOk) | |||
| return nullptr; | |||
| return magic_file(fMagic, filename); | |||
| @@ -58,13 +60,14 @@ public: | |||
| private: | |||
| const magic_t fMagic; | |||
| bool fLoadedOk; | |||
| CARLA_PREVENT_HEAP_ALLOCATION | |||
| CARLA_DECLARE_NON_COPY_CLASS(CarlaMagic) | |||
| }; | |||
| #endif | |||
| // ----------------------------------------------------------------------- | |||
| // -------------------------------------------------------------------------------------------------------------------- | |||
| static inline | |||
| BinaryType getBinaryTypeFromFile(const char* const filename) | |||
| @@ -74,14 +77,35 @@ BinaryType getBinaryTypeFromFile(const char* const filename) | |||
| if (filename == nullptr || filename[0] == '\0') | |||
| return BINARY_NATIVE; | |||
| #if defined(CARLA_OS_WIN) | |||
| #ifdef HAVE_LIBMAGIC | |||
| static const CarlaMagic magic; | |||
| const char* const output(magic.getFileDescription(filename)); | |||
| if (output != nullptr && output[0] != '\0') | |||
| { | |||
| if (std::strstr(output, "MS Windows") != nullptr) | |||
| if (std::strstr(output, "PE32 executable") != nullptr || std::strstr(output, "PE32+ executable") != nullptr) | |||
| return (std::strstr(output, "x86-64") != nullptr) | |||
| ? BINARY_WIN64 | |||
| : BINARY_WIN32; | |||
| if (std::strstr(output, "ELF") != nullptr) | |||
| return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr) | |||
| ? BINARY_POSIX64 | |||
| : BINARY_POSIX32; | |||
| return BINARY_NATIVE; | |||
| } | |||
| #endif | |||
| using water::File; | |||
| using water::FileInputStream; | |||
| ScopedPointer<FileInputStream> stream(File(filename).createInputStream()); | |||
| CARLA_SAFE_ASSERT_RETURN(stream != nullptr && ! stream->failedToOpen(), BINARY_NATIVE); | |||
| // ------------------------------------------------------------------- | |||
| // ---------------------------------------------------------------------------------------------------------------- | |||
| // binary type code based on Ardour's dll_info function | |||
| // See https://github.com/Ardour/ardour/blob/master/libs/ardour/plugin_manager.cc#L867,L925 | |||
| // Copyright (C) 2000-2006 Paul Davis | |||
| @@ -118,30 +142,11 @@ BinaryType getBinaryTypeFromFile(const char* const filename) | |||
| default: | |||
| return BINARY_NATIVE; | |||
| } | |||
| #elif defined(HAVE_LIBMAGIC) | |||
| static const CarlaMagic magic; | |||
| const char* const output(magic.getFileDescription(filename)); | |||
| if (output == nullptr || output[0] == '\0') | |||
| return BINARY_NATIVE; | |||
| if (std::strstr(output, "MS Windows") != nullptr) | |||
| if (std::strstr(output, "PE32 executable") != nullptr || std::strstr(output, "PE32+ executable") != nullptr) | |||
| return (std::strstr(output, "x86-64") != nullptr) | |||
| ? BINARY_WIN64 | |||
| : BINARY_WIN32; | |||
| if (std::strstr(output, "ELF") != nullptr) | |||
| return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr) | |||
| ? BINARY_POSIX64 | |||
| : BINARY_POSIX32; | |||
| #endif | |||
| return BINARY_NATIVE; | |||
| } | |||
| // ----------------------------------------------------------------------- | |||
| // -------------------------------------------------------------------------------------------------------------------- | |||
| CARLA_BACKEND_END_NAMESPACE | |||