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