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