tiny_impdef.c - converted to LF line-endings (and slight cleanup)
This commit is contained in:
parent
f99d3de221
commit
b0d40c12da
1 changed files with 393 additions and 372 deletions
199
tiny_impdef.c
199
tiny_impdef.c
|
|
@ -1,12 +1,26 @@
|
||||||
/* -------------------------------------------------------------- */
|
/* -------------------------------------------------------------- */
|
||||||
/*
|
/*
|
||||||
"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>
|
||||||
|
|
||||||
|
|
@ -69,8 +83,7 @@ LPVOID WINAPI ImageDirectoryOffset (
|
||||||
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)
|
||||||
|
|
@ -101,8 +114,7 @@ BOOL WINAPI GetSectionHdrByName (
|
||||||
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++)
|
||||||
|
|
@ -110,16 +122,13 @@ BOOL WINAPI GetSectionHdrByName (
|
||||||
if (!strcmp (psh->Name, szSection))
|
if (!strcmp (psh->Name, szSection))
|
||||||
{
|
{
|
||||||
/* copy data to header */
|
/* copy data to header */
|
||||||
memcpy ((LPVOID)sh,
|
memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
|
||||||
(LPVOID)psh,
|
|
||||||
sizeof (IMAGE_SECTION_HEADER));
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
psh++;
|
psh++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,25 +143,22 @@ BOOL WINAPI GetSectionHdrByAddress (
|
||||||
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 && addr < psh->VirtualAddress + psh->SizeOfRawData)
|
if (addr >= psh->VirtualAddress
|
||||||
|
&& 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,
|
|
||||||
sizeof (IMAGE_SECTION_HEADER));
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
psh++;
|
psh++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -174,14 +180,15 @@ int WINAPI GetExportFunctionNames (
|
||||||
|
|
||||||
/* 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);
|
||||||
|
|
||||||
|
|
@ -192,7 +199,8 @@ int WINAPI GetExportFunctionNames (
|
||||||
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. */
|
||||||
|
|
@ -202,25 +210,46 @@ int WINAPI GetExportFunctionNames (
|
||||||
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 += strlen(pSrc)+1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*pDest = 0;
|
*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];
|
||||||
char libname[80];
|
FILE *op;
|
||||||
|
char *p;
|
||||||
|
|
||||||
hHeap = NULL;
|
hHeap = NULL;
|
||||||
hFile = NULL;
|
hFile = NULL;
|
||||||
|
|
@ -228,27 +257,21 @@ int main(int argc, char **argv)
|
||||||
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"))
|
|
||||||
{
|
|
||||||
if (++argind == argc) goto usage;
|
|
||||||
strcpy(outfile, argv[argind]);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
goto usage;
|
goto usage;
|
||||||
}
|
|
||||||
else
|
} else if (0 == infile[0])
|
||||||
if (0 == infile[0])
|
|
||||||
strcpy(infile, a);
|
strcpy(infile, a);
|
||||||
else
|
else
|
||||||
goto usage;
|
goto usage;
|
||||||
|
|
@ -258,13 +281,9 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
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"
|
|
||||||
);
|
);
|
||||||
error:
|
|
||||||
ret = 1;
|
|
||||||
goto the_end;
|
goto the_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -274,13 +293,10 @@ error:
|
||||||
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, '/');
|
|
||||||
if (p) strcpy(outfile, p+1);
|
|
||||||
|
|
||||||
p = strrchr(outfile, '.');
|
p = strrchr(outfile, '.');
|
||||||
if (NULL == p) p = strchr(outfile, 0);
|
if (NULL == p)
|
||||||
|
p = strchr(outfile, 0);
|
||||||
strcpy(p, ".def");
|
strcpy(p, ".def");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -296,11 +312,10 @@ error:
|
||||||
|
|
||||||
if (hFile == INVALID_HANDLE_VALUE)
|
if (hFile == INVALID_HANDLE_VALUE)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "file not found: %s\n", infile);
|
fprintf(stderr, "No such file: %s\n", infile);
|
||||||
goto error;
|
goto the_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!std) printf("--> %s\n", infile);
|
|
||||||
|
|
||||||
hMapObject = CreateFileMapping(
|
hMapObject = CreateFileMapping(
|
||||||
hFile,
|
hFile,
|
||||||
|
|
@ -312,8 +327,8 @@ error:
|
||||||
|
|
||||||
if (NULL == hMapObject)
|
if (NULL == hMapObject)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "could not create file mapping.\n");
|
fprintf(stderr, "Could not create file mapping: %s\n", infile);
|
||||||
goto error;
|
goto the_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
pMem = MapViewOfFile(
|
pMem = MapViewOfFile(
|
||||||
|
|
@ -325,48 +340,54 @@ error:
|
||||||
|
|
||||||
if (NULL == pMem)
|
if (NULL == pMem)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "could not map view of file.\n");
|
fprintf(stderr, "Could not map view of file: %s\n", infile);
|
||||||
goto error;
|
goto the_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Not a PE file: %s\n", infile);
|
||||||
|
goto the_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
hHeap = GetProcessHeap();
|
hHeap = GetProcessHeap();
|
||||||
nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
|
nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
|
||||||
{
|
if (0 == nCnt) {
|
||||||
FILE *op; char *p; int n;
|
fprintf(stderr, "Could not get exported function names: %s\n", infile);
|
||||||
if (!std) printf("<-- %s\n", outfile);
|
goto the_end;
|
||||||
|
|
||||||
if (std)
|
|
||||||
op = stdout;
|
|
||||||
else
|
|
||||||
op = fopen(outfile, "wt");
|
|
||||||
|
|
||||||
if (NULL == op)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "could not create file: %s\n", outfile);
|
|
||||||
goto error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p = strrchr(infile, '\\');
|
printf("--> %s\n", infile);
|
||||||
if (NULL == p)
|
|
||||||
p = strrchr(infile, '/');
|
|
||||||
if (NULL == p) p = infile; else ++p;
|
|
||||||
|
|
||||||
fprintf(op, "LIBRARY %s\n\nEXPORTS", p);
|
op = fopen(outfile, "w");
|
||||||
if (std) fprintf(op, " (%d)", nCnt);
|
if (NULL == op)
|
||||||
fprintf(op, "\n");
|
{
|
||||||
|
fprintf(stderr, "Could not create file: %s\n", outfile);
|
||||||
|
goto the_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("<-- %s\n", outfile);
|
||||||
|
|
||||||
|
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);
|
|
||||||
|
if (hMapObject)
|
||||||
|
CloseHandle(hMapObject);
|
||||||
|
|
||||||
|
if (hFile)
|
||||||
|
CloseHandle(hFile);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
/* -------------------------------------------------------------- */
|
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------- */
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue