makefiles: Rename the SRCDIR, TOPSRCDIR and TOPOBJDIR variables to follow autoconf...
[wine] / tools / winebuild / res32.c
1 /*
2  * Builtin dlls resource support
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
35 #include "build.h"
36
37 typedef unsigned short WCHAR;
38
39 /* Unicode string or integer id */
40 struct string_id
41 {
42     WCHAR *str;  /* ptr to Unicode string */
43     unsigned short id;   /* integer id if str is NULL */
44 };
45
46 /* descriptor for a resource */
47 struct resource
48 {
49     struct string_id type;
50     struct string_id name;
51     const void      *data;
52     unsigned int     data_size;
53     unsigned int     data_offset;
54     unsigned short   mem_options;
55     unsigned short   lang;
56 };
57
58 /* name level of the resource tree */
59 struct res_name
60 {
61     const struct string_id  *name;         /* name */
62     struct resource         *res;          /* resource */
63     int                      nb_languages; /* number of languages */
64     unsigned int             dir_offset;   /* offset of directory in resource dir */
65     unsigned int             name_offset;  /* offset of name in resource dir */
66 };
67
68 /* type level of the resource tree */
69 struct res_type
70 {
71     const struct string_id  *type;         /* type name */
72     struct res_name         *names;        /* names array */
73     unsigned int             nb_names;     /* total number of names */
74     unsigned int             nb_id_names;  /* number of names that have a numeric id */
75     unsigned int             dir_offset;   /* offset of directory in resource dir */
76     unsigned int             name_offset;  /* offset of type name in resource dir */
77 };
78
79 /* top level of the resource tree */
80 struct res_tree
81 {
82     struct res_type *types;                /* types array */
83     unsigned int     nb_types;             /* total number of types */
84 };
85
86 /* size of a resource directory with n entries */
87 #define RESOURCE_DIR_SIZE        (4 * sizeof(unsigned int))
88 #define RESOURCE_DIR_ENTRY_SIZE  (2 * sizeof(unsigned int))
89 #define RESOURCE_DATA_ENTRY_SIZE (4 * sizeof(unsigned int))
90 #define RESDIR_SIZE(n)  (RESOURCE_DIR_SIZE + (n) * RESOURCE_DIR_ENTRY_SIZE)
91
92
93 static inline struct resource *add_resource( DLLSPEC *spec )
94 {
95     spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
96     return &spec->resources[spec->nb_resources++];
97 }
98
99 static inline unsigned int strlenW( const WCHAR *str )
100 {
101     const WCHAR *s = str;
102     while (*s) s++;
103     return s - str;
104 }
105
106 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
107 {
108     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
109     return *str1 - *str2;
110 }
111
112 static struct res_name *add_name( struct res_type *type, struct resource *res )
113 {
114     struct res_name *name;
115     type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
116     name = &type->names[type->nb_names++];
117     name->name         = &res->name;
118     name->res          = res;
119     name->nb_languages = 1;
120     if (!name->name->str) type->nb_id_names++;
121     return name;
122 }
123
124 static struct res_type *add_type( struct res_tree *tree, struct resource *res )
125 {
126     struct res_type *type;
127     tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
128     type = &tree->types[tree->nb_types++];
129     type->type        = &res->type;
130     type->names       = NULL;
131     type->nb_names    = 0;
132     type->nb_id_names = 0;
133     return type;
134 }
135
136 /* get a string from the current resource file */
137 static void get_string( struct string_id *str )
138 {
139     WCHAR wc = get_word();
140
141     if (wc == 0xffff)
142     {
143         str->str = NULL;
144         str->id = get_word();
145     }
146     else
147     {
148         WCHAR *p = xmalloc( (strlenW( (const WCHAR *)(input_buffer + input_buffer_pos) - 1) + 1) * sizeof(WCHAR) );
149         str->str = p;
150         str->id  = 0;
151         if ((*p++ = wc)) while ((*p++ = get_word()));
152     }
153 }
154
155 /* put a string into the resource file */
156 static void put_string( const struct string_id *str )
157 {
158     if (str->str)
159     {
160         const WCHAR *p = str->str;
161         while (*p) put_word( *p++ );
162         put_word( 0 );
163     }
164     else
165     {
166         put_word( 0xffff );
167         put_word( str->id );
168     }
169 }
170
171 static void dump_res_data( const struct resource *res )
172 {
173     unsigned int i = 0;
174     unsigned int size = (res->data_size + 3) & ~3;
175
176     if (!size) return;
177
178     input_buffer = res->data;
179     input_buffer_pos  = 0;
180     input_buffer_size = size;
181
182     output( "\t.long " );
183     while (size > 4)
184     {
185         if ((i++ % 16) == 15) output( "0x%08x\n\t.long ", get_dword() );
186         else output( "0x%08x,", get_dword() );
187         size -= 4;
188     }
189     output( "0x%08x\n", get_dword() );
190     size -= 4;
191     assert( input_buffer_pos == input_buffer_size );
192 }
193
194 /* check the file header */
195 /* all values must be zero except header size */
196 static int check_header(void)
197 {
198     unsigned int size;
199
200     if (get_dword()) return 0;        /* data size */
201     size = get_dword();               /* header size */
202     if (size == 0x20000000) byte_swapped = 1;
203     else if (size != 0x20) return 0;
204     if (get_word() != 0xffff || get_word()) return 0;  /* type, must be id 0 */
205     if (get_word() != 0xffff || get_word()) return 0;  /* name, must be id 0 */
206     if (get_dword()) return 0;        /* data version */
207     if (get_word()) return 0;         /* mem options */
208     if (get_word()) return 0;         /* language */
209     if (get_dword()) return 0;        /* version */
210     if (get_dword()) return 0;        /* characteristics */
211     return 1;
212 }
213
214 /* load the next resource from the current file */
215 static void load_next_resource( DLLSPEC *spec )
216 {
217     unsigned int hdr_size;
218     struct resource *res = add_resource( spec );
219
220     res->data_size = get_dword();
221     hdr_size = get_dword();
222     if (hdr_size & 3) fatal_error( "%s header size not aligned\n", input_buffer_filename );
223     if (hdr_size < 32) fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
224
225     res->data = input_buffer + input_buffer_pos - 2*sizeof(unsigned int) + hdr_size;
226     if ((const unsigned char *)res->data < input_buffer ||
227         (const unsigned char *)res->data >= input_buffer + input_buffer_size)
228         fatal_error( "%s invalid header size %u\n", input_buffer_filename, hdr_size );
229     get_string( &res->type );
230     get_string( &res->name );
231     if (input_buffer_pos & 2) get_word();  /* align to dword boundary */
232     get_dword();                        /* skip data version */
233     res->mem_options = get_word();
234     res->lang = get_word();
235     get_dword();                        /* skip version */
236     get_dword();                        /* skip characteristics */
237
238     input_buffer_pos = ((const unsigned char *)res->data - input_buffer) + ((res->data_size + 3) & ~3);
239     input_buffer_pos = (input_buffer_pos + 3) & ~3;
240     if (input_buffer_pos > input_buffer_size)
241         fatal_error( "%s is a truncated file\n", input_buffer_filename );
242 }
243
244 /* load a Win32 .res file */
245 int load_res32_file( const char *name, DLLSPEC *spec )
246 {
247     int ret;
248
249     init_input_buffer( name );
250
251     if ((ret = check_header()))
252     {
253         while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
254     }
255     return ret;
256 }
257
258 /* compare two unicode strings/ids */
259 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
260 {
261     if (!str1->str)
262     {
263         if (!str2->str) return str1->id - str2->id;
264         return 1;  /* an id compares larger than a string */
265     }
266     if (!str2->str) return -1;
267     return strcmpW( str1->str, str2->str );
268 }
269
270 /* compare two resources for sorting the resource directory */
271 /* resources are stored first by type, then by name, then by language */
272 static int cmp_res( const void *ptr1, const void *ptr2 )
273 {
274     const struct resource *res1 = ptr1;
275     const struct resource *res2 = ptr2;
276     int ret;
277
278     if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
279     if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
280     return res1->lang - res2->lang;
281 }
282
283 static char *format_res_string( const struct string_id *str )
284 {
285     int i, len = str->str ? strlenW(str->str) + 1 : 5;
286     char *ret = xmalloc( len );
287
288     if (!str->str) sprintf( ret, "%04x", str->id );
289     else for (i = 0; i < len; i++) ret[i] = str->str[i];  /* dumb W->A conversion */
290     return ret;
291 }
292
293 /* build the 3-level (type,name,language) resource tree */
294 static struct res_tree *build_resource_tree( DLLSPEC *spec, unsigned int *dir_size )
295 {
296     unsigned int i, k, n, offset, data_offset;
297     struct res_tree *tree;
298     struct res_type *type = NULL;
299     struct res_name *name = NULL;
300     struct resource *res;
301
302     qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
303
304     tree = xmalloc( sizeof(*tree) );
305     tree->types = NULL;
306     tree->nb_types = 0;
307
308     for (i = 0; i < spec->nb_resources; i++)
309     {
310         if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
311         {
312             type = add_type( tree, &spec->resources[i] );
313             name = add_name( type, &spec->resources[i] );
314         }
315         else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
316         {
317             name = add_name( type, &spec->resources[i] );
318         }
319         else if (spec->resources[i].lang == spec->resources[i-1].lang)
320         {
321             char *type_str = format_res_string( &spec->resources[i].type );
322             char *name_str = format_res_string( &spec->resources[i].name );
323             error( "winebuild: duplicate resource type %s name %s language %04x\n",
324                    type_str, name_str, spec->resources[i].lang );
325         }
326         else name->nb_languages++;
327     }
328
329     /* compute the offsets */
330
331     offset = RESDIR_SIZE( tree->nb_types );
332     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
333     {
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++)
337         {
338             name->dir_offset = offset;
339             offset += RESDIR_SIZE( name->nb_languages );
340         }
341     }
342     data_offset = offset;
343     offset += spec->nb_resources * RESOURCE_DATA_ENTRY_SIZE;
344
345     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
346     {
347         if (type->type->str)
348         {
349             type->name_offset = offset | 0x80000000;
350             offset += (strlenW(type->type->str)+1) * sizeof(WCHAR);
351         }
352         else type->name_offset = type->type->id;
353
354         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
355         {
356             if (name->name->str)
357             {
358                 name->name_offset = offset | 0x80000000;
359                 offset += (strlenW(name->name->str)+1) * sizeof(WCHAR);
360             }
361             else name->name_offset = name->name->id;
362             for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
363             {
364                 unsigned int entry_offset = (res - spec->resources) * RESOURCE_DATA_ENTRY_SIZE;
365                 res->data_offset = data_offset + entry_offset;
366             }
367         }
368     }
369     if (dir_size) *dir_size = (offset + 3) & ~3;
370     return tree;
371 }
372
373 /* free the resource tree */
374 static void free_resource_tree( struct res_tree *tree )
375 {
376     unsigned int i;
377
378     for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
379     free( tree->types );
380     free( tree );
381 }
382
383 /* output a Unicode string */
384 static void output_string( const WCHAR *name )
385 {
386     int i, len = strlenW(name);
387     output( "\t%s 0x%04x", get_asm_short_keyword(), len );
388     for (i = 0; i < len; i++) output( ",0x%04x", name[i] );
389     output( " /* " );
390     for (i = 0; i < len; i++) output( "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
391     output( " */\n" );
392 }
393
394 /* output a resource directory */
395 static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
396 {
397     output( "\t.long 0\n" );  /* Characteristics */
398     output( "\t.long 0\n" );  /* TimeDateStamp */
399     output( "\t%s 0,0\n",     /* Major/MinorVersion */
400              get_asm_short_keyword() );
401     output( "\t%s %u,%u\n",   /* NumberOfNamed/IdEntries */
402              get_asm_short_keyword(), nb_names, nb_ids );
403 }
404
405 /* output the resource definitions */
406 void output_resources( DLLSPEC *spec )
407 {
408     int k, nb_id_types;
409     unsigned int i, n;
410     struct res_tree *tree;
411     struct res_type *type;
412     struct res_name *name;
413     const struct resource *res;
414
415     if (!spec->nb_resources) return;
416
417     tree = build_resource_tree( spec, NULL );
418
419     /* output the resource directories */
420
421     output( "\n/* resources */\n\n" );
422     output( "\t.data\n" );
423     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
424     output( ".L__wine_spec_resources:\n" );
425
426     for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
427         if (!type->type->str) nb_id_types++;
428
429     output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
430
431     /* dump the type directory */
432
433     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
434         output( "\t.long 0x%08x,0x%08x\n",
435                  type->name_offset, type->dir_offset | 0x80000000 );
436
437     /* dump the names and languages directories */
438
439     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
440     {
441         output_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
442         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
443             output( "\t.long 0x%08x,0x%08x\n",
444                      name->name_offset, name->dir_offset | 0x80000000 );
445
446         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
447         {
448             output_res_dir( 0, name->nb_languages );
449             for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
450                 output( "\t.long 0x%08x,0x%08x\n", res->lang, res->data_offset );
451         }
452     }
453
454     /* dump the resource data entries */
455
456     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
457         output( "\t.long .L__wine_spec_res_%d-.L__wine_spec_rva_base,%u,0,0\n",
458                 i, (res->data_size + 3) & ~3 );
459
460     /* dump the name strings */
461
462     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
463     {
464         if (type->type->str) output_string( type->type->str );
465         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
466             if (name->name->str) output_string( name->name->str );
467     }
468
469     /* resource data */
470
471     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
472     {
473         output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
474         output( ".L__wine_spec_res_%d:\n", i );
475         dump_res_data( res );
476     }
477     output( ".L__wine_spec_resources_end:\n" );
478     output( "\t.byte 0\n" );
479
480     free_resource_tree( tree );
481 }
482
483 /* output a Unicode string in binary format */
484 static void output_bin_string( const WCHAR *name )
485 {
486     int i, len = strlenW(name);
487     put_word( len );
488     for (i = 0; i < len; i++) put_word( name[i] );
489 }
490
491 /* output a resource directory in binary format */
492 static inline void output_bin_res_dir( unsigned int nb_names, unsigned int nb_ids )
493 {
494     put_dword( 0 );        /* Characteristics */
495     put_dword( 0 );        /* TimeDateStamp */
496     put_word( 0 );         /* MajorVersion */
497     put_word( 0 );         /* MinorVersion */
498     put_word( nb_names );  /* NumberOfNamedEntries */
499     put_word( nb_ids );    /* NumberOfIdEntries */
500 }
501
502 /* output the resource definitions in binary format */
503 void output_bin_resources( DLLSPEC *spec, unsigned int start_rva )
504 {
505     int k, nb_id_types;
506     unsigned int i, n, data_offset;
507     struct res_tree *tree;
508     struct res_type *type;
509     struct res_name *name;
510     const struct resource *res;
511
512     if (!spec->nb_resources) return;
513
514     tree = build_resource_tree( spec, &data_offset );
515     init_output_buffer();
516
517     /* output the resource directories */
518
519     for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
520         if (!type->type->str) nb_id_types++;
521
522     output_bin_res_dir( tree->nb_types - nb_id_types, nb_id_types );
523
524     /* dump the type directory */
525
526     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
527     {
528         put_dword( type->name_offset );
529         put_dword( type->dir_offset | 0x80000000 );
530     }
531
532     /* dump the names and languages directories */
533
534     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
535     {
536         output_bin_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
537         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
538         {
539             put_dword( name->name_offset );
540             put_dword( name->dir_offset | 0x80000000 );
541         }
542
543         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
544         {
545             output_bin_res_dir( 0, name->nb_languages );
546             for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
547             {
548                 put_dword( res->lang );
549                 put_dword( res->data_offset );
550             }
551         }
552     }
553
554     /* dump the resource data entries */
555
556     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
557     {
558         put_dword( data_offset + start_rva );
559         put_dword( (res->data_size + 3) & ~3 );
560         put_dword( 0 );
561         put_dword( 0 );
562         data_offset += (res->data_size + 3) & ~3;
563     }
564
565     /* dump the name strings */
566
567     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
568     {
569         if (type->type->str) output_bin_string( type->type->str );
570         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
571             if (name->name->str) output_bin_string( name->name->str );
572     }
573
574     /* resource data */
575
576     align_output( 4 );
577     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
578     {
579         put_data( res->data, res->data_size );
580         align_output( 4 );
581     }
582
583     free_resource_tree( tree );
584 }
585
586 static unsigned int get_resource_header_size( const struct resource *res )
587 {
588     unsigned int size  = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
589
590     if (!res->type.str) size += 2 * sizeof(unsigned short);
591     else size += (strlenW(res->type.str) + 1) * sizeof(WCHAR);
592
593     if (!res->name.str) size += 2 * sizeof(unsigned short);
594     else size += (strlenW(res->name.str) + 1) * sizeof(WCHAR);
595
596     return size;
597 }
598
599 /* output the resources into a .o file */
600 void output_res_o_file( DLLSPEC *spec )
601 {
602     unsigned int i;
603     char *res_file = NULL;
604     int fd;
605
606     if (!spec->nb_resources) fatal_error( "--resources mode needs at least one resource file as input\n" );
607     if (!output_file_name) fatal_error( "No output file name specified\n" );
608
609     byte_swapped = 0;
610     init_output_buffer();
611
612     put_dword( 0 );      /* ResSize */
613     put_dword( 32 );     /* HeaderSize */
614     put_word( 0xffff );  /* ResType */
615     put_word( 0x0000 );
616     put_word( 0xffff );  /* ResName */
617     put_word( 0x0000 );
618     put_dword( 0 );      /* DataVersion */
619     put_word( 0 );       /* Memory options */
620     put_word( 0 );       /* Language */
621     put_dword( 0 );      /* Version */
622     put_dword( 0 );      /* Characteristics */
623
624     for (i = 0; i < spec->nb_resources; i++)
625     {
626         unsigned int header_size = get_resource_header_size( &spec->resources[i] );
627
628         put_dword( spec->resources[i].data_size );
629         put_dword( (header_size + 3) & ~3 );
630         put_string( &spec->resources[i].type );
631         put_string( &spec->resources[i].name );
632         align_output( 4 );
633         put_dword( 0 );
634         put_word( spec->resources[i].mem_options );
635         put_word( spec->resources[i].lang );
636         put_dword( 0 );
637         put_dword( 0 );
638         put_data( spec->resources[i].data, spec->resources[i].data_size );
639         align_output( 4 );
640     }
641
642     /* if the output file name is a .res too, don't run the results through windres */
643     if (strendswith( output_file_name, ".res"))
644     {
645         flush_output_buffer();
646         return;
647     }
648
649     res_file = get_temp_file_name( output_file_name, ".res" );
650     if ((fd = open( res_file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600 )) == -1)
651         fatal_error( "Cannot create %s\n", res_file );
652     if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
653         fatal_error( "Error writing to %s\n", res_file );
654     close( fd );
655     free( output_buffer );
656
657     if (res_file)
658     {
659         struct strarray *args = strarray_init();
660         strarray_add( args, find_tool( "windres", NULL ), "-i", res_file, "-o", output_file_name, NULL );
661         spawn( args );
662         strarray_free( args );
663     }
664     output_file_name = NULL;  /* so we don't try to assemble it */
665 }