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