2 * Builtin dlls resource support
4 * Copyright 2000 Alexandre Julliard
14 #include <sys/types.h>
17 #ifdef HAVE_SYS_MMAN_H
22 #include "wine/unicode.h"
25 /* Unicode string or integer id */
28 WCHAR *str; /* ptr to Unicode string */
29 WORD id; /* integer id if str is NULL */
32 /* descriptor for a resource */
35 struct string_id type;
36 struct string_id name;
38 unsigned int data_size;
42 /* name level of the resource tree */
45 const struct string_id *name; /* name */
46 const struct resource *res; /* resource */
47 int nb_languages; /* number of languages */
50 /* type level of the resource tree */
53 const struct string_id *type; /* type name */
54 struct res_name *names; /* names array */
55 unsigned int nb_names; /* total number of names */
56 unsigned int nb_id_names; /* number of names that have a numeric id */
59 static struct resource *resources;
60 static int nb_resources;
62 static struct res_type *res_types;
63 static int nb_types; /* total number of types */
64 static int nb_id_types; /* number of types that have a numeric id */
66 static const unsigned char *file_pos; /* current position in resource file */
67 static const unsigned char *file_end; /* end of resource file */
68 static const char *file_name; /* current resource file name */
71 inline static struct resource *add_resource(void)
73 resources = xrealloc( resources, (nb_resources + 1) * sizeof(*resources) );
74 return &resources[nb_resources++];
77 static struct res_name *add_name( struct res_type *type, const struct resource *res )
79 struct res_name *name;
80 type->names = xrealloc( type->names, (type->nb_names + 1) * sizeof(*type->names) );
81 name = &type->names[type->nb_names++];
82 name->name = &res->name;
84 name->nb_languages = 1;
85 if (!name->name->str) type->nb_id_names++;
89 static struct res_type *add_type( const struct resource *res )
91 struct res_type *type;
92 res_types = xrealloc( res_types, (nb_types + 1) * sizeof(*res_types) );
93 type = &res_types[nb_types++];
94 type->type = &res->type;
97 type->nb_id_names = 0;
98 if (!type->type->str) nb_id_types++;
102 /* get the next word from the current resource file */
103 static WORD get_word(void)
105 WORD ret = *(WORD *)file_pos;
106 file_pos += sizeof(WORD);
107 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
111 /* get the next dword from the current resource file */
112 static DWORD get_dword(void)
114 DWORD ret = *(DWORD *)file_pos;
115 file_pos += sizeof(DWORD);
116 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
120 /* get a string from the current resource file */
121 static void get_string( struct string_id *str )
123 if (*(WCHAR *)file_pos == 0xffff)
125 get_word(); /* skip the 0xffff */
127 str->id = get_word();
131 WCHAR *p = xmalloc( (strlenW((WCHAR*)file_pos) + 1) * sizeof(WCHAR) );
134 while ((*p++ = get_word()));
138 /* check the file header */
139 /* all values must be zero except header size */
140 static void check_header(void)
142 if (get_dword()) goto error; /* data size */
143 if (get_dword() != 32) goto error; /* header size */
144 if (get_word() != 0xffff || get_word()) goto error; /* type, must be id 0 */
145 if (get_word() != 0xffff || get_word()) goto error; /* name, must be id 0 */
146 if (get_dword()) goto error; /* data version */
147 if (get_word()) goto error; /* mem options */
148 if (get_word()) goto error; /* language */
149 if (get_dword()) goto error; /* version */
150 if (get_dword()) goto error; /* characteristics */
153 fatal_error( "%s is not a valid Win32 resource file\n", file_name );
156 /* load the next resource from the current file */
157 static void load_next_resource(void)
160 struct resource *res = add_resource();
162 res->data_size = (get_dword() + 3) & ~3;
163 hdr_size = get_dword();
164 if (hdr_size & 3) fatal_error( "%s header size not aligned\n", file_name );
166 res->data = file_pos - 2*sizeof(DWORD) + hdr_size;
167 get_string( &res->type );
168 get_string( &res->name );
169 if ((int)file_pos & 2) get_word(); /* align to dword boundary */
170 get_dword(); /* skip data version */
171 get_word(); /* skip mem options */
172 res->lang = get_word();
173 get_dword(); /* skip version */
174 get_dword(); /* skip characteristics */
176 file_pos = (char *)res->data + res->data_size;
177 if (file_pos > file_end) fatal_error( "%s is a truncated file\n", file_name );
180 /* load a Win32 .res file */
181 void load_res32_file( const char *name )
187 if ((fd = open( name, O_RDONLY )) == -1) fatal_perror( "Cannot open %s", name );
188 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", name );
189 if (!st.st_size) fatal_error( "%s is an empty file\n" );
190 if ((base = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
192 base = xmalloc( st.st_size );
193 if (read( fd, base, st.st_size ) != st.st_size)
194 fatal_error( "Cannot read %s\n", name );
199 file_end = file_pos + st.st_size;
201 while (file_pos < file_end) load_next_resource();
204 /* compare two unicode strings/ids */
205 static int cmp_string( const struct string_id *str1, const struct string_id *str2 )
209 if (!str2->str) return str1->id - str2->id;
210 return 1; /* an id compares larger than a string */
212 if (!str2->str) return -1;
213 return strcmpiW( str1->str, str2->str );
216 /* compare two resources for sorting the resource directory */
217 /* resources are stored first by type, then by name, then by language */
218 static int cmp_res( const void *ptr1, const void *ptr2 )
220 const struct resource *res1 = ptr1;
221 const struct resource *res2 = ptr2;
224 if ((ret = cmp_string( &res1->type, &res2->type ))) return ret;
225 if ((ret = cmp_string( &res1->name, &res2->name ))) return ret;
226 return res1->lang - res2->lang;
229 /* build the 3-level (type,name,language) resource tree */
230 static void build_resource_tree(void)
233 struct res_type *type = NULL;
234 struct res_name *name = NULL;
236 qsort( resources, nb_resources, sizeof(*resources), cmp_res );
238 for (i = 0; i < nb_resources; i++)
240 if (!i || cmp_string( &resources[i].type, &resources[i-1].type )) /* new type */
242 type = add_type( &resources[i] );
243 name = add_name( type, &resources[i] );
245 else if (cmp_string( &resources[i].name, &resources[i-1].name )) /* new name */
247 name = add_name( type, &resources[i] );
249 else name->nb_languages++;
253 /* output a Unicode string */
254 static void output_string( FILE *outfile, const WCHAR *name )
256 int i, len = strlenW(name);
257 fprintf( outfile, "0x%04x", len );
258 for (i = 0; i < len; i++) fprintf( outfile, ", 0x%04x", name[i] );
259 fprintf( outfile, " /* " );
260 for (i = 0; i < len; i++) fprintf( outfile, "%c", isprint((char)name[i]) ? (char)name[i] : '?' );
261 fprintf( outfile, " */" );
264 /* output the resource definitions */
265 int output_resources( FILE *outfile )
269 const struct res_type *type;
270 const struct res_name *name;
271 const struct resource *res;
273 if (!nb_resources) return 0;
275 build_resource_tree();
279 for (i = 0, res = resources; i < nb_resources; i++, res++)
281 const unsigned int *p = res->data;
282 int size = res->data_size / 4;
283 /* dump data as ints to ensure correct alignment */
284 fprintf( outfile, "static const unsigned int res_%d[%d] = {\n ", i, size );
285 for (j = 0; j < size - 1; j++, p++)
287 fprintf( outfile, "0x%08x,", *p );
288 if ((j % 8) == 7) fprintf( outfile, "\n " );
290 fprintf( outfile, "0x%08x\n};\n\n", *p );
293 /* directory structures */
295 fprintf( outfile, "struct res_dir {\n" );
296 fprintf( outfile, " unsigned int Characteristics;\n" );
297 fprintf( outfile, " unsigned int TimeDateStamp;\n" );
298 fprintf( outfile, " unsigned short MajorVersion, MinorVersion;\n" );
299 fprintf( outfile, " unsigned short NumerOfNamedEntries, NumberOfIdEntries;\n};\n\n" );
300 fprintf( outfile, "struct res_dir_entry {\n" );
301 fprintf( outfile, " unsigned int Name;\n" );
302 fprintf( outfile, " unsigned int OffsetToData;\n};\n\n" );
303 fprintf( outfile, "struct res_data_entry {\n" );
304 fprintf( outfile, " const unsigned int *OffsetToData;\n" );
305 fprintf( outfile, " unsigned int Size;\n" );
306 fprintf( outfile, " unsigned int CodePage;\n" );
307 fprintf( outfile, " unsigned int ResourceHandle;\n};\n\n" );
309 /* resource directory definition */
311 fprintf( outfile, "#define OFFSETOF(field) ((char*)&((struct res_struct *)0)->field - (char*)((struct res_struct *) 0))\n" );
312 fprintf( outfile, "static struct res_struct{\n" );
313 fprintf( outfile, " struct res_dir type_dir;\n" );
314 fprintf( outfile, " struct res_dir_entry type_entries[%d];\n", nb_types );
316 for (i = 0, type = res_types; i < nb_types; i++, type++)
318 fprintf( outfile, " struct res_dir name_%d_dir;\n", i );
319 fprintf( outfile, " struct res_dir_entry name_%d_entries[%d];\n", i, type->nb_names );
320 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
322 fprintf( outfile, " struct res_dir lang_%d_%d_dir;\n", i, n );
323 fprintf( outfile, " struct res_dir_entry lang_%d_%d_entries[%d];\n",
324 i, n, name->nb_languages );
328 fprintf( outfile, " struct res_data_entry data_entries[%d];\n", nb_resources );
330 for (i = 0, type = res_types; i < nb_types; i++, type++)
333 fprintf( outfile, " unsigned short type_%d_name[%d];\n",
334 i, strlenW(type->type->str)+1 );
335 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
338 fprintf( outfile, " unsigned short name_%d_%d_name[%d];\n",
339 i, n, strlenW(name->name->str)+1 );
343 /* resource directory contents */
345 fprintf( outfile, "} resources = {\n" );
346 fprintf( outfile, " { 0, 0, 0, 0, %d, %d },\n", nb_types - nb_id_types, nb_id_types );
348 /* dump the type directory */
349 fprintf( outfile, " {\n" );
350 for (i = 0, type = res_types; i < nb_types; i++, type++)
352 if (!type->type->str)
353 fprintf( outfile, " { 0x%04x, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
356 fprintf( outfile, " { OFFSETOF(type_%d_name) | 0x80000000, OFFSETOF(name_%d_dir) | 0x80000000 },\n",
359 fprintf( outfile, " },\n" );
361 /* dump the names and languages directories */
362 for (i = 0, type = res_types; i < nb_types; i++, type++)
364 fprintf( outfile, " { 0, 0, 0, 0, %d, %d }, /* name_%d_dir */\n {\n",
365 type->nb_names - type->nb_id_names, type->nb_id_names, i );
366 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
368 if (!name->name->str)
369 fprintf( outfile, " { 0x%04x, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
370 name->name->id, i, n );
372 fprintf( outfile, " { OFFSETOF(name_%d_%d_name) | 0x80000000, OFFSETOF(lang_%d_%d_dir) | 0x80000000 },\n",
375 fprintf( outfile, " },\n" );
377 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
379 fprintf( outfile, " { 0, 0, 0, 0, 0, %d }, /* lang_%d_%d_dir */\n {\n",
380 name->nb_languages, i, n );
381 for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
383 fprintf( outfile, " { 0x%04x, OFFSETOF(data_entries[%d]) },\n",
384 res->lang, res - resources );
386 fprintf( outfile, " },\n" );
390 /* dump the resource data entries */
391 fprintf( outfile, " {\n" );
392 for (i = 0, res = resources; i < nb_resources; i++, res++)
394 fprintf( outfile, " { res_%d, sizeof(res_%d), 0, 0 },\n", i, i );
397 /* dump the name strings */
398 for (i = 0, type = res_types; i < nb_types; i++, type++)
402 fprintf( outfile, " },\n { " );
403 output_string( outfile, type->type->str );
405 for (n = 0, name = type->names; n < type->nb_names; n++, name++)
409 fprintf( outfile, " },\n { " );
410 output_string( outfile, name->name->str );
414 fprintf( outfile, " }\n};\n#undef OFFSETOF\n\n" );