push 955563f995be8b0942dbb757ceb5b3a4c8ccafbf
[wine/hacks.git] / tools / winebuild / res32.c
blobed902bb1d4a84122fcc2957cf06c55cf2003fb8c
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"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33 #include <fcntl.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
41 #include "build.h"
43 typedef unsigned short WCHAR;
45 /* Unicode string or integer id */
46 struct string_id
48 WCHAR *str; /* ptr to Unicode string */
49 unsigned short id; /* integer id if str is NULL */
52 /* descriptor for a resource */
53 struct resource
55 struct string_id type;
56 struct string_id name;
57 const void *data;
58 unsigned int data_size;
59 unsigned short mem_options;
60 unsigned short lang;
63 /* name level of the resource tree */
64 struct res_name
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 */
73 struct res_type
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 */
83 struct res_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;
113 while (*s) s++;
114 return 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;
129 name->res = res;
130 name->nb_languages = 1;
131 if (!name->name->str) type->nb_id_names++;
132 return name;
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;
141 type->names = NULL;
142 type->nb_names = 0;
143 type->nb_id_names = 0;
144 return type;
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 );
154 return ret;
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;
161 if (byte_swapped)
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 );
165 return ret;
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 */
174 str->str = NULL;
175 str->id = get_word();
177 else
179 WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
180 str->str = p;
181 str->id = 0;
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 )
198 if (byte_swapped)
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 )
208 if (str->str)
210 const WCHAR *p = str->str;
211 while (*p) put_word( *p++ );
212 put_word( 0 );
214 else
216 put_word( 0xffff );
217 put_word( str->id );
221 /* check the file header */
222 /* all values must be zero except header size */
223 static int check_header(void)
225 unsigned int size;
227 if (get_dword()) return 0; /* data size */
228 size = get_dword(); /* header size */
229 if (size == 0x20000000) byte_swapped = 1;
230 else if (size != 0x20) return 0;
231 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
232 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
233 if (get_dword()) return 0; /* data version */
234 if (get_word()) return 0; /* mem options */
235 if (get_word()) return 0; /* language */
236 if (get_dword()) return 0; /* version */
237 if (get_dword()) return 0; /* characteristics */
238 return 1;
241 /* load the next resource from the current file */
242 static void load_next_resource( DLLSPEC *spec )
244 unsigned int hdr_size;
245 struct resource *res = add_resource( spec );
247 res->data_size = get_dword();
248 hdr_size = get_dword();
249 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
251 res->data = file_pos - 2*sizeof(unsigned int) + hdr_size;
252 get_string( &res->type );
253 get_string( &res->name );
254 if ((unsigned long)file_pos & 2) get_word(); /* align to dword boundary */
255 get_dword(); /* skip data version */
256 res->mem_options = get_word();
257 res->lang = get_word();
258 get_dword(); /* skip version */
259 get_dword(); /* skip characteristics */
261 file_pos = (const unsigned char *)res->data + ((res->data_size + 3) & ~3);
262 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
265 /* load a Win32 .res file */
266 int load_res32_file( const char *name, DLLSPEC *spec )
268 int fd, ret;
269 void *base;
270 struct stat st;
272 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
273 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
274 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
275 #ifdef HAVE_MMAP
276 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
277 #endif /* HAVE_MMAP */
279 base = xmalloc( st.st_size );
280 if (read( fd, base, st.st_size ) != st.st_size)
281 fatal_error( "Cannot read %s\n", name );
284 byte_swapped = 0;
285 file_name = name;
286 file_pos = base;
287 file_end = file_pos + st.st_size;
288 if ((ret = check_header()))
290 while (file_pos < file_end) load_next_resource( spec );
292 close( fd );
293 return ret;
296 /* compare two unicode strings/ids */
297 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
299 if (!str1->str)
301 if (!str2->str) return str1->id - str2->id;
302 return 1; /* an id compares larger than a string */
304 if (!str2->str) return -1;
305 return strcmpW( str1->str, str2->str );
308 /* compare two resources for sorting the resource directory */
309 /* resources are stored first by type, then by name, then by language */
310 static int cmp_res( const void *ptr1, const void *ptr2 )
312 const struct resource *res1 = ptr1;
313 const struct resource *res2 = ptr2;
314 int ret;
316 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
317 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
318 return res1->lang - res2->lang;
321 static char *format_res_string( const struct string_id *str )
323 int i, len = str->str ? strlenW(str->str) + 1 : 5;
324 char *ret = xmalloc( len );
326 if (!str->str) sprintf( ret, "%04x", str->id );
327 else for (i = 0; i < len; i++) ret[i] = str->str[i]; /* dumb W->A conversion */
328 return ret;
331 /* build the 3-level (type,name,language) resource tree */
332 static struct res_tree *build_resource_tree( DLLSPEC *spec )
334 unsigned int i;
335 struct res_tree *tree;
336 struct res_type *type = NULL;
337 struct res_name *name = NULL;
339 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
341 tree = xmalloc( sizeof(*tree) );
342 tree->types = NULL;
343 tree->nb_types = 0;
345 for (i = 0; i < spec->nb_resources; i++)
347 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
349 type = add_type( tree, &spec->resources[i] );
350 name = add_name( type, &spec->resources[i] );
352 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
354 name = add_name( type, &spec->resources[i] );
356 else if (spec->resources[i].lang == spec->resources[i-1].lang)
358 char *type_str = format_res_string( &spec->resources[i].type );
359 char *name_str = format_res_string( &spec->resources[i].name );
360 error( "winebuild: duplicate resource type %s name %s language %04x\n",
361 type_str, name_str, spec->resources[i].lang );
363 else name->nb_languages++;
365 return tree;
368 /* free the resource tree */
369 static void free_resource_tree( struct res_tree *tree )
371 unsigned int i;
373 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
374 free( tree->types );
375 free( tree );
378 /* output a Unicode string */
379 static void output_string( const WCHAR *name )
381 int i, len = strlenW(name);
382 output( "\t%s 0x%04x", get_asm_short_keyword(), len );
383 for (i = 0; i < len; i++) output( ",0x%04x", name[i] );
384 output( " /* " );
385 for (i = 0; i < len; i++) output( "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
386 output( " */\n" );
389 /* output a resource directory */
390 static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
392 output( "\t.long 0\n" ); /* Characteristics */
393 output( "\t.long 0\n" ); /* TimeDateStamp */
394 output( "\t%s 0,0\n", /* Major/MinorVersion */
395 get_asm_short_keyword() );
396 output( "\t%s %u,%u\n", /* NumberOfNamed/IdEntries */
397 get_asm_short_keyword(), nb_names, nb_ids );
400 /* output the resource definitions */
401 void output_resources( DLLSPEC *spec )
403 int k, nb_id_types;
404 unsigned int i, n, offset, data_offset;
405 struct res_tree *tree;
406 struct res_type *type;
407 struct res_name *name;
408 const struct resource *res;
410 if (!spec->nb_resources) return;
412 tree = build_resource_tree( spec );
414 /* compute the offsets */
416 offset = RESDIR_SIZE( tree->nb_types );
417 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
419 offset += RESDIR_SIZE( type->nb_names );
420 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
421 offset += RESDIR_SIZE( name->nb_languages );
423 offset += spec->nb_resources * RESOURCE_DATA_ENTRY_SIZE;
425 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
427 if (type->type->str)
429 type->name_offset = offset | 0x80000000;
430 offset += (strlenW(type->type->str)+1) * sizeof(WCHAR);
432 else
434 type->name_offset = type->type->id;
435 nb_id_types++;
438 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
440 if (name->name->str)
442 name->name_offset = offset | 0x80000000;
443 offset += (strlenW(name->name->str)+1) * sizeof(WCHAR);
445 else name->name_offset = name->name->id;
449 /* output the resource directories */
451 output( "\n/* resources */\n\n" );
452 output( "\t.data\n" );
453 output( "\t.align %d\n", get_alignment(get_ptr_size()) );
454 output( ".L__wine_spec_resources:\n" );
456 output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
458 /* dump the type directory */
460 offset = RESDIR_SIZE( tree->nb_types );
461 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
463 output( "\t.long 0x%08x,0x%08x\n",
464 type->name_offset, offset | 0x80000000 );
465 offset += RESDIR_SIZE( type->nb_names );
466 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
467 offset += RESDIR_SIZE( name->nb_languages );
470 data_offset = offset;
471 offset = RESDIR_SIZE( tree->nb_types );
473 /* dump the names and languages directories */
475 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
477 output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
478 offset += RESDIR_SIZE( type->nb_names );
479 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
481 output( "\t.long 0x%08x,0x%08x\n",
482 name->name_offset, offset | 0x80000000 );
483 offset += RESDIR_SIZE( name->nb_languages );
486 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
488 output_res_dir( 0, name->nb_languages );
489 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
491 unsigned int entry_offset = (res - spec->resources) * RESOURCE_DATA_ENTRY_SIZE;
492 output( "\t.long 0x%08x,0x%08x\n", res->lang, data_offset + entry_offset );
497 /* dump the resource data entries */
499 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
500 output( "\t.long .L__wine_spec_res_%d-.L__wine_spec_rva_base,%u,0,0\n",
501 i, (res->data_size + 3) & ~3 );
503 /* dump the name strings */
505 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
507 if (type->type->str) output_string( type->type->str );
508 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
509 if (name->name->str) output_string( name->name->str );
512 /* resource data */
514 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
516 output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
517 output( ".L__wine_spec_res_%d:\n", i );
518 dump_bytes( res->data, (res->data_size + 3) & ~3 );
520 output( ".L__wine_spec_resources_end:\n" );
521 output( "\t.byte 0\n" );
523 free_resource_tree( tree );
526 static unsigned int get_resource_header_size( const struct resource *res )
528 unsigned int size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
530 if (!res->type.str) size += 2 * sizeof(unsigned short);
531 else size += (strlenW(res->type.str) + 1) * sizeof(WCHAR);
533 if (!res->name.str) size += 2 * sizeof(unsigned short);
534 else size += (strlenW(res->name.str) + 1) * sizeof(WCHAR);
536 return size;
539 /* output the resources into a .o file */
540 void output_res_o_file( DLLSPEC *spec )
542 unsigned int i, total_size;
543 unsigned char *data;
544 char *res_file, *cmd;
545 const char *prog;
546 int fd, err;
548 if (!spec->nb_resources) fatal_error( "--resources mode needs at least one resource file as input\n" );
549 if (!output_file_name) fatal_error( "No output file name specified\n" );
551 total_size = 32; /* header */
553 for (i = 0; i < spec->nb_resources; i++)
555 total_size += (get_resource_header_size( &spec->resources[i] ) + 3) & ~3;
556 total_size += (spec->resources[i].data_size + 3) & ~3;
558 data = xmalloc( total_size );
560 byte_swapped = 0;
561 file_out_pos = data;
562 file_out_end = data + total_size;
564 put_dword( 0 ); /* ResSize */
565 put_dword( 32 ); /* HeaderSize */
566 put_word( 0xffff ); /* ResType */
567 put_word( 0x0000 );
568 put_word( 0xffff ); /* ResName */
569 put_word( 0x0000 );
570 put_dword( 0 ); /* DataVersion */
571 put_word( 0 ); /* Memory options */
572 put_word( 0 ); /* Language */
573 put_dword( 0 ); /* Version */
574 put_dword( 0 ); /* Characteristics */
576 for (i = 0; i < spec->nb_resources; i++)
578 unsigned int header_size = get_resource_header_size( &spec->resources[i] );
580 put_dword( spec->resources[i].data_size );
581 put_dword( header_size );
582 put_string( &spec->resources[i].type );
583 put_string( &spec->resources[i].name );
584 if ((unsigned long)file_out_pos & 2) put_word( 0 );
585 put_dword( 0 );
586 put_word( spec->resources[i].mem_options );
587 put_word( spec->resources[i].lang );
588 put_dword( 0 );
589 put_dword( 0 );
590 memcpy( file_out_pos, spec->resources[i].data, spec->resources[i].data_size );
591 file_out_pos += spec->resources[i].data_size;
592 while ((unsigned long)file_out_pos & 3) *file_out_pos++ = 0;
594 assert( file_out_pos == file_out_end );
596 res_file = get_temp_file_name( output_file_name, ".res" );
597 if ((fd = open( res_file, O_WRONLY|O_CREAT|O_TRUNC, 0600 )) == -1)
598 fatal_error( "Cannot create %s\n", res_file );
599 if (write( fd, data, total_size ) != total_size)
600 fatal_error( "Error writing to %s\n", res_file );
601 close( fd );
602 free( data );
604 prog = get_windres_command();
605 cmd = xmalloc( strlen(prog) + strlen(res_file) + strlen(output_file_name) + 9 );
606 sprintf( cmd, "%s -i %s -o %s", prog, res_file, output_file_name );
607 if (verbose) fprintf( stderr, "%s\n", cmd );
608 err = system( cmd );
609 if (err) fatal_error( "%s failed with status %d\n", prog, err );
610 free( cmd );
611 output_file_name = NULL; /* so we don't try to assemble it */