Added empty DllRegisterServer.
[wine] / tools / bin2res.c
1 /************************************************
2  *
3  * Converting binary resources from/to *.rc files
4  *
5  * Copyright 1999 Juergen Schmied
6  * Copyright 2003 Dimitrie O. Paun
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <limits.h>
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 #endif
35
36 static const char* help =
37         "Usage: bin2res [OPTIONS] <rsrc.rc>\n"
38         "  -a archive binaries into the <rsrc.rc> file\n"
39         "  -x extract binaries from the <rsrc.rc> file\n"
40         "  -i <filename> archive the named file into the <rsrc.rc> file\n"
41         "  -o <filename> extract the named file from the <rsrc.rc> file\n"
42         "  -f force processing of older resources\n"
43         "  -v causes the command to be verbous during processing\n" 
44         "  -h print this help screen and exit\n"
45         "\n"
46         "This tool allows the insertion/extractions of embedded binary\n"
47         "resources to/from .rc files, for storage within the cvs tree.\n"
48         "This is accomplished by placing a magic marker in a comment\n"
49         "just above the resource. The marker consists of the BINRES\n"
50         "string followed by the file name. For example, to insert a\n"
51         "brand new binary resource in a .rc file, place the marker\n"
52         "above empty brackets:\n"
53         "    /* BINRES idb_std_small.bmp */\n"
54         "   {}\n"
55         "To merge the binary resources into the .rc file, run:\n"
56         "   bin2res -a myrsrc.rc\n"
57         "Only resources that are newer than the .rc are processed.\n"
58         "To extract the binary resources from the .rc file, run:\n"
59         "  bin2res -x myrsrc.rc\n"
60         "Binary files newer than the .rc file are not overwritten.\n"
61         "\n"
62         "To force processing of all resources, use the -f flag.\n"
63         "To process a particular file, use the -i/-o options.\n";
64
65 void usage(void)
66 {
67     printf(help);
68     exit(1);
69 }
70
71 int insert_hexdump (FILE* outfile, FILE* infile)
72 {
73     int i, c;
74
75     fprintf (outfile, "{\n '");
76     for (i = 0; (c = fgetc(infile)) != EOF; i++)
77     {
78         if (i && (i % 16) == 0) fprintf (outfile, "'\n '");
79         if (i % 16)  fprintf (outfile, " ");
80         fprintf(outfile, "%02X", c);
81     }
82     fprintf (outfile, "'\n}");
83
84     return 1;
85 }
86
87 int hex2bin(char c)
88 {
89     if (!isxdigit(c)) return -1024;
90     if (isdigit(c)) return c - '0';
91     return toupper(c) - 'A' + 10;
92 }
93
94 int extract_hexdump (FILE* outfile, FILE* infile)
95 {
96     int byte, c;
97
98     while ( (c = fgetc(infile)) != EOF && c != '}')
99     {
100         if (isspace(c) || c == '\'') continue;
101         byte = 16 * hex2bin(c);
102         c = fgetc(infile);
103         if (c == EOF) return 0;
104         byte += hex2bin(c);
105         if (byte < 0) return 0;
106         fputc(byte, outfile);
107     }
108     return 1;
109 }
110
111 const char* parse_marker(const char *line, time_t* last_updated)
112 {
113     static char res_file_name[PATH_MAX], *rpos, *wpos;
114     struct stat st;
115
116     if (!(rpos = strstr(line, "BINRES"))) return 0;
117     for (rpos += 6; *rpos && isspace(*rpos); rpos++) /**/;
118     for (wpos = res_file_name; *rpos && !isspace(*rpos); ) *wpos++ = *rpos++;
119     *wpos = 0;
120
121     *last_updated = (stat(res_file_name, &st) < 0) ? 0 : st.st_mtime;
122
123     return res_file_name;
124 }
125
126 int process_resources(const char* input_file_name, const char* specific_file_name, 
127                       int inserting, int force_processing, int verbose)
128 {
129     char buffer[2048], tmp_file_name[PATH_MAX];
130     const char *res_file_name;
131     time_t rc_last_update, res_last_update;
132     FILE *fin, *fres, *ftmp = 0;
133     struct stat st;
134     int fd, c;
135
136     if (!(fin = fopen(input_file_name, "r"))) return 0;
137     if (stat(input_file_name, &st) < 0) return 0;
138     rc_last_update = st.st_mtime;
139
140     if (inserting)
141     {
142         strcpy(tmp_file_name, input_file_name);
143         strcat(tmp_file_name, "-XXXXXX.temp");
144         if ((fd = mkstemps(tmp_file_name, 5)) == -1)
145         {
146             strcpy(tmp_file_name, "/tmp/bin2res-XXXXXX.temp");
147             if ((fd = mkstemps(tmp_file_name, 5)) == -1) return 0;
148         }
149         if (!(ftmp = fdopen(fd, "w"))) return 0;
150     }
151
152     for (c = EOF; fgets(buffer, sizeof(buffer), fin); c = EOF)
153     {
154         if (inserting) fprintf(ftmp, "%s", buffer);
155         if (!(res_file_name = parse_marker(buffer, &res_last_update))) continue;
156         if ( (specific_file_name && strcmp(specific_file_name, res_file_name)) ||
157              (!force_processing && ((rc_last_update < res_last_update) == !inserting)) )
158         {
159             if (verbose) printf("skipping '%s'\n", res_file_name);
160             continue;
161         }
162
163         if (verbose) printf("processing '%s'\n", res_file_name);
164         while ( (c = fgetc(fin)) != EOF && c != '{')
165             if (inserting) fputc(c, ftmp);
166         if (c == EOF) break;
167
168         if (!(fres = fopen(res_file_name, inserting ? "rb" : "wb"))) break;
169         if (inserting)
170         {
171             if (!insert_hexdump(ftmp, fres)) break;
172             while ( (c = fgetc(fin)) != EOF && c != '}') /**/;
173         }
174         else
175         {
176             if (!extract_hexdump(fres, fin)) break;
177         }
178         fclose(fres);
179     }
180
181     fclose(fin);
182
183     if (inserting)
184     {
185         fclose(ftmp);
186         if (c == EOF)
187         {
188             if (rename(tmp_file_name, input_file_name) < 0)
189             {
190                 /* try unlinking first, Windows rename is brain-damaged */
191                 if (unlink(input_file_name) < 0 || rename(tmp_file_name, input_file_name) < 0)
192                 {
193                     unlink(tmp_file_name);
194                     return 0;
195                 }
196             }
197         }
198         else unlink(tmp_file_name);
199     }
200
201     return c == EOF;
202 }
203
204 int main(int argc, char **argv)
205 {
206     int convert_dir = 0, optc;
207     int force_overwrite = 0, verbose = 0;
208     const char* input_file_name = 0;
209     const char* specific_file_name = 0;
210
211     while((optc = getopt(argc, argv, "axi:o:fhv")) != EOF)
212     {
213         switch(optc)
214         {
215         case 'a':
216         case 'x':
217             if (convert_dir) usage();
218             convert_dir = optc;
219         break;
220         case 'i':
221         case 'o':
222             if (specific_file_name) usage();
223             specific_file_name = optarg;
224             optc = ((optc == 'i') ? 'a' : 'x');
225             if (convert_dir && convert_dir != optc) usage();
226             convert_dir = optc;
227         break;
228         case 'f':
229             force_overwrite = 1;
230         break;
231         case 'v':
232             verbose = 1;
233         break;
234         case 'h':
235             printf(help);
236             exit(0);
237         break;
238         default:
239             usage();
240         }
241     }
242
243     if (optind + 1 != argc) usage();
244     input_file_name = argv[optind];
245
246     if (!convert_dir) usage();
247
248     if (!process_resources(input_file_name, specific_file_name, 
249                            convert_dir == 'a', force_overwrite, verbose))
250     {
251         perror("Processing failed");
252         exit(1);
253     }
254
255     return 0;
256 }