msgsm32.acm: Implement a stub dll.
[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 #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
40
41 #include "build.h"
42
43 typedef unsigned short WCHAR;
44
45 /* Unicode string or integer id */
46 struct string_id
47 {
48     WCHAR *str;  /* ptr to Unicode string */
49     unsigned short id;   /* integer id if str is NULL */
50 };
51
52 /* descriptor for a resource */
53 struct resource
54 {
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;
61 };
62
63 /* name level of the resource tree */
64 struct res_name
65 {
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 */
70 };
71
72 /* type level of the resource tree */
73 struct res_type
74 {
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 */
80 };
81
82 /* top level of the resource tree */
83 struct res_tree
84 {
85     struct res_type *types;                /* types array */
86     unsigned int     nb_types;             /* total number of types */
87 };
88
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 */
93
94 static unsigned char *file_out_pos;   /* current position in output resource file */
95 static unsigned char *file_out_end;   /* end of output buffer */
96
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)
102
103
104 static inline struct resource *add_resource( DLLSPEC *spec )
105 {
106     spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
107     return &spec->resources[spec->nb_resources++];
108 }
109
110 static inline unsigned int strlenW( const WCHAR *str )
111 {
112     const WCHAR *s = str;
113     while (*s) s++;
114     return s - str;
115 }
116
117 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
118 {
119     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
120     return *str1 - *str2;
121 }
122
123 static struct res_name *add_name( struct res_type *type, const struct resource *res )
124 {
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;
133 }
134
135 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
136 {
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;
145 }
146
147 /* get the next word from the current resource file */
148 static unsigned short get_word(void)
149 {
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;
155 }
156
157 /* get the next dword from the current resource file */
158 static unsigned int get_dword(void)
159 {
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;
166 }
167
168 /* get a string from the current resource file */
169 static void get_string( struct string_id *str )
170 {
171     if (*(const WCHAR *)file_pos == 0xffff)
172     {
173         get_word();  /* skip the 0xffff */
174         str->str = NULL;
175         str->id = get_word();
176     }
177     else
178     {
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()));
183     }
184 }
185
186 /* put a word into the resource file */
187 static void put_word( unsigned short val )
188 {
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 );
193 }
194
195 /* put a dword into the resource file */
196 static void put_dword( unsigned int val )
197 {
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 );
203 }
204
205 /* put a string into the resource file */
206 static void put_string( const struct string_id *str )
207 {
208     if (str->str)
209     {
210         const WCHAR *p = str->str;
211         while (*p) put_word( *p++ );
212         put_word( 0 );
213     }
214     else
215     {
216         put_word( 0xffff );
217         put_word( str->id );
218     }
219 }
220
221 static void dump_res_data( const struct resource *res )
222 {
223     unsigned int i = 0;
224     unsigned int size = (res->data_size + 3) & ~3;
225
226     if (!size) return;
227
228     file_pos = res->data;
229     file_end = (const unsigned char *)res->data + size;
230
231     output( "\t.long " );
232     while (size > 4)
233     {
234         if ((i++ % 16) == 15) output( "0x%08x\n\t.long ", get_dword() );
235         else output( "0x%08x,", get_dword() );
236         size -= 4;
237     }
238     output( "0x%08x\n", get_dword() );
239     size -= 4;
240     assert( file_pos == file_end );
241 }
242
243 /* check the file header */
244 /* all values must be zero except header size */
245 static int check_header(void)
246 {
247     unsigned int size;
248
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 */
260     return 1;
261 }
262
263 /* load the next resource from the current file */
264 static void load_next_resource( DLLSPEC *spec )
265 {
266     unsigned int hdr_size;
267     struct resource *res = add_resource( spec );
268
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 );
272
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 */
282
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 );
285 }
286
287 /* load a Win32 .res file */
288 int load_res32_file( const char *name, DLLSPEC *spec )
289 {
290     int fd, ret;
291     void *base;
292     struct stat st;
293
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 );
297 #ifdef  HAVE_MMAP
298     if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
299 #endif  /* HAVE_MMAP */
300     {
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 );
304     }
305
306     byte_swapped = 0;
307     file_name = name;
308     file_pos  = base;
309     file_end  = file_pos + st.st_size;
310     if ((ret = check_header()))
311     {
312         while (file_pos < file_end) load_next_resource( spec );
313     }
314     close( fd );
315     return ret;
316 }
317
318 /* compare two unicode strings/ids */
319 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
320 {
321     if (!str1->str)
322     {
323         if (!str2->str) return str1->id - str2->id;
324         return 1;  /* an id compares larger than a string */
325     }
326     if (!str2->str) return -1;
327     return strcmpW( str1->str, str2->str );
328 }
329
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 )
333 {
334     const struct resource *res1 = ptr1;
335     const struct resource *res2 = ptr2;
336     int ret;
337
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;
341 }
342
343 static char *format_res_string( const struct string_id *str )
344 {
345     int i, len = str->str ? strlenW(str->str) + 1 : 5;
346     char *ret = xmalloc( len );
347
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 */
350     return ret;
351 }
352
353 /* build the 3-level (type,name,language) resource tree */
354 static struct res_tree *build_resource_tree( DLLSPEC *spec )
355 {
356     unsigned int i;
357     struct res_tree *tree;
358     struct res_type *type = NULL;
359     struct res_name *name = NULL;
360
361     qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
362
363     tree = xmalloc( sizeof(*tree) );
364     tree->types = NULL;
365     tree->nb_types = 0;
366
367     for (i = 0; i < spec->nb_resources; i++)
368     {
369         if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type ))  /* new type */
370         {
371             type = add_type( tree, &spec->resources[i] );
372             name = add_name( type, &spec->resources[i] );
373         }
374         else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
375         {
376             name = add_name( type, &spec->resources[i] );
377         }
378         else if (spec->resources[i].lang == spec->resources[i-1].lang)
379         {
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 );
384         }
385         else name->nb_languages++;
386     }
387     return tree;
388 }
389
390 /* free the resource tree */
391 static void free_resource_tree( struct res_tree *tree )
392 {
393     unsigned int i;
394
395     for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
396     free( tree->types );
397     free( tree );
398 }
399
400 /* output a Unicode string */
401 static void output_string( const WCHAR *name )
402 {
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] );
406     output( " /* " );
407     for (i = 0; i < len; i++) output( "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
408     output( " */\n" );
409 }
410
411 /* output a resource directory */
412 static inline void output_res_dir( unsigned int nb_names, unsigned int nb_ids )
413 {
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 );
420 }
421
422 /* output the resource definitions */
423 void output_resources( DLLSPEC *spec )
424 {
425     int k, nb_id_types;
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;
431
432     if (!spec->nb_resources) return;
433
434     tree = build_resource_tree( spec );
435
436     /* compute the offsets */
437
438     offset = RESDIR_SIZE( tree->nb_types );
439     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
440     {
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 );
444     }
445     offset += spec->nb_resources * RESOURCE_DATA_ENTRY_SIZE;
446
447     for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
448     {
449         if (type->type->str)
450         {
451             type->name_offset = offset | 0x80000000;
452             offset += (strlenW(type->type->str)+1) * sizeof(WCHAR);
453         }
454         else
455         {
456             type->name_offset = type->type->id;
457             nb_id_types++;
458         }
459
460         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
461         {
462             if (name->name->str)
463             {
464                 name->name_offset = offset | 0x80000000;
465                 offset += (strlenW(name->name->str)+1) * sizeof(WCHAR);
466             }
467             else name->name_offset = name->name->id;
468         }
469     }
470
471     /* output the resource directories */
472
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" );
477
478     output_res_dir( tree->nb_types - nb_id_types, nb_id_types );
479
480     /* dump the type directory */
481
482     offset = RESDIR_SIZE( tree->nb_types );
483     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
484     {
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 );
490     }
491
492     data_offset = offset;
493     offset = RESDIR_SIZE( tree->nb_types );
494
495     /* dump the names and languages directories */
496
497     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
498     {
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++)
502         {
503             output( "\t.long 0x%08x,0x%08x\n",
504                      name->name_offset, offset | 0x80000000 );
505             offset += RESDIR_SIZE( name->nb_languages );
506         }
507
508         for (n = 0, name = type->names; n < type->nb_names; n++, name++)
509         {
510             output_res_dir( 0, name->nb_languages );
511             for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
512             {
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 );
515             }
516         }
517     }
518
519     /* dump the resource data entries */
520
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 );
524
525     /* dump the name strings */
526
527     for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
528     {
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 );
532     }
533
534     /* resource data */
535
536     for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
537     {
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 );
541     }
542     output( ".L__wine_spec_resources_end:\n" );
543     output( "\t.byte 0\n" );
544
545     free_resource_tree( tree );
546 }
547
548 static unsigned int get_resource_header_size( const struct resource *res )
549 {
550     unsigned int size  = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
551
552     if (!res->type.str) size += 2 * sizeof(unsigned short);
553     else size += (strlenW(res->type.str) + 1) * sizeof(WCHAR);
554
555     if (!res->name.str) size += 2 * sizeof(unsigned short);
556     else size += (strlenW(res->name.str) + 1) * sizeof(WCHAR);
557
558     return size;
559 }
560
561 /* output the resources into a .o file */
562 void output_res_o_file( DLLSPEC *spec )
563 {
564     unsigned int i, total_size;
565     unsigned char *data;
566     char *res_file = NULL;
567     int fd, err;
568
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" );
571
572     total_size = 32;  /* header */
573
574     for (i = 0; i < spec->nb_resources; i++)
575     {
576         total_size += (get_resource_header_size( &spec->resources[i] ) + 3) & ~3;
577         total_size += (spec->resources[i].data_size + 3) & ~3;
578     }
579     data = xmalloc( total_size );
580
581     byte_swapped = 0;
582     file_out_pos = data;
583     file_out_end = data + total_size;
584
585     put_dword( 0 );      /* ResSize */
586     put_dword( 32 );     /* HeaderSize */
587     put_word( 0xffff );  /* ResType */
588     put_word( 0x0000 );
589     put_word( 0xffff );  /* ResName */
590     put_word( 0x0000 );
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 */
596
597     for (i = 0; i < spec->nb_resources; i++)
598     {
599         unsigned int header_size = get_resource_header_size( &spec->resources[i] );
600
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 );
606         put_dword( 0 );
607         put_word( spec->resources[i].mem_options );
608         put_word( spec->resources[i].lang );
609         put_dword( 0 );
610         put_dword( 0 );
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;
614     }
615     assert( file_out_pos == file_out_end );
616
617     /* if the output file name is a .res too, don't run the results through windres */
618     if (strendswith( output_file_name, ".res"))
619     {
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 );
622     }
623     else
624     {
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 );
628     }
629     if (write( fd, data, total_size ) != total_size)
630         fatal_error( "Error writing to %s\n", res_file );
631     close( fd );
632     free( data );
633
634     if (res_file)
635     {
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 );
640         err = system( cmd );
641         if (err) fatal_error( "%s failed with status %d\n", prog, err );
642         free( cmd );
643     }
644     output_file_name = NULL;  /* so we don't try to assemble it */
645 }