makedep: Don't add dependencies for tests of disabled dlls.
[wine.git] / tools / winebuild / res32.c
blob676693e178da49c985668ac886283fdc09baf808
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "build.h"
32 typedef unsigned short WCHAR;
34 /* Unicode string or integer id */
35 struct string_id
37 WCHAR *str; /* ptr to Unicode string */
38 unsigned int len; /* len in characters */
39 unsigned short id; /* integer id if str is NULL */
42 /* descriptor for a resource */
43 struct resource
45 struct string_id type;
46 struct string_id name;
47 const char *input_name;
48 unsigned int input_offset;
49 const void *data;
50 unsigned int data_size;
51 unsigned int data_offset;
52 unsigned short mem_options;
53 unsigned short lang;
54 unsigned int version;
57 /* name level of the resource tree */
58 struct res_name
60 const struct string_id *name; /* name */
61 struct resource *res; /* resource */
62 int nb_languages; /* number of languages */
63 unsigned int dir_offset; /* offset of directory in resource dir */
64 unsigned int name_offset; /* offset of name in resource dir */
67 /* type level of the resource tree */
68 struct res_type
70 const struct string_id *type; /* type name */
71 struct res_name *names; /* names array */
72 unsigned int nb_names; /* total number of names */
73 unsigned int nb_id_names; /* number of names that have a numeric id */
74 unsigned int dir_offset; /* offset of directory in resource dir */
75 unsigned int name_offset; /* offset of type name in resource dir */
78 /* top level of the resource tree */
79 struct res_tree
81 struct res_type *types; /* types array */
82 unsigned int nb_types; /* total number of types */
85 /* size of a resource directory with n entries */
86 #define RESOURCE_DIR_SIZE (4 * sizeof(unsigned int))
87 #define RESOURCE_DIR_ENTRY_SIZE (2 * sizeof(unsigned int))
88 #define RESOURCE_DATA_ENTRY_SIZE (4 * sizeof(unsigned int))
89 #define RESDIR_SIZE(n) (RESOURCE_DIR_SIZE + (n) * RESOURCE_DIR_ENTRY_SIZE)
92 static inline struct resource *add_resource( DLLSPEC *spec )
94 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
95 return &spec->resources[spec->nb_resources++];
98 static struct res_name *add_name( struct res_type *type, struct resource *res )
100 struct res_name *name;
101 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
102 name = &type->names[type->nb_names++];
103 name->name = &res->name;
104 name->res = res;
105 name->nb_languages = 1;
106 if (!name->name->str) type->nb_id_names++;
107 return name;
110 static struct res_type *add_type( struct res_tree *tree, struct resource *res )
112 struct res_type *type;
113 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
114 type = &tree->types[tree->nb_types++];
115 type->type = &res->type;
116 type->names = NULL;
117 type->nb_names = 0;
118 type->nb_id_names = 0;
119 return type;
122 /* get a string from the current resource file */
123 static void get_string( struct string_id *str )
125 unsigned int i = 0;
126 size_t start_pos = input_buffer_pos;
127 WCHAR wc = get_word();
129 str->len = 0;
130 if (wc == 0xffff)
132 str->str = NULL;
133 str->id = get_word();
135 else
137 input_buffer_pos = start_pos;
138 while (get_word()) str->len++;
139 str->str = xmalloc( str->len * sizeof(WCHAR) );
140 input_buffer_pos = start_pos;
141 while ((wc = get_word())) str->str[i++] = wc;
145 /* put a string into the resource file */
146 static void put_string( const struct string_id *str )
148 if (str->str)
150 unsigned int i;
151 for (i = 0; i < str->len; i++) put_word( str->str[i] );
152 put_word( 0 );
154 else
156 put_word( 0xffff );
157 put_word( str->id );
161 /* check the file header */
162 /* all values must be zero except header size */
163 static int check_header(void)
165 unsigned int size;
167 if (get_dword()) return 0; /* data size */
168 size = get_dword(); /* header size */
169 if (size == 0x20000000) byte_swapped = 1;
170 else if (size != 0x20) return 0;
171 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
172 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
173 if (get_dword()) return 0; /* data version */
174 if (get_word()) return 0; /* mem options */
175 if (get_word()) return 0; /* language */
176 if (get_dword()) return 0; /* version */
177 if (get_dword()) return 0; /* characteristics */
178 return 1;
181 /* load the next resource from the current file */
182 static void load_next_resource( DLLSPEC *spec, const char *name )
184 unsigned int hdr_size;
185 struct resource *res = add_resource( spec );
187 res->data_size = get_dword();
188 hdr_size = get_dword();
189 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", input_buffer_filename );
190 if (hdr_size < 32) fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
192 res->input_name = xstrdup( name );
193 res->input_offset = input_buffer_pos - 2*sizeof(unsigned int) + hdr_size;
195 res->data = input_buffer + input_buffer_pos - 2*sizeof(unsigned int) + hdr_size;
196 if ((const unsigned char *)res->data < input_buffer ||
197 (const unsigned char *)res->data >= input_buffer + input_buffer_size)
198 fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
199 get_string( &res->type );
200 get_string( &res->name );
201 if (input_buffer_pos & 2) get_word(); /* align to dword boundary */
202 get_dword(); /* skip data version */
203 res->mem_options = get_word();
204 res->lang = get_word();
205 res->version = get_dword();
206 get_dword(); /* skip characteristics */
208 input_buffer_pos = ((const unsigned char *)res->data - input_buffer) + ((res->data_size + 3) & ~3);
209 input_buffer_pos = (input_buffer_pos + 3) & ~3;
210 if (input_buffer_pos > input_buffer_size)
211 fatal_error( "%s is a truncated file\n", input_buffer_filename );
214 /* load a Win32 .res file */
215 int load_res32_file( const char *name, DLLSPEC *spec )
217 int ret;
219 init_input_buffer( name );
221 if ((ret = check_header()))
223 while (input_buffer_pos < input_buffer_size) load_next_resource( spec, name );
225 return ret;
228 /* compare two unicode strings/ids */
229 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
231 unsigned int i;
233 if (!str1->str)
235 if (!str2->str) return str1->id - str2->id;
236 return 1; /* an id compares larger than a string */
238 if (!str2->str) return -1;
240 for (i = 0; i < str1->len && i < str2->len; i++)
241 if (str1->str[i] != str2->str[i]) return str1->str[i] - str2->str[i];
242 return str1->len - str2->len;
245 /* compare two resources for sorting the resource directory */
246 /* resources are stored first by type, then by name, then by language */
247 static int cmp_res( const void *ptr1, const void *ptr2 )
249 const struct resource *res1 = ptr1;
250 const struct resource *res2 = ptr2;
251 int ret;
253 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
254 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
255 if ((ret = res1->lang - res2->lang)) return ret;
256 return res1->version - res2->version;
259 static char *format_res_string( const struct string_id *str )
261 unsigned int i;
262 char *ret;
264 if (!str->str) return strmake( "#%04x", str->id );
265 ret = xmalloc( str->len + 1 );
266 for (i = 0; i < str->len; i++) ret[i] = str->str[i]; /* dumb W->A conversion */
267 ret[i] = 0;
268 return ret;
271 /* get rid of duplicate resources with different versions */
272 static void remove_duplicate_resources( DLLSPEC *spec )
274 unsigned int i, n;
276 for (i = n = 0; i < spec->nb_resources; i++, n++)
278 if (i && !cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ) &&
279 !cmp_string( &spec->resources[i].name, &spec->resources[i-1].name ) &&
280 spec->resources[i].lang == spec->resources[i-1].lang)
282 if (spec->resources[i].version == spec->resources[i-1].version)
284 char *type_str = format_res_string( &spec->resources[i].type );
285 char *name_str = format_res_string( &spec->resources[i].name );
286 error( "winebuild: duplicate resource type %s name %s language %04x version %08x\n",
287 type_str, name_str, spec->resources[i].lang, spec->resources[i].version );
289 else n--; /* replace the previous one */
291 if (n < i) spec->resources[n] = spec->resources[i];
293 spec->nb_resources = n;
296 /* build the 3-level (type,name,language) resource tree */
297 static struct res_tree *build_resource_tree( DLLSPEC *spec, unsigned int *dir_size )
299 unsigned int i, k, n, offset, data_offset;
300 struct res_tree *tree;
301 struct res_type *type = NULL;
302 struct res_name *name = NULL;
303 struct resource *res;
305 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
306 remove_duplicate_resources( spec );
308 tree = xmalloc( sizeof(*tree) );
309 tree->types = NULL;
310 tree->nb_types = 0;
312 for (i = 0; i < spec->nb_resources; i++)
314 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
316 type = add_type( tree, &spec->resources[i] );
317 name = add_name( type, &spec->resources[i] );
319 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
321 name = add_name( type, &spec->resources[i] );
323 else
325 name->nb_languages++;
329 /* compute the offsets */
331 offset = RESDIR_SIZE( tree->nb_types );
332 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
334 type->dir_offset = offset;
335 offset += RESDIR_SIZE( type->nb_names );
336 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
338 name->dir_offset = offset;
339 offset += RESDIR_SIZE( name->nb_languages );
342 data_offset = offset;
343 offset += spec->nb_resources * RESOURCE_DATA_ENTRY_SIZE;
345 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
347 if (type->type->str)
349 type->name_offset = offset | 0x80000000;
350 offset += (type->type->len + 1) * sizeof(WCHAR);
352 else type->name_offset = type->type->id;
354 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
356 if (name->name->str)
358 name->name_offset = offset | 0x80000000;
359 offset += (name->name->len + 1) * sizeof(WCHAR);
361 else name->name_offset = name->name->id;
362 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
364 unsigned int entry_offset = (res - spec->resources) * RESOURCE_DATA_ENTRY_SIZE;
365 res->data_offset = data_offset + entry_offset;
369 if (dir_size) *dir_size = (offset + 3) & ~3;
370 return tree;
373 /* free the resource tree */
374 static void free_resource_tree( struct res_tree *tree )
376 unsigned int i;
378 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
379 free( tree->types );
380 free( tree );
383 /* output a Unicode string */
384 static void output_string( const struct string_id *str )
386 unsigned int i;
387 output( "\t.short 0x%04x", str->len );
388 for (i = 0; i < str->len; i++) output( ",0x%04x", str->str[i] );
389 output( " /* " );
390 for (i = 0; i < str->len; i++) output( "%c", isprint((char)str->str[i]) ? (char)str->str[i] : '?' );
391 output( " */\n" );
394 /* output a resource directory */
395 static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
397 output( "\t.long 0\n" ); /* Characteristics */
398 output( "\t.long 0\n" ); /* TimeDateStamp */
399 output( "\t.short 0,0\n" ); /* Major/MinorVersion */
400 output( "\t.short %u,%u\n", /* NumberOfNamed/IdEntries */
401 nb_names, nb_ids );
404 /* output the resource definitions */
405 void output_resources( DLLSPEC *spec )
407 int k, nb_id_types;
408 unsigned int i, n;
409 struct res_tree *tree;
410 struct res_type *type;
411 struct res_name *name;
412 const struct resource *res;
414 if (!spec->nb_resources) return;
416 tree = build_resource_tree( spec, NULL );
418 /* output the resource directories */
420 output( "\n/* resources */\n\n" );
421 output( "\t%s\n", get_asm_rsrc_section() );
422 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
423 output( ".L__wine_spec_resources:\n" );
425 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
426 if (!type->type->str) nb_id_types++;
428 output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
430 /* dump the type directory */
432 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
433 output( "\t.long 0x%08x,0x%08x\n",
434 type->name_offset, type->dir_offset | 0x80000000 );
436 /* dump the names and languages directories */
438 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
440 output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
441 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
442 output( "\t.long 0x%08x,0x%08x\n",
443 name->name_offset, name->dir_offset | 0x80000000 );
445 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
447 output_res_dir( 0, name->nb_languages );
448 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
449 output( "\t.long 0x%08x,0x%08x\n", res->lang, res->data_offset );
453 /* dump the resource data entries */
455 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
457 output_rva( ".L__wine_spec_res_%d", i );
458 output( "\t.long %u,0,0\n", res->data_size );
461 /* dump the name strings */
463 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
465 if (type->type->str) output_string( type->type );
466 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
467 if (name->name->str) output_string( name->name );
470 /* resource data */
472 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
474 output( "\n\t.align %d\n", get_alignment(4) );
475 output( ".L__wine_spec_res_%d:\n", i );
476 output( "\t.incbin \"%s\",%d,%d\n", res->input_name, res->input_offset, res->data_size );
479 if (!is_pe())
481 output( ".L__wine_spec_resources_end:\n" );
482 output( "\t.byte 0\n" );
484 free_resource_tree( tree );
487 /* output a Unicode string in binary format */
488 static void output_bin_string( const struct string_id *str )
490 unsigned int i;
492 put_word( str->len );
493 for (i = 0; i < str->len; i++) put_word( str->str[i] );
496 /* output a resource directory in binary format */
497 static inline void output_bin_res_dir( unsigned int nb_names, unsigned int nb_ids )
499 put_dword( 0 ); /* Characteristics */
500 put_dword( 0 ); /* TimeDateStamp */
501 put_word( 0 ); /* MajorVersion */
502 put_word( 0 ); /* MinorVersion */
503 put_word( nb_names ); /* NumberOfNamedEntries */
504 put_word( nb_ids ); /* NumberOfIdEntries */
507 /* output the resource definitions in binary format */
508 void output_bin_resources( DLLSPEC *spec, unsigned int start_rva )
510 int k, nb_id_types;
511 unsigned int i, n, data_offset;
512 struct res_tree *tree;
513 struct res_type *type;
514 struct res_name *name;
515 const struct resource *res;
517 if (!spec->nb_resources) return;
519 tree = build_resource_tree( spec, &data_offset );
520 init_output_buffer();
522 /* output the resource directories */
524 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
525 if (!type->type->str) nb_id_types++;
527 output_bin_res_dir( tree->nb_types - nb_id_types, nb_id_types );
529 /* dump the type directory */
531 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
533 put_dword( type->name_offset );
534 put_dword( type->dir_offset | 0x80000000 );
537 /* dump the names and languages directories */
539 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
541 output_bin_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
542 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
544 put_dword( name->name_offset );
545 put_dword( name->dir_offset | 0x80000000 );
548 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
550 output_bin_res_dir( 0, name->nb_languages );
551 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
553 put_dword( res->lang );
554 put_dword( res->data_offset );
559 /* dump the resource data entries */
561 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
563 put_dword( data_offset + start_rva );
564 put_dword( res->data_size );
565 put_dword( 0 );
566 put_dword( 0 );
567 data_offset += (res->data_size + 3) & ~3;
570 /* dump the name strings */
572 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
574 if (type->type->str) output_bin_string( type->type );
575 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
576 if (name->name->str) output_bin_string( name->name );
579 /* resource data */
581 align_output( 4 );
582 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
584 put_data( res->data, res->data_size );
585 align_output( 4 );
588 free_resource_tree( tree );
591 static unsigned int get_resource_header_size( const struct resource *res )
593 unsigned int size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
595 if (!res->type.str) size += 2 * sizeof(unsigned short);
596 else size += (res->type.len + 1) * sizeof(WCHAR);
598 if (!res->name.str) size += 2 * sizeof(unsigned short);
599 else size += (res->name.len + 1) * sizeof(WCHAR);
601 return size;
604 /* output the resources into a .o file */
605 void output_res_o_file( DLLSPEC *spec )
607 unsigned int i;
608 char *res_file = NULL;
609 struct strarray args;
611 if (!spec->nb_resources) fatal_error( "--resources mode needs at least one resource file as input\n" );
612 if (!output_file_name) fatal_error( "No output file name specified\n" );
614 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
615 remove_duplicate_resources( spec );
617 byte_swapped = 0;
618 init_output_buffer();
620 put_dword( 0 ); /* ResSize */
621 put_dword( 32 ); /* HeaderSize */
622 put_word( 0xffff ); /* ResType */
623 put_word( 0x0000 );
624 put_word( 0xffff ); /* ResName */
625 put_word( 0x0000 );
626 put_dword( 0 ); /* DataVersion */
627 put_word( 0 ); /* Memory options */
628 put_word( 0 ); /* Language */
629 put_dword( 0 ); /* Version */
630 put_dword( 0 ); /* Characteristics */
632 for (i = 0; i < spec->nb_resources; i++)
634 unsigned int header_size = get_resource_header_size( &spec->resources[i] );
636 put_dword( spec->resources[i].data_size );
637 put_dword( (header_size + 3) & ~3 );
638 put_string( &spec->resources[i].type );
639 put_string( &spec->resources[i].name );
640 align_output( 4 );
641 put_dword( 0 );
642 put_word( spec->resources[i].mem_options );
643 put_word( spec->resources[i].lang );
644 put_dword( spec->resources[i].version );
645 put_dword( 0 );
646 put_data( spec->resources[i].data, spec->resources[i].data_size );
647 align_output( 4 );
650 /* if the output file name is a .res too, don't run the results through windres */
651 if (strendswith( output_file_name, ".res"))
653 flush_output_buffer( output_file_name );
654 return;
657 res_file = make_temp_file( output_file_name, ".res" );
658 flush_output_buffer( res_file );
660 args = find_tool( "windres", NULL );
661 strarray_add( &args, "-i" );
662 strarray_add( &args, res_file );
663 strarray_add( &args, "-o" );
664 strarray_add( &args, output_file_name );
665 switch (target.cpu)
667 case CPU_i386:
668 strarray_add( &args, "-F" );
669 strarray_add( &args, "pe-i386" );
670 break;
671 case CPU_x86_64:
672 strarray_add( &args, "-F" );
673 strarray_add( &args, "pe-x86-64" );
674 break;
675 default:
676 break;
678 spawn( args );