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