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,12 +1,26 @@
/* -------------------------------------------------------------- */
/*
"tiny_impdef creates a .def file from a dll"
"Usage: tiny_impdef [-p] <library.dll> [-o outputfile]"
"Options:"
" -p print to stdout"
* tiny_impdef creates an export definition file (.def) from a dll
* on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
*
* Copyright (c) 2005,2007 grischka
*
* 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 <stdio.h>
@ -69,8 +83,7 @@ LPVOID WINAPI ImageDirectoryOffset (
return NULL;
/* Locate image directory's relative virtual address. */
VAImageDir = (LPVOID)poh->DataDirectory
[dwIMAGE_DIRECTORY].VirtualAddress;
VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress;
/* Locate section containing image directory. */
while (i++<nSections)
@ -101,8 +114,7 @@ BOOL WINAPI GetSectionHdrByName (
int nSections = NumOfSections (lpFile);
int i;
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) !=
NULL)
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
{
/* find the section by name */
for (i=0; i<nSections; i++)
@ -110,16 +122,13 @@ BOOL WINAPI GetSectionHdrByName (
if (!strcmp (psh->Name, szSection))
{
/* copy data to header */
memcpy ((LPVOID)sh,
(LPVOID)psh,
sizeof (IMAGE_SECTION_HEADER));
memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
return TRUE;
}
else
psh++;
}
}
return FALSE;
}
@ -134,25 +143,22 @@ BOOL WINAPI GetSectionHdrByAddress (
int nSections = NumOfSections (lpFile);
int i;
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) !=
NULL)
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
{
/* find the section by name */
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 */
memcpy ((LPVOID)sh,
(LPVOID)psh,
sizeof (IMAGE_SECTION_HEADER));
memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
return TRUE;
}
else
psh++;
}
}
return FALSE;
}
@ -174,14 +180,15 @@ int WINAPI GetExportFunctionNames (
/* Get section header and pointer to data directory
for .edata section. */
if ((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset
(lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)) == NULL)
if (NULL == (ped = (PIMAGE_EXPORT_DIRECTORY)
ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)))
return 0;
poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
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);
@ -192,7 +199,8 @@ int WINAPI GetExportFunctionNames (
for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
{
pSrc = (pOffset + *pCnt++);
if (pSrc) nCnt += strlen(pSrc)+1;
if (pSrc)
nCnt += strlen(pSrc)+1;
}
/* Allocate memory off heap for function names. */
@ -202,25 +210,46 @@ int WINAPI GetExportFunctionNames (
for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
{
pSrc = (pOffset + *pCnt++);
if (pSrc) { strcpy(pDest, pSrc); pDest += strlen(pSrc)+1; }
if (pSrc) {
strcpy(pDest, pSrc);
pDest += strlen(pSrc)+1;
}
}
*pDest = 0;
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)
{
HANDLE hHeap;
HANDLE hFile;
HANDLE hMapObject;
VOID *pMem;
HANDLE hHeap; HANDLE hFile; HANDLE hMapObject; VOID *pMem;
int nCnt, ret, argind, std;
int nCnt, ret, n;
char *pNames;
char infile[MAX_PATH];
char buffer[MAX_PATH];
char outfile[MAX_PATH];
char libname[80];
FILE *op;
char *p;
hHeap = NULL;
hFile = NULL;
@ -228,27 +257,21 @@ int main(int argc, char **argv)
pMem = NULL;
infile[0] = 0;
outfile[0] = 0;
ret = 0;
std = 0;
ret = 1;
for (argind = 1; argind < argc; ++argind)
for (n = 1; n < argc; ++n)
{
const char *a = argv[argind];
if ('-' == a[0])
{
if (0 == strcmp(a, "-p"))
std = 1;
else
if (0 == strcmp(a, "-o"))
{
if (++argind == argc) goto usage;
strcpy(outfile, argv[argind]);
const char *a = argv[n];
if ('-' == a[0]) {
if (0 == strcmp(a, "-o")) {
if (++n == argc)
goto usage;
strcpy(outfile, argv[n]);
}
else
goto usage;
}
else
if (0 == infile[0])
} else if (0 == infile[0])
strcpy(infile, a);
else
goto usage;
@ -258,13 +281,9 @@ int main(int argc, char **argv)
{
usage:
fprintf(stderr,
"tiny_impdef creates a .def file from a dll\n"
"Usage: tiny_impdef [-p] <library.dll> [-o outputfile]\n"
"Options:\n"
" -p print to stdout\n"
"tiny_impdef creates an export definition file (.def) from a dll\n"
"Usage: tiny_impdef library.dll [-o outputfile]\n"
);
error:
ret = 1;
goto the_end;
}
@ -274,13 +293,10 @@ error:
if (0 == outfile[0])
{
char *p;
p = strrchr(strcpy(outfile, infile), '\\');
if (NULL == p)
p = strrchr(outfile, '/');
if (p) strcpy(outfile, p+1);
strcpy(outfile, file_basename(infile));
p = strrchr(outfile, '.');
if (NULL == p) p = strchr(outfile, 0);
if (NULL == p)
p = strchr(outfile, 0);
strcpy(p, ".def");
}
@ -296,11 +312,10 @@ error:
if (hFile == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "file not found: %s\n", infile);
goto error;
fprintf(stderr, "No such file: %s\n", infile);
goto the_end;
}
if (!std) printf("--> %s\n", infile);
hMapObject = CreateFileMapping(
hFile,
@ -312,8 +327,8 @@ error:
if (NULL == hMapObject)
{
fprintf(stderr, "could not create file mapping.\n");
goto error;
fprintf(stderr, "Could not create file mapping: %s\n", infile);
goto the_end;
}
pMem = MapViewOfFile(
@ -325,48 +340,54 @@ error:
if (NULL == pMem)
{
fprintf(stderr, "could not map view of file.\n");
goto error;
fprintf(stderr, "Could not map view of file: %s\n", infile);
goto the_end;
}
if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2))
{
fprintf(stderr, "Not a PE file: %s\n", infile);
goto the_end;
}
hHeap = GetProcessHeap();
nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
{
FILE *op; char *p; int n;
if (!std) printf("<-- %s\n", outfile);
if (std)
op = stdout;
else
op = fopen(outfile, "wt");
if (NULL == op)
{
fprintf(stderr, "could not create file: %s\n", outfile);
goto error;
if (0 == nCnt) {
fprintf(stderr, "Could not get exported function names: %s\n", infile);
goto the_end;
}
p = strrchr(infile, '\\');
if (NULL == p)
p = strrchr(infile, '/');
if (NULL == p) p = infile; else ++p;
printf("--> %s\n", infile);
fprintf(op, "LIBRARY %s\n\nEXPORTS", p);
if (std) fprintf(op, " (%d)", nCnt);
fprintf(op, "\n");
op = fopen(outfile, "w");
if (NULL == op)
{
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)
{
fprintf(op, "%s\n", p);
while (*p++);
}
if (!std) fclose(op);
}
ret = 0;
the_end:
if (pMem) UnmapViewOfFile(pMem);
if (hMapObject) CloseHandle(hMapObject);
if (hFile) CloseHandle(hFile);
if (pMem)
UnmapViewOfFile(pMem);
if (hMapObject)
CloseHandle(hMapObject);
if (hFile)
CloseHandle(hFile);
return ret;
}
/* -------------------------------------------------------------- */
/* -------------------------------------------------------------- */