Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winebuild/res16.c
4389 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
/* Unicode string or integer id */
33
struct string_id
34
{
35
char *str; /* ptr to string */
36
unsigned short id; /* integer id if str is NULL */
37
};
38
39
/* descriptor for a resource */
40
struct resource
41
{
42
struct string_id type;
43
struct string_id name;
44
const void *data;
45
unsigned int name_offset;
46
unsigned int data_size;
47
unsigned int memopt;
48
};
49
50
/* type level of the resource tree */
51
struct res_type
52
{
53
const struct string_id *type; /* type name */
54
struct resource *res; /* first resource of this type */
55
unsigned int name_offset; /* name offset if string */
56
unsigned int nb_names; /* total number of names */
57
};
58
59
/* top level of the resource tree */
60
struct res_tree
61
{
62
struct res_type *types; /* types array */
63
unsigned int nb_types; /* total number of types */
64
};
65
66
67
static inline struct resource *add_resource( DLLSPEC *spec )
68
{
69
spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(*spec->resources) );
70
return &spec->resources[spec->nb_resources++];
71
}
72
73
static struct res_type *add_type( struct res_tree *tree, struct resource *res )
74
{
75
struct res_type *type;
76
tree->types = xrealloc( tree->types, (tree->nb_types + 1) * sizeof(*tree->types) );
77
type = &tree->types[tree->nb_types++];
78
type->type = &res->type;
79
type->res = res;
80
type->nb_names = 0;
81
return type;
82
}
83
84
/* get a string from the current resource file */
85
static void get_string( struct string_id *str )
86
{
87
unsigned char c = get_byte();
88
89
if (c == 0xff)
90
{
91
str->str = NULL;
92
str->id = get_word();
93
}
94
else
95
{
96
str->str = (char *)input_buffer + input_buffer_pos - 1;
97
str->id = 0;
98
while (get_byte()) /* nothing */;
99
}
100
}
101
102
/* load the next resource from the current file */
103
static void load_next_resource( DLLSPEC *spec )
104
{
105
struct resource *res = add_resource( spec );
106
107
get_string( &res->type );
108
get_string( &res->name );
109
res->memopt = get_word();
110
res->data_size = get_dword();
111
res->data = input_buffer + input_buffer_pos;
112
input_buffer_pos += res->data_size;
113
if (input_buffer_pos > input_buffer_size)
114
fatal_error( "%s is a truncated/corrupted file\n", input_buffer_filename );
115
}
116
117
/* load a Win16 .res file */
118
void load_res16_file( const char *name, DLLSPEC *spec )
119
{
120
init_input_buffer( name );
121
while (input_buffer_pos < input_buffer_size) load_next_resource( spec );
122
}
123
124
/* compare two strings/ids */
125
static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
126
{
127
if (!str1->str)
128
{
129
if (!str2->str) return str1->id - str2->id;
130
return 1; /* an id compares larger than a string */
131
}
132
if (!str2->str) return -1;
133
return strcasecmp( str1->str, str2->str );
134
}
135
136
/* compare two resources for sorting the resource directory */
137
/* resources are stored first by type, then by name */
138
static int cmp_res( const void *ptr1, const void *ptr2 )
139
{
140
const struct resource *res1 = ptr1;
141
const struct resource *res2 = ptr2;
142
int ret;
143
144
if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
145
return cmp_string( &res1->name, &res2->name );
146
}
147
148
/* build the 2-level (type,name) resource tree */
149
static struct res_tree *build_resource_tree( DLLSPEC *spec )
150
{
151
unsigned int i, j, offset;
152
struct res_tree *tree;
153
struct res_type *type = NULL;
154
struct resource *res;
155
156
qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
157
158
offset = 2; /* alignment */
159
tree = xmalloc( sizeof(*tree) );
160
tree->types = NULL;
161
tree->nb_types = 0;
162
163
for (i = 0; i < spec->nb_resources; i++)
164
{
165
if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
166
{
167
type = add_type( tree, &spec->resources[i] );
168
offset += 8;
169
}
170
type->nb_names++;
171
offset += 12;
172
}
173
offset += 2; /* terminator */
174
175
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
176
{
177
if (type->type->str)
178
{
179
type->name_offset = offset;
180
offset += strlen(type->type->str) + 1;
181
}
182
else type->name_offset = type->type->id | 0x8000;
183
184
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
185
{
186
if (res->name.str)
187
{
188
res->name_offset = offset;
189
offset += strlen(res->name.str) + 1;
190
}
191
else res->name_offset = res->name.id | 0x8000;
192
}
193
}
194
return tree;
195
}
196
197
/* free the resource tree */
198
static void free_resource_tree( struct res_tree *tree )
199
{
200
free( tree->types );
201
free( tree );
202
}
203
204
/* output a string preceded by its length */
205
static void output_string( const char *str )
206
{
207
unsigned int i, len = strlen(str);
208
output( "\t.byte 0x%02x", len );
209
for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)str[i] );
210
output( " /* %s */\n", str );
211
}
212
213
/* output a string preceded by its length in binary format*/
214
static void output_bin_string( const char *str )
215
{
216
put_byte( strlen(str) );
217
while (*str) put_byte( *str++ );
218
}
219
220
/* output the resource data */
221
void output_res16_data( DLLSPEC *spec )
222
{
223
const struct resource *res;
224
unsigned int i;
225
226
for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
227
{
228
output( ".L__wine_spec_resource_%u:\n", i );
229
dump_bytes( res->data, res->data_size );
230
}
231
}
232
233
/* output the resource definitions */
234
void output_res16_directory( DLLSPEC *spec )
235
{
236
unsigned int i, j;
237
struct res_tree *tree;
238
const struct res_type *type;
239
const struct resource *res;
240
241
tree = build_resource_tree( spec );
242
243
output( "\n.L__wine_spec_ne_rsrctab:\n" );
244
output( "\t.short 0\n" ); /* alignment */
245
246
/* type and name structures */
247
248
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
249
{
250
output( "\t.short 0x%04x,%u,0,0\n", type->name_offset, type->nb_names );
251
252
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
253
{
254
output( "\t.short .L__wine_spec_resource_%lu-.L__wine_spec_dos_header,%u\n",
255
(unsigned long)(res - spec->resources), res->data_size );
256
output( "\t.short 0x%04x,0x%04x,0,0\n", res->memopt, res->name_offset );
257
}
258
}
259
output( "\t.short 0\n" ); /* terminator */
260
261
/* name strings */
262
263
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
264
{
265
if (type->type->str) output_string( type->type->str );
266
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
267
if (res->name.str) output_string( res->name.str );
268
}
269
output( "\t.byte 0\n" ); /* names terminator */
270
271
free_resource_tree( tree );
272
}
273
274
/* output the resource data in binary format */
275
void output_bin_res16_data( DLLSPEC *spec )
276
{
277
const struct resource *res;
278
unsigned int i;
279
280
for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
281
put_data( res->data, res->data_size );
282
}
283
284
/* output the resource definitions in binary format */
285
void output_bin_res16_directory( DLLSPEC *spec, unsigned int data_offset )
286
{
287
unsigned int i, j;
288
struct res_tree *tree;
289
const struct res_type *type;
290
const struct resource *res;
291
292
tree = build_resource_tree( spec );
293
294
put_word( 0 ); /* alignment */
295
296
/* type and name structures */
297
298
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
299
{
300
put_word( type->name_offset );
301
put_word( type->nb_names );
302
put_word( 0 );
303
put_word( 0 );
304
305
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
306
{
307
put_word( data_offset );
308
put_word( res->data_size );
309
put_word( res->memopt );
310
put_word( res->name_offset );
311
put_word( 0 );
312
put_word( 0 );
313
data_offset += res->data_size;
314
}
315
}
316
put_word( 0 ); /* terminator */
317
318
/* name strings */
319
320
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
321
{
322
if (type->type->str) output_bin_string( type->type->str );
323
for (j = 0, res = type->res; j < type->nb_names; j++, res++)
324
if (res->name.str) output_bin_string( res->name.str );
325
}
326
put_byte( 0 ); /* names terminator */
327
328
free_resource_tree( tree );
329
}
330
331