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