We should set the global $all_modules variable otherwise there's no
[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 <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #include <fcntl.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
39
40 #include "windef.h"
41 #include "winbase.h"
42 #include "build.h"
43
44 /* Unicode string or integer id */
45 struct string_id
46 {
47     WCHAR *str;  /* ptr to Unicode string */
48     WORD   id;   /* integer id if str is NULL */
49 };
50
51 /* descriptor for a resource */
52 struct resource
53 {
54     struct string_id type;
55     struct string_id name;
56     const void      *data;
57     unsigned int     data_size;
58     WORD             lang;
59 };
60
61 /* name level of the resource tree */
62 struct res_name
63 {
64     const struct string_id  *name;         /* name */
65     const struct resource   *res;          /* resource */
66     int                      nb_languages; /* number of languages */
67     unsigned int             name_offset;  /* offset of name in resource dir */
68 };
69
70 /* type level of the resource tree */
71 struct res_type
72 {
73     const struct string_id  *type;         /* type name */
74     struct res_name         *names;        /* names array */
75     unsigned int             nb_names;     /* total number of names */
76     unsigned int             nb_id_names;  /* number of names that have a numeric id */
77     unsigned int             name_offset;  /* offset of type name in resource dir */
78 };
79
80 /* top level of the resource tree */
81 struct res_tree
82 {
83     struct res_type *types;                /* types array */
84     unsigned int     nb_types;             /* total number of types */
85 };
86
87 static const unsigned char *file_pos;   /* current position in resource file */
88 static const unsigned char *file_end;   /* end of resource file */
89 static const char *file_name;  /* current resource file name */
90
91 /* size of a resource directory with n entries */
92 #define RESDIR_SIZE(n)  ((4 + 2 * (n)) * sizeof(int))
93
94
95 inline static struct resource *add_resource( DLLSPEC *spec )
96 {
97     spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
98     return &spec->resources[spec->nb_resources++];
99 }
100
101 static inline unsigned int strlenW( const WCHAR *str )
102 {
103     const WCHAR *s = str;
104     while (*s) s++;
105     return s - str;
106 }
107
108 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
109 {
110     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
111     return *str1 - *str2;
112 }
113
114 static struct res_name *add_name( struct res_type *type, const struct resource *res )
115 {
116     struct res_name *name;
117     type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
118     name = &type->names[type->nb_names++];
119     name->name         = &res->name;
120     name->res          = res;
121     name->nb_languages = 1;
122     if (!name->name->str) type->nb_id_names++;
123     return name;
124 }
125
126 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
127 {
128     struct res_type *type;
129     tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
130     type = &tree->types[tree->nb_types++];
131     type->type        = &res->type;
132     type->names       = NULL;
133     type->nb_names    = 0;
134     type->nb_id_names = 0;
135     return type;
136 }
137
138 /* get the next word from the current resource file */
139 static WORD get_word(void)
140 {
141     WORD ret = *(const WORD *)file_pos;
142     file_pos += sizeof(WORD);
143     if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
144     return ret;
145 }
146
147 /* get the next dword from the current resource file */
148 static DWORD get_dword(void)
149 {
150     DWORD ret = *(const DWORD *)file_pos;
151     file_pos += sizeof(DWORD);
152     if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
153     return ret;
154 }
155
156 /* get a string from the current resource file */
157 static void get_string( struct string_id *str )
158 {
159     if (*(const WCHAR *)file_pos == 0xffff)
160     {
161         get_word();  /* skip the 0xffff */
162         str->str = NULL;
163         str->id = get_word();
164     }
165     else
166     {
167         WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
168         str->str = p;
169         str->id  = 0;
170         while ((*p++ = get_word()));
171     }
172 }
173
174 /* check the file header */
175 /* all values must be zero except header size */
176 static int check_header(void)
177 {
178     if (get_dword()) return 0;        /* data size */
179     if (get_dword() != 32) return 0;  /* header size */
180     if (get_word() != 0xffff || get_word()) return 0;  /* type, must be id 0 */
181     if (get_word() != 0xffff || get_word()) return 0;  /* name, must be id 0 */
182     if (get_dword()) return 0;        /* data version */
183     if (get_word()) return 0;         /* mem options */
184     if (get_word()) return 0;         /* language */
185     if (get_dword()) return 0;        /* version */
186     if (get_dword()) return 0;        /* characteristics */
187     return 1;
188 }
189
190 /* load the next resource from the current file */
191 static void load_next_resource( DLLSPEC *spec )
192 {
193     DWORD hdr_size;
194     struct resource *res = add_resource( spec );
195
196     res->data_size = (get_dword() + 3) & ~3;
197     hdr_size = get_dword();
198     if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
199
200     res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
201     get_string( &res->type );
202     get_string( &res->name );
203     if ((int)file_pos & 2) get_word();  /* align to dword boundary */
204     get_dword();                        /* skip data version */
205     get_word();                         /* skip mem options */
206     res->lang = get_word();
207     get_dword();                        /* skip version */
208     get_dword();                        /* skip characteristics */
209
210     file_pos = (const char *)res->data + res->data_size;
211     if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
212 }
213
214 /* load a Win32 .res file */
215 int load_res32_file( const char *name, DLLSPEC *spec )
216 {
217     int fd, ret;
218     void *base;
219     struct stat st;
220
221     if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
222     if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
223     if (!st.st_size) fatal_error( "%s is an empty file\n", name );
224 #ifdef  HAVE_MMAP
225     if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
226 #endif  /* HAVE_MMAP */
227     {
228         base = xmalloc( st.st_size );
229         if (read( fd, base, st.st_size ) != st.st_size)
230             fatal_error( "Cannot read %s\n", name );
231     }
232
233     file_name = name;
234     file_pos  = base;
235     file_end  = file_pos + st.st_size;
236     if ((ret = check_header()))
237     {
238         while (file_pos < file_end) load_next_resource( spec );
239     }
240     close( fd );
241     return ret;
242 }
243
244 /* compare two unicode strings/ids */
245 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
246 {
247     if (!str1->str)
248     {
249         if (!str2->str) return str1->id - str2->id;
250         return 1;  /* an id compares larger than a string */
251     }
252     if (!str2->str) return -1;
253     return strcmpW( str1->str, str2->str );
254 }
255
256 /* compare two resources for sorting the resource directory */
257 /* resources are stored first by type, then by name, then by language */
258 static int cmp_res( const void *ptr1, const void *ptr2 )
259 {
260     const struct resource *res1 = ptr1;
261     const struct resource *res2 = ptr2;
262     int ret;
263
264     if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
265     if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
266     return res1->lang - res2->lang;
267 }
268
269 /* build the 3-level (type,name,language) resource tree */
270 static struct res_tree *build_resource_tree( DLLSPEC *spec )
271 {
272     unsigned int i;
273     struct res_tree *tree;
274     struct res_type *type = NULL;
275     struct res_name *name = NULL;
276
277     qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
278
279     tree = xmalloc( sizeof(*tree) );
280     tree->types = NULL;
281     tree->nb_types = 0;
282
283     for (i = 0; i < spec->nb_resources; i++)
284     {
285         if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
286         {
287             type = add_type( tree, &spec->resources[i] );
288             name = add_name( type, &spec->resources[i] );
289         }
290         else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
291         {
292             name = add_name( type, &spec->resources[i] );
293         }
294         else name->nb_languages++;
295     }
296     return tree;
297 }
298
299 /* free the resource tree */
300 static void free_resource_tree( struct res_tree *tree )
301 {
302     unsigned int i;
303
304     for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
305     free( tree->types );
306     free( tree );
307 }
308
309 /* output a Unicode string */
310 static void output_string( FILE *outfile, const WCHAR *name )
311 {
312     int i, len = strlenW(name);
313     fprintf( outfile, "0x%04x", len );
314     for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
315     fprintf( outfile, " /* " );
316     for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
317     fprintf( outfile, " */" );
318 }
319
320 /* output the resource definitions */
321 void output_resources( FILE *outfile, DLLSPEC *spec )
322 {
323     int j, k, nb_id_types;
324     unsigned int i, n, offset, data_offset;
325     struct res_tree *tree;
326     struct res_type *type;
327     struct res_name *name;
328     const struct resource *res;
329
330     if (!spec->nb_resources) return;
331
332     tree = build_resource_tree( spec );
333
334     /* directory structures */
335
336     fprintf( outfile, "struct res_dir {\n" );
337     fprintf( outfile, "  unsigned int Characteristics;\n" );
338     fprintf( outfile, "  unsigned int TimeDateStamp;\n" );
339     fprintf( outfile, "  unsigned short MajorVersion, MinorVersion;\n" );
340     fprintf( outfile, "  unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
341     fprintf( outfile, "struct res_dir_entry {\n" );
342     fprintf( outfile, "  unsigned int Name;\n" );
343     fprintf( outfile, "  unsigned int OffsetToData;\n};\n\n" );
344     fprintf( outfile, "struct res_data_entry {\n" );
345     fprintf( outfile, "  const unsigned int *OffsetToData;\n" );
346     fprintf( outfile, "  unsigned int Size;\n" );
347     fprintf( outfile, "  unsigned int CodePage;\n" );
348     fprintf( outfile, "  unsigned int ResourceHandle;\n};\n\n" );
349
350     /* resource directory definition */
351
352     fprintf( outfile, "static struct res_struct{\n" );
353     fprintf( outfile, "  struct res_dir        type_dir;\n" );
354     fprintf( outfile, "  struct res_dir_entry  type_entries[%d];\n", tree->nb_types );
355     offset = RESDIR_SIZE( tree->nb_types );
356
357     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
358     {
359         offset += RESDIR_SIZE( type->nb_names );
360         fprintf( outfile, "  struct res_dir        name_%d_dir;\n", i );
361         fprintf( outfile, "  struct res_dir_entry  name_%d_entries[%d];\n", i, type->nb_names );
362         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
363         {
364             offset += RESDIR_SIZE( name->nb_languages );
365             fprintf( outfile, "  struct res_dir        lang_%d_%d_dir;\n", i, n );
366             fprintf( outfile, "  struct res_dir_entry  lang_%d_%d_entries[%d];\n",
367                      i, n, name->nb_languages );
368         }
369     }
370
371     fprintf( outfile, "  struct res_data_entry data_entries[%d];\n", spec->nb_resources );
372     offset += spec->nb_resources * 4 * sizeof(int);
373
374     for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
375     {
376         if (type->type->str)
377         {
378             type->name_offset = offset | 0x80000000;
379             offset += (strlenW(type->type->str)+1) * sizeof(unsigned short);
380             fprintf( outfile, "  unsigned short        type_%d_name[%d];\n",
381                      i, strlenW(type->type->str)+1 );
382         }
383         else
384         {
385             type->name_offset = type->type->id;
386             nb_id_types++;
387         }
388
389         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
390         {
391             if (name->name->str)
392             {
393                 name->name_offset = offset | 0x80000000;
394                 offset += (strlenW(name->name->str)+1) * sizeof(unsigned short);
395                 fprintf( outfile, "  unsigned short        name_%d_%d_name[%d];\n",
396                          i, n, strlenW(name->name->str)+1 );
397             }
398             else name->name_offset = name->name->id;
399         }
400     }
401     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
402         fprintf( outfile, "  unsigned int          res_%d[%d];\n", i, res->data_size / 4 );
403
404     /* resource directory contents */
405
406     fprintf( outfile, "} __wine_spec_resources = {\n" );
407     fprintf( outfile, "  { 0, 0, 0, 0, %d, %d },\n", tree->nb_types - nb_id_types, nb_id_types );
408
409     /* dump the type directory */
410     offset = RESDIR_SIZE( tree->nb_types );
411     fprintf( outfile, "  {\n" );
412     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
413     {
414         fprintf( outfile, "    { 0x%08x, 0x%08x },\n", type->name_offset, offset | 0x80000000 );
415         offset += RESDIR_SIZE( type->nb_names );
416         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
417             offset += RESDIR_SIZE( name->nb_languages );
418     }
419     fprintf( outfile, "  },\n" );
420
421     data_offset = offset;
422     offset = RESDIR_SIZE( tree->nb_types );
423
424     /* dump the names and languages directories */
425     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
426     {
427         fprintf( outfile, "  { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n  {\n",
428                  type->nb_names - type->nb_id_names, type->nb_id_names, i );
429         offset += RESDIR_SIZE( type->nb_names );
430         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
431         {
432             fprintf( outfile, "    { 0x%08x, 0x%08x },\n", name->name_offset, offset | 0x80000000 );
433             offset += RESDIR_SIZE( name->nb_languages );
434         }
435         fprintf( outfile, "  },\n" );
436
437         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
438         {
439             fprintf( outfile, "  { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n  {\n",
440                      name->nb_languages, i, n );
441             for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
442             {
443                 fprintf( outfile, "    { 0x%04x, 0x%08x },\n",
444                          res->lang, data_offset + (res - spec->resources) * 4 * sizeof(int) );
445             }
446             fprintf( outfile, "  },\n" );
447         }
448     }
449
450     /* dump the resource data entries */
451     fprintf( outfile, "  {\n" );
452     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
453     {
454         fprintf( outfile, "    { __wine_spec_resources.res_%d, sizeof(__wine_spec_resources.res_%d), 0, 0 }, /* %08x */\n", i, i,
455                  data_offset + i * 4 * sizeof(int) );
456     }
457
458     /* dump the name strings */
459     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
460     {
461         if (type->type->str)
462         {
463             fprintf( outfile, "  },\n  { " );
464             output_string( outfile, type->type->str );
465         }
466         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
467         {
468             if (name->name->str)
469             {
470                 fprintf( outfile, "  },\n  { " );
471                 output_string( outfile, name->name->str );
472             }
473         }
474     }
475     fprintf( outfile, "  },\n" );
476
477     /* resource data */
478
479     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
480     {
481         const unsigned int *p = res->data;
482         int size = res->data_size / 4;
483         /* dump data as ints to ensure correct alignment */
484         fprintf( outfile, "  {  /* res_%d */\n    ", i );
485         for (j = 0; j < size - 1; j++, p++)
486         {
487             fprintf( outfile, "0x%08x,", *p );
488             if ((j % 8) == 7) fprintf( outfile, "\n    " );
489         }
490         fprintf( outfile, "0x%08x\n  },\n", *p );
491     }
492     fprintf( outfile, "};\n\n" );
493
494     free_resource_tree( tree );
495 }