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"
28 #include <sys/types.h>
31 #ifdef HAVE_SYS_MMAN_H
36 #include "wine/unicode.h"
39 /* Unicode string or integer id */
42 WCHAR *str; /* ptr to Unicode string */
43 WORD id; /* integer id if str is NULL */
46 /* descriptor for a resource */
49 struct string_id type;
50 struct string_id name;
52 unsigned int data_size;
56 /* name level of the resource tree */
59 const struct string_id *name; /* name */
60 const struct resource *res; /* resource */
61 int nb_languages; /* number of languages */
64 /* type level of the resource tree */
67 const struct string_id *type; /* type name */
68 struct res_name *names; /* names array */
69 unsigned int nb_names; /* total number of names */
70 unsigned int nb_id_names; /* number of names that have a numeric id */
73 static struct resource *resources;
74 static int nb_resources;
76 static struct res_type *res_types;
77 static int nb_types; /* total number of types */
78 static int nb_id_types; /* number of types that have a numeric id */
80 static const unsigned char *file_pos; /* current position in resource file */
81 static const unsigned char *file_end; /* end of resource file */
82 static const char *file_name; /* current resource file name */
85 inline static struct resource *add_resource(void)
87 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
88 return &resources[nb_resources++];
91 static struct res_name *add_name( struct res_type *type, const struct resource *res )
93 struct res_name *name;
94 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
95 name = &type->names[type->nb_names++];
96 name->name = &res->name;
98 name->nb_languages = 1;
99 if (!name->name->str) type->nb_id_names++;
103 static struct res_type *add_type( const struct resource *res )
105 struct res_type *type;
106 res_types = xrealloc( res_types, (nb_types + 1) * sizeof(*res_types) );
107 type = &res_types[nb_types++];
108 type->type = &res->type;
111 type->nb_id_names = 0;
112 if (!type->type->str) nb_id_types++;
116 /* get the next word from the current resource file */
117 static WORD get_word(void)
119 WORD ret = *(WORD *)file_pos;
120 file_pos += sizeof(WORD);
121 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
125 /* get the next dword from the current resource file */
126 static DWORD get_dword(void)
128 DWORD ret = *(DWORD *)file_pos;
129 file_pos += sizeof(DWORD);
130 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
134 /* get a string from the current resource file */
135 static void get_string( struct string_id *str )
137 if (*(WCHAR *)file_pos == 0xffff)
139 get_word(); /* skip the 0xffff */
141 str->id = get_word();
145 WCHAR *p = xmalloc( (strlenW((WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
148 while ((*p++ = get_word()));
152 /* check the file header */
153 /* all values must be zero except header size */
154 static void check_header(void)
156 if (get_dword()) goto error; /* data size */
157 if (get_dword() != 32) goto error; /* header size */
158 if (get_word() != 0xffff || get_word()) goto error; /* type, must be id 0 */
159 if (get_word() != 0xffff || get_word()) goto error; /* name, must be id 0 */
160 if (get_dword()) goto error; /* data version */
161 if (get_word()) goto error; /* mem options */
162 if (get_word()) goto error; /* language */
163 if (get_dword()) goto error; /* version */
164 if (get_dword()) goto error; /* characteristics */
167 fatal_error( "%s is not a valid Win32 resource file\n", file_name );
170 /* load the next resource from the current file */
171 static void load_next_resource(void)
174 struct resource *res = add_resource();
176 res->data_size = (get_dword() + 3) & ~3;
177 hdr_size = get_dword();
178 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
180 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
181 get_string( &res->type );
182 get_string( &res->name );
183 if ((int)file_pos & 2) get_word(); /* align to dword boundary */
184 get_dword(); /* skip data version */
185 get_word(); /* skip mem options */
186 res->lang = get_word();
187 get_dword(); /* skip version */
188 get_dword(); /* skip characteristics */
190 file_pos = (char *)res->data + res->data_size;
191 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
194 /* load a Win32 .res file */
195 void load_res32_file( const char *name )
201 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
202 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
203 if (!st.st_size) fatal_error( "%s is an empty file\n" );
205 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
206 #endif /* HAVE_MMAP */
208 base = xmalloc( st.st_size );
209 if (read( fd, base, st.st_size ) != st.st_size)
210 fatal_error( "Cannot read %s\n", name );
215 file_end = file_pos + st.st_size;
217 while (file_pos < file_end) load_next_resource();
220 /* compare two unicode strings/ids */
221 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
225 if (!str2->str) return str1->id - str2->id;
226 return 1; /* an id compares larger than a string */
228 if (!str2->str) return -1;
229 return strcmpiW( str1->str, str2->str );
232 /* compare two resources for sorting the resource directory */
233 /* resources are stored first by type, then by name, then by language */
234 static int cmp_res( const void *ptr1, const void *ptr2 )
236 const struct resource *res1 = ptr1;
237 const struct resource *res2 = ptr2;
240 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
241 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
242 return res1->lang - res2->lang;
245 /* build the 3-level (type,name,language) resource tree */
246 static void build_resource_tree(void)
249 struct res_type *type = NULL;
250 struct res_name *name = NULL;
252 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
254 for (i = 0; i < nb_resources; i++)
256 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
258 type = add_type( &resources[i] );
259 name = add_name( type, &resources[i] );
261 else if (cmp_string( &resources[i].name, &resources[i-1].name )) /* new name */
263 name = add_name( type, &resources[i] );
265 else name->nb_languages++;
269 /* output a Unicode string */
270 static void output_string( FILE *outfile, const WCHAR *name )
272 int i, len = strlenW(name);
273 fprintf( outfile, "0x%04x", len );
274 for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
275 fprintf( outfile, " /* " );
276 for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
277 fprintf( outfile, " */" );
280 /* output the resource definitions */
281 int output_resources( FILE *outfile )
285 const struct res_type *type;
286 const struct res_name *name;
287 const struct resource *res;
289 if (!nb_resources) return 0;
291 build_resource_tree();
295 for (i = 0, res = resources; i < nb_resources; i++, res++)
297 const unsigned int *p = res->data;
298 int size = res->data_size / 4;
299 /* dump data as ints to ensure correct alignment */
300 fprintf( outfile, "static const unsigned int res_%d[%d] = {\n ", i, size );
301 for (j = 0; j < size - 1; j++, p++)
303 fprintf( outfile, "0x%08x,", *p );
304 if ((j % 8) == 7) fprintf( outfile, "\n " );
306 fprintf( outfile, "0x%08x\n};\n\n", *p );
309 /* directory structures */
311 fprintf( outfile, "struct res_dir {\n" );
312 fprintf( outfile, " unsigned int Characteristics;\n" );
313 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
314 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
315 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
316 fprintf( outfile, "struct res_dir_entry {\n" );
317 fprintf( outfile, " unsigned int Name;\n" );
318 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
319 fprintf( outfile, "struct res_data_entry {\n" );
320 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
321 fprintf( outfile, " unsigned int Size;\n" );
322 fprintf( outfile, " unsigned int CodePage;\n" );
323 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
325 /* resource directory definition */
327 fprintf( outfile, "#define OFFSETOF(field) ((char*)&((struct res_struct *)0)->field - (char*)((struct res_struct *) 0))\n" );
328 fprintf( outfile, "static struct res_struct{\n" );
329 fprintf( outfile, " struct res_dir type_dir;\n" );
330 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", nb_types );
332 for (i = 0, type = res_types; i < nb_types; i++, type++)
334 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
335 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
336 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
338 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
339 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
340 i, n, name->nb_languages );
344 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", nb_resources );
346 for (i = 0, type = res_types; i < nb_types; i++, type++)
349 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
350 i, strlenW(type->type->str)+1 );
351 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
354 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
355 i, n, strlenW(name->name->str)+1 );
359 /* resource directory contents */
361 fprintf( outfile, "} resources = {\n" );
362 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", nb_types - nb_id_types, nb_id_types );
364 /* dump the type directory */
365 fprintf( outfile, " {\n" );
366 for (i = 0, type = res_types; i < nb_types; i++, type++)
368 if (!type->type->str)
369 fprintf( outfile, " { 0x%04x, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
372 fprintf( outfile, " { OFFSETOF(type_%d_name) | 0x80000000, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
375 fprintf( outfile, " },\n" );
377 /* dump the names and languages directories */
378 for (i = 0, type = res_types; i < nb_types; i++, type++)
380 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
381 type->nb_names - type->nb_id_names, type->nb_id_names, i );
382 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
384 if (!name->name->str)
385 fprintf( outfile, " { 0x%04x, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
386 name->name->id, i, n );
388 fprintf( outfile, " { OFFSETOF(name_%d_%d_name) | 0x80000000, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
391 fprintf( outfile, " },\n" );
393 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
395 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
396 name->nb_languages, i, n );
397 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
399 fprintf( outfile, " { 0x%04x, OFFSETOF(data_entries[%d]) },\n",
400 res->lang, res - resources );
402 fprintf( outfile, " },\n" );
406 /* dump the resource data entries */
407 fprintf( outfile, " {\n" );
408 for (i = 0, res = resources; i < nb_resources; i++, res++)
410 fprintf( outfile, " { res_%d, sizeof(res_%d), 0, 0 },\n", i, i );
413 /* dump the name strings */
414 for (i = 0, type = res_types; i < nb_types; i++, type++)
418 fprintf( outfile, " },\n { " );
419 output_string( outfile, type->type->str );
421 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
425 fprintf( outfile, " },\n { " );
426 output_string( outfile, name->name->str );
430 fprintf( outfile, " }\n};\n#undef OFFSETOF\n\n" );