History

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

grischka committed 18 years ago

Commit b0d40c11 parent f99d3de1 file · +393 −372Browse files

Unified · Split

tiny_impdef.c+393−372
@@ -1,372 +1,393 @@
1/* -------------------------------------------------------------- */1/* -------------------------------------------------------------- */
2/*2/*
3 "tiny_impdef creates a .def file from a dll"3 * tiny_impdef creates an export definition file (.def) from a dll
4 4 * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]"
5 "Usage: tiny_impdef [-p] <library.dll> [-o outputfile]"5 *
6 "Options:"6 * Copyright (c) 2005,2007 grischka
7 " -p print to stdout"7 *
8*/8 * This program is free software; you can redistribute it and/or modify
9 9 * it under the terms of the GNU General Public License as published by
10#include <windows.h>10 * the Free Software Foundation; either version 2 of the License, or
11#include <stdio.h>11 * (at your option) any later version.
12 12 *
13/* Offset to PE file signature */13 * This program is distributed in the hope that it will be useful,
14#define NTSIGNATURE(a) ((LPVOID)((BYTE *)a + \14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ((PIMAGE_DOS_HEADER)a)->e_lfanew))15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 16 * GNU General Public License for more details.
17/* MS-OS header identifies the NT PEFile signature dword;17 *
18 the PEFILE header exists just after that dword. */18 * You should have received a copy of the GNU General Public License
19#define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \19 * along with this program; if not, write to the Free Software
20 ((PIMAGE_DOS_HEADER)a)->e_lfanew + \20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 SIZE_OF_NT_SIGNATURE))21 */
22 22
23/* PE optional header is immediately after PEFile header. */23#define WIN32_LEAN_AND_MEAN
24#define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \24#include <windows.h>
25 ((PIMAGE_DOS_HEADER)a)->e_lfanew + \25#include <stdio.h>
26 SIZE_OF_NT_SIGNATURE + \26
27 sizeof (IMAGE_FILE_HEADER)))27/* Offset to PE file signature */
28 28#define NTSIGNATURE(a) ((LPVOID)((BYTE *)a + \
29/* Section headers are immediately after PE optional header. */29 ((PIMAGE_DOS_HEADER)a)->e_lfanew))
30#define SECHDROFFSET(a) ((LPVOID)((BYTE *)a + \30
31 ((PIMAGE_DOS_HEADER)a)->e_lfanew + \31/* MS-OS header identifies the NT PEFile signature dword;
32 SIZE_OF_NT_SIGNATURE + \32 the PEFILE header exists just after that dword. */
33 sizeof (IMAGE_FILE_HEADER) + \33#define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \
34 sizeof (IMAGE_OPTIONAL_HEADER)))34 ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
35 35 SIZE_OF_NT_SIGNATURE))
36 36
37#define SIZE_OF_NT_SIGNATURE 437/* PE optional header is immediately after PEFile header. */
38 38#define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \
39/* -------------------------------------------------------------- */39 ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
40 40 SIZE_OF_NT_SIGNATURE + \
41int WINAPI NumOfSections (41 sizeof (IMAGE_FILE_HEADER)))
42 LPVOID lpFile)42
43{43/* Section headers are immediately after PE optional header. */
44 /* Number of sections is indicated in file header. */44#define SECHDROFFSET(a) ((LPVOID)((BYTE *)a + \
45 return (int)45 ((PIMAGE_DOS_HEADER)a)->e_lfanew + \
46 ((PIMAGE_FILE_HEADER)46 SIZE_OF_NT_SIGNATURE + \
47 PEFHDROFFSET(lpFile))->NumberOfSections;47 sizeof (IMAGE_FILE_HEADER) + \
48}48 sizeof (IMAGE_OPTIONAL_HEADER)))
49 49
50 50
51/* -------------------------------------------------------------- */51#define SIZE_OF_NT_SIGNATURE 4
52 52
53LPVOID WINAPI ImageDirectoryOffset (53/* -------------------------------------------------------------- */
54 LPVOID lpFile,54
55 DWORD dwIMAGE_DIRECTORY)55int WINAPI NumOfSections (
56{56 LPVOID lpFile)
57 PIMAGE_OPTIONAL_HEADER poh;57{
58 PIMAGE_SECTION_HEADER psh;58 /* Number of sections is indicated in file header. */
59 int nSections = NumOfSections (lpFile);59 return (int)
60 int i = 0;60 ((PIMAGE_FILE_HEADER)
61 LPVOID VAImageDir;61 PEFHDROFFSET(lpFile))->NumberOfSections;
62 62}
63 /* Retrieve offsets to optional and section headers. */63
64 poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);64
65 psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile);65/* -------------------------------------------------------------- */
66 66
67 /* Must be 0 thru (NumberOfRvaAndSizes-1). */67LPVOID WINAPI ImageDirectoryOffset (
68 if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)68 LPVOID lpFile,
69 return NULL;69 DWORD dwIMAGE_DIRECTORY)
70 70{
71 /* Locate image directory's relative virtual address. */71 PIMAGE_OPTIONAL_HEADER poh;
72 VAImageDir = (LPVOID)poh->DataDirectory72 PIMAGE_SECTION_HEADER psh;
73 [dwIMAGE_DIRECTORY].VirtualAddress;73 int nSections = NumOfSections (lpFile);
74 74 int i = 0;
75 /* Locate section containing image directory. */75 LPVOID VAImageDir;
76 while (i++<nSections)76
77 {77 /* Retrieve offsets to optional and section headers. */
78 if (psh->VirtualAddress <= (DWORD)VAImageDir78 poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
79 && psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir)79 psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile);
80 break;80
81 psh++;81 /* Must be 0 thru (NumberOfRvaAndSizes-1). */
82 }82 if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
83 83 return NULL;
84 if (i > nSections)84
85 return NULL;85 /* Locate image directory's relative virtual address. */
86 86 VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress;
87 /* Return image import directory offset. */87
88 return (LPVOID)(((int)lpFile +88 /* Locate section containing image directory. */
89 (int)VAImageDir - psh->VirtualAddress) +89 while (i++<nSections)
90 (int)psh->PointerToRawData);90 {
91}91 if (psh->VirtualAddress <= (DWORD)VAImageDir
92 92 && psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir)
93/* -------------------------------------------------------------- */93 break;
94 94 psh++;
95BOOL WINAPI GetSectionHdrByName (95 }
96 LPVOID lpFile,96
97 IMAGE_SECTION_HEADER *sh,97 if (i > nSections)
98 char *szSection)98 return NULL;
99{99
100 PIMAGE_SECTION_HEADER psh;100 /* Return image import directory offset. */
101 int nSections = NumOfSections (lpFile);101 return (LPVOID)(((int)lpFile +
102 int i;102 (int)VAImageDir - psh->VirtualAddress) +
103 103 (int)psh->PointerToRawData);
104 if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) !=104}
105 NULL)105
106 {106/* -------------------------------------------------------------- */
107 /* find the section by name */107
108 for (i=0; i<nSections; i++)108BOOL WINAPI GetSectionHdrByName (
109 {109 LPVOID lpFile,
110 if (!strcmp (psh->Name, szSection))110 IMAGE_SECTION_HEADER *sh,
111 {111 char *szSection)
112 /* copy data to header */112{
113 memcpy ((LPVOID)sh,113 PIMAGE_SECTION_HEADER psh;
114 (LPVOID)psh,114 int nSections = NumOfSections (lpFile);
115 sizeof (IMAGE_SECTION_HEADER));115 int i;
116 return TRUE;116
117 }117 if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
118 else118 {
119 psh++;119 /* find the section by name */
120 }120 for (i=0; i<nSections; i++)
121 }121 {
122 122 if (!strcmp (psh->Name, szSection))
123 return FALSE;123 {
124}124 /* copy data to header */
125 125 memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
126/* -------------------------------------------------------------- */126 return TRUE;
127 127 }
128BOOL WINAPI GetSectionHdrByAddress (128 else
129 LPVOID lpFile,129 psh++;
130 IMAGE_SECTION_HEADER *sh,130 }
131 DWORD addr)131 }
132{132 return FALSE;
133 PIMAGE_SECTION_HEADER psh;133}
134 int nSections = NumOfSections (lpFile);134
135 int i;135/* -------------------------------------------------------------- */
136 136
137 if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) !=137BOOL WINAPI GetSectionHdrByAddress (
138 NULL)138 LPVOID lpFile,
139 {139 IMAGE_SECTION_HEADER *sh,
140 /* find the section by name */140 DWORD addr)
141 for (i=0; i<nSections; i++)141{
142 {142 PIMAGE_SECTION_HEADER psh;
143 if (addr >= psh->VirtualAddress && addr < psh->VirtualAddress + psh->SizeOfRawData)143 int nSections = NumOfSections (lpFile);
144 {144 int i;
145 /* copy data to header */145
146 memcpy ((LPVOID)sh,146 if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
147 (LPVOID)psh,147 {
148 sizeof (IMAGE_SECTION_HEADER));148 /* find the section by name */
149 return TRUE;149 for (i=0; i<nSections; i++)
150 }150 {
151 else151 if (addr >= psh->VirtualAddress
152 psh++;152 && addr < psh->VirtualAddress + psh->SizeOfRawData)
153 }153 {
154 }154 /* copy data to header */
155 155 memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
156 return FALSE;156 return TRUE;
157}157 }
158 158 else
159/* -------------------------------------------------------------- */159 psh++;
160 160 }
161int WINAPI GetExportFunctionNames (161 }
162 LPVOID lpFile,162 return FALSE;
163 HANDLE hHeap,163}
164 char **pszFunctions)164
165{165/* -------------------------------------------------------------- */
166 IMAGE_SECTION_HEADER sh;166
167 PIMAGE_EXPORT_DIRECTORY ped;167int WINAPI GetExportFunctionNames (
168 int *pNames, *pCnt;168 LPVOID lpFile,
169 char *pSrc, *pDest;169 HANDLE hHeap,
170 int i, nCnt;170 char **pszFunctions)
171 DWORD VAImageDir;171{
172 PIMAGE_OPTIONAL_HEADER poh;172 IMAGE_SECTION_HEADER sh;
173 char *pOffset;173 PIMAGE_EXPORT_DIRECTORY ped;
174 174 int *pNames, *pCnt;
175 /* Get section header and pointer to data directory175 char *pSrc, *pDest;
176 for .edata section. */176 int i, nCnt;
177 if ((ped = (PIMAGE_EXPORT_DIRECTORY)ImageDirectoryOffset177 DWORD VAImageDir;
178 (lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)) == NULL)178 PIMAGE_OPTIONAL_HEADER poh;
179 return 0;179 char *pOffset;
180 180
181 poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);181 /* Get section header and pointer to data directory
182 VAImageDir = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;182 for .edata section. */
183 183 if (NULL == (ped = (PIMAGE_EXPORT_DIRECTORY)
184 if (FALSE == GetSectionHdrByAddress (lpFile, &sh, VAImageDir)) return 0;184 ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)))
185 185 return 0;
186 pOffset = (char *)lpFile + (sh.PointerToRawData - sh.VirtualAddress);186
187 187 poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
188 pNames = (int *)(pOffset + (DWORD)ped->AddressOfNames);188 VAImageDir = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
189 189
190 /* Figure out how much memory to allocate for all strings. */190 if (FALSE == GetSectionHdrByAddress (lpFile, &sh, VAImageDir))
191 nCnt = 1;191 return 0;
192 for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)192
193 {193 pOffset = (char *)lpFile + (sh.PointerToRawData - sh.VirtualAddress);
194 pSrc = (pOffset + *pCnt++);194
195 if (pSrc) nCnt += strlen(pSrc)+1;195 pNames = (int *)(pOffset + (DWORD)ped->AddressOfNames);
196 }196
197 197 /* Figure out how much memory to allocate for all strings. */
198 /* Allocate memory off heap for function names. */198 nCnt = 1;
199 pDest = *pszFunctions = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, nCnt);199 for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
200 200 {
201 /* Copy all strings to buffer. */201 pSrc = (pOffset + *pCnt++);
202 for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)202 if (pSrc)
203 {203 nCnt += strlen(pSrc)+1;
204 pSrc = (pOffset + *pCnt++);204 }
205 if (pSrc) { strcpy(pDest, pSrc); pDest += strlen(pSrc)+1; }205
206 }206 /* Allocate memory off heap for function names. */
207 *pDest = 0;207 pDest = *pszFunctions = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, nCnt);
208 208
209 return ped->NumberOfNames;209 /* Copy all strings to buffer. */
210}210 for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
211 211 {
212/* -------------------------------------------------------------- */212 pSrc = (pOffset + *pCnt++);
213 213 if (pSrc) {
214int main(int argc, char **argv)214 strcpy(pDest, pSrc);
215{215 pDest += strlen(pSrc)+1;
216 216 }
217 HANDLE hHeap; HANDLE hFile; HANDLE hMapObject; VOID *pMem;217 }
218 int nCnt, ret, argind, std;218 *pDest = 0;
219 char *pNames;219
220 char infile[MAX_PATH];220 return ped->NumberOfNames;
221 char buffer[MAX_PATH];221}
222 char outfile[MAX_PATH];222
223 char libname[80];223/* -------------------------------------------------------------- */
224 224/* extract the basename of a file */
225 hHeap = NULL;225
226 hFile = NULL;226static char *file_basename(const char *name)
227 hMapObject = NULL;227{
228 pMem = NULL;228 const char *p = strchr(name, 0);
229 infile[0] = 0;229 while (p > name
230 outfile[0] = 0;230 && p[-1] != '/'
231 ret = 0;231 && p[-1] != '\\'
232 std = 0;232 )
233 233 --p;
234 for (argind = 1; argind < argc; ++argind)234 return (char*)p;
235 {235}
236 const char *a = argv[argind];236
237 if ('-' == a[0])237/* -------------------------------------------------------------- */
238 {238
239 if (0 == strcmp(a, "-p"))239int main(int argc, char **argv)
240 std = 1;240{
241 else241 HANDLE hHeap;
242 if (0 == strcmp(a, "-o"))242 HANDLE hFile;
243 {243 HANDLE hMapObject;
244 if (++argind == argc) goto usage;244 VOID *pMem;
245 strcpy(outfile, argv[argind]);245
246 }246 int nCnt, ret, n;
247 else247 char *pNames;
248 goto usage;248 char infile[MAX_PATH];
249 }249 char buffer[MAX_PATH];
250 else250 char outfile[MAX_PATH];
251 if (0 == infile[0])251 FILE *op;
252 strcpy(infile, a);252 char *p;
253 else253
254 goto usage;254 hHeap = NULL;
255 }255 hFile = NULL;
256 256 hMapObject = NULL;
257 if (0 == infile[0])257 pMem = NULL;
258 {258 infile[0] = 0;
259usage:259 outfile[0] = 0;
260 fprintf(stderr,260 ret = 1;
261 "tiny_impdef creates a .def file from a dll\n"261
262 "Usage: tiny_impdef [-p] <library.dll> [-o outputfile]\n"262 for (n = 1; n < argc; ++n)
263 "Options:\n"263 {
264 " -p print to stdout\n"264 const char *a = argv[n];
265 );265 if ('-' == a[0]) {
266error:266 if (0 == strcmp(a, "-o")) {
267 ret = 1;267 if (++n == argc)
268 goto the_end;268 goto usage;
269 }269 strcpy(outfile, argv[n]);
270 270 }
271 if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL))271 else
272 strcpy(infile, buffer);272 goto usage;
273 273
274 if (0 == outfile[0])274 } else if (0 == infile[0])
275 {275 strcpy(infile, a);
276 char *p;276 else
277 p = strrchr(strcpy(outfile, infile), '\\');277 goto usage;
278 if (NULL == p)278 }
279 p = strrchr(outfile, '/');279
280 if (p) strcpy(outfile, p+1);280 if (0 == infile[0])
281 281 {
282 p = strrchr(outfile, '.');282usage:
283 if (NULL == p) p = strchr(outfile, 0);283 fprintf(stderr,
284 strcpy(p, ".def");284 "tiny_impdef creates an export definition file (.def) from a dll\n"
285 }285 "Usage: tiny_impdef library.dll [-o outputfile]\n"
286 286 );
287 hFile=CreateFile(287 goto the_end;
288 infile,288 }
289 GENERIC_READ,289
290 FILE_SHARE_READ,290 if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL))
291 NULL,291 strcpy(infile, buffer);
292 OPEN_EXISTING,292
293 0,293 if (0 == outfile[0])
294 NULL294 {
295 );295 char *p;
296 296 strcpy(outfile, file_basename(infile));
297 if (hFile == INVALID_HANDLE_VALUE)297 p = strrchr(outfile, '.');
298 {298 if (NULL == p)
299 fprintf(stderr, "file not found: %s\n", infile);299 p = strchr(outfile, 0);
300 goto error;300 strcpy(p, ".def");
301 }301 }
302 302
303 if (!std) printf("--> %s\n", infile);303 hFile = CreateFile(
304 304 infile,
305 hMapObject = CreateFileMapping(305 GENERIC_READ,
306 hFile,306 FILE_SHARE_READ,
307 NULL,307 NULL,
308 PAGE_READONLY,308 OPEN_EXISTING,
309 0, 0,309 0,
310 NULL310 NULL
311 );311 );
312 312
313 if (NULL == hMapObject)313 if (hFile == INVALID_HANDLE_VALUE)
314 {314 {
315 fprintf(stderr, "could not create file mapping.\n");315 fprintf(stderr, "No such file: %s\n", infile);
316 goto error;316 goto the_end;
317 }317 }
318 318
319 pMem = MapViewOfFile(319
320 hMapObject, // object to map view of320 hMapObject = CreateFileMapping(
321 FILE_MAP_READ, // read access321 hFile,
322 0, // high offset: map from322 NULL,
323 0, // low offset: beginning323 PAGE_READONLY,
324 0); // default: map entire file324 0, 0,
325 325 NULL
326 if (NULL == pMem)326 );
327 {327
328 fprintf(stderr, "could not map view of file.\n");328 if (NULL == hMapObject)
329 goto error;329 {
330 }330 fprintf(stderr, "Could not create file mapping: %s\n", infile);
331 331 goto the_end;
332 hHeap = GetProcessHeap();332 }
333 nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);333
334 {334 pMem = MapViewOfFile(
335 FILE *op; char *p; int n;335 hMapObject, // object to map view of
336 if (!std) printf("<-- %s\n", outfile);336 FILE_MAP_READ, // read access
337 337 0, // high offset: map from
338 if (std)338 0, // low offset: beginning
339 op = stdout;339 0); // default: map entire file
340 else340
341 op = fopen(outfile, "wt");341 if (NULL == pMem)
342 342 {
343 if (NULL == op)343 fprintf(stderr, "Could not map view of file: %s\n", infile);
344 {344 goto the_end;
345 fprintf(stderr, "could not create file: %s\n", outfile);345 }
346 goto error;346
347 }347 if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2))
348 348 {
349 p = strrchr(infile, '\\');349 fprintf(stderr, "Not a PE file: %s\n", infile);
350 if (NULL == p)350 goto the_end;
351 p = strrchr(infile, '/');351 }
352 if (NULL == p) p = infile; else ++p;352
353 353
354 fprintf(op, "LIBRARY %s\n\nEXPORTS", p);354 hHeap = GetProcessHeap();
355 if (std) fprintf(op, " (%d)", nCnt);355 nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
356 fprintf(op, "\n");356 if (0 == nCnt) {
357 for (n = 0, p = pNames; n < nCnt; ++n)357 fprintf(stderr, "Could not get exported function names: %s\n", infile);
358 {358 goto the_end;
359 fprintf(op, "%s\n", p);359 }
360 while (*p++);360
361 }361 printf("--> %s\n", infile);
362 if (!std) fclose(op);362
363 }363 op = fopen(outfile, "w");
364 364 if (NULL == op)
365the_end:365 {
366 if (pMem) UnmapViewOfFile(pMem);366 fprintf(stderr, "Could not create file: %s\n", outfile);
367 if (hMapObject) CloseHandle(hMapObject);367 goto the_end;
368 if (hFile) CloseHandle(hFile);368 }
369 return ret;369
370}370 printf("<-- %s\n", outfile);
371/* -------------------------------------------------------------- */371
372 372 fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile));
373 for (n = 0, p = pNames; n < nCnt; ++n)
374 {
375 fprintf(op, "%s\n", p);
376 while (*p++);
377 }
378 ret = 0;
379
380the_end:
381 if (pMem)
382 UnmapViewOfFile(pMem);
383
384 if (hMapObject)
385 CloseHandle(hMapObject);
386
387 if (hFile)
388 CloseHandle(hFile);
389
390 return ret;
391}
392
393/* -------------------------------------------------------------- */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t