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"
|
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>
|
|
|
|
# include <glob.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
|
|
|
{
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
int length = strlen(path);
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2004-03-29 20:21:00 +00:00
|
|
|
char *lc = CharPrev(path, path + strlen(path));
|
|
|
|
if (*lc == '\\' || *lc == '/')
|
2002-08-05 19:13:52 +00:00
|
|
|
{
|
|
|
|
length--;
|
|
|
|
}
|
2002-08-05 02:05:00 +00:00
|
|
|
|
|
|
|
char* basePath = new char [length+1];
|
|
|
|
strncpy(basePath,path,length);
|
|
|
|
basePath[length] = 0;
|
|
|
|
|
|
|
|
char* pathAndWildcard = new char [length+7];
|
|
|
|
strcpy(pathAndWildcard,basePath);
|
2004-08-20 15:40:38 +00:00
|
|
|
strcat(pathAndWildcard,PLATFORM_PATH_SEPARATOR_STR "*.dll");
|
2004-03-29 20:21:00 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
HANDLE handle;
|
2002-08-05 02:05:00 +00:00
|
|
|
|
|
|
|
handle = FindFirstFile(pathAndWildcard,&data);
|
|
|
|
if (handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
do
|
2004-03-29 20:21:00 +00:00
|
|
|
#else
|
|
|
|
glob_t globbuf;
|
|
|
|
globbuf.gl_offs = 0;
|
|
|
|
globbuf.gl_pathc = 0;
|
|
|
|
if (!glob(pathAndWildcard, 0, NULL, &globbuf))
|
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
for (unsigned int i = 0; i < globbuf.gl_pathc; i++)
|
|
|
|
{
|
|
|
|
if (stat(globbuf.gl_pathv[i], &s) || !S_ISREG(s.st_mode))
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
2002-08-05 02:05:00 +00:00
|
|
|
{
|
|
|
|
char* dllPath = new char [length+strlen(data.cFileName)+2];
|
2004-08-20 15:40:38 +00:00
|
|
|
wsprintf(dllPath,"%s" PLATFORM_PATH_SEPARATOR_STR "%s",basePath,data.cFileName);
|
2004-03-29 20:21:00 +00:00
|
|
|
#else
|
|
|
|
char *dllPath = new char [strlen(globbuf.gl_pathv[i])+1];
|
|
|
|
strcpy(dllPath,globbuf.gl_pathv[i]);
|
|
|
|
#endif
|
2002-08-05 02:05:00 +00:00
|
|
|
GetExports(dllPath,displayInfo);
|
|
|
|
delete[] dllPath;
|
2004-03-29 20:21:00 +00:00
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
while (FindNextFile(handle,&data));
|
|
|
|
#else
|
|
|
|
globfree(&globbuf);
|
|
|
|
#endif
|
2002-08-05 02:05:00 +00:00
|
|
|
}
|
|
|
|
|
2004-03-29 20:21:00 +00:00
|
|
|
#ifdef _WIN32
|
2002-08-05 02:05:00 +00:00
|
|
|
delete[] pathAndWildcard;
|
2004-03-29 20:21:00 +00:00
|
|
|
#endif
|
2002-08-05 02:05:00 +00:00
|
|
|
delete[] basePath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|