Properly declare handles.
[wine/multimedia.git] / tools / winebuild / res32.c
blob0466a23287ff095e645b28adf1ced3fff9bde177
1 /*
2 * Builtin dlls resource support
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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
40 #include "windef.h"
41 #include "winbase.h"
42 #include "build.h"
44 /* Unicode string or integer id */
45 struct string_id
47 WCHAR *str; /* ptr to Unicode string */
48 WORD id; /* integer id if str is NULL */
51 /* descriptor for a resource */
52 struct resource
54 struct string_id type;
55 struct string_id name;
56 const void *data;
57 unsigned int data_size;
58 WORD lang;
61 /* name level of the resource tree */
62 struct res_name
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 */
70 /* type level of the resource tree */
71 struct res_type
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 */
80 /* top level of the resource tree */
81 struct res_tree
83 struct res_type *types; /* types array */
84 unsigned int nb_types; /* total number of types */
87 static int byte_swapped; /* whether the current resource file is byte-swapped */
88 static const unsigned char *file_pos; /* current position in resource file */
89 static const unsigned char *file_end; /* end of resource file */
90 static const char *file_name; /* current resource file name */
92 /* size of a resource directory with n entries */
93 #define RESDIR_SIZE(n) ((4 + 2 * (n)) * sizeof(int))
96 inline static struct resource *add_resource( DLLSPEC *spec )
98 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
99 return &spec->resources[spec->nb_resources++];
102 static inline unsigned int strlenW( const WCHAR *str )
104 const WCHAR *s = str;
105 while (*s) s++;
106 return s - str;
109 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
111 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
112 return *str1 - *str2;
115 static struct res_name *add_name( struct res_type *type, const struct resource *res )
117 struct res_name *name;
118 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
119 name = &type->names[type->nb_names++];
120 name->name = &res->name;
121 name->res = res;
122 name->nb_languages = 1;
123 if (!name->name->str) type->nb_id_names++;
124 return name;
127 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
129 struct res_type *type;
130 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
131 type = &tree->types[tree->nb_types++];
132 type->type = &res->type;
133 type->names = NULL;
134 type->nb_names = 0;
135 type->nb_id_names = 0;
136 return type;
139 /* get the next word from the current resource file */
140 static WORD get_word(void)
142 WORD ret = *(const WORD *)file_pos;
143 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
144 file_pos += sizeof(WORD);
145 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
146 return ret;
149 /* get the next dword from the current resource file */
150 static DWORD get_dword(void)
152 DWORD ret = *(const DWORD *)file_pos;
153 if (byte_swapped)
154 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
155 file_pos += sizeof(DWORD);
156 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
157 return ret;
160 /* get a string from the current resource file */
161 static void get_string( struct string_id *str )
163 if (*(const WCHAR *)file_pos == 0xffff)
165 get_word(); /* skip the 0xffff */
166 str->str = NULL;
167 str->id = get_word();
169 else
171 WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
172 str->str = p;
173 str->id = 0;
174 while ((*p++ = get_word()));
178 /* check the file header */
179 /* all values must be zero except header size */
180 static int check_header(void)
182 DWORD size;
184 if (get_dword()) return 0; /* data size */
185 size = get_dword(); /* header size */
186 if (size == 0x20000000) byte_swapped = 1;
187 else if (size != 0x20) return 0;
188 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
189 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
190 if (get_dword()) return 0; /* data version */
191 if (get_word()) return 0; /* mem options */
192 if (get_word()) return 0; /* language */
193 if (get_dword()) return 0; /* version */
194 if (get_dword()) return 0; /* characteristics */
195 return 1;
198 /* load the next resource from the current file */
199 static void load_next_resource( DLLSPEC *spec )
201 DWORD hdr_size;
202 struct resource *res = add_resource( spec );
204 res->data_size = (get_dword() + 3) & ~3;
205 hdr_size = get_dword();
206 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
208 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
209 get_string( &res->type );
210 get_string( &res->name );
211 if ((int)file_pos & 2) get_word(); /* align to dword boundary */
212 get_dword(); /* skip data version */
213 get_word(); /* skip mem options */
214 res->lang = get_word();
215 get_dword(); /* skip version */
216 get_dword(); /* skip characteristics */
218 file_pos = (const unsigned char *)res->data + res->data_size;
219 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
222 /* load a Win32 .res file */
223 int load_res32_file( const char *name, DLLSPEC *spec )
225 int fd, ret;
226 void *base;
227 struct stat st;
229 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
230 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
231 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
232 #ifdef HAVE_MMAP
233 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
234 #endif /* HAVE_MMAP */
236 base = xmalloc( st.st_size );
237 if (read( fd, base, st.st_size ) != st.st_size)
238 fatal_error( "Cannot read %s\n", name );
241 byte_swapped = 0;
242 file_name = name;
243 file_pos = base;
244 file_end = file_pos + st.st_size;
245 if ((ret = check_header()))
247 while (file_pos < file_end) load_next_resource( spec );
249 close( fd );
250 return ret;
253 /* compare two unicode strings/ids */
254 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
256 if (!str1->str)
258 if (!str2->str) return str1->id - str2->id;
259 return 1; /* an id compares larger than a string */
261 if (!str2->str) return -1;
262 return strcmpW( str1->str, str2->str );
265 /* compare two resources for sorting the resource directory */
266 /* resources are stored first by type, then by name, then by language */
267 static int cmp_res( const void *ptr1, const void *ptr2 )
269 const struct resource *res1 = ptr1;
270 const struct resource *res2 = ptr2;
271 int ret;
273 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
274 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
275 return res1->lang - res2->lang;
278 /* build the 3-level (type,name,language) resource tree */
279 static struct res_tree *build_resource_tree( DLLSPEC *spec )
281 unsigned int i;
282 struct res_tree *tree;
283 struct res_type *type = NULL;
284 struct res_name *name = NULL;
286 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
288 tree = xmalloc( sizeof(*tree) );
289 tree->types = NULL;
290 tree->nb_types = 0;
292 for (i = 0; i < spec->nb_resources; i++)
294 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
296 type = add_type( tree, &spec->resources[i] );
297 name = add_name( type, &spec->resources[i] );
299 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
301 name = add_name( type, &spec->resources[i] );
303 else name->nb_languages++;
305 return tree;
308 /* free the resource tree */
309 static void free_resource_tree( struct res_tree *tree )
311 unsigned int i;
313 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
314 free( tree->types );
315 free( tree );
318 /* output a Unicode string */
319 static void output_string( FILE *outfile, const WCHAR *name )
321 int i, len = strlenW(name);
322 fprintf( outfile, "0x%04x", len );
323 for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
324 fprintf( outfile, " /* " );
325 for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
326 fprintf( outfile, " */" );
329 /* output the resource definitions */
330 void output_resources( FILE *outfile, DLLSPEC *spec )
332 int j, k, nb_id_types;
333 unsigned int i, n, offset, data_offset;
334 struct res_tree *tree;
335 struct res_type *type;
336 struct res_name *name;
337 const struct resource *res;
339 if (!spec->nb_resources) return;
341 tree = build_resource_tree( spec );
343 /* directory structures */
345 fprintf( outfile, "struct res_dir {\n" );
346 fprintf( outfile, " unsigned int Characteristics;\n" );
347 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
348 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
349 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
350 fprintf( outfile, "struct res_dir_entry {\n" );
351 fprintf( outfile, " unsigned int Name;\n" );
352 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
353 fprintf( outfile, "struct res_data_entry {\n" );
354 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
355 fprintf( outfile, " unsigned int Size;\n" );
356 fprintf( outfile, " unsigned int CodePage;\n" );
357 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
359 /* resource directory definition */
361 fprintf( outfile, "static struct res_struct{\n" );
362 fprintf( outfile, " struct res_dir type_dir;\n" );
363 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", tree->nb_types );
364 offset = RESDIR_SIZE( tree->nb_types );
366 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
368 offset += RESDIR_SIZE( type->nb_names );
369 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
370 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
371 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
373 offset += RESDIR_SIZE( name->nb_languages );
374 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
375 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
376 i, n, name->nb_languages );
380 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", spec->nb_resources );
381 offset += spec->nb_resources * 4 * sizeof(int);
383 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
385 if (type->type->str)
387 type->name_offset = offset | 0x80000000;
388 offset += (strlenW(type->type->str)+1) * sizeof(unsigned short);
389 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
390 i, strlenW(type->type->str)+1 );
392 else
394 type->name_offset = type->type->id;
395 nb_id_types++;
398 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
400 if (name->name->str)
402 name->name_offset = offset | 0x80000000;
403 offset += (strlenW(name->name->str)+1) * sizeof(unsigned short);
404 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
405 i, n, strlenW(name->name->str)+1 );
407 else name->name_offset = name->name->id;
410 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
411 fprintf( outfile, " unsigned int res_%d[%d];\n", i, res->data_size / 4 );
413 /* resource directory contents */
415 fprintf( outfile, "} __wine_spec_resources = {\n" );
416 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", tree->nb_types - nb_id_types, nb_id_types );
418 /* dump the type directory */
419 offset = RESDIR_SIZE( tree->nb_types );
420 fprintf( outfile, " {\n" );
421 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
423 fprintf( outfile, " { 0x%08x, 0x%08x },\n", type->name_offset, offset | 0x80000000 );
424 offset += RESDIR_SIZE( type->nb_names );
425 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
426 offset += RESDIR_SIZE( name->nb_languages );
428 fprintf( outfile, " },\n" );
430 data_offset = offset;
431 offset = RESDIR_SIZE( tree->nb_types );
433 /* dump the names and languages directories */
434 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
436 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
437 type->nb_names - type->nb_id_names, type->nb_id_names, i );
438 offset += RESDIR_SIZE( type->nb_names );
439 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
441 fprintf( outfile, " { 0x%08x, 0x%08x },\n", name->name_offset, offset | 0x80000000 );
442 offset += RESDIR_SIZE( name->nb_languages );
444 fprintf( outfile, " },\n" );
446 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
448 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
449 name->nb_languages, i, n );
450 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
452 fprintf( outfile, " { 0x%04x, 0x%08x },\n",
453 res->lang, data_offset + (res - spec->resources) * 4 * sizeof(int) );
455 fprintf( outfile, " },\n" );
459 /* dump the resource data entries */
460 fprintf( outfile, " {\n" );
461 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
463 fprintf( outfile, " { __wine_spec_resources.res_%d, sizeof(__wine_spec_resources.res_%d), 0, 0 }, /* %08x */\n", i, i,
464 data_offset + i * 4 * sizeof(int) );
467 /* dump the name strings */
468 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
470 if (type->type->str)
472 fprintf( outfile, " },\n { " );
473 output_string( outfile, type->type->str );
475 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
477 if (name->name->str)
479 fprintf( outfile, " },\n { " );
480 output_string( outfile, name->name->str );
484 fprintf( outfile, " },\n" );
486 /* resource data */
488 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
490 const unsigned int *p = res->data;
491 int size = res->data_size / 4;
492 /* dump data as ints to ensure correct alignment */
493 fprintf( outfile, " { /* res_%d */\n ", i );
494 for (j = 0; j < size - 1; j++, p++)
496 fprintf( outfile, "0x%08x,", *p );
497 if ((j % 8) == 7) fprintf( outfile, "\n " );
499 fprintf( outfile, "0x%08x\n },\n", *p );
501 fprintf( outfile, "};\n\n" );
503 free_resource_tree( tree );