2002-08-07 21:55:00 +00:00
|
|
|
#include "exehead/config.h"
|
|
|
|
#ifdef NSIS_CONFIG_PLUGIN_SUPPORT
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2002-08-05 19:13:52 +00:00
|
|
|
#include "Plugins.h"
|
2004-01-30 22:04:10 +00:00
|
|
|
#include "Platform.h"
|
2004-03-29 20:21:00 +00:00
|
|
|
#include "util.h"
|
2005-08-27 19:56:00 +00:00
|
|
|
|
2005-07-01 21:24:32 +00:00
|
|
|
#include "dirreader.h"
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2004-03-29 20:21:00 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <WinNT.h>
|
|
|
|
#else
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
using namespace std;
|
2004-10-12 21:27:09 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
extern FILE *g_output;
|
2004-10-12 21:27:09 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
void Plugins::FindCommands(const string &path, bool displayInfo)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-07-01 21:24:32 +00:00
|
|
|
dir_reader *dr = new_dir_reader();
|
|
|
|
dr->read(path);
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-07-01 21:24:32 +00:00
|
|
|
dir_reader::iterator files_itr = dr->files().begin();
|
|
|
|
dir_reader::iterator files_end = dr->files().end();
|
2004-03-29 20:21:00 +00:00
|
|
|
|
2005-07-01 21:24:32 +00:00
|
|
|
for (; files_itr != files_end; files_itr++) {
|
|
|
|
if (!dir_reader::matches(*files_itr, "*.dll"))
|
|
|
|
continue;
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
const string plugin = path + PLATFORM_PATH_SEPARATOR_C + *files_itr;
|
|
|
|
GetExports(plugin, displayInfo);
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
2005-07-01 21:24:32 +00:00
|
|
|
|
|
|
|
delete dr;
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
void Plugins::GetExports(const string &pathToDll, bool displayInfo)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-08-27 19:56:00 +00:00
|
|
|
unsigned char* dlldata = 0;
|
|
|
|
bool loaded = false;
|
|
|
|
|
|
|
|
string dllName = remove_file_extension(get_file_name(pathToDll));
|
|
|
|
|
|
|
|
FILE* dll = fopen(pathToDll.c_str() ,"rb");
|
|
|
|
if (dll == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fseek(dll,0,SEEK_END);
|
|
|
|
long dlldatalen = ftell(dll);
|
|
|
|
fseek(dll,0,SEEK_SET);
|
|
|
|
if (dlldatalen > 0)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-08-27 19:56:00 +00:00
|
|
|
dlldata = new unsigned char [dlldatalen];
|
|
|
|
assert(dlldata);
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
size_t bytesread = fread((void*)dlldata,1,dlldatalen,dll);
|
|
|
|
if (bytesread == (size_t)dlldatalen)
|
|
|
|
loaded = true;
|
|
|
|
}
|
|
|
|
fclose(dll);
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
if (!loaded)
|
|
|
|
{
|
|
|
|
delete[] dlldata;
|
|
|
|
return;
|
|
|
|
}
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
PIMAGE_NT_HEADERS NTHeaders = PIMAGE_NT_HEADERS(dlldata + PIMAGE_DOS_HEADER(dlldata)->e_lfanew);
|
|
|
|
if (NTHeaders->Signature == IMAGE_NT_SIGNATURE)
|
|
|
|
{
|
|
|
|
if (NTHeaders->FileHeader.Characteristics & IMAGE_FILE_DLL)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-08-27 19:56:00 +00:00
|
|
|
if (NTHeaders->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXPORT) return;
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
DWORD ExportDirVA = NTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
|
|
|
|
DWORD ExportDirSize = NTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
|
|
|
|
PIMAGE_SECTION_HEADER sections = IMAGE_FIRST_SECTION(NTHeaders);
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
for (int i = 0; i < NTHeaders->FileHeader.NumberOfSections; i++)
|
|
|
|
{
|
|
|
|
if (sections[i].VirtualAddress <= ExportDirVA
|
|
|
|
&& sections[i].VirtualAddress+sections[i].Misc.VirtualSize >= ExportDirVA+ExportDirSize)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-08-27 19:56:00 +00:00
|
|
|
PIMAGE_EXPORT_DIRECTORY exports = PIMAGE_EXPORT_DIRECTORY(dlldata + sections[i].PointerToRawData + ExportDirVA - sections[i].VirtualAddress);
|
|
|
|
unsigned long *names = (unsigned long*)((unsigned long)exports + (char *)exports->AddressOfNames - ExportDirVA);
|
|
|
|
for (unsigned long j = 0; j < exports->NumberOfNames; j++)
|
|
|
|
{
|
|
|
|
const string name = string((char*)exports + names[j] - ExportDirVA);
|
|
|
|
const string signature = dllName + "::" + name;
|
|
|
|
m_command_to_path[signature] = pathToDll;
|
|
|
|
m_command_lowercase_to_command[lowercase(signature)] = signature;
|
|
|
|
if (displayInfo)
|
|
|
|
fprintf(g_output, " - %s\n", signature.c_str());
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
2005-08-27 19:56:00 +00:00
|
|
|
break;
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-08-27 19:56:00 +00:00
|
|
|
|
|
|
|
delete[] dlldata;
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
bool Plugins::IsPluginCommand(const string& token) const {
|
|
|
|
return m_command_to_path.find(NormalizedCommand(token)) != m_command_to_path.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template <class Key, class Value>
|
|
|
|
Value get_value(const map<Key, Value>& the_map,
|
|
|
|
const Key& key)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-08-27 19:56:00 +00:00
|
|
|
assert(the_map.find(key) != the_map.end());
|
|
|
|
return the_map.find(key)->second;
|
2002-11-14 19:43:14 +00:00
|
|
|
}
|
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
template <class Key, class Value>
|
|
|
|
Value get_value(const map<Key, Value>& the_map,
|
|
|
|
const Key& key,
|
|
|
|
const Value& defaultValue)
|
|
|
|
{
|
|
|
|
if (the_map.find(key) == the_map.end())
|
|
|
|
return defaultValue;
|
|
|
|
return the_map.find(key)->second;
|
|
|
|
}
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2005-08-27 19:56:00 +00:00
|
|
|
string Plugins::NormalizedCommand(const string& command) const {
|
|
|
|
return get_value(m_command_lowercase_to_command, lowercase(command));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Plugins::GetPluginHandle(bool uninst, const string& command) const {
|
|
|
|
if (uninst) {
|
|
|
|
return get_value(m_command_to_uninstall_data_handle, command, -1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return get_value(m_command_to_data_handle, command, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string Plugins::GetPluginPath(const string& command) const {
|
|
|
|
return get_value(m_command_to_path, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Plugins::SetDllDataHandle(bool uninst, const string& command, int dataHandle)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-08-27 19:56:00 +00:00
|
|
|
if (uninst) {
|
|
|
|
m_command_to_uninstall_data_handle[command] = dataHandle;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_command_to_data_handle[command] = dataHandle;
|
|
|
|
}
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2004-03-12 20:43:54 +00:00
|
|
|
#endif
|