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
22 #include "wine/port.h"
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
37 #ifdef HAVE_SYS_MMAN_H
43 typedef unsigned short WCHAR
;
45 /* Unicode string or integer id */
48 WCHAR
*str
; /* ptr to Unicode string */
49 unsigned short id
; /* integer id if str is NULL */
52 /* descriptor for a resource */
55 struct string_id type
;
56 struct string_id name
;
58 unsigned int data_size
;
59 unsigned short mem_options
;
63 /* name level of the resource tree */
66 const struct string_id
*name
; /* name */
67 const struct resource
*res
; /* resource */
68 int nb_languages
; /* number of languages */
69 unsigned int name_offset
; /* offset of name in resource dir */
72 /* type level of the resource tree */
75 const struct string_id
*type
; /* type name */
76 struct res_name
*names
; /* names array */
77 unsigned int nb_names
; /* total number of names */
78 unsigned int nb_id_names
; /* number of names that have a numeric id */
79 unsigned int name_offset
; /* offset of type name in resource dir */
82 /* top level of the resource tree */
85 struct res_type
*types
; /* types array */
86 unsigned int nb_types
; /* total number of types */
89 static int byte_swapped
; /* whether the current resource file is byte-swapped */
90 static const unsigned char *file_pos
; /* current position in resource file */
91 static const unsigned char *file_end
; /* end of resource file */
92 static const char *file_name
; /* current resource file name */
94 static unsigned char *file_out_pos
; /* current position in output resource file */
95 static unsigned char *file_out_end
; /* end of output buffer */
97 /* size of a resource directory with n entries */
98 #define RESOURCE_DIR_SIZE (4 * sizeof(unsigned int))
99 #define RESOURCE_DIR_ENTRY_SIZE (2 * sizeof(unsigned int))
100 #define RESOURCE_DATA_ENTRY_SIZE (4 * sizeof(unsigned int))
101 #define RESDIR_SIZE(n) (RESOURCE_DIR_SIZE + (n) * RESOURCE_DIR_ENTRY_SIZE)
104 static inline struct resource
*add_resource( DLLSPEC
*spec
)
106 spec
->resources
= xrealloc( spec
->resources
, (spec
->nb_resources
+ 1) * sizeof(spec
->resources
[0]) );
107 return &spec
->resources
[spec
->nb_resources
++];
110 static inline unsigned int strlenW( const WCHAR
*str
)
112 const WCHAR
*s
= str
;
117 static inline int strcmpW( const WCHAR
*str1
, const WCHAR
*str2
)
119 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
120 return *str1
- *str2
;
123 static struct res_name
*add_name( struct res_type
*type
, const struct resource
*res
)
125 struct res_name
*name
;
126 type
->names
= xrealloc( type
->names
, (type
->nb_names
+ 1) * sizeof(*type
->names
) );
127 name
= &type
->names
[type
->nb_names
++];
128 name
->name
= &res
->name
;
130 name
->nb_languages
= 1;
131 if (!name
->name
->str
) type
->nb_id_names
++;
135 static struct res_type
*add_type( struct res_tree
*tree
, const struct resource
*res
)
137 struct res_type
*type
;
138 tree
->types
= xrealloc( tree
->types
, (tree
->nb_types
+ 1) * sizeof(*tree
->types
) );
139 type
= &tree
->types
[tree
->nb_types
++];
140 type
->type
= &res
->type
;
143 type
->nb_id_names
= 0;
147 /* get the next word from the current resource file */
148 static unsigned short get_word(void)
150 unsigned short ret
= *(const unsigned short *)file_pos
;
151 if (byte_swapped
) ret
= (ret
<< 8) | (ret
>> 8);
152 file_pos
+= sizeof(unsigned short);
153 if (file_pos
> file_end
) fatal_error( "%s is a truncated file\n", file_name
);
157 /* get the next dword from the current resource file */
158 static unsigned int get_dword(void)
160 unsigned int ret
= *(const unsigned int *)file_pos
;
162 ret
= ((ret
<< 24) | ((ret
<< 8) & 0x00ff0000) | ((ret
>> 8) & 0x0000ff00) | (ret
>> 24));
163 file_pos
+= sizeof(unsigned int);
164 if (file_pos
> file_end
) fatal_error( "%s is a truncated file\n", file_name
);
168 /* get a string from the current resource file */
169 static void get_string( struct string_id
*str
)
171 if (*(const WCHAR
*)file_pos
== 0xffff)
173 get_word(); /* skip the 0xffff */
175 str
->id
= get_word();
179 WCHAR
*p
= xmalloc( (strlenW((const WCHAR
*)file_pos
) + 1) * sizeof(WCHAR
) );
182 while ((*p
++ = get_word()));
186 /* put a word into the resource file */
187 static void put_word( unsigned short val
)
189 if (byte_swapped
) val
= (val
<< 8) | (val
>> 8);
190 *(unsigned short *)file_out_pos
= val
;
191 file_out_pos
+= sizeof(unsigned short);
192 assert( file_out_pos
<= file_out_end
);
195 /* put a dword into the resource file */
196 static void put_dword( unsigned int val
)
199 val
= ((val
<< 24) | ((val
<< 8) & 0x00ff0000) | ((val
>> 8) & 0x0000ff00) | (val
>> 24));
200 *(unsigned int *)file_out_pos
= val
;
201 file_out_pos
+= sizeof(unsigned int);
202 assert( file_out_pos
<= file_out_end
);
205 /* put a string into the resource file */
206 static void put_string( const struct string_id
*str
)
210 const WCHAR
*p
= str
->str
;
211 while (*p
) put_word( *p
++ );
221 static void dump_res_data( const struct resource
*res
)
224 unsigned int size
= (res
->data_size
+ 3) & ~3;
228 file_pos
= res
->data
;
229 file_end
= (const unsigned char *)res
->data
+ size
;
231 output( "\t.long " );
234 if ((i
++ % 16) == 15) output( "0x%08x\n\t.long ", get_dword() );
235 else output( "0x%08x,", get_dword() );
238 output( "0x%08x\n", get_dword() );
240 assert( file_pos
== file_end
);
243 /* check the file header */
244 /* all values must be zero except header size */
245 static int check_header(void)
249 if (get_dword()) return 0; /* data size */
250 size
= get_dword(); /* header size */
251 if (size
== 0x20000000) byte_swapped
= 1;
252 else if (size
!= 0x20) return 0;
253 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
254 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
255 if (get_dword()) return 0; /* data version */
256 if (get_word()) return 0; /* mem options */
257 if (get_word()) return 0; /* language */
258 if (get_dword()) return 0; /* version */
259 if (get_dword()) return 0; /* characteristics */
263 /* load the next resource from the current file */
264 static void load_next_resource( DLLSPEC
*spec
)
266 unsigned int hdr_size
;
267 struct resource
*res
= add_resource( spec
);
269 res
->data_size
= get_dword();
270 hdr_size
= get_dword();
271 if (hdr_size
& 3) fatal_error( "%s header size not aligned\n", file_name
);
273 res
->data
= file_pos
- 2*sizeof(unsigned int) + hdr_size
;
274 get_string( &res
->type
);
275 get_string( &res
->name
);
276 if ((unsigned long)file_pos
& 2) get_word(); /* align to dword boundary */
277 get_dword(); /* skip data version */
278 res
->mem_options
= get_word();
279 res
->lang
= get_word();
280 get_dword(); /* skip version */
281 get_dword(); /* skip characteristics */
283 file_pos
= (const unsigned char *)res
->data
+ ((res
->data_size
+ 3) & ~3);
284 if (file_pos
> file_end
) fatal_error( "%s is a truncated file\n", file_name
);
287 /* load a Win32 .res file */
288 int load_res32_file( const char *name
, DLLSPEC
*spec
)
294 if ((fd
= open( name
, O_RDONLY
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", name
);
295 if ((fstat( fd
, &st
) == -1)) fatal_perror( "Cannot stat %s", name
);
296 if (!st
.st_size
) fatal_error( "%s is an empty file\n", name
);
298 if ((base
= mmap( NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0 )) == (void*)-1)
299 #endif /* HAVE_MMAP */
301 base
= xmalloc( st
.st_size
);
302 if (read( fd
, base
, st
.st_size
) != st
.st_size
)
303 fatal_error( "Cannot read %s\n", name
);
309 file_end
= file_pos
+ st
.st_size
;
310 if ((ret
= check_header()))
312 while (file_pos
< file_end
) load_next_resource( spec
);
318 /* compare two unicode strings/ids */
319 static int cmp_string( const struct string_id
*str1
, const struct string_id
*str2
)
323 if (!str2
->str
) return str1
->id
- str2
->id
;
324 return 1; /* an id compares larger than a string */
326 if (!str2
->str
) return -1;
327 return strcmpW( str1
->str
, str2
->str
);
330 /* compare two resources for sorting the resource directory */
331 /* resources are stored first by type, then by name, then by language */
332 static int cmp_res( const void *ptr1
, const void *ptr2
)
334 const struct resource
*res1
= ptr1
;
335 const struct resource
*res2
= ptr2
;
338 if ((ret
= cmp_string( &res1
->type
, &res2
->type
))) return ret
;
339 if ((ret
= cmp_string( &res1
->name
, &res2
->name
))) return ret
;
340 return res1
->lang
- res2
->lang
;
343 static char *format_res_string( const struct string_id
*str
)
345 int i
, len
= str
->str
? strlenW(str
->str
) + 1 : 5;
346 char *ret
= xmalloc( len
);
348 if (!str
->str
) sprintf( ret
, "%04x", str
->id
);
349 else for (i
= 0; i
< len
; i
++) ret
[i
] = str
->str
[i
]; /* dumb W->A conversion */
353 /* build the 3-level (type,name,language) resource tree */
354 static struct res_tree
*build_resource_tree( DLLSPEC
*spec
)
357 struct res_tree
*tree
;
358 struct res_type
*type
= NULL
;
359 struct res_name
*name
= NULL
;
361 qsort( spec
->resources
, spec
->nb_resources
, sizeof(*spec
->resources
), cmp_res
);
363 tree
= xmalloc( sizeof(*tree
) );
367 for (i
= 0; i
< spec
->nb_resources
; i
++)
369 if (!i
|| cmp_string( &spec
->resources
[i
].type
, &spec
->resources
[i
-1].type
)) /* new type */
371 type
= add_type( tree
, &spec
->resources
[i
] );
372 name
= add_name( type
, &spec
->resources
[i
] );
374 else if (cmp_string( &spec
->resources
[i
].name
, &spec
->resources
[i
-1].name
)) /* new name */
376 name
= add_name( type
, &spec
->resources
[i
] );
378 else if (spec
->resources
[i
].lang
== spec
->resources
[i
-1].lang
)
380 char *type_str
= format_res_string( &spec
->resources
[i
].type
);
381 char *name_str
= format_res_string( &spec
->resources
[i
].name
);
382 error( "winebuild: duplicate resource type %s name %s language %04x\n",
383 type_str
, name_str
, spec
->resources
[i
].lang
);
385 else name
->nb_languages
++;
390 /* free the resource tree */
391 static void free_resource_tree( struct res_tree
*tree
)
395 for (i
= 0; i
< tree
->nb_types
; i
++) free( tree
->types
[i
].names
);
400 /* output a Unicode string */
401 static void output_string( const WCHAR
*name
)
403 int i
, len
= strlenW(name
);
404 output( "\t%s 0x%04x", get_asm_short_keyword(), len
);
405 for (i
= 0; i
< len
; i
++) output( ",0x%04x", name
[i
] );
407 for (i
= 0; i
< len
; i
++) output( "%c", isprint((char)name
[i
]) ? (char)name
[i
] : '?' );
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%s 0,0\n", /* Major/MinorVersion */
417 get_asm_short_keyword() );
418 output( "\t%s %u,%u\n", /* NumberOfNamed/IdEntries */
419 get_asm_short_keyword(), nb_names
, nb_ids
);
422 /* output the resource definitions */
423 void output_resources( DLLSPEC
*spec
)
426 unsigned int i
, n
, offset
, data_offset
;
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
);
436 /* compute the offsets */
438 offset
= RESDIR_SIZE( tree
->nb_types
);
439 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
441 offset
+= RESDIR_SIZE( type
->nb_names
);
442 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
443 offset
+= RESDIR_SIZE( name
->nb_languages
);
445 offset
+= spec
->nb_resources
* RESOURCE_DATA_ENTRY_SIZE
;
447 for (i
= nb_id_types
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
451 type
->name_offset
= offset
| 0x80000000;
452 offset
+= (strlenW(type
->type
->str
)+1) * sizeof(WCHAR
);
456 type
->name_offset
= type
->type
->id
;
460 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
464 name
->name_offset
= offset
| 0x80000000;
465 offset
+= (strlenW(name
->name
->str
)+1) * sizeof(WCHAR
);
467 else name
->name_offset
= name
->name
->id
;
471 /* output the resource directories */
473 output( "\n/* resources */\n\n" );
474 output( "\t.data\n" );
475 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
476 output( ".L__wine_spec_resources:\n" );
478 output_res_dir( tree
->nb_types
- nb_id_types
, nb_id_types
);
480 /* dump the type directory */
482 offset
= RESDIR_SIZE( tree
->nb_types
);
483 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
485 output( "\t.long 0x%08x,0x%08x\n",
486 type
->name_offset
, offset
| 0x80000000 );
487 offset
+= RESDIR_SIZE( type
->nb_names
);
488 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
489 offset
+= RESDIR_SIZE( name
->nb_languages
);
492 data_offset
= offset
;
493 offset
= RESDIR_SIZE( tree
->nb_types
);
495 /* dump the names and languages directories */
497 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
499 output_res_dir( type
->nb_names
- type
->nb_id_names
, type
->nb_id_names
);
500 offset
+= RESDIR_SIZE( type
->nb_names
);
501 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
503 output( "\t.long 0x%08x,0x%08x\n",
504 name
->name_offset
, offset
| 0x80000000 );
505 offset
+= RESDIR_SIZE( name
->nb_languages
);
508 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
510 output_res_dir( 0, name
->nb_languages
);
511 for (k
= 0, res
= name
->res
; k
< name
->nb_languages
; k
++, res
++)
513 unsigned int entry_offset
= (res
- spec
->resources
) * RESOURCE_DATA_ENTRY_SIZE
;
514 output( "\t.long 0x%08x,0x%08x\n", res
->lang
, data_offset
+ entry_offset
);
519 /* dump the resource data entries */
521 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
522 output( "\t.long .L__wine_spec_res_%d-.L__wine_spec_rva_base,%u,0,0\n",
523 i
, (res
->data_size
+ 3) & ~3 );
525 /* dump the name strings */
527 for (i
= 0, type
= tree
->types
; i
< tree
->nb_types
; i
++, type
++)
529 if (type
->type
->str
) output_string( type
->type
->str
);
530 for (n
= 0, name
= type
->names
; n
< type
->nb_names
; n
++, name
++)
531 if (name
->name
->str
) output_string( name
->name
->str
);
536 for (i
= 0, res
= spec
->resources
; i
< spec
->nb_resources
; i
++, res
++)
538 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
539 output( ".L__wine_spec_res_%d:\n", i
);
540 dump_res_data( res
);
542 output( ".L__wine_spec_resources_end:\n" );
543 output( "\t.byte 0\n" );
545 free_resource_tree( tree
);
548 static unsigned int get_resource_header_size( const struct resource
*res
)
550 unsigned int size
= 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
552 if (!res
->type
.str
) size
+= 2 * sizeof(unsigned short);
553 else size
+= (strlenW(res
->type
.str
) + 1) * sizeof(WCHAR
);
555 if (!res
->name
.str
) size
+= 2 * sizeof(unsigned short);
556 else size
+= (strlenW(res
->name
.str
) + 1) * sizeof(WCHAR
);
561 /* output the resources into a .o file */
562 void output_res_o_file( DLLSPEC
*spec
)
564 unsigned int i
, total_size
;
566 char *res_file
= NULL
;
569 if (!spec
->nb_resources
) fatal_error( "--resources mode needs at least one resource file as input\n" );
570 if (!output_file_name
) fatal_error( "No output file name specified\n" );
572 total_size
= 32; /* header */
574 for (i
= 0; i
< spec
->nb_resources
; i
++)
576 total_size
+= (get_resource_header_size( &spec
->resources
[i
] ) + 3) & ~3;
577 total_size
+= (spec
->resources
[i
].data_size
+ 3) & ~3;
579 data
= xmalloc( total_size
);
583 file_out_end
= data
+ total_size
;
585 put_dword( 0 ); /* ResSize */
586 put_dword( 32 ); /* HeaderSize */
587 put_word( 0xffff ); /* ResType */
589 put_word( 0xffff ); /* ResName */
591 put_dword( 0 ); /* DataVersion */
592 put_word( 0 ); /* Memory options */
593 put_word( 0 ); /* Language */
594 put_dword( 0 ); /* Version */
595 put_dword( 0 ); /* Characteristics */
597 for (i
= 0; i
< spec
->nb_resources
; i
++)
599 unsigned int header_size
= get_resource_header_size( &spec
->resources
[i
] );
601 put_dword( spec
->resources
[i
].data_size
);
602 put_dword( (header_size
+ 3) & ~3 );
603 put_string( &spec
->resources
[i
].type
);
604 put_string( &spec
->resources
[i
].name
);
605 if ((unsigned long)file_out_pos
& 2) put_word( 0 );
607 put_word( spec
->resources
[i
].mem_options
);
608 put_word( spec
->resources
[i
].lang
);
611 memcpy( file_out_pos
, spec
->resources
[i
].data
, spec
->resources
[i
].data_size
);
612 file_out_pos
+= spec
->resources
[i
].data_size
;
613 while ((unsigned long)file_out_pos
& 3) *file_out_pos
++ = 0;
615 assert( file_out_pos
== file_out_end
);
617 /* if the output file name is a .res too, don't run the results through windres */
618 if (strendswith( output_file_name
, ".res"))
620 if ((fd
= open( output_file_name
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666 )) == -1)
621 fatal_error( "Cannot create %s\n", output_file_name
);
625 res_file
= get_temp_file_name( output_file_name
, ".res" );
626 if ((fd
= open( res_file
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600 )) == -1)
627 fatal_error( "Cannot create %s\n", res_file
);
629 if (write( fd
, data
, total_size
) != total_size
)
630 fatal_error( "Error writing to %s\n", res_file
);
636 const char *prog
= get_windres_command();
637 char *cmd
= xmalloc( strlen(prog
) + strlen(res_file
) + strlen(output_file_name
) + 9 );
638 sprintf( cmd
, "%s -i %s -o %s", prog
, res_file
, output_file_name
);
639 if (verbose
) fprintf( stderr
, "%s\n", cmd
);
641 if (err
) fatal_error( "%s failed with status %d\n", prog
, err
);
644 output_file_name
= NULL
; /* so we don't try to assemble it */