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