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