2 * Builtin dlls resource support
4 * Copyright 2000 Alexandre Julliard
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
36 #ifdef HAVE_SYS_MMAN_H
44 /* Unicode string or integer id */
47 WCHAR *str; /* ptr to Unicode string */
48 WORD id; /* integer id if str is NULL */
51 /* descriptor for a resource */
54 struct string_id type;
55 struct string_id name;
57 unsigned int data_size;
61 /* name level of the resource tree */
64 const struct string_id *name; /* name */
65 const struct resource *res; /* resource */
66 int nb_languages; /* number of languages */
67 unsigned int name_offset; /* offset of name in resource dir */
70 /* type level of the resource tree */
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 name_offset; /* offset of type name in resource dir */
80 /* top level of the resource tree */
83 struct res_type *types; /* types array */
84 unsigned int nb_types; /* total number of types */
87 static const unsigned char *file_pos; /* current position in resource file */
88 static const unsigned char *file_end; /* end of resource file */
89 static const char *file_name; /* current resource file name */
91 /* size of a resource directory with n entries */
92 #define RESDIR_SIZE(n) ((4 + 2 * (n)) * sizeof(int))
95 inline static struct resource *add_resource( DLLSPEC *spec )
97 spec->resources = xrealloc( spec->resources, (spec->nb_resources + 1) * sizeof(spec->resources[0]) );
98 return &spec->resources[spec->nb_resources++];
101 static inline unsigned int strlenW( const WCHAR *str )
103 const WCHAR *s = str;
108 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
110 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
111 return *str1 - *str2;
114 static struct res_name *add_name( struct res_type *type, const struct resource *res )
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;
121 name->nb_languages = 1;
122 if (!name->name->str) type->nb_id_names++;
126 static struct res_type *add_type( struct res_tree *tree, const struct resource *res )
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;
134 type->nb_id_names = 0;
138 /* get the next word from the current resource file */
139 static WORD get_word(void)
141 WORD ret = *(const WORD *)file_pos;
142 file_pos += sizeof(WORD);
143 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
147 /* get the next dword from the current resource file */
148 static DWORD get_dword(void)
150 DWORD ret = *(const DWORD *)file_pos;
151 file_pos += sizeof(DWORD);
152 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
156 /* get a string from the current resource file */
157 static void get_string( struct string_id *str )
159 if (*(const WCHAR *)file_pos == 0xffff)
161 get_word(); /* skip the 0xffff */
163 str->id = get_word();
167 WCHAR *p = xmalloc( (strlenW((const WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
170 while ((*p++ = get_word()));
174 /* check the file header */
175 /* all values must be zero except header size */
176 static int check_header(void)
178 if (get_dword()) return 0; /* data size */
179 if (get_dword() != 32) return 0; /* header size */
180 if (get_word() != 0xffff || get_word()) return 0; /* type, must be id 0 */
181 if (get_word() != 0xffff || get_word()) return 0; /* name, must be id 0 */
182 if (get_dword()) return 0; /* data version */
183 if (get_word()) return 0; /* mem options */
184 if (get_word()) return 0; /* language */
185 if (get_dword()) return 0; /* version */
186 if (get_dword()) return 0; /* characteristics */
190 /* load the next resource from the current file */
191 static void load_next_resource( DLLSPEC *spec )
194 struct resource *res = add_resource( spec );
196 res->data_size = (get_dword() + 3) & ~3;
197 hdr_size = get_dword();
198 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
200 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
201 get_string( &res->type );
202 get_string( &res->name );
203 if ((int)file_pos & 2) get_word(); /* align to dword boundary */
204 get_dword(); /* skip data version */
205 get_word(); /* skip mem options */
206 res->lang = get_word();
207 get_dword(); /* skip version */
208 get_dword(); /* skip characteristics */
210 file_pos = (const char *)res->data + res->data_size;
211 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
214 /* load a Win32 .res file */
215 int load_res32_file( const char *name, DLLSPEC *spec )
221 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
222 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
223 if (!st.st_size) fatal_error( "%s is an empty file\n", name );
225 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
226 #endif /* HAVE_MMAP */
228 base = xmalloc( st.st_size );
229 if (read( fd, base, st.st_size ) != st.st_size)
230 fatal_error( "Cannot read %s\n", name );
235 file_end = file_pos + st.st_size;
236 if ((ret = check_header()))
238 while (file_pos < file_end) load_next_resource( spec );
244 /* compare two unicode strings/ids */
245 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
249 if (!str2->str) return str1->id - str2->id;
250 return 1; /* an id compares larger than a string */
252 if (!str2->str) return -1;
253 return strcmpW( str1->str, str2->str );
256 /* compare two resources for sorting the resource directory */
257 /* resources are stored first by type, then by name, then by language */
258 static int cmp_res( const void *ptr1, const void *ptr2 )
260 const struct resource *res1 = ptr1;
261 const struct resource *res2 = ptr2;
264 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
265 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
266 return res1->lang - res2->lang;
269 /* build the 3-level (type,name,language) resource tree */
270 static struct res_tree *build_resource_tree( DLLSPEC *spec )
273 struct res_tree *tree;
274 struct res_type *type = NULL;
275 struct res_name *name = NULL;
277 qsort( spec->resources, spec->nb_resources, sizeof(*spec->resources), cmp_res );
279 tree = xmalloc( sizeof(*tree) );
283 for (i = 0; i < spec->nb_resources; i++)
285 if (!i || cmp_string( &spec->resources[i].type, &spec->resources[i-1].type )) /* new type */
287 type = add_type( tree, &spec->resources[i] );
288 name = add_name( type, &spec->resources[i] );
290 else if (cmp_string( &spec->resources[i].name, &spec->resources[i-1].name )) /* new name */
292 name = add_name( type, &spec->resources[i] );
294 else name->nb_languages++;
299 /* free the resource tree */
300 static void free_resource_tree( struct res_tree *tree )
304 for (i = 0; i < tree->nb_types; i++) free( tree->types[i].names );
309 /* output a Unicode string */
310 static void output_string( FILE *outfile, const WCHAR *name )
312 int i, len = strlenW(name);
313 fprintf( outfile, "0x%04x", len );
314 for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
315 fprintf( outfile, " /* " );
316 for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
317 fprintf( outfile, " */" );
320 /* output the resource definitions */
321 void output_resources( FILE *outfile, DLLSPEC *spec )
323 int j, k, nb_id_types;
324 unsigned int i, n, offset, data_offset;
325 struct res_tree *tree;
326 struct res_type *type;
327 struct res_name *name;
328 const struct resource *res;
330 if (!spec->nb_resources) return;
332 tree = build_resource_tree( spec );
334 /* directory structures */
336 fprintf( outfile, "struct res_dir {\n" );
337 fprintf( outfile, " unsigned int Characteristics;\n" );
338 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
339 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
340 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
341 fprintf( outfile, "struct res_dir_entry {\n" );
342 fprintf( outfile, " unsigned int Name;\n" );
343 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
344 fprintf( outfile, "struct res_data_entry {\n" );
345 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
346 fprintf( outfile, " unsigned int Size;\n" );
347 fprintf( outfile, " unsigned int CodePage;\n" );
348 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
350 /* resource directory definition */
352 fprintf( outfile, "static struct res_struct{\n" );
353 fprintf( outfile, " struct res_dir type_dir;\n" );
354 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", tree->nb_types );
355 offset = RESDIR_SIZE( tree->nb_types );
357 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
359 offset += RESDIR_SIZE( type->nb_names );
360 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
361 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
362 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
364 offset += RESDIR_SIZE( name->nb_languages );
365 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
366 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
367 i, n, name->nb_languages );
371 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", spec->nb_resources );
372 offset += spec->nb_resources * 4 * sizeof(int);
374 for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
378 type->name_offset = offset | 0x80000000;
379 offset += (strlenW(type->type->str)+1) * sizeof(unsigned short);
380 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
381 i, strlenW(type->type->str)+1 );
385 type->name_offset = type->type->id;
389 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
393 name->name_offset = offset | 0x80000000;
394 offset += (strlenW(name->name->str)+1) * sizeof(unsigned short);
395 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
396 i, n, strlenW(name->name->str)+1 );
398 else name->name_offset = name->name->id;
401 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
402 fprintf( outfile, " unsigned int res_%d[%d];\n", i, res->data_size / 4 );
404 /* resource directory contents */
406 fprintf( outfile, "} __wine_spec_resources = {\n" );
407 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", tree->nb_types - nb_id_types, nb_id_types );
409 /* dump the type directory */
410 offset = RESDIR_SIZE( tree->nb_types );
411 fprintf( outfile, " {\n" );
412 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
414 fprintf( outfile, " { 0x%08x, 0x%08x },\n", type->name_offset, offset | 0x80000000 );
415 offset += RESDIR_SIZE( type->nb_names );
416 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
417 offset += RESDIR_SIZE( name->nb_languages );
419 fprintf( outfile, " },\n" );
421 data_offset = offset;
422 offset = RESDIR_SIZE( tree->nb_types );
424 /* dump the names and languages directories */
425 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
427 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
428 type->nb_names - type->nb_id_names, type->nb_id_names, i );
429 offset += RESDIR_SIZE( type->nb_names );
430 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
432 fprintf( outfile, " { 0x%08x, 0x%08x },\n", name->name_offset, offset | 0x80000000 );
433 offset += RESDIR_SIZE( name->nb_languages );
435 fprintf( outfile, " },\n" );
437 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
439 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
440 name->nb_languages, i, n );
441 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
443 fprintf( outfile, " { 0x%04x, 0x%08x },\n",
444 res->lang, data_offset + (res - spec->resources) * 4 * sizeof(int) );
446 fprintf( outfile, " },\n" );
450 /* dump the resource data entries */
451 fprintf( outfile, " {\n" );
452 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
454 fprintf( outfile, " { __wine_spec_resources.res_%d, sizeof(__wine_spec_resources.res_%d), 0, 0 }, /* %08x */\n", i, i,
455 data_offset + i * 4 * sizeof(int) );
458 /* dump the name strings */
459 for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
463 fprintf( outfile, " },\n { " );
464 output_string( outfile, type->type->str );
466 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
470 fprintf( outfile, " },\n { " );
471 output_string( outfile, name->name->str );
475 fprintf( outfile, " },\n" );
479 for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
481 const unsigned int *p = res->data;
482 int size = res->data_size / 4;
483 /* dump data as ints to ensure correct alignment */
484 fprintf( outfile, " { /* res_%d */\n ", i );
485 for (j = 0; j < size - 1; j++, p++)
487 fprintf( outfile, "0x%08x,", *p );
488 if ((j % 8) == 7) fprintf( outfile, "\n " );
490 fprintf( outfile, "0x%08x\n },\n", *p );
492 fprintf( outfile, "};\n\n" );
494 free_resource_tree( tree );