Fix last commit
[carla.git] / source / utils / CarlaBinaryUtils.hpp
blobaed5e7e39249ada7290ea96abe4ca45c6a1b63d4
1 /*
2 * Carla binary utils
3 * Copyright (C) 2014-2023 Filipe Coelho <falktx@falktx.com>
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.
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.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #ifndef CARLA_BINARY_UTILS_HPP_INCLUDED
19 #define CARLA_BINARY_UTILS_HPP_INCLUDED
21 #include "CarlaBackend.h"
22 #include "CarlaScopeUtils.hpp"
24 #if defined(BUILDING_CARLA)
25 # include "water/files/FileInputStream.h"
26 #elif defined(CARLA_UTILS_USE_QT)
27 # include <QtCore/QFile>
28 # include <QtCore/QString>
29 #endif
31 #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
32 # include <magic.h>
33 # ifdef CARLA_OS_MAC
34 # include "CarlaMacUtils.hpp"
35 # endif
36 #endif
38 CARLA_BACKEND_START_NAMESPACE
40 #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
41 // --------------------------------------------------------------------------------------------------------------------
43 class CarlaMagic
45 public:
46 CarlaMagic()
47 : fMagic(magic_open(MAGIC_SYMLINK)),
48 fLoadedOk(false)
50 CARLA_SAFE_ASSERT_RETURN(fMagic != nullptr,);
52 fLoadedOk = magic_load(fMagic, std::getenv("CARLA_MAGIC_FILE")) == 0;
55 ~CarlaMagic()
57 if (fMagic != nullptr)
58 magic_close(fMagic);
61 const char* getFileDescription(const char* const filename) const
63 if (fMagic == nullptr || ! fLoadedOk)
64 return nullptr;
66 return magic_file(fMagic, filename);
69 private:
70 const magic_t fMagic;
71 bool fLoadedOk;
73 CARLA_PREVENT_HEAP_ALLOCATION
74 CARLA_DECLARE_NON_COPYABLE(CarlaMagic)
76 #endif
78 // --------------------------------------------------------------------------------------------------------------------
80 static inline
81 BinaryType getBinaryTypeFromFile(const char* const filename)
83 carla_debug("getBinaryTypeFromFile(\"%s\")", filename);
85 if (filename == nullptr || filename[0] == '\0')
86 return BINARY_NATIVE;
88 #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
89 static const CarlaMagic magic;
91 const char* const output = magic.getFileDescription(filename);
93 if (output != nullptr && output[0] != '\0')
95 if (std::strstr(output, "MS Windows") != nullptr)
96 if (std::strstr(output, "PE32 executable") != nullptr || std::strstr(output, "PE32+ executable") != nullptr)
97 return (std::strstr(output, "x86-64") != nullptr)
98 ? BINARY_WIN64
99 : BINARY_WIN32;
101 if (std::strstr(output, "MS-DOS executable, MZ for MS-DOS") != nullptr)
102 return BINARY_WIN32;
104 if (std::strstr(output, "ELF") != nullptr)
105 return (std::strstr(output, "x86-64") != nullptr || std::strstr(output, "aarch64") != nullptr)
106 ? BINARY_POSIX64
107 : BINARY_POSIX32;
109 #ifdef CARLA_OS_MAC
110 if (std::strcmp(output, "directory") == 0)
111 if (const char* const binary = findBinaryInBundle(filename))
112 return getBinaryTypeFromFile(binary);
114 if (std::strstr(output, "Mach-O universal binary") != nullptr)
116 // This is tricky, binary actually contains multiple architectures
117 // We just assume what architectures are more important, and check for them first
118 if (std::strstr(output, "x86_64") != nullptr)
119 return BINARY_POSIX64;
120 if (std::strstr(output, "arm64") != nullptr)
121 return BINARY_POSIX64;
122 if (std::strstr(output, "i386") != nullptr)
123 return BINARY_POSIX32;
124 if (std::strstr(output, "ppc") != nullptr)
125 return BINARY_OTHER;
128 carla_debug("getBinaryTypeFromFile(\"%s\") - have output:\n%s", filename, output);
129 #endif
131 return BINARY_NATIVE;
133 #endif
135 #if defined(BUILDING_CARLA) || defined(CARLA_UTILS_USE_QT)
136 #if defined(CARLA_UTILS_USE_QT)
137 QFile file(QString::fromUtf8(filename));
138 CARLA_SAFE_ASSERT_RETURN(file.open(QIODevice::ReadOnly), BINARY_NATIVE);
139 #else
140 using water::File;
141 using water::FileInputStream;
143 CarlaScopedPointer<FileInputStream> stream(File(filename).createInputStream());
144 CARLA_SAFE_ASSERT_RETURN(stream != nullptr && ! stream->failedToOpen(), BINARY_NATIVE);
145 #endif
147 // ----------------------------------------------------------------------------------------------------------------
148 // binary type code based on Ardour's dll_info function
149 // See https://github.com/Ardour/ardour/blob/master/libs/ardour/plugin_manager.cc#L867,L925
150 // Copyright (C) 2000-2006 Paul Davis
152 #if defined(CARLA_UTILS_USE_QT)
153 char buf[68];
154 if (file.read(buf, 68) != 68)
155 #else
156 uint8_t buf[68];
157 if (stream->read(buf, 68) != 68)
158 #endif
159 return BINARY_NATIVE;
161 if (buf[0] != 'M' && buf[1] != 'Z')
162 return BINARY_NATIVE;
164 const int32_t* const pe_hdr_off_ptr = (int32_t*)&buf[60];
165 const int32_t pe_hdr_off = *pe_hdr_off_ptr;
167 #if defined(CARLA_UTILS_USE_QT)
168 if (! file.seek(pe_hdr_off))
169 #else
170 if (! stream->setPosition(pe_hdr_off))
171 #endif
172 return BINARY_NATIVE;
174 #if defined(CARLA_UTILS_USE_QT)
175 if (file.read(buf, 6) != 6)
176 #else
177 if (stream->read(buf, 6) != 6)
178 #endif
179 return BINARY_NATIVE;
181 if (buf[0] != 'P' && buf[1] != 'E')
182 return BINARY_NATIVE;
184 const uint16_t* const type_ptr = (uint16_t*)&buf[4];
185 const uint16_t type = *type_ptr;
187 switch (type)
189 case 0x014c:
190 return BINARY_WIN32;
191 case 0x8664:
192 return BINARY_WIN64;
193 default:
194 return BINARY_NATIVE;
196 #else
197 return BINARY_NATIVE;
198 #endif
201 // --------------------------------------------------------------------------------------------------------------------
203 CARLA_BACKEND_END_NAMESPACE
205 #endif // CARLA_BINARY_UTILS_HPP_INCLUDED