Menu

[ecd09e]: / res2coff / res2coff.c  Maximize  Restore  History

Download this file

238 lines (185 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#define __main__
#include "res2coff.h"
/*
res2coff.c
Convert a 32 bit resource file into a COFF file
to integrate the resources into an application
at link time
v1.00 : First official release, seems to be quite
debugged
v1.01 : stdargs for AppExit()
standalone CleanUp()
Compilable with mingw32 and Jacob Navia's WIN32 headers
Most of the credit for getting me doing this thing is
for Matt Pietrek's and his Windows95 Programming Secrets.
I got the book before I even thought of doing this, but
without it, I wouldn't have even dared to start.
* Contributors:
* Created by Pedro A. Aranda <paag@tid.es>
*
* THIS SOFTWARE IS NOT COPYRIGHTED
*
* This source code is offered for use in the public domain. You may
* use, modify or distribute it freely.
*
* This code is distributed in the hope that it will be useful but
* WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY
* DISCLAMED. This includes but is not limited to warrenties of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Madrid, 06-06-96
*/
/*
* Exit the program in an orderly way
* Any open handles are closed,
* All dynamic memory is freed
* Error messages are printed out through stderr
*/
void CleanUp(void)
{
if (Directory.Image)
free(Directory.Image);
if (Strings.Image)
free(Strings.Image);
if (ResourceHeads.Image)
free(ResourceHeads.Image);
if (RelocTable.pElems)
free(RelocTable.pElems);
FreeResourceInfo();
if (RESview)
UnmapViewOfFile(RESview);
if (RESmapping)
CloseHandle(RESmapping);
if ((RESfile) && (RESfile != (HANDLE)INVALID_HANDLE_VALUE))
CloseHandle(RESfile);
if ((OBJFile) && (OBJFile != (HANDLE)INVALID_HANDLE_VALUE))
CloseHandle(OBJFile);
}
void AppExit(int ErrorLevel,char *msg,...)
{
va_list args;
va_start(args,msg);
if ((ErrorLevel) && (msg))
{
fprintf(stderr,"%s :",ProgName);
vfprintf(stderr,msg,args);
}
va_end(args);
CleanUp();
exit(ErrorLevel);
}
void CheckedWrite(HANDLE of,LPVOID buffer,DWORD size,char *sectmsg)
{
DWORD written;
WriteFile(of,buffer,size,&written,NULL);
if (written == size)
return;
else
AppExit(1,"Error writing %s , disk full(?)\n",sectmsg);
}
void usage(char wrong)
{
printf("\nUsage: %s [-v] -i infile -o outfile\n\n"
" -v verbose mode\n"
" -i <file>.res RES32 file (input)\n"
" -o <file>.o COFF object file (output)\n",ProgName);
if (wrong)
printf("\n -%c is not defined as a valid option \n",wrong);
exit(-1);
}
int main(int argc,char **argv)
{
int arg,result;
char *src,*dst;
DWORD cbFileSize;
ProgName = argv[0];
if (argc == 1) {
usage(0);
exit(-1);
}
ResourceTime = (unsigned long)time(NULL);
verbose = 0;
src = dst = NULL;
InitResourceInfo();
for (arg = 1;arg < argc;arg++) {
if (argv[arg][0] == '-')
switch(argv[arg][1]) {
case 'v':
case 'V': verbose = 1;
break;
case 'i':
case 'I': if (argv[arg][2])
src = &argv[arg][2];
else
src = argv[++arg];
break;
case 'o':
case 'O': if (argv[arg][2])
dst = &argv[arg][2];
else
dst = argv[++arg];
break;
default : usage(argv[arg][1]);
break;
} else
usage(0);
}
if (src == NULL) {
usage(0);
printf("No input file\n");
}
if (dst == NULL) {
usage(0);
printf("No output file\n");
}
if (verbose)
printf("%s version %x.%02x\n\n",ProgName,__MAVERSION__,__MIVERSION__);
/*
* Now we enter a very orderly procedure:
* 1.- Open the resource file
* 2.- Create a maping for it
* 3.- Map it into virtual memory
* 4.- Check it is indeed a RES32 file
* 5.- Sort the unnamed resources (v1.2)
* 6.- Create the COFF object file
*
* Should any of these steps fail, the application is
* exited in an orderly way
*/
RESfile = CreateFileA(src, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if ((!RESfile) || (RESfile == (HANDLE)INVALID_HANDLE_VALUE))
AppExit(1,"Couldn't open '%s'\n",src);
RESmapping = CreateFileMappingA(RESfile,NULL,PAGE_READONLY,0,0,NULL);
if (RESmapping == NULL)
AppExit(1,"Couldn't create file mapping for '%s'\n",src);
RESview = MapViewOfFile(RESmapping, FILE_MAP_READ, 0, 0, 0);
if (RESview == NULL)
AppExit(1,"Couldn't map view of '%s'\n",src);
cbFileSize = GetFileSize(RESfile, NULL);
if ((cbFileSize == -1) && (GetLastError() != NO_ERROR))
AppExit(1,"Error getting size of '%s'",src);
RESviewEnd = (void *)((BYTE *)RESview + cbFileSize);
if (!Check32bitResourceFile((PRESOURCEHEAD)RESview))
AppExit(1,"Error in RES32 file format in '%s'\n",src);
SortUnnamedResources();
OBJFile = CreateFileA(dst,GENERIC_WRITE,0,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if ((!OBJFile) || (OBJFile == (HANDLE)INVALID_HANDLE_VALUE))
AppExit(1,"Couldn't create '%s'\n",dst);
/*
* Now we do the real processing. All functions have
* autodocumenting names, just follow the code.
* When all is done, we exit in an ordely way,
* cleaning up any kind off mess we could have
* stored in memory and flagging success (error level = 0)
*/
MakeDirectoryTree();
MakeCOFFSections(OBJFile);
DumpDirectoryImage(OBJFile);
DumpResources(OBJFile);
DumpRelocations(OBJFile);
DumpSymbols(OBJFile);
CleanUp();
return 0;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.