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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/************************************************
*
* Converting binary resources from/to *.rc files
*
* Copyright 1999 Juergen Schmied
* Copyright 2003 Dimitrie O. Paun
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/stat.h>
#include <limits.h>
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
static const char *clean_file;
static const char* help =
"Usage: bin2res [OPTIONS] <rsrc.rc>\n"
" -a archive binaries into the <rsrc.rc> file\n"
" -x extract binaries from the <rsrc.rc> file\n"
" -i <filename> archive the named file into the <rsrc.rc> file\n"
" -o <filename> extract the named file from the <rsrc.rc> file\n"
" -f force processing of older resources\n"
" -v causes the command to be verbous during processing\n"
" -h print this help screen and exit\n"
"\n"
"This tool allows the insertion/extractions of embedded binary\n"
"resources to/from .rc files, for storage within the cvs tree.\n"
"This is accomplished by placing a magic marker in a comment\n"
"just above the resource. The marker consists of the BINRES\n"
"string followed by the file name. For example, to insert a\n"
"brand new binary resource in a .rc file, place the marker\n"
"above empty brackets:\n"
" /* BINRES idb_std_small.bmp */\n"
" IDB_STD_SMALL BITMAP idb_std_small.bmp\n"
" /* {\n"
" } */\n"
"To merge the binary resources into the .rc file, run:\n"
" bin2res -a myrsrc.rc\n"
"Only resources that are newer than the .rc are processed.\n"
"To extract the binary resources from the .rc file, run:\n"
" bin2res -x myrsrc.rc\n"
"Binary files newer than the .rc file are not overwritten.\n"
"\n"
"To force processing of all resources, use the -f flag.\n"
"To process a particular file, use the -i/-o options.\n";
static void usage(void)
{
printf(help);
exit(1);
}
static void cleanup_files(void)
{
if (clean_file) unlink( clean_file );
}
static void exit_on_signal( int sig )
{
exit(1); /* this will call the atexit functions */
}
static int insert_hexdump (FILE* outfile, FILE* infile)
{
int i, c;
fprintf (outfile, "{\n '");
for (i = 0; (c = fgetc(infile)) != EOF; i++)
{
if (i && (i % 16) == 0) fprintf (outfile, "'\n '");
if (i % 16) fprintf (outfile, " ");
fprintf(outfile, "%02X", c);
}
fprintf (outfile, "'\n}");
return 1;
}
static int hex2bin(char c)
{
if (!isxdigit(c)) return -1024;
if (isdigit(c)) return c - '0';
return toupper(c) - 'A' + 10;
}
static int extract_hexdump (FILE* outfile, FILE* infile)
{
int byte, c;
while ( (c = fgetc(infile)) != EOF && c != '}')
{
if (isspace(c) || c == '\'') continue;
byte = 16 * hex2bin(c);
c = fgetc(infile);
if (c == EOF) return 0;
byte += hex2bin(c);
if (byte < 0) return 0;
fputc(byte, outfile);
}
return 1;
}
static const char* parse_marker(const char *line, time_t* last_updated)
{
static char res_file_name[PATH_MAX], *rpos, *wpos;
struct stat st;
if (!(rpos = strstr(line, "BINRES"))) return 0;
for (rpos += 6; *rpos && isspace(*rpos); rpos++) /**/;
for (wpos = res_file_name; *rpos && !isspace(*rpos); ) *wpos++ = *rpos++;
*wpos = 0;
*last_updated = (stat(res_file_name, &st) < 0) ? 0 : st.st_mtime;
return res_file_name;
}
static int process_resources(const char* input_file_name, const char* specific_file_name,
int inserting, int force_processing, int verbose)
{
char buffer[2048], tmp_file_name[PATH_MAX];
const char *res_file_name;
time_t rc_last_update, res_last_update;
FILE *fin, *fres, *ftmp = 0;
struct stat st;
int fd, c;
if (!(fin = fopen(input_file_name, "r"))) return 0;
if (stat(input_file_name, &st) < 0) return 0;
rc_last_update = st.st_mtime;
if (inserting)
{
strcpy(tmp_file_name, input_file_name);
strcat(tmp_file_name, "-XXXXXX.temp");
if ((fd = mkstemps(tmp_file_name, 5)) == -1)
{
strcpy(tmp_file_name, "/tmp/bin2res-XXXXXX.temp");
if ((fd = mkstemps(tmp_file_name, 5)) == -1) return 0;
}
clean_file = tmp_file_name;
if (!(ftmp = fdopen(fd, "w"))) return 0;
}
for (c = EOF; fgets(buffer, sizeof(buffer), fin); c = EOF)
{
if (inserting) fprintf(ftmp, "%s", buffer);
if (!(res_file_name = parse_marker(buffer, &res_last_update))) continue;
if ( (specific_file_name && strcmp(specific_file_name, res_file_name)) ||
(!force_processing && ((rc_last_update < res_last_update) == !inserting)) )
{
if (verbose) printf("skipping '%s'\n", res_file_name);
continue;
}
if (verbose) printf("processing '%s'\n", res_file_name);
while ( (c = fgetc(fin)) != EOF && c != '{')
if (inserting) fputc(c, ftmp);
if (c == EOF) break;
if (inserting)
{
if (!(fres = fopen(res_file_name, "rb"))) break;
if (!insert_hexdump(ftmp, fres)) break;
while ( (c = fgetc(fin)) != EOF && c != '}') /**/;
fclose(fres);
}
else
{
clean_file = res_file_name;
if (!(fres = fopen(res_file_name, "wb"))) break;
if (!extract_hexdump(fres, fin)) break;
fclose(fres);
clean_file = NULL;
}
}
fclose(fin);
if (inserting)
{
fclose(ftmp);
if (c == EOF)
{
if (rename(tmp_file_name, input_file_name) < 0)
{
/* try unlinking first, Windows rename is brain-damaged */
if (unlink(input_file_name) < 0 || rename(tmp_file_name, input_file_name) < 0)
return 0;
}
clean_file = NULL;
}
}
return c == EOF;
}
int main(int argc, char **argv)
{
int convert_dir = 0, optc;
int force_overwrite = 0, verbose = 0;
const char* input_file_name = 0;
const char* specific_file_name = 0;
atexit( cleanup_files );
signal( SIGTERM, exit_on_signal );
signal( SIGINT, exit_on_signal );
#ifdef SIGHUP
signal( SIGHUP, exit_on_signal );
#endif
while((optc = getopt(argc, argv, "axi:o:fhv")) != EOF)
{
switch(optc)
{
case 'a':
case 'x':
if (convert_dir) usage();
convert_dir = optc;
break;
case 'i':
case 'o':
if (specific_file_name) usage();
specific_file_name = optarg;
optc = ((optc == 'i') ? 'a' : 'x');
if (convert_dir && convert_dir != optc) usage();
convert_dir = optc;
break;
case 'f':
force_overwrite = 1;
break;
case 'v':
verbose = 1;
break;
case 'h':
printf(help);
exit(0);
break;
default:
usage();
}
}
if (optind + 1 != argc) usage();
input_file_name = argv[optind];
if (!convert_dir) usage();
if (!process_resources(input_file_name, specific_file_name,
convert_dir == 'a', force_overwrite, verbose))
{
perror("Processing failed");
exit(1);
}
return 0;
}