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
32 typedef unsigned short WCHAR
;
34 /* Unicode string or integer id */
37 WCHAR
*str
; /* ptr to Unicode string */
38 unsigned short id
; /* integer id if str is NULL */
41 /* descriptor for a resource */
44 struct string_id type
;
45 struct string_id name
;
47 unsigned int data_size
;
48 unsigned int data_offset
;
49 unsigned short mem_options
;
54 /* name level of the resource tree */
57 const struct string_id
*name
; /* name */
58 struct resource
*res
; /* resource */
59 int nb_languages
; /* number of languages */
60 unsigned int dir_offset
; /* offset of directory in resource dir */
61 unsigned int name_offset
; /* offset of name in resource dir */
64 /* type level of the resource tree */
67 const struct string_id
*type
; /* type name */
68 struct res_name
*names
; /* names array */
69 unsigned int nb_names
; /* total number of names */
70 unsigned int nb_id_names
; /* number of names that have a numeric id */
71 unsigned int dir_offset
; /* offset of directory in resource dir */
72 unsigned int name_offset
; /* offset of type name in resource dir */
75 /* top level of the resource tree */
78 struct res_type
*types
; /* types array */
79 unsigned int nb_types
; /* total number of types */
82 /* size of a resource directory with n entries */
83 #define RESOURCE_DIR_SIZE (4 * sizeof(unsigned int))
84 #define RESOURCE_DIR_ENTRY_SIZE (2 * sizeof(unsigned int))
85 #define RESOURCE_DATA_ENTRY_SIZE (4 * sizeof(unsigned int))
86 #define RESDIR_SIZE(n) (RESOURCE_DIR_SIZE + (n) * RESOURCE_DIR_ENTRY_SIZE)
89 static inline struct resource
*add_resource( DLLSPEC
*spec
)
91 spec
->resources
= xrealloc( spec
->resources
, (spec
->nb_resources
+ 1) * sizeof(spec
->resources
[0]) );
92 return &spec
->resources
[spec
->nb_resources
++];
95 static inline unsigned int strlenW( const WCHAR
*str
)
102 static inline int strcmpW( const WCHAR
*str1
, const WCHAR
*str2
)
104 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
105 return *str1
- *str2
;
108 static struct res_name
*add_name( struct res_type
*type
, struct resource
*res
)
110 struct res_name
*name
;
111 type
->names
= xrealloc( type
->names
, (type
->nb_names
+ 1) * sizeof(*type
->names
) );
112 name
= &type
->names
[type
->nb_names
++];
113 name
->name
= &res
->name
;
115 name
->nb_languages
= 1;
116 if (!name
->name
->str
) type
->nb_id_names
++;
120 static struct res_type
*add_type( struct res_tree
*tree
, struct resource
*res
)
122 struct res_type
*type
;
123 tree
->types
= xrealloc( tree
->types
, (tree
->nb_types
+ 1) * sizeof(*tree
->types
) );
124 type
= &tree
->types
[tree
->nb_types
++];
125 type
->type
= &res
->type
;
128 type
->nb_id_names
= 0;
132 /* get a string from the current resource file */
133 static void get_string( struct string_id
*str
)
135 WCHAR wc
= get_word();
140 str
->id
= get_word();
144 WCHAR
*p
= xmalloc( (strlenW( (const WCHAR
*)(input_buffer
+ input_buffer_pos
) - 1) + 1) * sizeof(WCHAR
) );
147 if ((*p
++ = wc
)) while ((*p
++ = get_word()));
151 /* put a string into the resource file */
152 static void put_string( const struct string_id
*str
)
156 const WCHAR
*p
= str
->str
;
157 while (*p
) put_word( *p
++ );
167 static void dump_res_data( const struct resource
*res
)
170 unsigned int size
= (res
->data_size
+ 3) & ~3;
174 input_buffer
= res
->data
;
175 input_buffer_pos
= 0;
176 input_buffer_size
= size
;
178 output( "\t.long " );
181 if ((i
++ % 16) == 15) output( "0x%08x\n\t.long ", get_dword() );
182 else output( "0x%08x,", get_dword() );
185 output( "0x%08x\n", get_dword() );
186 assert( input_buffer_pos
== input_buffer_size
);
189 /* check the file header */
190 /* all values must be zero except header size */
191 static int check_header(void)
195 if (get_dword()) return 0; /* data size */
196 size
= get_dword(); /* header size */
197 if (size
== 0x20000000) byte_swapped
= 1;
198 else if (size
!= 0x20) return 0;
199 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
200 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
201 if (get_dword()) return 0; /* data version */
202 if (get_word()) return 0; /* mem options */
203 if (get_word()) return 0; /* language */
204 if (get_dword()) return 0; /* version */
205 if (get_dword()) return 0; /* characteristics */
209 /* load the next resource from the current file */
210 static void load_next_resource( DLLSPEC
*spec
)
212 unsigned int hdr_size
;
213 struct resource
*res
= add_resource( spec
);
215 res
->data_size
= get_dword();
216 hdr_size
= get_dword();
217 if (hdr_size
& 3) fatal_error( "%s header size not aligned\n", input_buffer_filename
);
218 if (hdr_size
< 32) fatal_error( "%s invalid header size %u\n", input_buffer_filename
, hdr_size
);
220 res
->data
= input_buffer
+ input_buffer_pos
- 2*sizeof(unsigned int) + hdr_size
;
221 if ((const unsigned char *)res
->data
< input_buffer
||
222 (const unsigned char *)res
->data
>= input_buffer
+ input_buffer_size
)
223 fatal_error( "%s invalid header size %u\n", input_buffer_filename
, hdr_size
);
224 get_string( &res
->type
);
225 get_string( &res
->name
);
226 if (input_buffer_pos
& 2) get_word(); /* align to dword boundary */
227 get_dword(); /* skip data version */
228 res
->mem_options
= get_word();
229 res
->lang
= get_word();
230 res
->version
= get_dword();
231 get_dword(); /* skip characteristics */
233 input_buffer_pos
= ((const unsigned char *)res
->data
- input_buffer
) + ((res
->data_size
+ 3) & ~3);
234 input_buffer_pos
= (input_buffer_pos
+ 3) & ~3;
235 if (input_buffer_pos
> input_buffer_size
)
236 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
239 /* load a Win32 .res file */
240 int load_res32_file( const char *name
, DLLSPEC
*spec
)
244 init_input_buffer( name
);
246 if ((ret
= check_header()))
248 while (input_buffer_pos
< input_buffer_size
) load_next_resource( spec
);
253 /* compare two unicode strings/ids */
254 static int cmp_string( const struct string_id
*str1
, const struct string_id
*str2
)
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
;
273 if ((ret
= cmp_string( &res1
->type
, &res2
->type
))) return ret
;
274 if ((ret
= cmp_string( &res1
->name
, &res2
->name
))) return ret
;
275 if ((ret
= res1
->lang
- res2
->lang
)) return ret
;
276 return res1
->version
- res2
->version
;
279 static char *format_res_string( const struct string_id
*str
)
281 int i
, len
= str
->str
? strlenW(str
->str
) + 1 : 5;
282 char *ret
= xmalloc( len
);
284 if (!str
->str
) sprintf( ret
, "%04x", str
->id
);
285 else for (i
= 0; i
< len
; i
++) ret
[i
] = str
->str
[i
]; /* dumb W->A conversion */
289 /* get rid of duplicate resources with different versions */
290 static void remove_duplicate_resources( DLLSPEC
*spec
)
294 for (i
= n
= 0; i
< spec
->nb_resources
; i
++, n
++)
296 if (i
&& !cmp_string( &spec
->resources
[i
].type
, &spec
->resources
[i
-1].type
) &&
297 !cmp_string( &spec
->resources
[i
].name
, &spec
->resources
[i
-1].name
) &&
298 spec
->resources
[i
].lang
== spec
->resources
[i
-1].lang
)
300 if (spec
->resources
[i
].version
== spec
->resources
[i
-1].version
)
302 char *type_str
= format_res_string( &spec
->resources
[i
].type
);
303 char *name_str
= format_res_string( &spec
->resources
[i
].name
);
304 error( "winebuild: duplicate resource type %s name %s language %04x version %08x\n",
305 type_str
, name_str
, spec
->resources
[i
].lang
, spec
->resources
[i
].version
);
307 else n
--; /* replace the previous one */
309 if (n
< i
) spec
->resources
[n
] = spec
->resources
[i
];
311 spec
->nb_resources
= n
;
314 /* build the 3-level (type,name,language) resource tree */
315 static struct res_tree
*build_resource_tree( DLLSPEC
*spec
, unsigned int *dir_size
)
317 unsigned int i
, k
, n
, offset
, data_offset
;
318 struct res_tree
*tree
;
319 struct res_type
*type
= NULL
;
320 struct res_name
*name
= NULL
;
321 struct resource
*res
;
323 qsort( spec
->resources
, spec
->nb_resources
, sizeof(*spec
->resources
), cmp_res
);
324 remove_duplicate_resources( spec
);
326 tree
= xmalloc( sizeof(*tree
) );
330 for (i
= 0; i
< spec
->nb_resources
; i
++)
332 if (!i
|| cmp_string( &spec
->resources
[i
].type
, &spec
->resources
[i
-1].type
)) /* new type */
334 type
= add_type( tree
, &spec
->resources
[i
] );
335 name
= add_name( type
, &spec
->resources
[i
] );
337 else if (cmp_string( &spec
->resources
[i
].name
, &spec
->resources
[i
-1].name
)) /* new name */
339 name
= add_name( type
, &spec
->resources
[i
] );
343 name
->nb_languages
++;
347 /* compute the offsets */
349 offset
= RESDIR_SIZE( tree
->nb_types
);
350 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
352 type
->dir_offset
= offset
;
353 offset
+= RESDIR_SIZE( type
->nb_names
);
354 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
356 name
->dir_offset
= offset
;
357 offset
+= RESDIR_SIZE( name
->nb_languages
);
360 data_offset
= offset
;
361 offset
+= spec
->nb_resources
* RESOURCE_DATA_ENTRY_SIZE
;
363 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
367 type
->name_offset
= offset
| 0x80000000;
368 offset
+= (strlenW(type
->type
->str
)+1) * sizeof(WCHAR
);
370 else type
->name_offset
= type
->type
->id
;
372 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
376 name
->name_offset
= offset
| 0x80000000;
377 offset
+= (strlenW(name
->name
->str
)+1) * sizeof(WCHAR
);
379 else name
->name_offset
= name
->name
->id
;
380 for (k
= 0, res
= name
->res
; k
< name
->nb_languages
; k
++, res
++)
382 unsigned int entry_offset
= (res
- spec
->resources
) * RESOURCE_DATA_ENTRY_SIZE
;
383 res
->data_offset
= data_offset
+ entry_offset
;
387 if (dir_size
) *dir_size
= (offset
+ 3) & ~3;
391 /* free the resource tree */
392 static void free_resource_tree( struct res_tree
*tree
)
396 for (i
= 0; i
< tree
->nb_types
; i
++) free( tree
->types
[i
].names
);
401 /* output a Unicode string */
402 static void output_string( const WCHAR
*name
)
404 int i
, len
= strlenW(name
);
405 output( "\t.short 0x%04x", len
);
406 for (i
= 0; i
< len
; i
++) output( ",0x%04x", name
[i
] );
408 for (i
= 0; i
< len
; i
++) output( "%c", isprint((char)name
[i
]) ? (char)name
[i
] : '?' );
412 /* output a resource directory */
413 static inline void output_res_dir( unsigned int nb_names
, unsigned int nb_ids
)
415 output( "\t.long 0\n" ); /* Characteristics */
416 output( "\t.long 0\n" ); /* TimeDateStamp */
417 output( "\t.short 0,0\n" ); /* Major/MinorVersion */
418 output( "\t.short %u,%u\n", /* NumberOfNamed/IdEntries */
422 /* output the resource definitions */
423 void output_resources( DLLSPEC
*spec
)
427 struct res_tree
*tree
;
428 struct res_type
*type
;
429 struct res_name
*name
;
430 const struct resource
*res
;
432 if (!spec
->nb_resources
) return;
434 tree
= build_resource_tree( spec
, NULL
);
436 /* output the resource directories */
438 output( "\n/* resources */\n\n" );
439 output( "\t%s\n", get_asm_rsrc_section() );
440 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
441 output( ".L__wine_spec_resources:\n" );
443 for (i
= nb_id_types
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
444 if (!type
->type
->str
) nb_id_types
++;
446 output_res_dir( tree
->nb_types
- nb_id_types
, nb_id_types
);
448 /* dump the type directory */
450 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
451 output( "\t.long 0x%08x,0x%08x\n",
452 type
->name_offset
, type
->dir_offset
| 0x80000000 );
454 /* dump the names and languages directories */
456 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
458 output_res_dir( type
->nb_names
- type
->nb_id_names
, type
->nb_id_names
);
459 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
460 output( "\t.long 0x%08x,0x%08x\n",
461 name
->name_offset
, name
->dir_offset
| 0x80000000 );
463 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
465 output_res_dir( 0, name
->nb_languages
);
466 for (k
= 0, res
= name
->res
; k
< name
->nb_languages
; k
++, res
++)
467 output( "\t.long 0x%08x,0x%08x\n", res
->lang
, res
->data_offset
);
471 /* dump the resource data entries */
473 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
475 output_rva( ".L__wine_spec_res_%d", i
);
476 output( "\t.long %u,0,0\n", res
->data_size
);
479 /* dump the name strings */
481 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
483 if (type
->type
->str
) output_string( type
->type
->str
);
484 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
485 if (name
->name
->str
) output_string( name
->name
->str
);
490 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
492 output( "\n\t.align %d\n", get_alignment(4) );
493 output( ".L__wine_spec_res_%d:\n", i
);
494 dump_res_data( res
);
499 output( ".L__wine_spec_resources_end:\n" );
500 output( "\t.byte 0\n" );
502 free_resource_tree( tree
);
505 /* output a Unicode string in binary format */
506 static void output_bin_string( const WCHAR
*name
)
508 int i
, len
= strlenW(name
);
510 for (i
= 0; i
< len
; i
++) put_word( name
[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
)
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
);
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
->str
);
592 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
593 if (name
->name
->str
) output_bin_string( name
->name
->str
);
599 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
601 put_data( res
->data
, res
->data_size
);
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
+= (strlenW(res
->type
.str
) + 1) * sizeof(WCHAR
);
615 if (!res
->name
.str
) size
+= 2 * sizeof(unsigned short);
616 else size
+= (strlenW(res
->name
.str
) + 1) * sizeof(WCHAR
);
621 /* output the resources into a .o file */
622 void output_res_o_file( DLLSPEC
*spec
)
625 char *res_file
= NULL
;
627 struct strarray args
;
629 if (!spec
->nb_resources
) fatal_error( "--resources mode needs at least one resource file as input\n" );
630 if (!output_file_name
) fatal_error( "No output file name specified\n" );
632 qsort( spec
->resources
, spec
->nb_resources
, sizeof(*spec
->resources
), cmp_res
);
633 remove_duplicate_resources( spec
);
636 init_output_buffer();
638 put_dword( 0 ); /* ResSize */
639 put_dword( 32 ); /* HeaderSize */
640 put_word( 0xffff ); /* ResType */
642 put_word( 0xffff ); /* ResName */
644 put_dword( 0 ); /* DataVersion */
645 put_word( 0 ); /* Memory options */
646 put_word( 0 ); /* Language */
647 put_dword( 0 ); /* Version */
648 put_dword( 0 ); /* Characteristics */
650 for (i
= 0; i
< spec
->nb_resources
; i
++)
652 unsigned int header_size
= get_resource_header_size( &spec
->resources
[i
] );
654 put_dword( spec
->resources
[i
].data_size
);
655 put_dword( (header_size
+ 3) & ~3 );
656 put_string( &spec
->resources
[i
].type
);
657 put_string( &spec
->resources
[i
].name
);
660 put_word( spec
->resources
[i
].mem_options
);
661 put_word( spec
->resources
[i
].lang
);
662 put_dword( spec
->resources
[i
].version
);
664 put_data( spec
->resources
[i
].data
, spec
->resources
[i
].data_size
);
668 /* if the output file name is a .res too, don't run the results through windres */
669 if (strendswith( output_file_name
, ".res"))
671 flush_output_buffer( output_file_name
);
675 res_file
= get_temp_file_name( output_file_name
, ".res" );
676 if ((fd
= open( res_file
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_BINARY
, 0600 )) == -1)
677 fatal_error( "Cannot create %s\n", res_file
);
678 if (write( fd
, output_buffer
, output_buffer_pos
) != output_buffer_pos
)
679 fatal_error( "Error writing to %s\n", res_file
);
681 free( output_buffer
);
683 args
= find_tool( "windres", NULL
);
684 strarray_add( &args
, "-i" );
685 strarray_add( &args
, res_file
);
686 strarray_add( &args
, "-o" );
687 strarray_add( &args
, output_file_name
);
691 strarray_add( &args
, "-F" );
692 strarray_add( &args
, "pe-i386" );
695 strarray_add( &args
, "-F" );
696 strarray_add( &args
, "pe-x86-64" );