1999-11-13 23:32:21 +01:00
|
|
|
/************************************************
|
|
|
|
*
|
|
|
|
* Converting binary resources from/to *.rc files
|
|
|
|
*
|
|
|
|
* Copyright 1999 Juergen Schmied
|
2003-09-18 22:53:10 +02:00
|
|
|
* Copyright 2003 Dimitrie O. Paun
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1999-11-13 23:32:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2003-09-18 22:53:10 +02:00
|
|
|
#include "wine/port.h"
|
1999-11-13 23:32:21 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-09-18 22:53:10 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2004-01-14 06:12:33 +01:00
|
|
|
#include <limits.h>
|
2003-09-18 22:53:10 +02:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
1999-12-12 00:02:15 +01:00
|
|
|
#endif
|
2000-12-19 05:53:20 +01:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
static const char* help =
|
2003-10-01 05:13:31 +02:00
|
|
|
"Usage: bin2res [OPTIONS] <rsrc.rc>\n"
|
2003-09-18 22:53:10 +02:00
|
|
|
" -a archive binaries into the <rsrc.rc> file\n"
|
|
|
|
" -x extract binaries from the <rsrc.rc> file\n"
|
2003-10-01 05:13:31 +02:00
|
|
|
" -i <filename> archive the named file into the <rsrc.rc> file\n"
|
|
|
|
" -o <filename> extract the named file from the <rsrc.rc> file\n"
|
2003-09-18 22:53:10 +02:00
|
|
|
" -f force processing of older resources\n"
|
2003-10-03 05:34:10 +02:00
|
|
|
" -v causes the command to be verbous during processing\n"
|
2003-09-18 22:53:10 +02:00
|
|
|
" -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"
|
2004-09-06 22:26:35 +02:00
|
|
|
" IDB_STD_SMALL BITMAP idb_std_small.bmp\n"
|
|
|
|
" /* {\n"
|
|
|
|
" } */\n"
|
2003-09-18 22:53:10 +02:00
|
|
|
"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"
|
2003-10-01 05:13:31 +02:00
|
|
|
"To force processing of all resources, use the -f flag.\n"
|
|
|
|
"To process a particular file, use the -i/-o options.\n";
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2005-06-15 20:10:39 +02:00
|
|
|
static void usage(void)
|
2003-09-18 22:53:10 +02:00
|
|
|
{
|
|
|
|
printf(help);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2005-06-15 20:10:39 +02:00
|
|
|
static int insert_hexdump (FILE* outfile, FILE* infile)
|
2003-09-18 22:53:10 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2005-06-15 20:10:39 +02:00
|
|
|
static int hex2bin(char c)
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
if (!isxdigit(c)) return -1024;
|
|
|
|
if (isdigit(c)) return c - '0';
|
|
|
|
return toupper(c) - 'A' + 10;
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|
|
|
|
|
2005-06-15 20:10:39 +02:00
|
|
|
static int extract_hexdump (FILE* outfile, FILE* infile)
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
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;
|
|
|
|
}
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2005-06-15 20:10:39 +02:00
|
|
|
static const char* parse_marker(const char *line, time_t* last_updated)
|
2003-09-18 22:53:10 +02:00
|
|
|
{
|
|
|
|
static char res_file_name[PATH_MAX], *rpos, *wpos;
|
|
|
|
struct stat st;
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
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;
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
*last_updated = (stat(res_file_name, &st) < 0) ? 0 : st.st_mtime;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
return res_file_name;
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|
|
|
|
|
2005-06-15 20:10:39 +02:00
|
|
|
static int process_resources(const char* input_file_name, const char* specific_file_name,
|
2003-10-03 05:34:10 +02:00
|
|
|
int inserting, int force_processing, int verbose)
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
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)
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
strcpy(tmp_file_name, "/tmp/bin2res-XXXXXX.temp");
|
|
|
|
if ((fd = mkstemps(tmp_file_name, 5)) == -1) return 0;
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|
2003-09-18 22:53:10 +02:00
|
|
|
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;
|
2003-10-01 05:13:31 +02:00
|
|
|
if ( (specific_file_name && strcmp(specific_file_name, res_file_name)) ||
|
|
|
|
(!force_processing && ((rc_last_update < res_last_update) == !inserting)) )
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-10-03 05:34:10 +02:00
|
|
|
if (verbose) printf("skipping '%s'\n", res_file_name);
|
2003-09-18 22:53:10 +02:00
|
|
|
continue;
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|
|
|
|
|
2003-10-03 05:34:10 +02:00
|
|
|
if (verbose) printf("processing '%s'\n", res_file_name);
|
2003-09-18 22:53:10 +02:00
|
|
|
while ( (c = fgetc(fin)) != EOF && c != '{')
|
|
|
|
if (inserting) fputc(c, ftmp);
|
|
|
|
if (c == EOF) break;
|
2000-11-26 00:54:12 +01:00
|
|
|
|
2003-10-06 23:05:28 +02:00
|
|
|
if (!(fres = fopen(res_file_name, inserting ? "rb" : "wb"))) break;
|
2003-09-18 22:53:10 +02:00
|
|
|
if (inserting)
|
2000-11-26 00:54:12 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
if (!insert_hexdump(ftmp, fres)) break;
|
|
|
|
while ( (c = fgetc(fin)) != EOF && c != '}') /**/;
|
2000-11-26 00:54:12 +01:00
|
|
|
}
|
2003-09-18 22:53:10 +02:00
|
|
|
else
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
if (!extract_hexdump(fres, fin)) break;
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|
2003-09-18 22:53:10 +02:00
|
|
|
fclose(fres);
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
fclose(fin);
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
if (inserting)
|
|
|
|
{
|
|
|
|
fclose(ftmp);
|
2004-01-19 00:32:56 +01:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
unlink(tmp_file_name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-18 22:53:10 +02:00
|
|
|
else unlink(tmp_file_name);
|
|
|
|
}
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
return c == EOF;
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
int main(int argc, char **argv)
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
int convert_dir = 0, optc;
|
2003-10-03 05:34:10 +02:00
|
|
|
int force_overwrite = 0, verbose = 0;
|
2003-10-01 05:13:31 +02:00
|
|
|
const char* input_file_name = 0;
|
|
|
|
const char* specific_file_name = 0;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2004-03-09 04:44:22 +01:00
|
|
|
while((optc = getopt(argc, argv, "axi:o:fhv")) != EOF)
|
2003-09-18 22:53:10 +02:00
|
|
|
{
|
|
|
|
switch(optc)
|
1999-11-13 23:32:21 +01:00
|
|
|
{
|
2003-09-18 22:53:10 +02:00
|
|
|
case 'a':
|
|
|
|
case 'x':
|
|
|
|
if (convert_dir) usage();
|
|
|
|
convert_dir = optc;
|
|
|
|
break;
|
2003-10-01 05:13:31 +02:00
|
|
|
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;
|
2003-09-18 22:53:10 +02:00
|
|
|
case 'f':
|
|
|
|
force_overwrite = 1;
|
|
|
|
break;
|
2003-10-03 05:34:10 +02:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
2003-09-18 22:53:10 +02:00
|
|
|
case 'h':
|
|
|
|
printf(help);
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|
2003-09-18 22:53:10 +02:00
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
if (optind + 1 != argc) usage();
|
|
|
|
input_file_name = argv[optind];
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
if (!convert_dir) usage();
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2003-10-01 05:13:31 +02:00
|
|
|
if (!process_resources(input_file_name, specific_file_name,
|
2003-10-03 05:34:10 +02:00
|
|
|
convert_dir == 'a', force_overwrite, verbose))
|
2003-09-18 22:53:10 +02:00
|
|
|
{
|
|
|
|
perror("Processing failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-11-13 23:32:21 +01:00
|
|
|
|
2003-09-18 22:53:10 +02:00
|
|
|
return 0;
|
1999-11-13 23:32:21 +01:00
|
|
|
}
|