Check file header to differentiate between object files and resources
[wine] / tools / winebuild / res32.c
1 /*
2  * Builtin dlls resource support
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
30 #endif
31 #include <fcntl.h>
32 #ifdef HAVE_SYS_STAT_H
33 # include <sys/stat.h>
34 #endif
35 #ifdef HAVE_SYS_MMAN_H
36 #include <sys/mman.h>
37 #endif
38
39 #include "winbase.h"
40 #include "wine/unicode.h"
41 #include "build.h"
42
43 /* Unicode string or integer id */
44 struct string_id
45 {
46     WCHAR *str;  /* ptr to Unicode string */
47     WORD   id;   /* integer id if str is NULL */
48 };
49
50 /* descriptor for a resource */
51 struct resource
52 {
53     struct string_id type;
54     struct string_id name;
55     const void      *data;
56     unsigned int     data_size;
57     WORD             lang;
58 };
59
60 /* name level of the resource tree */
61 struct res_name
62 {
63     const struct string_id  *name;         /* name */
64     const struct resource   *res;          /* resource */
65     int                      nb_languages; /* number of languages */
66 };
67
68 /* type level of the resource tree */
69 struct res_type
70 {
71     const struct string_id  *type;         /* type name */
72     struct res_name         *names;        /* names array */
73     unsigned int             nb_names;     /* total number of names */
74     unsigned int             nb_id_names;  /* number of names that have a numeric id */
75 };
76
77 static struct resource *resources;
78 static int nb_resources;
79
80 static struct res_type *res_types;
81 static int nb_types;     /* total number of types */
82 static int nb_id_types;  /* number of types that have a numeric id */
83
84 static const unsigned char *file_pos;   /* current position in resource file */
85 static const unsigned char *file_end;   /* end of resource file */
86 static const char *file_name;  /* current resource file name */
87
88
89 inline static struct resource *add_resource(void)
90 {
91     resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
92     return &resources[nb_resources++];
93 }
94
95 static struct res_name *add_name( struct res_type *type, const struct resource *res )
96 {
97     struct res_name *name;
98     type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
99     name = &type->names[type->nb_names++];
100     name->name         = &res->name;
101     name->res          = res;
102     name->nb_languages = 1;
103     if (!name->name->str) type->nb_id_names++;
104     return name;
105 }
106
107 static struct res_type *add_type( const struct resource *res )
108 {
109     struct res_type *type;
110     res_types = xrealloc( res_types, (nb_types + 1) * sizeof(*res_types) );
111     type = &res_types[nb_types++];
112     type->type        = &res->type;
113     type->names       = NULL;
114     type->nb_names    = 0;
115     type->nb_id_names = 0;
116     if (!type->type->str) nb_id_types++;
117     return type;
118 }
119
120 /* get the next word from the current resource file */
121 static WORD get_word(void)
122 {
123     WORD ret = *(WORD *)file_pos;
124     file_pos += sizeof(WORD);
125     if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
126     return ret;
127 }
128
129 /* get the next dword from the current resource file */
130 static DWORD get_dword(void)
131 {
132     DWORD ret = *(DWORD *)file_pos;
133     file_pos += sizeof(DWORD);
134     if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
135     return ret;
136 }
137
138 /* get a string from the current resource file */
139 static void get_string( struct string_id *str )
140 {
141     if (*(WCHAR *)file_pos == 0xffff)
142     {
143         get_word();  /* skip the 0xffff */
144         str->str = NULL;
145         str->id = get_word();
146     }
147     else
148     {
149         WCHAR *p = xmalloc( (strlenW((WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
150         str->str = p;
151         str->id  = 0;
152         while ((*p++ = get_word()));
153     }
154 }
155
156 /* check the file header */
157 /* all values must be zero except header size */
158 static int check_header(void)
159 {
160     if (get_dword()) return 0;        /* data size */
161     if (get_dword() != 32) return 0;  /* header size */
162     if (get_word() != 0xffff || get_word()) return 0;  /* type, must be id 0 */
163     if (get_word() != 0xffff || get_word()) return 0;  /* name, must be id 0 */
164     if (get_dword()) return 0;        /* data version */
165     if (get_word()) return 0;         /* mem options */
166     if (get_word()) return 0;         /* language */
167     if (get_dword()) return 0;        /* version */
168     if (get_dword()) return 0;        /* characteristics */
169     return 1;
170 }
171
172 /* load the next resource from the current file */
173 static void load_next_resource(void)
174 {
175     DWORD hdr_size;
176     struct resource *res = add_resource();
177
178     res->data_size = (get_dword() + 3) & ~3;
179     hdr_size = get_dword();
180     if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
181
182     res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
183     get_string( &res->type );
184     get_string( &res->name );
185     if ((int)file_pos & 2) get_word();  /* align to dword boundary */
186     get_dword();                        /* skip data version */
187     get_word();                         /* skip mem options */
188     res->lang = get_word();
189     get_dword();                        /* skip version */
190     get_dword();                        /* skip characteristics */
191
192     file_pos = (char *)res->data + res->data_size;
193     if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
194 }
195
196 /* load a Win32 .res file */
197 int load_res32_file( const char *name )
198 {
199     int fd, ret;
200     void *base;
201     struct stat st;
202
203     if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
204     if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
205     if (!st.st_size) fatal_error( "%s is an empty file\n" );
206 #ifdef  HAVE_MMAP
207     if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
208 #endif  /* HAVE_MMAP */
209     {
210         base = xmalloc( st.st_size );
211         if (read( fd, base, st.st_size ) != st.st_size)
212             fatal_error( "Cannot read %s\n", name );
213     }
214
215     file_name = name;
216     file_pos  = base;
217     file_end  = file_pos + st.st_size;
218     if ((ret = check_header()))
219     {
220         while (file_pos < file_end) load_next_resource();
221     }
222     close( fd );
223     return ret;
224 }
225
226 /* compare two unicode strings/ids */
227 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
228 {
229     if (!str1->str)
230     {
231         if (!str2->str) return str1->id - str2->id;
232         return 1;  /* an id compares larger than a string */
233     }
234     if (!str2->str) return -1;
235     return strcmpiW( str1->str, str2->str );
236 }
237
238 /* compare two resources for sorting the resource directory */
239 /* resources are stored first by type, then by name, then by language */
240 static int cmp_res( const void *ptr1, const void *ptr2 )
241 {
242     const struct resource *res1 = ptr1;
243     const struct resource *res2 = ptr2;
244     int ret;
245
246     if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
247     if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
248     return res1->lang - res2->lang;
249 }
250
251 /* build the 3-level (type,name,language) resource tree */
252 static void build_resource_tree(void)
253 {
254     int i;
255     struct res_type *type = NULL;
256     struct res_name *name = NULL;
257
258     qsort( resources, nb_resources, sizeof(*resources), cmp_res );
259
260     for (i = 0; i < nb_resources; i++)
261     {
262         if (!i || cmp_string( &resources[i].type, &resources[i-1].type ))  /* new type */
263         {
264             type = add_type( &resources[i] );
265             name = add_name( type, &resources[i] );
266         }
267         else if (cmp_string( &resources[i].name, &resources[i-1].name )) /* new name */
268         {
269             name = add_name( type, &resources[i] );
270         }
271         else name->nb_languages++;
272     }
273 }
274
275 /* output a Unicode string */
276 static void output_string( FILE *outfile, const WCHAR *name )
277 {
278     int i, len = strlenW(name);
279     fprintf( outfile, "0x%04x", len );
280     for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
281     fprintf( outfile, " /* " );
282     for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
283     fprintf( outfile, " */" );
284 }
285
286 /* output the resource definitions */
287 int output_resources( FILE *outfile )
288 {
289     int i, j, k;
290     unsigned int n;
291     const struct res_type *type;
292     const struct res_name *name;
293     const struct resource *res;
294
295     if (!nb_resources) return 0;
296
297     build_resource_tree();
298
299     /* resource data */
300
301     for (i = 0, res = resources; i < nb_resources; i++, res++)
302     {
303         const unsigned int *p = res->data;
304         int size = res->data_size / 4;
305         /* dump data as ints to ensure correct alignment */
306         fprintf( outfile, "static const unsigned int res_%d[%d] = {\n  ", i, size );
307         for (j = 0; j < size - 1; j++, p++)
308         {
309             fprintf( outfile, "0x%08x,", *p );
310             if ((j % 8) == 7) fprintf( outfile, "\n  " );
311         }
312         fprintf( outfile, "0x%08x\n};\n\n", *p );
313     }
314
315     /* directory structures */
316
317     fprintf( outfile, "struct res_dir {\n" );
318     fprintf( outfile, "  unsigned int Characteristics;\n" );
319     fprintf( outfile, "  unsigned int TimeDateStamp;\n" );
320     fprintf( outfile, "  unsigned short MajorVersion, MinorVersion;\n" );
321     fprintf( outfile, "  unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
322     fprintf( outfile, "struct res_dir_entry {\n" );
323     fprintf( outfile, "  unsigned int Name;\n" );
324     fprintf( outfile, "  unsigned int OffsetToData;\n};\n\n" );
325     fprintf( outfile, "struct res_data_entry {\n" );
326     fprintf( outfile, "  const unsigned int *OffsetToData;\n" );
327     fprintf( outfile, "  unsigned int Size;\n" );
328     fprintf( outfile, "  unsigned int CodePage;\n" );
329     fprintf( outfile, "  unsigned int ResourceHandle;\n};\n\n" );
330
331     /* resource directory definition */
332
333     fprintf( outfile, "#define OFFSETOF(field) ((char*)&((struct res_struct *)0)->field - (char*)((struct res_struct *) 0))\n" );
334     fprintf( outfile, "static struct res_struct{\n" );
335     fprintf( outfile, "  struct res_dir        type_dir;\n" );
336     fprintf( outfile, "  struct res_dir_entry  type_entries[%d];\n", nb_types );
337
338     for (i = 0, type = res_types; i < nb_types; i++, type++)
339     {
340         fprintf( outfile, "  struct res_dir        name_%d_dir;\n", i );
341         fprintf( outfile, "  struct res_dir_entry  name_%d_entries[%d];\n", i, type->nb_names );
342         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
343         {
344             fprintf( outfile, "  struct res_dir        lang_%d_%d_dir;\n", i, n );
345             fprintf( outfile, "  struct res_dir_entry  lang_%d_%d_entries[%d];\n",
346                      i, n, name->nb_languages );
347         }
348     }
349
350     fprintf( outfile, "  struct res_data_entry data_entries[%d];\n", nb_resources );
351
352     for (i = 0, type = res_types; i < nb_types; i++, type++)
353     {
354         if (type->type->str)
355             fprintf( outfile, "  unsigned short        type_%d_name[%d];\n",
356                      i, strlenW(type->type->str)+1 );
357         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
358         {
359             if (name->name->str)
360                 fprintf( outfile, "  unsigned short        name_%d_%d_name[%d];\n",
361                          i, n, strlenW(name->name->str)+1 );
362         }
363     }
364
365     /* resource directory contents */
366
367     fprintf( outfile, "} resources = {\n" );
368     fprintf( outfile, "  { 0, 0, 0, 0, %d, %d },\n", nb_types - nb_id_types, nb_id_types );
369
370     /* dump the type directory */
371     fprintf( outfile, "  {\n" );
372     for (i = 0, type = res_types; i < nb_types; i++, type++)
373     {
374         if (!type->type->str)
375             fprintf( outfile, "    { 0x%04x, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
376                      type->type->id, i );
377         else
378             fprintf( outfile, "    { OFFSETOF(type_%d_name) | 0x80000000, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
379                      i, i );
380     }
381     fprintf( outfile, "  },\n" );
382
383     /* dump the names and languages directories */
384     for (i = 0, type = res_types; i < nb_types; i++, type++)
385     {
386         fprintf( outfile, "  { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n  {\n",
387                  type->nb_names - type->nb_id_names, type->nb_id_names, i );
388         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
389         {
390             if (!name->name->str)
391                 fprintf( outfile, "    { 0x%04x, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
392                          name->name->id, i, n );
393             else
394                 fprintf( outfile, "    { OFFSETOF(name_%d_%d_name) | 0x80000000, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
395                          i, n, i, n );
396         }
397         fprintf( outfile, "  },\n" );
398
399         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
400         {
401             fprintf( outfile, "  { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n  {\n",
402                      name->nb_languages, i, n );
403             for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
404             {
405                 fprintf( outfile, "    { 0x%04x, OFFSETOF(data_entries[%d]) },\n",
406                          res->lang, res - resources );
407             }
408             fprintf( outfile, "  },\n" );
409         }
410     }
411
412     /* dump the resource data entries */
413     fprintf( outfile, "  {\n" );
414     for (i = 0, res = resources; i < nb_resources; i++, res++)
415     {
416         fprintf( outfile, "    { res_%d, sizeof(res_%d), 0, 0 },\n", i, i );
417     }
418
419     /* dump the name strings */
420     for (i = 0, type = res_types; i < nb_types; i++, type++)
421     {
422         if (type->type->str)
423         {
424             fprintf( outfile, "  },\n  { " );
425             output_string( outfile, type->type->str );
426         }
427         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
428         {
429             if (name->name->str)
430             {
431                 fprintf( outfile, "  },\n  { " );
432                 output_string( outfile, name->name->str );
433             }
434         }
435     }
436     fprintf( outfile, "  }\n};\n#undef OFFSETOF\n\n" );
437     return nb_resources;
438 }