Move PluginArchitecture to common

Along with the accompanying function to parse a PE32 file. We're going
to have to define some common tags for different plugin types.
This commit is contained in:
Robbert van der Helm
2020-11-30 21:24:41 +01:00
parent 687696ec6b
commit 7fc7a51a46
6 changed files with 100 additions and 69 deletions
+53
View File
@@ -0,0 +1,53 @@
#include "plugins.h"
#include <fstream>
#include <sstream>
namespace fs = boost::filesystem;
PluginArchitecture find_vst_architecture(fs::path plugin_path) {
std::ifstream file(plugin_path, std::ifstream::binary | std::ifstream::in);
// The linker will place the offset where the PE signature is placed at the
// end of the MS-DOS stub, at offset 0x3c
uint32_t pe_signature_offset;
file.seekg(0x3c);
file.read(reinterpret_cast<char*>(&pe_signature_offset),
sizeof(pe_signature_offset));
// The PE32 signature will be followed by a magic number that indicates the
// target architecture of the binary
uint32_t pe_signature;
uint16_t machine_type;
file.seekg(pe_signature_offset);
file.read(reinterpret_cast<char*>(&pe_signature), sizeof(pe_signature));
file.read(reinterpret_cast<char*>(&machine_type), sizeof(machine_type));
constexpr char expected_pe_signature[4] = {'P', 'E', '\0', '\0'};
if (pe_signature !=
*reinterpret_cast<const uint32_t*>(expected_pe_signature)) {
throw std::runtime_error("'" + plugin_path.string() +
"' is not a valid .dll file");
}
// These constants are specified in
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#machine-types
switch (machine_type) {
case 0x014c: // IMAGE_FILE_MACHINE_I386
return PluginArchitecture::vst_32;
break;
case 0x8664: // IMAGE_FILE_MACHINE_AMD64
case 0x0000: // IMAGE_FILE_MACHINE_UNKNOWN
return PluginArchitecture::vst_64;
break;
}
// When compiled without optimizations, GCC 9.3 will warn that the function
// does not return if we put this in a `default:` case instead.
std::ostringstream error_msg;
error_msg << "'" << plugin_path
<< "' is neither a x86 nor a x86_64 PE32 file. Actual "
"architecture: 0x"
<< std::hex << machine_type;
throw std::runtime_error(error_msg.str());
}
+44
View File
@@ -0,0 +1,44 @@
// yabridge: a Wine VST bridge
// Copyright (C) 2020 Robbert van der Helm
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#ifdef __WINE__
#include "../wine-host/boost-fix.h"
#endif
#include <boost/filesystem.hpp>
// Utilities and tags for plugin types and architectures
/**
* A tag to differentiate between 32 and 64-bit plugins, used to determine which
* host application to use.
*/
enum class PluginArchitecture { vst_32, vst_64 };
/**
* Determine the architecture of a VST plugin (or rather, a .dll file) based on
* it's header values.
*
* See https://docs.microsoft.com/en-us/windows/win32/debug/pe-format for more
* information on the PE32 format.
*
* @param plugin_path The path to the .dll file we're going to check.
*
* @return The detected architecture.
* @throw std::runtime_error If the file is not a .dll file.
*/
PluginArchitecture find_vst_architecture(boost::filesystem::path);