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
|
|
|
|
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-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
|
|
|
|
|
|
|
extern FILE *g_output;
|
|
|
|
|
2004-10-12 21:27:09 +00:00
|
|
|
int PluginsList::add(const char *name, const char *path)
|
|
|
|
{
|
|
|
|
int pos=SortedStringListND<struct plugin>::add(name);
|
|
|
|
if (pos == -1) return 1;
|
|
|
|
|
|
|
|
((struct plugin*)gr.get())[pos].path=strings.add(path, strlen(path)+1);
|
|
|
|
((struct plugin*)gr.get())[pos].dataHandle=-1;
|
|
|
|
((struct plugin*)gr.get())[pos].unDataHandle=-1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* PluginsList::get(char **name, int *dataHandle/*=0*/, int *uninstDataHandle/*=0*/)
|
|
|
|
{
|
|
|
|
if (dataHandle) *dataHandle=-1;
|
|
|
|
if (uninstDataHandle) *uninstDataHandle=-1;
|
|
|
|
int v=SortedStringListND<struct plugin>::find(*name);
|
|
|
|
if (v==-1) return NULL;
|
|
|
|
strcpy(*name, (char*)strings.get()+((struct plugin*)gr.get())[v].name);
|
|
|
|
if (dataHandle) *dataHandle=((struct plugin*)gr.get())[v].dataHandle;
|
|
|
|
if (uninstDataHandle) *uninstDataHandle=((struct plugin*)gr.get())[v].unDataHandle;
|
|
|
|
return (char*)strings.get()+((struct plugin*)gr.get())[v].path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginsList::setDataHandle(const char *name, int dataHandle, int uninstDataHandle)
|
|
|
|
{
|
|
|
|
int v=SortedStringListND<struct plugin>::find(name);
|
|
|
|
if (v==-1) return;
|
|
|
|
((struct plugin*)gr.get())[v].dataHandle=dataHandle;
|
|
|
|
((struct plugin*)gr.get())[v].unDataHandle=uninstDataHandle;
|
|
|
|
}
|
|
|
|
|
2004-03-29 20:21:00 +00:00
|
|
|
void Plugins::FindCommands(char* path, bool displayInfo)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2005-07-01 21:24:32 +00:00
|
|
|
if (!path)
|
|
|
|
return;
|
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-07-01 21:24:32 +00:00
|
|
|
string plugin = string(path) + PLATFORM_PATH_SEPARATOR_C + *files_itr;
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2005-07-01 21:24:32 +00:00
|
|
|
GetExports((char *) plugin.c_str(), 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
|
|
|
}
|
|
|
|
|
2004-03-11 19:29:04 +00:00
|
|
|
void Plugins::GetExports(char* pathToDll, bool displayInfo)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
|
|
|
if (pathToDll)
|
|
|
|
{
|
|
|
|
unsigned char* dlldata = 0;
|
|
|
|
long dlldatalen = 0;
|
|
|
|
bool loaded = false;
|
2004-03-29 20:21:00 +00:00
|
|
|
char dllName[1024];
|
|
|
|
char signature[1024];
|
2002-08-05 02:05:00 +00:00
|
|
|
|
|
|
|
dllName[0] = 0;
|
2004-08-20 15:40:38 +00:00
|
|
|
char* ptr = strrchr(pathToDll,PLATFORM_PATH_SEPARATOR_C);
|
2002-08-05 02:05:00 +00:00
|
|
|
if (ptr && *ptr && *(ptr+1)) strcpy(dllName,ptr+1);
|
2004-03-11 19:29:04 +00:00
|
|
|
|
|
|
|
// find .dll
|
2004-03-29 20:21:00 +00:00
|
|
|
int len = strlen(dllName);
|
|
|
|
if (len > 4 && !stricmp(dllName + len - 4, ".dll"))
|
2004-03-11 19:29:04 +00:00
|
|
|
{
|
2004-03-29 20:21:00 +00:00
|
|
|
dllName[len - 4] = 0;
|
2004-03-11 19:29:04 +00:00
|
|
|
}
|
2002-08-05 02:05:00 +00:00
|
|
|
|
|
|
|
FILE* dll = fopen(pathToDll,"rb");
|
|
|
|
if (dll)
|
|
|
|
{
|
|
|
|
fseek(dll,0,SEEK_END);
|
|
|
|
dlldatalen = ftell(dll);
|
|
|
|
fseek(dll,0,SEEK_SET);
|
|
|
|
if (dlldatalen > 0)
|
|
|
|
{
|
|
|
|
dlldata = new unsigned char [dlldatalen];
|
|
|
|
if (dlldata)
|
|
|
|
{
|
|
|
|
size_t bytesread = fread((void*)dlldata,1,dlldatalen,dll);
|
|
|
|
if ((long)bytesread == dlldatalen)
|
|
|
|
loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(dll);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loaded)
|
|
|
|
{
|
|
|
|
if (dlldata) delete[] dlldata;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-07 21:55:00 +00:00
|
|
|
PIMAGE_NT_HEADERS NTHeaders = PIMAGE_NT_HEADERS(dlldata + PIMAGE_DOS_HEADER(dlldata)->e_lfanew);
|
|
|
|
if (NTHeaders->Signature == IMAGE_NT_SIGNATURE)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2002-08-07 21:55:00 +00:00
|
|
|
if (NTHeaders->FileHeader.Characteristics & IMAGE_FILE_DLL)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2002-08-07 21:55:00 +00:00
|
|
|
if (NTHeaders->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXPORT) return;
|
2002-08-05 02:05:00 +00:00
|
|
|
|
2002-08-07 21:55: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
|
|
|
|
2002-08-07 21:55:00 +00:00
|
|
|
for (int i = 0; i < NTHeaders->FileHeader.NumberOfSections; i++)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2002-08-07 21:55:00 +00:00
|
|
|
if (sections[i].VirtualAddress <= ExportDirVA
|
|
|
|
&& sections[i].VirtualAddress+sections[i].Misc.VirtualSize >= ExportDirVA+ExportDirSize)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2002-08-07 21:55:00 +00:00
|
|
|
PIMAGE_EXPORT_DIRECTORY exports = PIMAGE_EXPORT_DIRECTORY(dlldata + sections[i].PointerToRawData + ExportDirVA - sections[i].VirtualAddress);
|
2004-03-12 20:43:54 +00:00
|
|
|
unsigned long *names = (unsigned long*)((unsigned long)exports + (char *)exports->AddressOfNames - ExportDirVA);
|
2002-08-07 21:55:00 +00:00
|
|
|
for (unsigned long j = 0; j < exports->NumberOfNames; j++)
|
|
|
|
{
|
|
|
|
char *name = (char*)exports + names[j] - ExportDirVA;
|
2002-08-08 16:35:53 +00:00
|
|
|
wsprintf(signature, "%s::%s", dllName, name);
|
2003-04-17 15:27:12 +00:00
|
|
|
m_list.add(signature, pathToDll);
|
2002-08-07 21:55:00 +00:00
|
|
|
if (displayInfo)
|
|
|
|
fprintf(g_output, " - %s\n", signature);
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
2002-08-07 21:55:00 +00:00
|
|
|
break;
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] dlldata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-05 19:13:52 +00:00
|
|
|
bool Plugins::IsPluginCommand(char* token)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2003-04-17 15:27:12 +00:00
|
|
|
return m_list.get(&token) ? true : false;
|
2002-11-14 19:43:14 +00:00
|
|
|
}
|
|
|
|
|
2002-12-21 09:14:28 +00:00
|
|
|
char* Plugins::GetPluginDll(int uninst, char** command, int* dataHandle)
|
2003-04-17 15:27:12 +00:00
|
|
|
{
|
|
|
|
*dataHandle = -1;
|
|
|
|
|
|
|
|
if (uninst)
|
|
|
|
return m_list.get(command, 0, dataHandle);
|
|
|
|
else
|
|
|
|
return m_list.get(command, dataHandle, 0);
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2002-11-22 12:45:38 +00:00
|
|
|
void Plugins::SetDllDataHandle(int uninst, char* command, int dataHandle)
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
2003-04-17 15:27:12 +00:00
|
|
|
if (uninst)
|
|
|
|
m_list.setDataHandle(command, -1, dataHandle);
|
|
|
|
else
|
|
|
|
m_list.setDataHandle(command, dataHandle, -1);
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2004-03-12 20:43:54 +00:00
|
|
|
#endif
|