tiny_impdef.c - converted to LF line-endings (and slight cleanup)

This commit is contained in:
grischka 2007-12-09 18:55:18 +00:00
parent f99d3de221
commit b0d40c12da

View file

@ -1,372 +1,393 @@
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
/* /*
"tiny_impdef creates a .def file from a dll" * tiny_impdef creates an export definition file (.def) from a dll
* on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
"Usage: tiny_impdef [-p] <library.dll> [-o outputfile]" *
"Options:" * Copyright (c) 2005,2007 grischka
" -p print to stdout" *
*/ * 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
/* Offset to PE file signature */ /* Offset to PE file signature */
#define NTSIGNATURE(a) ((LPVOID)((BYTE *)a + \ #define NTSIGNATURE(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew)) ((PIMAGE_DOS_HEADER)a)->e_lfanew))
/* MS-OS header identifies the NT PEFile signature dword; /* MS-OS header identifies the NT PEFile signature dword;
the PEFILE header exists just after that dword. */ the PEFILE header exists just after that dword. */
#define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \ #define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew + \ ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
SIZE_OF_NT_SIGNATURE)) SIZE_OF_NT_SIGNATURE))
/* PE optional header is immediately after PEFile header. */ /* PE optional header is immediately after PEFile header. */
#define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \ #define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew + \ ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
SIZE_OF_NT_SIGNATURE + \ SIZE_OF_NT_SIGNATURE + \
sizeof (IMAGE_FILE_HEADER))) sizeof (IMAGE_FILE_HEADER)))
/* Section headers are immediately after PE optional header. */ /* Section headers are immediately after PE optional header. */
#define SECHDROFFSET(a) ((LPVOID)((BYTE *)a + \ #define SECHDROFFSET(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew + \ ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
SIZE_OF_NT_SIGNATURE + \ SIZE_OF_NT_SIGNATURE + \
sizeof (IMAGE_FILE_HEADER) + \ sizeof (IMAGE_FILE_HEADER) + \
sizeof (IMAGE_OPTIONAL_HEADER))) sizeof (IMAGE_OPTIONAL_HEADER)))
#define SIZE_OF_NT_SIGNATURE 4 #define SIZE_OF_NT_SIGNATURE 4
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
int WINAPI NumOfSections ( int WINAPI NumOfSections (
LPVOID lpFile) LPVOID lpFile)
{ {
/* Number of sections is indicated in file header. */ /* Number of sections is indicated in file header. */
return (int) return (int)
((PIMAGE_FILE_HEADER) ((PIMAGE_FILE_HEADER)
PEFHDROFFSET(lpFile))->NumberOfSections; PEFHDROFFSET(lpFile))->NumberOfSections;
} }
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
LPVOID WINAPI ImageDirectoryOffset ( LPVOID WINAPI ImageDirectoryOffset (
LPVOID lpFile, LPVOID lpFile,
DWORD dwIMAGE_DIRECTORY) DWORD dwIMAGE_DIRECTORY)
{ {
PIMAGE_OPTIONAL_HEADER poh; PIMAGE_OPTIONAL_HEADER poh;
PIMAGE_SECTION_HEADER psh; PIMAGE_SECTION_HEADER psh;
int nSections = NumOfSections (lpFile); int nSections = NumOfSections (lpFile);
int i = 0; int i = 0;
LPVOID VAImageDir; LPVOID VAImageDir;
/* Retrieve offsets to optional and section headers. */ /* Retrieve offsets to optional and section headers. */
poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile); poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile); psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile);
/* Must be 0 thru (NumberOfRvaAndSizes-1). */ /* Must be 0 thru (NumberOfRvaAndSizes-1). */
if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes) if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
return NULL; return NULL;
/* Locate image directory's relative virtual address. */ /* Locate image directory's relative virtual address. */
VAImageDir = (LPVOID)poh->DataDirectory VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress;
[dwIMAGE_DIRECTORY].VirtualAddress;
/* Locate section containing image directory. */ /* Locate section containing image directory. */
while (i++<nSections) while (i++<nSections)
{ {
if (psh->VirtualAddress <= (DWORD)VAImageDir if (psh->VirtualAddress <= (DWORD)VAImageDir
&& psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir) && psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir)
break; break;
psh++; psh++;
} }
if (i > nSections) if (i > nSections)
return NULL; return NULL;
/* Return image import directory offset. */ /* Return image import directory offset. */
return (LPVOID)(((int)lpFile + return (LPVOID)(((int)lpFile +
(int)VAImageDir - psh->VirtualAddress) + (int)VAImageDir - psh->VirtualAddress) +
(int)psh->PointerToRawData); (int)psh->PointerToRawData);
} }
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
BOOL WINAPI GetSectionHdrByName ( BOOL WINAPI GetSectionHdrByName (
LPVOID lpFile, LPVOID lpFile,
IMAGE_SECTION_HEADER *sh, IMAGE_SECTION_HEADER *sh,
char *szSection) char *szSection)
{ {
PIMAGE_SECTION_HEADER psh; PIMAGE_SECTION_HEADER psh;
int nSections = NumOfSections (lpFile); int nSections = NumOfSections (lpFile);
int i; int i;
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
NULL) {
{ /* find the section by name */
/* find the section by name */ for (i=0; i<nSections; i++)
for (i=0; i<nSections; i++) {
{ if (!strcmp (psh->Name, szSection))
if (!strcmp (psh->Name, szSection)) {
{ /* copy data to header */
/* copy data to header */ memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
memcpy ((LPVOID)sh, return TRUE;
(LPVOID)psh, }
sizeof (IMAGE_SECTION_HEADER)); else
return TRUE; psh++;
} }
else }
psh++; return FALSE;
}
}
return FALSE;
} }
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
BOOL WINAPI GetSectionHdrByAddress ( BOOL WINAPI GetSectionHdrByAddress (
LPVOID lpFile, LPVOID lpFile,
IMAGE_SECTION_HEADER *sh, IMAGE_SECTION_HEADER *sh,
DWORD addr) DWORD addr)
{ {
PIMAGE_SECTION_HEADER psh; PIMAGE_SECTION_HEADER psh;
int nSections = NumOfSections (lpFile); int nSections = NumOfSections (lpFile);
int i; int i;
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
NULL) {
{ /* find the section by name */
/* find the section by name */ for (i=0; i<nSections; i++)
for (i=0; i<nSections; i++) {
{ if (addr >= psh->VirtualAddress
if (addr >= psh->VirtualAddress && addr < psh->VirtualAddress + psh->SizeOfRawData) && addr < psh->VirtualAddress + psh->SizeOfRawData)
{ {
/* copy data to header */ /* copy data to header */
memcpy ((LPVOID)sh, memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
(LPVOID)psh, return TRUE;
sizeof (IMAGE_SECTION_HEADER)); }
return TRUE; else
} psh++;
else }
psh++; }
} return FALSE;
}
return FALSE;
} }
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
int WINAPI GetExportFunctionNames ( int WINAPI GetExportFunctionNames (
LPVOID lpFile, LPVOID lpFile,
HANDLE hHeap, HANDLE hHeap,
char **pszFunctions) char **pszFunctions)
{ {
IMAGE_SECTION_HEADER sh; IMAGE_SECTION_HEADER sh;
PIMAGE_EXPORT_DIRECTORY ped; PIMAGE_EXPORT_DIRECTORY ped;
int *pNames, *pCnt; int *pNames, *pCnt;
char *pSrc, *pDest; char *pSrc, *pDest;
int i, nCnt; int i, nCnt;
DWORD VAImageDir; DWORD VAImageDir;
PIMAGE_OPTIONAL_HEADER poh; PIMAGE_OPTIONAL_HEADER poh;
char *pOffset; char *pOffset;
/* Get section header and pointer to data directory /* Get section header and pointer to data directory
for .edata section. */ for .edata section. */
if ((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset if (NULL == (ped = (PIMAGE_EXPORT_DIRECTORY)
(lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)) == NULL) ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)))
return 0; return 0;
poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile); poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
VAImageDir = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; VAImageDir = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
if (FALSE == GetSectionHdrByAddress (lpFile, &sh, VAImageDir)) return 0; if (FALSE == GetSectionHdrByAddress (lpFile, &sh, VAImageDir))
return 0;
pOffset = (char *)lpFile + (sh.PointerToRawData - sh.VirtualAddress); pOffset = (char *)lpFile + (sh.PointerToRawData - sh.VirtualAddress);
pNames = (int *)(pOffset + (DWORD)ped->AddressOfNames); pNames = (int *)(pOffset + (DWORD)ped->AddressOfNames);
/* Figure out how much memory to allocate for all strings. */ /* Figure out how much memory to allocate for all strings. */
nCnt = 1; nCnt = 1;
for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++) for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
{ {
pSrc = (pOffset + *pCnt++); pSrc = (pOffset + *pCnt++);
if (pSrc) nCnt += strlen(pSrc)+1; if (pSrc)
} nCnt += strlen(pSrc)+1;
}
/* Allocate memory off heap for function names. */ /* Allocate memory off heap for function names. */
pDest = *pszFunctions = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, nCnt); pDest = *pszFunctions = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, nCnt);
/* Copy all strings to buffer. */ /* Copy all strings to buffer. */
for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++) for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
{ {
pSrc = (pOffset + *pCnt++); pSrc = (pOffset + *pCnt++);
if (pSrc) { strcpy(pDest, pSrc); pDest += strlen(pSrc)+1; } if (pSrc) {
} strcpy(pDest, pSrc);
*pDest = 0; pDest += strlen(pSrc)+1;
}
}
*pDest = 0;
return ped->NumberOfNames; return ped->NumberOfNames;
}
/* -------------------------------------------------------------- */
/* extract the basename of a file */
static char *file_basename(const char *name)
{
const char *p = strchr(name, 0);
while (p > name
&& p[-1] != '/'
&& p[-1] != '\\'
)
--p;
return (char*)p;
} }
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
HANDLE hHeap;
HANDLE hFile;
HANDLE hMapObject;
VOID *pMem;
HANDLE hHeap; HANDLE hFile; HANDLE hMapObject; VOID *pMem; int nCnt, ret, n;
int nCnt, ret, argind, std; char *pNames;
char *pNames; char infile[MAX_PATH];
char infile[MAX_PATH]; char buffer[MAX_PATH];
char buffer[MAX_PATH]; char outfile[MAX_PATH];
char outfile[MAX_PATH]; FILE *op;
char libname[80]; char *p;
hHeap = NULL; hHeap = NULL;
hFile = NULL; hFile = NULL;
hMapObject = NULL; hMapObject = NULL;
pMem = NULL; pMem = NULL;
infile[0] = 0; infile[0] = 0;
outfile[0] = 0; outfile[0] = 0;
ret = 0; ret = 1;
std = 0;
for (argind = 1; argind < argc; ++argind) for (n = 1; n < argc; ++n)
{ {
const char *a = argv[argind]; const char *a = argv[n];
if ('-' == a[0]) if ('-' == a[0]) {
{ if (0 == strcmp(a, "-o")) {
if (0 == strcmp(a, "-p")) if (++n == argc)
std = 1; goto usage;
else strcpy(outfile, argv[n]);
if (0 == strcmp(a, "-o")) }
{ else
if (++argind == argc) goto usage; goto usage;
strcpy(outfile, argv[argind]);
}
else
goto usage;
}
else
if (0 == infile[0])
strcpy(infile, a);
else
goto usage;
}
if (0 == infile[0]) } else if (0 == infile[0])
{ strcpy(infile, a);
else
goto usage;
}
if (0 == infile[0])
{
usage: usage:
fprintf(stderr, fprintf(stderr,
"tiny_impdef creates a .def file from a dll\n" "tiny_impdef creates an export definition file (.def) from a dll\n"
"Usage: tiny_impdef [-p] <library.dll> [-o outputfile]\n" "Usage: tiny_impdef library.dll [-o outputfile]\n"
"Options:\n" );
" -p print to stdout\n" goto the_end;
); }
error:
ret = 1;
goto the_end;
}
if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL)) if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL))
strcpy(infile, buffer); strcpy(infile, buffer);
if (0 == outfile[0]) if (0 == outfile[0])
{ {
char *p; char *p;
p = strrchr(strcpy(outfile, infile), '\\'); strcpy(outfile, file_basename(infile));
if (NULL == p) p = strrchr(outfile, '.');
p = strrchr(outfile, '/'); if (NULL == p)
if (p) strcpy(outfile, p+1); p = strchr(outfile, 0);
strcpy(p, ".def");
}
p = strrchr(outfile, '.'); hFile = CreateFile(
if (NULL == p) p = strchr(outfile, 0); infile,
strcpy(p, ".def"); GENERIC_READ,
} FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL
);
hFile=CreateFile( if (hFile == INVALID_HANDLE_VALUE)
infile, {
GENERIC_READ, fprintf(stderr, "No such file: %s\n", infile);
FILE_SHARE_READ, goto the_end;
NULL, }
OPEN_EXISTING,
0,
NULL
);
if (hFile == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "file not found: %s\n", infile);
goto error;
}
if (!std) printf("--> %s\n", infile); hMapObject = CreateFileMapping(
hFile,
NULL,
PAGE_READONLY,
0, 0,
NULL
);
hMapObject = CreateFileMapping( if (NULL == hMapObject)
hFile, {
NULL, fprintf(stderr, "Could not create file mapping: %s\n", infile);
PAGE_READONLY, goto the_end;
0, 0, }
NULL
);
if (NULL == hMapObject) pMem = MapViewOfFile(
{ hMapObject, // object to map view of
fprintf(stderr, "could not create file mapping.\n"); FILE_MAP_READ, // read access
goto error; 0, // high offset: map from
} 0, // low offset: beginning
0); // default: map entire file
pMem = MapViewOfFile( if (NULL == pMem)
hMapObject, // object to map view of {
FILE_MAP_READ, // read access fprintf(stderr, "Could not map view of file: %s\n", infile);
0, // high offset: map from goto the_end;
0, // low offset: beginning }
0); // default: map entire file
if (NULL == pMem) if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2))
{ {
fprintf(stderr, "could not map view of file.\n"); fprintf(stderr, "Not a PE file: %s\n", infile);
goto error; goto the_end;
} }
hHeap = GetProcessHeap();
nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
{
FILE *op; char *p; int n;
if (!std) printf("<-- %s\n", outfile);
if (std) hHeap = GetProcessHeap();
op = stdout; nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
else if (0 == nCnt) {
op = fopen(outfile, "wt"); fprintf(stderr, "Could not get exported function names: %s\n", infile);
goto the_end;
}
if (NULL == op) printf("--> %s\n", infile);
{
fprintf(stderr, "could not create file: %s\n", outfile);
goto error;
}
p = strrchr(infile, '\\'); op = fopen(outfile, "w");
if (NULL == p) if (NULL == op)
p = strrchr(infile, '/'); {
if (NULL == p) p = infile; else ++p; fprintf(stderr, "Could not create file: %s\n", outfile);
goto the_end;
}
fprintf(op, "LIBRARY %s\n\nEXPORTS", p); printf("<-- %s\n", outfile);
if (std) fprintf(op, " (%d)", nCnt);
fprintf(op, "\n"); fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile));
for (n = 0, p = pNames; n < nCnt; ++n) for (n = 0, p = pNames; n < nCnt; ++n)
{ {
fprintf(op, "%s\n", p); fprintf(op, "%s\n", p);
while (*p++); while (*p++);
} }
if (!std) fclose(op); ret = 0;
}
the_end: the_end:
if (pMem) UnmapViewOfFile(pMem); if (pMem)
if (hMapObject) CloseHandle(hMapObject); UnmapViewOfFile(pMem);
if (hFile) CloseHandle(hFile);
return ret;
}
/* -------------------------------------------------------------- */
if (hMapObject)
CloseHandle(hMapObject);
if (hFile)
CloseHandle(hFile);
return ret;
}
/* -------------------------------------------------------------- */