gdiplus: Replace GpImage's busy flag with SRWLOCK.
[wine.git] / tools / winebuild / res32.c
blob80890f55458d2c20bdf0ff27a528a72cab0a34e3
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 void *data;
48 unsigned int data_size;
49 unsigned int data_offset;
50 unsigned short mem_options;
51 unsigned short lang;
52 unsigned int version;
55 /* name level of the resource tree */
56 struct res_name
58 const struct string_id *name; /* name */
59 struct resource *res; /* resource */
60 int nb_languages; /* number of languages */
61 unsigned int dir_offset; /* offset of directory in resource dir */
62 unsigned int name_offset; /* offset of name in resource dir */
65 /* type level of the resource tree */
66 struct res_type
68 const struct string_id *type; /* type name */
69 struct res_name *names; /* names array */
70 unsigned int nb_names; /* total number of names */
71 unsigned int nb_id_names; /* number of names that have a numeric id */
72 unsigned int dir_offset; /* offset of directory in resource dir */
73 unsigned int name_offset; /* offset of type name in resource dir */
76 /* top level of the resource tree */
77 struct res_tree
79 struct res_type *types; /* types array */
80 unsigned int nb_types; /* total number of types */
83 /* size of a resource directory with n entries */
84 #define RESOURCE_DIR_SIZE (4 * sizeof(unsigned int))
85 #define RESOURCE_DIR_ENTRY_SIZE (2 * sizeof(unsigned int))
86 #define RESOURCE_DATA_ENTRY_SIZE (4 * sizeof(unsigned int))
87 #define RESDIR_SIZE(n) (RESOURCE_DIR_SIZE + (n) * RESOURCE_DIR_ENTRY_SIZE)
90 static inline struct resource *add_resource( DLLSPEC *spec )
92 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
93 return &spec->resources[spec->nb_resources++];
96 static struct res_name *add_name( struct res_type *type, struct resource *res )
98 struct res_name *name;
99 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
100 name = &type->names[type->nb_names++];
101 name->name = &res->name;
102 name->res = res;
103 name->nb_languages = 1;
104 if (!name->name->str) type->nb_id_names++;
105 return name;
108 static struct res_type *add_type( struct res_tree *tree, struct resource *res )
110 struct res_type *type;
111 tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
112 type = &tree->types[tree->nb_types++];
113 type->type = &res->type;
114 type->names = NULL;
115 type->nb_names = 0;
116 type->nb_id_names = 0;
117 return type;
120 /* get a string from the current resource file */
121 static void get_string( struct string_id *str )
123 unsigned int i = 0;
124 size_t start_pos = input_buffer_pos;
125 WCHAR wc = get_word();
127 str->len = 0;
128 if (wc == 0xffff)
130 str->str = NULL;
131 str->id = get_word();
133 else
135 input_buffer_pos = start_pos;
136 while (get_word()) str->len++;
137 str->str = xmalloc( str->len * sizeof(WCHAR) );
138 input_buffer_pos = start_pos;
139 while ((wc = get_word())) str->str[i++] = wc;
143 /* put a string into the resource file */
144 static void put_string( const struct string_id *str )
146 if (str->str)
148 unsigned int i;
149 for (i = 0; i < str->len; i++) put_word( str->str[i] );
150 put_word( 0 );
152 else
154 put_word( 0xffff );
155 put_word( str->id );
159 static void dump_res_data( const struct resource *res )
161 unsigned int i = 0;
162 unsigned int size = (res->data_size + 3) & ~3;
164 if (!size) return;
166 input_buffer = res->data;
167 input_buffer_pos = 0;
168 input_buffer_size = size;
170 output( "\t.long " );
171 while (size > 4)
173 if ((i++ % 16) == 15) output( "0x%08x\n\t.long ", get_dword() );
174 else output( "0x%08x,", get_dword() );
175 size -= 4;
177 output( "0x%08x\n", get_dword() );
178 assert( input_buffer_pos == input_buffer_size );
181 /* check the file header */
182 /* all values must be zero except header size */
183 static int check_header(void)
185 unsigned int size;
187 if (get_dword()) return 0; /* data size */
188 size = get_dword(); /* header size */
189 if (size == 0x20000000) byte_swapped = 1;
190 else if (size != 0x20) return 0;
191 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
192 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
193 if (get_dword()) return 0; /* data version */
194 if (get_word()) return 0; /* mem options */
195 if (get_word()) return 0; /* language */
196 if (get_dword()) return 0; /* version */
197 if (get_dword()) return 0; /* characteristics */
198 return 1;
201 /* load the next resource from the current file */
202 static void load_next_resource( DLLSPEC *spec )
204 unsigned int hdr_size;
205 struct resource *res = add_resource( spec );
207 res->data_size = get_dword();
208 hdr_size = get_dword();
209 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", input_buffer_filename );
210 if (hdr_size < 32) fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
212 res->data = input_buffer + input_buffer_pos - 2*sizeof(unsigned int) + hdr_size;
213 if ((const unsigned char *)res->data < input_buffer ||
214 (const unsigned char *)res->data >= input_buffer + input_buffer_size)
215 fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
216 get_string( &res->type );
217 get_string( &res->name );
218 if (input_buffer_pos & 2) get_word(); /* align to dword boundary */
219 get_dword(); /* skip data version */
220 res->mem_options = get_word();
221 res->lang = get_word();
222 res->version = get_dword();
223 get_dword(); /* skip characteristics */
225 input_buffer_pos = ((const unsigned char *)res->data - input_buffer) + ((res->data_size + 3) & ~3);
226 input_buffer_pos = (input_buffer_pos + 3) & ~3;
227 if (input_buffer_pos > input_buffer_size)
228 fatal_error( "%s is a truncated file\n", input_buffer_filename );
231 /* load a Win32 .res file */
232 int load_res32_file( const char *name, DLLSPEC *spec )
234 int ret;
236 init_input_buffer( name );
238 if ((ret = check_header()))
240 while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
242 return ret;
245 /* compare two unicode strings/ids */
246 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
248 unsigned int i;
250 if (!str1->str)
252 if (!str2->str) return str1->id - str2->id;
253 return 1; /* an id compares larger than a string */
255 if (!str2->str) return -1;
257 for (i = 0; i < str1->len && i < str2->len; i++)
258 if (str1->str[i] != str2->str[i]) return str1->str[i] - str2->str[i];
259 return str1->len - str2->len;
262 /* compare two resources for sorting the resource directory */
263 /* resources are stored first by type, then by name, then by language */
264 static int cmp_res( const void *ptr1, const void *ptr2 )
266 const struct resource *res1 = ptr1;
267 const struct resource *res2 = ptr2;
268 int ret;
270 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
271 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
272 if ((ret = res1->lang - res2->lang)) return ret;
273 return res1->version - res2->version;
276 static char *format_res_string( const struct string_id *str )
278 unsigned int i;
279 char *ret;
281 if (!str->str) return strmake( "#%04x", str->id );
282 ret = xmalloc( str->len + 1 );
283 for (i = 0; i < str->len; i++) ret[i] = str->str[i]; /* dumb W->A conversion */
284 ret[i] = 0;
285 return ret;
288 /* get rid of duplicate resources with different versions */
289 static void remove_duplicate_resources( DLLSPEC *spec )
291 unsigned int i, n;
293 for (i = n = 0; i < spec->nb_resources; i++, n++)
295 if (i && !cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ) &&
296 !cmp_string( &spec->resources[i].name, &spec->resources[i-1].name ) &&
297 spec->resources[i].lang == spec->resources[i-1].lang)
299 if (spec->resources[i].version == spec->resources[i-1].version)
301 char *type_str = format_res_string( &spec->resources[i].type );
302 char *name_str = format_res_string( &spec->resources[i].name );
303 error( "winebuild: duplicate resource type %s name %s language %04x version %08x\n",
304 type_str, name_str, spec->resources[i].lang, spec->resources[i].version );
306 else n--; /* replace the previous one */
308 if (n < i) spec->resources[n] = spec->resources[i];
310 spec->nb_resources = n;
313 /* build the 3-level (type,name,language) resource tree */
314 static struct res_tree *build_resource_tree( DLLSPEC *spec, unsigned int *dir_size )
316 unsigned int i, k, n, offset, data_offset;
317 struct res_tree *tree;
318 struct res_type *type = NULL;
319 struct res_name *name = NULL;
320 struct resource *res;
322 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
323 remove_duplicate_resources( spec );
325 tree = xmalloc( sizeof(*tree) );
326 tree->types = NULL;
327 tree->nb_types = 0;
329 for (i = 0; i < spec->nb_resources; i++)
331 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
333 type = add_type( tree, &spec->resources[i] );
334 name = add_name( type, &spec->resources[i] );
336 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
338 name = add_name( type, &spec->resources[i] );
340 else
342 name->nb_languages++;
346 /* compute the offsets */
348 offset = RESDIR_SIZE( tree->nb_types );
349 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
351 type->dir_offset = offset;
352 offset += RESDIR_SIZE( type->nb_names );
353 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
355 name->dir_offset = offset;
356 offset += RESDIR_SIZE( name->nb_languages );
359 data_offset = offset;
360 offset += spec->nb_resources * RESOURCE_DATA_ENTRY_SIZE;
362 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
364 if (type->type->str)
366 type->name_offset = offset | 0x80000000;
367 offset += (type->type->len + 1) * sizeof(WCHAR);
369 else type->name_offset = type->type->id;
371 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
373 if (name->name->str)
375 name->name_offset = offset | 0x80000000;
376 offset += (name->name->len + 1) * sizeof(WCHAR);
378 else name->name_offset = name->name->id;
379 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
381 unsigned int entry_offset = (res - spec->resources) * RESOURCE_DATA_ENTRY_SIZE;
382 res->data_offset = data_offset + entry_offset;
386 if (dir_size) *dir_size = (offset + 3) & ~3;
387 return tree;
390 /* free the resource tree */
391 static void free_resource_tree( struct res_tree *tree )
393 unsigned int i;
395 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
396 free( tree->types );
397 free( tree );
400 /* output a Unicode string */
401 static void output_string( const struct string_id *str )
403 unsigned int i;
404 output( "\t.short 0x%04x", str->len );
405 for (i = 0; i < str->len; i++) output( ",0x%04x", str->str[i] );
406 output( " /* " );
407 for (i = 0; i < str->len; i++) output( "%c", isprint((char)str->str[i]) ? (char)str->str[i] : '?' );
408 output( " */\n" );
411 /* output a resource directory */
412 static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
414 output( "\t.long 0\n" ); /* Characteristics */
415 output( "\t.long 0\n" ); /* TimeDateStamp */
416 output( "\t.short 0,0\n" ); /* Major/MinorVersion */
417 output( "\t.short %u,%u\n", /* NumberOfNamed/IdEntries */
418 nb_names, nb_ids );
421 /* output the resource definitions */
422 void output_resources( DLLSPEC *spec )
424 int k, nb_id_types;
425 unsigned int i, n;
426 struct res_tree *tree;
427 struct res_type *type;
428 struct res_name *name;
429 const struct resource *res;
431 if (!spec->nb_resources) return;
433 tree = build_resource_tree( spec, NULL );
435 /* output the resource directories */
437 output( "\n/* resources */\n\n" );
438 output( "\t%s\n", get_asm_rsrc_section() );
439 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
440 output( ".L__wine_spec_resources:\n" );
442 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
443 if (!type->type->str) nb_id_types++;
445 output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
447 /* dump the type directory */
449 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
450 output( "\t.long 0x%08x,0x%08x\n",
451 type->name_offset, type->dir_offset | 0x80000000 );
453 /* dump the names and languages directories */
455 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
457 output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
458 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
459 output( "\t.long 0x%08x,0x%08x\n",
460 name->name_offset, name->dir_offset | 0x80000000 );
462 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
464 output_res_dir( 0, name->nb_languages );
465 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
466 output( "\t.long 0x%08x,0x%08x\n", res->lang, res->data_offset );
470 /* dump the resource data entries */
472 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
474 output_rva( ".L__wine_spec_res_%d", i );
475 output( "\t.long %u,0,0\n", res->data_size );
478 /* dump the name strings */
480 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
482 if (type->type->str) output_string( type->type );
483 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
484 if (name->name->str) output_string( name->name );
487 /* resource data */
489 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
491 output( "\n\t.align %d\n", get_alignment(4) );
492 output( ".L__wine_spec_res_%d:\n", i );
493 dump_res_data( res );
496 if (!is_pe())
498 output( ".L__wine_spec_resources_end:\n" );
499 output( "\t.byte 0\n" );
501 free_resource_tree( tree );
504 /* output a Unicode string in binary format */
505 static void output_bin_string( const struct string_id *str )
507 unsigned int i;
509 put_word( str->len );
510 for (i = 0; i < str->len; i++) put_word( str->str[i] );
513 /* output a resource directory in binary format */
514 static inline void output_bin_res_dir( unsigned int nb_names, unsigned int nb_ids )
516 put_dword( 0 ); /* Characteristics */
517 put_dword( 0 ); /* TimeDateStamp */
518 put_word( 0 ); /* MajorVersion */
519 put_word( 0 ); /* MinorVersion */
520 put_word( nb_names ); /* NumberOfNamedEntries */
521 put_word( nb_ids ); /* NumberOfIdEntries */
524 /* output the resource definitions in binary format */
525 void output_bin_resources( DLLSPEC *spec, unsigned int start_rva )
527 int k, nb_id_types;
528 unsigned int i, n, data_offset;
529 struct res_tree *tree;
530 struct res_type *type;
531 struct res_name *name;
532 const struct resource *res;
534 if (!spec->nb_resources) return;
536 tree = build_resource_tree( spec, &data_offset );
537 init_output_buffer();
539 /* output the resource directories */
541 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
542 if (!type->type->str) nb_id_types++;
544 output_bin_res_dir( tree->nb_types - nb_id_types, nb_id_types );
546 /* dump the type directory */
548 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
550 put_dword( type->name_offset );
551 put_dword( type->dir_offset | 0x80000000 );
554 /* dump the names and languages directories */
556 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
558 output_bin_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
559 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
561 put_dword( name->name_offset );
562 put_dword( name->dir_offset | 0x80000000 );
565 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
567 output_bin_res_dir( 0, name->nb_languages );
568 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
570 put_dword( res->lang );
571 put_dword( res->data_offset );
576 /* dump the resource data entries */
578 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
580 put_dword( data_offset + start_rva );
581 put_dword( res->data_size );
582 put_dword( 0 );
583 put_dword( 0 );
584 data_offset += (res->data_size + 3) & ~3;
587 /* dump the name strings */
589 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
591 if (type->type->str) output_bin_string( type->type );
592 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
593 if (name->name->str) output_bin_string( name->name );
596 /* resource data */
598 align_output( 4 );
599 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
601 put_data( res->data, res->data_size );
602 align_output( 4 );
605 free_resource_tree( tree );
608 static unsigned int get_resource_header_size( const struct resource *res )
610 unsigned int size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
612 if (!res->type.str) size += 2 * sizeof(unsigned short);
613 else size += (res->type.len + 1) * sizeof(WCHAR);
615 if (!res->name.str) size += 2 * sizeof(unsigned short);
616 else size += (res->name.len + 1) * sizeof(WCHAR);
618 return size;
621 /* output the resources into a .o file */
622 void output_res_o_file( DLLSPEC *spec )
624 unsigned int i;
625 char *res_file = NULL;
626 struct strarray args;
628 if (!spec->nb_resources) fatal_error( "--resources mode needs at least one resource file as input\n" );
629 if (!output_file_name) fatal_error( "No output file name specified\n" );
631 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
632 remove_duplicate_resources( spec );
634 byte_swapped = 0;
635 init_output_buffer();
637 put_dword( 0 ); /* ResSize */
638 put_dword( 32 ); /* HeaderSize */
639 put_word( 0xffff ); /* ResType */
640 put_word( 0x0000 );
641 put_word( 0xffff ); /* ResName */
642 put_word( 0x0000 );
643 put_dword( 0 ); /* DataVersion */
644 put_word( 0 ); /* Memory options */
645 put_word( 0 ); /* Language */
646 put_dword( 0 ); /* Version */
647 put_dword( 0 ); /* Characteristics */
649 for (i = 0; i < spec->nb_resources; i++)
651 unsigned int header_size = get_resource_header_size( &spec->resources[i] );
653 put_dword( spec->resources[i].data_size );
654 put_dword( (header_size + 3) & ~3 );
655 put_string( &spec->resources[i].type );
656 put_string( &spec->resources[i].name );
657 align_output( 4 );
658 put_dword( 0 );
659 put_word( spec->resources[i].mem_options );
660 put_word( spec->resources[i].lang );
661 put_dword( spec->resources[i].version );
662 put_dword( 0 );
663 put_data( spec->resources[i].data, spec->resources[i].data_size );
664 align_output( 4 );
667 /* if the output file name is a .res too, don't run the results through windres */
668 if (strendswith( output_file_name, ".res"))
670 flush_output_buffer( output_file_name );
671 return;
674 res_file = get_temp_file_name( output_file_name, ".res" );
675 flush_output_buffer( res_file );
677 args = find_tool( "windres", NULL );
678 strarray_add( &args, "-i" );
679 strarray_add( &args, res_file );
680 strarray_add( &args, "-o" );
681 strarray_add( &args, output_file_name );
682 switch (target.cpu)
684 case CPU_i386:
685 strarray_add( &args, "-F" );
686 strarray_add( &args, "pe-i386" );
687 break;
688 case CPU_x86_64:
689 strarray_add( &args, "-F" );
690 strarray_add( &args, "pe-x86-64" );
691 break;
692 default:
693 break;
695 spawn( args );