Release 20040914.
[wine/gsoc-2012-control.git] / tools / winebuild / res32.c
blobca90d7124b28014211cc28ba15ae6ea0a40b021c
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 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 */
91 /* size of a resource directory with n entries */
92 #define RESDIR_SIZE(n) ((4 + 2 * (n)) * sizeof(int))
95 inline static struct resource *add_resource( DLLSPEC *spec )
97 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
98 return &spec->resources[spec->nb_resources++];
101 static inline unsigned int strlenW( const WCHAR *str )
103 const WCHAR *s = str;
104 while (*s) s++;
105 return s - str;
108 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
110 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
111 return *str1 - *str2;
114 static struct res_name *add_name( struct res_type *type, const struct resource *res )
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;
126 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
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;
138 /* get the next word from the current resource file */
139 static WORD get_word(void)
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;
147 /* get the next dword from the current resource file */
148 static DWORD get_dword(void)
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;
156 /* get a string from the current resource file */
157 static void get_string( struct string_id *str )
159 if (*(const WCHAR *)file_pos == 0xffff)
161 get_word(); /* skip the 0xffff */
162 str->str = NULL;
163 str->id = get_word();
165 else
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()));
174 /* check the file header */
175 /* all values must be zero except header size */
176 static int check_header(void)
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;
190 /* load the next resource from the current file */
191 static void load_next_resource( DLLSPEC *spec )
193 DWORD hdr_size;
194 struct resource *res = add_resource( spec );
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 );
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 */
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 );
214 /* load a Win32 .res file */
215 int load_res32_file( const char *name, DLLSPEC *spec )
217 int fd, ret;
218 void *base;
219 struct stat st;
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 */
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 );
233 file_name = name;
234 file_pos = base;
235 file_end = file_pos + st.st_size;
236 if ((ret = check_header()))
238 while (file_pos < file_end) load_next_resource( spec );
240 close( fd );
241 return ret;
244 /* compare two unicode strings/ids */
245 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
247 if (!str1->str)
249 if (!str2->str) return str1->id - str2->id;
250 return 1; /* an id compares larger than a string */
252 if (!str2->str) return -1;
253 return strcmpW( str1->str, str2->str );
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 )
260 const struct resource *res1 = ptr1;
261 const struct resource *res2 = ptr2;
262 int ret;
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;
269 /* build the 3-level (type,name,language) resource tree */
270 static struct res_tree *build_resource_tree( DLLSPEC *spec )
272 int i;
273 struct res_tree *tree;
274 struct res_type *type = NULL;
275 struct res_name *name = NULL;
277 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
279 tree = xmalloc( sizeof(*tree) );
280 tree->types = NULL;
281 tree->nb_types = 0;
283 for (i = 0; i < spec->nb_resources; i++)
285 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
287 type = add_type( tree, &spec->resources[i] );
288 name = add_name( type, &spec->resources[i] );
290 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
292 name = add_name( type, &spec->resources[i] );
294 else name->nb_languages++;
296 return tree;
299 /* free the resource tree */
300 static void free_resource_tree( struct res_tree *tree )
302 int i;
304 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
305 free( tree->types );
306 free( tree );
309 /* output a Unicode string */
310 static void output_string( FILE *outfile, const WCHAR *name )
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, " */" );
320 /* output the resource definitions */
321 void output_resources( FILE *outfile, DLLSPEC *spec )
323 int i, j, k, nb_id_types;
324 unsigned int n, offset, data_offset;
325 struct res_tree *tree;
326 struct res_type *type;
327 struct res_name *name;
328 const struct resource *res;
330 if (!spec->nb_resources) return;
332 tree = build_resource_tree( spec );
334 /* resource data */
336 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
338 const unsigned int *p = res->data;
339 int size = res->data_size / 4;
340 /* dump data as ints to ensure correct alignment */
341 fprintf( outfile, "static const unsigned int res_%d[%d] = {\n ", i, size );
342 for (j = 0; j < size - 1; j++, p++)
344 fprintf( outfile, "0x%08x,", *p );
345 if ((j % 8) == 7) fprintf( outfile, "\n " );
347 fprintf( outfile, "0x%08x\n};\n\n", *p );
350 /* directory structures */
352 fprintf( outfile, "struct res_dir {\n" );
353 fprintf( outfile, " unsigned int Characteristics;\n" );
354 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
355 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
356 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
357 fprintf( outfile, "struct res_dir_entry {\n" );
358 fprintf( outfile, " unsigned int Name;\n" );
359 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
360 fprintf( outfile, "struct res_data_entry {\n" );
361 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
362 fprintf( outfile, " unsigned int Size;\n" );
363 fprintf( outfile, " unsigned int CodePage;\n" );
364 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
366 /* resource directory definition */
368 fprintf( outfile, "static struct res_struct{\n" );
369 fprintf( outfile, " struct res_dir type_dir;\n" );
370 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", tree->nb_types );
371 offset = RESDIR_SIZE( tree->nb_types );
373 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
375 offset += RESDIR_SIZE( type->nb_names );
376 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
377 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
378 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
380 offset += RESDIR_SIZE( name->nb_languages );
381 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
382 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
383 i, n, name->nb_languages );
387 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", spec->nb_resources );
388 offset += spec->nb_resources * 4 * sizeof(int);
390 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
392 if (type->type->str)
394 type->name_offset = offset | 0x80000000;
395 offset += (strlenW(type->type->str)+1) * sizeof(unsigned short);
396 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
397 i, strlenW(type->type->str)+1 );
399 else
401 type->name_offset = type->type->id;
402 nb_id_types++;
405 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
407 if (name->name->str)
409 name->name_offset = offset | 0x80000000;
410 offset += (strlenW(name->name->str)+1) * sizeof(unsigned short);
411 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
412 i, n, strlenW(name->name->str)+1 );
414 else name->name_offset = name->name->id;
418 /* resource directory contents */
420 fprintf( outfile, "} resources = {\n" );
421 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", tree->nb_types - nb_id_types, nb_id_types );
423 /* dump the type directory */
424 offset = RESDIR_SIZE( tree->nb_types );
425 fprintf( outfile, " {\n" );
426 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
428 fprintf( outfile, " { 0x%08x, 0x%08x },\n", type->name_offset, offset | 0x80000000 );
429 offset += RESDIR_SIZE( type->nb_names );
430 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
431 offset += RESDIR_SIZE( name->nb_languages );
433 fprintf( outfile, " },\n" );
435 data_offset = offset;
436 offset = RESDIR_SIZE( tree->nb_types );
438 /* dump the names and languages directories */
439 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
441 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
442 type->nb_names - type->nb_id_names, type->nb_id_names, i );
443 offset += RESDIR_SIZE( type->nb_names );
444 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
446 fprintf( outfile, " { 0x%08x, 0x%08x },\n", name->name_offset, offset | 0x80000000 );
447 offset += RESDIR_SIZE( name->nb_languages );
449 fprintf( outfile, " },\n" );
451 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
453 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
454 name->nb_languages, i, n );
455 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
457 fprintf( outfile, " { 0x%04x, 0x%08x },\n",
458 res->lang, data_offset + (res - spec->resources) * 4 * sizeof(int) );
460 fprintf( outfile, " },\n" );
464 /* dump the resource data entries */
465 fprintf( outfile, " {\n" );
466 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
468 fprintf( outfile, " { res_%d, sizeof(res_%d), 0, 0 }, /* %08x */\n", i, i,
469 data_offset + i * 4 * sizeof(int) );
472 /* dump the name strings */
473 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
475 if (type->type->str)
477 fprintf( outfile, " },\n { " );
478 output_string( outfile, type->type->str );
480 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
482 if (name->name->str)
484 fprintf( outfile, " },\n { " );
485 output_string( outfile, name->name->str );
489 fprintf( outfile, " }\n};\n\n" );
490 free_resource_tree( tree );