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>
31 #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
34 # include "CarlaMacUtils.hpp"
38 CARLA_BACKEND_START_NAMESPACE
40 #if defined(HAVE_LIBMAGIC) && ! defined(BUILD_BRIDGE_ALTERNATIVE_ARCH)
41 // --------------------------------------------------------------------------------------------------------------------
47 : fMagic(magic_open(MAGIC_SYMLINK
)),
50 CARLA_SAFE_ASSERT_RETURN(fMagic
!= nullptr,);
52 fLoadedOk
= magic_load(fMagic
, std::getenv("CARLA_MAGIC_FILE")) == 0;
57 if (fMagic
!= nullptr)
61 const char* getFileDescription(const char* const filename
) const
63 if (fMagic
== nullptr || ! fLoadedOk
)
66 return magic_file(fMagic
, filename
);
73 CARLA_PREVENT_HEAP_ALLOCATION
74 CARLA_DECLARE_NON_COPYABLE(CarlaMagic
)
78 // --------------------------------------------------------------------------------------------------------------------
81 BinaryType
getBinaryTypeFromFile(const char* const filename
)
83 carla_debug("getBinaryTypeFromFile(\"%s\")", filename
);
85 if (filename
== nullptr || filename
[0] == '\0')
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)
101 if (std::strstr(output
, "MS-DOS executable, MZ for MS-DOS") != nullptr)
104 if (std::strstr(output
, "ELF") != nullptr)
105 return (std::strstr(output
, "x86-64") != nullptr || std::strstr(output
, "aarch64") != nullptr)
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)
128 carla_debug("getBinaryTypeFromFile(\"%s\") - have output:\n%s", filename
, output
);
131 return BINARY_NATIVE
;
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
);
141 using water::FileInputStream
;
143 CarlaScopedPointer
<FileInputStream
> stream(File(filename
).createInputStream());
144 CARLA_SAFE_ASSERT_RETURN(stream
!= nullptr && ! stream
->failedToOpen(), BINARY_NATIVE
);
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)
154 if (file
.read(buf
, 68) != 68)
157 if (stream
->read(buf
, 68) != 68)
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
))
170 if (! stream
->setPosition(pe_hdr_off
))
172 return BINARY_NATIVE
;
174 #if defined(CARLA_UTILS_USE_QT)
175 if (file
.read(buf
, 6) != 6)
177 if (stream
->read(buf
, 6) != 6)
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
;
194 return BINARY_NATIVE
;
197 return BINARY_NATIVE
;
201 // --------------------------------------------------------------------------------------------------------------------
203 CARLA_BACKEND_END_NAMESPACE
205 #endif // CARLA_BINARY_UTILS_HPP_INCLUDED